mortgage_prequal_form = function() {
	
	return {
		
		initAfterFormLoad : function() {
			
			// Note: Called by the popup form itself, not the parent page that contains the popup.
			
			// Set the tabindex on the controls 
			var inputs = $("#prequal > :input").get();
			var i_qty = inputs.length;
			for (var i = 0; i < i_qty; i++) {
				$(inputs[i]).attr("tabindex", 100+i);
			}
			
			// Reset the tabindex on a few of the fields
			/* not working @@
			var a = Number($("#prequal_Debt_Name_1").attr("tabindex"));
			$("#prequal_Debt_Name_2").attr("tabindex", a+3);
			$("#prequal_Debt_Name_3").attr("tabindex", a+6);
			$("#prequal_Debt_Name_4").attr("tabindex", a+9);
			$("#prequal_Debt_UnpaidOwed_1").attr("tabindex", a+1);
			$("#prequal_Debt_UnpaidOwed_2").attr("tabindex", a+4);
			$("#prequal_Debt_UnpaidOwed_3").attr("tabindex", a+7);
			$("#prequal_Debt_UnpaidOwed_4").attr("tabindex", a+10);
			$("#prequal_Debt_MonthlyPayment_1").attr("tabindex", a+2);
			$("#prequal_Debt_MonthlyPayment_2").attr("tabindex", a+5);
			$("#prequal_Debt_MonthlyPayment_3").attr("tabindex", a+8);
			$("#prequal_Debt_MonthlyPayment_4").attr("tabindex", a+11);*/
			
			// Hide the co-borrower controls
			// Setup the ability to toggle their display
			$("#prequal .co-borrower").hide();
			$("#prequal_IncludeCoBorrower").click(function() {
				if ($("#prequal_IncludeCoBorrower").is(":checked")) {
					$("#prequal .co-borrower").show();
				} else {
					$("#prequal .co-borrower").hide();
				}
			});
			
			$("#prequal_Name_1").focus();
		},
	
		submit : function(region, communityName, lotAddress, planID) {
			
			// Sorted by the order they're shown
			var parameters = {
				Name_1 : $("#prequal_Name_1").val(),
				PrimaryPhone_1 : $("#prequal_PrimaryPhone_1").val(),
				AlternatePhone_1 : $("#prequal_AlternatePhone_1").val(),
				WorkPhone_1 : $("#prequal_WorkPhone_1").val(),
				WorkPhoneExt_1 : $("#prequal_WorkPhoneExt_1").val(),
				Email_1 : $("#prequal_Email_1").val(),
				SS_1 : $("#prequal_SS_1").val(),
				DOB_1 : $("#prequal_DOB_1").val(),
				Address_1 : $("#prequal_Address_1").val(),
				Apt_1 : $("#prequal_Apt_1").val(),
				City_1 : $("#prequal_City_1").val(),
				State_1 : $("#prequal_State_1").val(),
				Zip_1 : $("#prequal_Zip_1").val(),
				HowLongAtAdrs_1 : $("#prequal_HowLongAtAdrs_1").val(),
				AreYouMarried_1 : $("#prequal_AreYouMarried_Yes_1").is(":checked") ? "Yes" : 
					$("#prequal_AreYouMarried_No_1").is(":checked") ? "No" : "",				
				Employer_1 : $("#prequal_Employer_1").val(),
				MonthlyGrossIncome_1 : $("#prequal_MonthlyGrossIncome_1").val(),
				TimeOnJob_1 : $("#prequal_TimeOnJob_1").val(),
				PreviousEmployer_1 : $("#prequal_PreviousEmployer_1").val(),
				HowPaid_Salary_1 : $("#prequal_HowPaid_Salary_1").is(":checked"),
				HowPaid_Hourly_1 : $("#prequal_HowPaid_Hourly_1").is(":checked"),
				HowPaid_Overtime_1 : $("#prequal_HowPaid_Overtime_1").is(":checked"),
				HowPaid_BonusComm_1 : $("#prequal_HowPaid_BonusComm_1").is(":checked"),
				Savings_1 : $("#prequal_Savings_1").val(),
				Signature_1 : $("#prequal_Signature_1").val(),
				
				Name_2 : $("#prequal_Name_2").val(),
				PrimaryPhone_2 : $("#prequal_PrimaryPhone_2").val(),
				AlternatePhone_2 : $("#prequal_AlternatePhone_2").val(),
				WorkPhone_2 : $("#prequal_WorkPhone_2").val(),
				WorkPhoneExt_2 : $("#prequal_WorkPhoneExt_2").val(),
				Email_2 : $("#prequal_Email_2").val(),
				SS_2 : $("#prequal_SS_2").val(),
				DOB_2 : $("#prequal_DOB_2").val(),
				Address_2 : $("#prequal_Address_2").val(),
				Apt_2 : $("#prequal_Apt_2").val(),
				City_2 : $("#prequal_City_2").val(),
				State_2 : $("#prequal_State_2").val(),
				Zip_2 : $("#prequal_Zip_2").val(),
				HowLongAtAdrs_2 : $("#prequal_HowLongAtAdrs_2").val(),
				AreYouMarried_2 : $("#prequal_AreYouMarried_Yes_2").is(":checked") ? "Yes" : $("#prequal_AreYouMarried_No_2").is(":checked") ? "No" : "",				
				Employer_2 : $("#prequal_Employer_2").val(),
				MonthlyGrossIncome_2 : $("#prequal_MonthlyGrossIncome_2").val(),
				TimeOnJob_2 : $("#prequal_TimeOnJob_2").val(),
				PreviousEmployer_2 : $("#prequal_PreviousEmployer_2").val(),
				HowPaid_Salary_2 : $("#prequal_HowPaid_Salary_2").is(":checked"),
				HowPaid_Hourly_2 : $("#prequal_HowPaid_Hourly_2").is(":checked"),
				HowPaid_Overtime_2 : $("#prequal_HowPaid_Overtime_2").is(":checked"),
				HowPaid_BonusComm_2 : $("#prequal_HowPaid_BonusComm_2").is(":checked"),
				Savings_2 : $("#prequal_Savings_2").val(),
				Signature_2 : $("#prequal_Signature_2").val(),
				
				Debt_Name_1 : $("#prequal_Debt_Name_1").val(),
				Debt_Name_2 : $("#prequal_Debt_Name_2").val(),
				Debt_Name_3 : $("#prequal_Debt_Name_3").val(),
				Debt_Name_4 : $("#prequal_Debt_Name_4").val(),
				
				Debt_UnpaidOwed_1 : $("#prequal_Debt_UnpaidOwed_1").val(),
				Debt_UnpaidOwed_2 : $("#prequal_Debt_UnpaidOwed_2").val(),
				Debt_UnpaidOwed_3 : $("#prequal_Debt_UnpaidOwed_3").val(),
				Debt_UnpaidOwed_4 : $("#prequal_Debt_UnpaidOwed_4").val(),
				
				Debt_MonthlyPayment_1 : $("#prequal_Debt_MonthlyPayment_1").val(),
				Debt_MonthlyPayment_2 : $("#prequal_Debt_MonthlyPayment_2").val(),
				Debt_MonthlyPayment_3 : $("#prequal_Debt_MonthlyPayment_3").val(),
				Debt_MonthlyPayment_4 : $("#prequal_Debt_MonthlyPayment_4").val(),
				
				Signature_1 : $("#prequal_Signature_1").val(),
				Signature_2 : $("#prequal_Signature_2").val(),
				
				FreeCreditReport : $("#prequal_FreeCreditReport_Yes").is(":checked") ? "Yes" : $("#prequal_FreeCreditReport_No").is(":checked") ? "No" : "",
				Comment : $("#prequal_Comment").val(),
				
				CommunityName: communityName, 
				LotAddress: lotAddress, 
				PlanID: planID,
				
				Region : region
			};
			
			var errorMsg = new Array();
			
			// ---------------------------
			// Primary borrower validation
			
			if (parameters.Name_1 == "") { errorMsg.push("Primary borrower Name is a required value"); }
			else if (parameters.Name_1.indexOf(" ") == -1) { errorMsg.push("Primary borrow first and last name is required"); }
			
			if (parameters.PrimaryPhone_1 == "") { errorMsg.push("Primary borrower Primary Phone is a required value"); }
			
			// Tested
			if (parameters.Email_1 == "") { errorMsg.push("Primary borrower Email Address is a required value"); }
			else if (!fieldstone.validation.isValidEmail(parameters.Email_1)) { errorMsg.push("Primary borrower Email Address is bad"); }
			
			if (parameters.Signature_1 == "") { errorMsg.push("Borrower Signature is a required value. Type your name."); }
			
			// ----------------------
			// Co-borrower validation
			
			if ($("#prequal_IncludeCoBorrower").is(":checked")) {
					
				if (parameters.Name_2 == "") { errorMsg.push("Co-Borrower Name is a required value"); }
				else if (parameters.Name_2.indexOf(" ") == -1) { errorMsg.push("Co-Borrow first and last name is required"); }
			
				if (parameters.PrimaryPhone_2 == "") { errorMsg.push("Co-Borrower Primary Phone is a required value"); }
				
				// Tested
				if (parameters.Email_2 == "") { errorMsg.push("Co-Borrower Email Address is a required value"); }
				else if (!fieldstone.validation.isValidEmail(parameters.Email_2)) { errorMsg.push("Co-Borrower Email Address is bad"); }
				
				if (parameters.Signature_2 == "") { errorMsg.push("Co-Borrower Signature is a required value. Type your name."); }
			}
			
			// Tested
			if (errorMsg.length > 0) {
				$("#prequal_Error").html(errorMsg.join('<br />')).show();
				return;
			}
			
			mortgage_prequal_form.send(parameters);
		},
		
		send : function(data) {
			// Returns boolean
			
			// Google AdWord tracking 			
			$("<div style='display:inline;'><img height='1' width='1' style='border-style:none;' alt='' src='http://www.googleadservices.com/pagead/conversion/1040454231/?label=DePNCJ_94wEQ16SQ8AM&amp;guid=ON&amp;script=0'/></div>");
			
			var url = "https://" + document.location.host + "/actions/ajax/mortgage_prequal_submit.php";
			$.ajax({ 
				cache: false,
				data: data,
				dataType: "text",
				error: function(XMLHttpRequest, textStatus) {
					// Tested %%
					window.alert(textStatus);
					$("<div title='Mortgage Pre-Qualification Request Status'>Error (1). Unable to process your mortgage pre-qualification request. Please try again or use our phone in the mean time.</div>").dialog({buttons: {"OK": function() {$(this).dialog("close");}}});
				},
				success: function(response) {
					if (response == "OK") {
						// Tested %%
						var msg = "<span style='font-size:200%;font-weight:bold'>Mortgage pre-qualification request processed</span><br /><br />";
						msg += "To further speed up your qualification process you should begin assembling the following documents. This is a fundamental requirement of every mortgage application<br />";
						msg += "<ol><li>Most recent pay stubs.</li>";
						msg += "<li>Last 2 years of W 2's and tax returns.</li>";
						msg += "<li>If self employed, year to date (or last years) profit and loss balance sheet.</li>";
						msg += "<li>Most recent 2 months bank statements.</li>";
						msg += "<li>Residence address and landlord contact information for last 2 years.</li>";
						msg += "<li>List od debts with balances and monthly payments.</li>";
						msg += "<li>Divorce decree/bankruptcy papers (if applicable).</li>";
						msg += "</ol>";
						$("<div title='Mortgage Pre-Qualification Request Status'>" + msg + "</div>").dialog({buttons: {"OK": function() {window.close();}}});
					} else {
						// Tested %%
						$("<div title='Mortgage Pre-Qualification Request Status'>Error (2). Unable to process your mortgage pre-qualification request. Please try again or use our phone in the mean time.</div>").dialog({buttons: {"OK": function() {$(this).dialog("close");}}});
					}
				},
				type: 'POST',
				url: url
			});
		}		
	}
}();