$(document).ready(function()
{ 
	var txtWorkPhone = $("#" + txtWorkPhone_ClientID);
	var txtCellPhone = $("#" + txtCellPhone_ClientID);
	
	txtWorkPhone.blur(function() { clearTelephoneBracket(txtWorkPhone_ClientID); }); 
	txtCellPhone.blur(function() { clearTelephoneBracket(txtCellPhone_ClientID); }); 
 
}); 



goToTop = function()
{
	setTimeout("document.getElementById('home').focus()", 1500);
}



clearTelephoneBracket = function (txtBoxID)
{
	var txtBox = $("#" + txtBoxID);
	if (txtBox.val() == "(") txtBox.val("");
}



/***** GENERAL INFORMATION *******************************************/
checkGeneralInformation = function(btnNext)
{

	if (!checkRequiredField(txtFirstName_ClientID, "tdFirstName", "Please enter your first name.")) { }
	else if (!checkRequiredField(txtLastName_ClientID, "tdLastName", "Please enter your last name.")) { }
	else if (!checkRequiredField(dropMonth_ClientID, "tdBirthDate", "Please select your birth month.")) { }
	else if (!checkRequiredField(dropDay_ClientID, "tdBirthDate", "Please select your birth day.")) { }
	else if (!checkRequiredField(dropYear_ClientID, "tdBirthDate", "Please select your birth year.")) { }
	else if (!checkDateField(dropYear_ClientID, dropMonth_ClientID, dropDay_ClientID, "tdBirthDate", "Please select a valid birth date.")) { }
	else if (!checkRequiredField(dropMaritalStatus_ClientID, "tdMaritalStatus", "Please select your marital status.")) { }
	else if (!checkTelephoneField(txtHomePhone_ClientID, "tdHomePhone", "Please enter your home telephone number.")) { }
	else if (!checkEmailField(txtEmailAddress_ClientID, "tdEmailAddress", "Please enter a valid email address.")) { }
	else if (!compareEqualFields(txtEmailAddress_ClientID, txtConfirmEmail_ClientID, "tdEmailAddress", "tdConfirmEmail", "Your email addresses do not match.")) { }
	else { return true;	}

	return false;
}



/***** RESIDENCE INFORMATION *****************************************/
checkResidenceInformation = function()
{
	if (!checkRequiredField(dropResidence_ClientID, "tdResidence", "Please select your residence.")) { }
	else if (!checkRequiredField(txtAddress_ClientID, "tdAddress", "Please enter your address.")) { }
	else if (!checkRequiredField(txtCity_ClientID, "tdCity", "Please enter your city.")) { }
	else if (!checkIntegerField(txtResidenceYears_ClientID, "tdResidenceLength", "Please enter the number of years you have lived at your current residence.")) { }
	else if (!checkIntegerField(txtResidenceMonths_ClientID, "tdResidenceLength", "Please enter the number of months you have lived at your current residence.")) { }
	else if (!checkMoneyField(true, txtMonthlyPayment_ClientID, "tdMonthlyPayment", "Please enter your monthly payment.")) { }
	else if (!checkIntegerField(txt100kYears_ClientID, "tdWithin100k", "Please enter the number of years you have lived within 100km of your current residence.")) { }
	else if (!checkIntegerField(txt100kMonths_ClientID, "tdWithin100k", "Please enter the number of months you have lived within 100km of your current residence.")) { }
	else { return true; }
	
	return false;
}



/***** PREVIOUS RESIDENCE INFORMATION ********************************/
checkPreviousResidenceInformation = function()
{
	if (!checkIntegerField(txtTimesMoved_ClientID, "tdTimesMoved", "Please enter the number of times you've moved in the last three years.")) { }
	else if (!checkRequiredField(dropPreviousResidence_ClientID, "tdPreviousResidence", "Please select your previous residence.")) { }
	else if (!checkRequiredField(txtPreviousAddress_ClientID, "tdPreviousAddress", "Please enter your previous address.")) { }
	else if (!checkRequiredField(txtPreviousCity_ClientID, "tdPreviousCity", "Please enter your previous city.")) { }
	else if (!checkIntegerField(txtPreviousResidenceYears_ClientID, "tdPreviousResidenceLength", "Please enter the number of years you lived at your previous residence.")) { }
	else if (!checkIntegerField(txtPreviousResidenceMonths_ClientID, "tdPreviousResidenceLength", "Please enter the number of months you lived at your previous residence.")) { }
	else if (!checkMoneyField(true, txtPreviousMonthlyPayment_ClientID, "tdPreviousMonthlyPayment", "Please enter your previous monthly payment.")) { }
	else { return true; }
	
	return false;
}



