var XMLHttp = null;

function validate_qq_required(field,alerttxt)
{
	with (field)
	{
  		if (value==null||value=="")
  			{alert(alerttxt);return false;}
	  	else
  			{return true;}
	}
}

function validate_qq_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
  			{alert(alerttxt);return false;}
		else {return true;}
	}
}

function validate_qq_form(thisform)
{
	with (thisform)
	{
		if (validate_qq_required(loan_purpose,"Please fill out Loan Type")==false)
  			{loan_purpose.focus();return false;}
		if (validate_qq_required(custom_field1,"Please select Loan Duration")==false)
  			{custom_field1.focus();return false;}
		if (validate_qq_required(loan_amount,"Please fill Desired Loan Amount")==false)
  			{loan_amount.focus();return false;}
		if (validate_qq_required(property_present_value,"Please fill out Estimated Home Value")==false)
  			{property_present_value.focus();return false;}
		if (validate_qq_required(property_state,"Please fill out Property State")==false)
  			{property_state.focus();return false;}
		if (validate_qq_required(borrower_email,"Please fill out Email Address")==false)
  			{borrower_email.focus();return false;}
		if (validate_qq_required(borrower_phone1,"Please fill out Best Contact Number")==false)
  			{borrower_phone1.focus();return false;}
		if (validate_qq_required(borrower_first_name,"Please fill out First Name")==false)
  			{borrower_first_name.focus();return false;}
		if (validate_qq_required(borrower_last_name,"Please fill out Last Name")==false)
  			{borrower_last_name.focus();return false;}
		if (validate_qq_email(borrower_email,"Not a valid e-mail address")==false)
  			{borrower_email.focus();return false;}

		return true;
	}
}

function getXMLHttp() {
	var XMLHttp = null;
	try	{
		// Firefox, Opera, Safari
		XMLHttp = new XMLHttpRequest();
		}
	catch (e){
		try	{
			// IE 6.0
			XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e){
			try 	{
				// IE 5.0
				XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch (e){
				//window.alert("Your browser does not support AJAX!");
				//return false;
				}
			}
		}
	return XMLHttp;
}

	function runAJAX(thisform) {
		// first, verify fields are entered
		if (!validate_qq_form(thisform)) { return; }

		XMLHttp = getXMLHttp();

		if (XMLHttp == null) {
			window.alert("Your browser does not support AJAX.  Try enabling Javascript on your web-browser and try again.  Thank You.");
			return;
		}

		//var url = "get_data.php";
		//var url = "https://www.iffgdirect.com/lrws.asmx";
		var url = "/lrws.asmx/LeadRoiData";

		var loan_purpose = document.getElementById("loan_purpose").value;
		var application_notes = document.getElementById("application_notes").value;
		var loan_amount = document.getElementById("loan_amount").value;
		var property_present_value = document.getElementById("property_present_value").value;
		var property_state = document.getElementById("property_state").value;
		var borrower_email = document.getElementById("borrower_email").value;
		var borrower_phone1 = document.getElementById("borrower_phone1").value;
		var borrower_first_name = document.getElementById("borrower_first_name").value;
		var borrower_last_name = document.getElementById("borrower_last_name").value;
		var campaign_name = document.getElementById("campaign_name").value;
		var custom_field1 = document.getElementById("custom_field1").value;
		//var custom_field2 = document.getElementById("custom_field2").value;

		// interpret "loan_purpose"
		switch(loan_purpose)
		{
			case "Refinance1":
				loan_purpose="Refinance";
				application_notes = "Mortgage Refinance";
				break;
			case "Refinance2":
				loan_purpose="Refinance";
				application_notes = "Debt Consolidation";
				break;
			case "Refinance3":
				loan_purpose="Refinance";
				application_notes = "Cash Out";
				break;
			case "Purchase1":
				loan_purpose="Purchase";
				application_notes = "Signed a purchase agreement";
				break;
			case "Purchase2":
				loan_purpose="Purchase";
				application_notes = "Making an offer within 30 days";
				break;
			case "Purchase3":
				loan_purpose="Purchase";
				application_notes = "Not sure of timing; house hunting";
				break;
			default:
				//loan_purpose="Purchase";
				//application_notes = "unknown selection";
		}

		//var params = "loan_purpose=" + loan_purpose + "&application_notes=" + application_notes + "&loan_amount=" + loan_amount + "&property_present_value=" + property_present_value + "&property_state=" + property_state + "&borrower_email=" + borrower_email + "&borrower_phone1=" + borrower_phone1 + "&borrower_first_name=" + borrower_first_name + "&borrower_last_name=" + borrower_last_name + "&campaign_name=" + campaign_name + "&custom_field1=" + custom_field1 + "&custom_field2=" + custom_field2;
		var params = "loan_purpose=" + loan_purpose + "&application_notes=" + application_notes + "&loan_amount=" + loan_amount + "&property_present_value=" + property_present_value + "&property_state=" + property_state + "&borrower_email=" + borrower_email + "&borrower_phone1=" + borrower_phone1 + "&borrower_first_name=" + borrower_first_name + "&borrower_last_name=" + borrower_last_name + "&campaign_name=" + campaign_name + "&custom_field1=" + custom_field1;

		XMLHttp.open("POST", url, true);
		XMLHttp.onreadystatechange = handlerFunction;

		XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		XMLHttp.setRequestHeader("Content-length", params.length);
		XMLHttp.setRequestHeader("Connection", "close");

		XMLHttp.send("postedParams=" + escape(params));

		// disable submit button
		document.getElementById("submit").src = "/images/processing.jpg";
		document.getElementById("submit").disabled = true;
	}

	function handlerFunction() {
		if (XMLHttp.readyState == 4) {
			// go to redirect page [Thank You page]
			//var response = XMLHttp.responseText;
			//window.alert("WebService Response: " + response);
			var redirect_page = document.getElementById("redirect").value;
			window.location.href = "http://www.iffgdirect.com/" + redirect_page;
		}
	}