/***** INCOME/EMPLOYMENT INFORMATION *********************************/
checkEmploymentInformation = function()
{
	var primaryIncome = $("#" + dropPrimaryIncome_ClientID).val();
	
	var dropOtherIncome1 = $("#" + dropOtherIncome1_ClientID);
	var dropOtherIncome2 = $("#" + dropOtherIncome2_ClientID);
	var txtOtherIncomeAmount1 = $("#" + txtOtherIncomeAmount1_ClientID);
	var txtOtherIncomeAmount2 = $("#" + txtOtherIncomeAmount2_ClientID);
	var txtOtherIncomeName1 = $("#" + txtOtherIncomeName1_ClientID);
	var txtOtherIncomeName2 = $("#" + txtOtherIncomeName2_ClientID);
	
	var validEmployer =	(
							(primaryIncome != "Full Time Employment" && primaryIncome != "Part Time Employment")
							|| (
								(primaryIncome == "Full Time Employment" || primaryIncome == "Part Time Employment")
								&& ($("#" + txtEmployerName_ClientID).val() != "")
							)
						);
								
	var validOccupation =	(
								(primaryIncome != "Full Time Employment" && primaryIncome != "Part Time Employment")
								|| (
									(primaryIncome == "Full Time Employment" || primaryIncome == "Part Time Employment")
									&& ($("#" + txtOccupation_ClientID).val() != "")
								)
							);
	
	if (primaryIncome == "Homemaker") $("#" + txtGrossMonthlyIncome_ClientID).val("0");

	if (!checkRequiredField(dropPrimaryIncome_ClientID, "tdPrimaryIncome", "Please select your primary income.")) { }
	else if (!checkCustomField(((primaryIncome != "Other") || (primaryIncome == "Other" && $("#" + txtOtherIncome_ClientID).val() != "")), txtOtherIncome_ClientID, "tdOtherIncome", "Please enter the source of your primary income.")) { }
	else if (!checkIntegerField(txtEmploymentYears_ClientID, "tdEmploymentLength", "Please enter the number of years you have been earning your primary income.")) { }
	else if (!checkIntegerField(txtEmploymentMonths_ClientID, "tdEmploymentLength", "Please enter the number of months you have been earning your primary income.")) { }
	else if (!checkMoneyField(true, txtGrossMonthlyIncome_ClientID, "tdGrossMonthlyIncome", "Please enter your gross monthly income.")) { }
	else if (!checkCustomField(validEmployer, txtEmployerName_ClientID, "tdEmployerName", "Please enter your employer's name.")) { }
	else if (!checkCustomField(validOccupation, txtOccupation_ClientID, "tdOccupation", "Please enter your occupation.")) { }
	else if (!checkCustomField(((dropOtherIncome1.val() != "Other") || ((dropOtherIncome1.val() == "Other") && txtOtherIncomeName1.val() != "")), txtOtherIncomeName1_ClientID, "tdOtherIncome1", "Please enter the source of your other income.")) { }
	else if (!checkCustomField((dropOtherIncome1.val() == "" || (dropOtherIncome1.val() != "" && NumberMask.test(txtOtherIncomeAmount1.val()))), txtOtherIncomeAmount1_ClientID, "tdOtherIncome1", "Please enter the amount of your other income.")) { }
	else if (!checkCustomField(((dropOtherIncome2.val() != "Other") || ((dropOtherIncome2.val() == "Other") && txtOtherIncomeName2.val() != "")), txtOtherIncomeName2_ClientID, "tdOtherIncome2", "Please enter the source of your other income.")) { }
	else if (!checkCustomField((dropOtherIncome2.val() == "" || (dropOtherIncome2.val() != "" && NumberMask.test(txtOtherIncomeAmount2.val()))), txtOtherIncomeAmount2_ClientID, "tdOtherIncome2", "Please enter the amount of your other income.")) { }
	else
	{
		if($("#" + txtEmploymentYears_ClientID).val() > 2 && applicantType == "secondary") goToTop();
		return true;
	}
	
	return false;
}



/***** PREVIOUS INCOME/EMPLOYMENT INFORMATION ************************/
checkPreviousEmploymentInformation = function()
{
	var txtTimesEmployed = $("#" + txtTimesEmployed_ClientID);
	var txtPreviousEmployerName = $("#" + txtPreviousEmployerName_ClientID);
	var txtPreviousOccupation = $("#" + txtPreviousOccupation_ClientID);
	var txtPreviousEmploymentYears = $("#" + txtPreviousEmploymentYears_ClientID);
	var txtPreviousEmploymentMonths = $("#" + txtPreviousEmploymentMonths_ClientID);
	
	var txtPreviousGrossMonthlyIncome = $("#" + txtPreviousGrossMonthlyIncome_ClientID);

	var previousPrimaryIncome = $("#" + dropPreviousPrimaryIncome_ClientID).val();

	var validPreviousEmployer =	(
									(previousPrimaryIncome != "Full Time Employment" && previousPrimaryIncome != "Part Time Employment")
									|| (
										(previousPrimaryIncome == "Full Time Employment" || previousPrimaryIncome == "Part Time Employment")
										&& ($("#" + txtPreviousEmployerName_ClientID).val() != "")
									)
								);
								
	var validPreviousOccupation =	(
										(previousPrimaryIncome != "Full Time Employment" && previousPrimaryIncome != "Part Time Employment")
										|| (
											(previousPrimaryIncome == "Full Time Employment" || previousPrimaryIncome == "Part Time Employment")
											&& ($("#" + txtPreviousOccupation_ClientID).val() != "")
										)
									);
	if (previousPrimaryIncome == "Homemaker") txtPreviousGrossMonthlyIncome.val("0");
	
	if (!checkIntegerField(txtTimesEmployed_ClientID, "tdTimesEmployed", "Please enter the number of times you've changed employment in the last three years.")) { }
	else if (!checkRequiredField(dropPreviousPrimaryIncome_ClientID, "tdPreviousPrimaryIncome", "Please select your previous primary income.")) { }
	else if (!checkCustomField(((previousPrimaryIncome != "Other") || (previousPrimaryIncome == "Other" && $("#" + txtPreviousOtherIncome_ClientID).val() != "")), txtPreviousOtherIncome_ClientID, "tdPreviousOtherIncome", "Please enter the source of your previous primary income.")) { }
	else if (!checkCustomField(validPreviousEmployer, txtPreviousEmployerName_ClientID, "tdPreviousEmployerName", "Please enter your previous employer's name.")) { }
	else if (!checkCustomField(validPreviousOccupation, txtPreviousOccupation_ClientID, "tdPreviousOccupation", "Please enter your previous occupation.")) { }
	else if (!checkIntegerField(txtPreviousEmploymentYears_ClientID, "tdPreviousEmploymentLength", "Please enter the number of years of your previous employment.")) { }
	else if (!checkIntegerField(txtPreviousEmploymentMonths_ClientID, "tdPreviousEmploymentLength", "Please enter the number of months of your previous employment.")) { }
	else if (!checkMoneyField(false, txtPreviousGrossMonthlyIncome_ClientID, "tdPreviousGrossMonthlyIncome", "Please enter a valid previous monthly income.")) { }
	else
	{
		if (applicantType == "secondary")
			goToTop();

		return true;
	}
	
	return false;
}



/***** VEHICLE INFORMATION *******************************************/
checkVehicleInformation = function()
{
	var rboTradeVehicle_0 = document.getElementById(rblTradeVehicle_ClientID + "_0");
	var rboTradeVehicle_1 = document.getElementById(rblTradeVehicle_ClientID + "_1");
	var txtTradeVehicleDescription = $("#" + txtTradeVehicleDescription_ClientID);
	
	
	var validTradeVehicle =	(
								(rboTradeVehicle_1.checked)
								|| (rboTradeVehicle_0.checked && (txtTradeVehicleDescription.val() != ""))
							);
	
	
	if (!checkCustomField(validTradeVehicle, txtTradeVehicleDescription_ClientID, "tdVehicleDescription", "Please describe your trade vehicle.")) { }
	else { return true; }
	
	return false;
}



btnNo_Click = function(btnNo)
{	
	var valid = false;
	
	valid = checkGeneralInformation();
	valid = checkResidenceInformation();
	valid = checkEmploymentInformation();
	valid = checkVehicleInformation();
	
	if (!valid) return false;
	
	var rboSecondaryApplicant_0 = document.getElementById(rboSecondaryApplicant_ClientID + "_0");
	var rboSecondaryApplicant_1 = document.getElementById(rboSecondaryApplicant_ClientID + "_1");
	
	if (rboSecondaryApplicant_1.checked)
	{
		var returnVal = confirm("Are you sure you want to submit your application without a secondary applicant?  A secondary applicant could strengthen your application. \n\nOK: Continue without a secondary applicant. \n\nCancel:  Continue with a secondary applicant.");
		
		if (!returnVal)
		{
			rboSecondaryApplicant_0.checked = true;
			rboSecondaryApplicant_1.checked = false;
		}
	}
	
	goToTop();
	
	return true;
}



btnDontSubmit_Click = function(btnDontSubmit)
{
	var result = confirm("Are you sure?  You cannot submit your application and obtain credit without agreeing to a credit investigation. \n\nOK: Go back to the credit submission. \n\nCancel:  Your application will not proceed for credit review without Ezee Credit contacting you first.");

	if (!result) { goToTop(); }
	
	return (!(result));
}