// This library contains useful client-side script functions for checking mortgage forms.
// Requires checks.js to be included before this one

// This function will check the format of a social insurance number
function checkSIN(strSIN)
{
	var strDigits = "0123456789";
	var bValid = true;
	var nLen = strSIN.length;
	if (nLen < 9 || nLen > 11) {
		bValid = false;
	} else {
		var i;
		var nDigits = 0;
		for (i=0; i < nLen; i++) {
			var ch = strSIN.charAt(i).toUpperCase();
			if (strDigits.indexOf(ch, 0) < 0) {
				// We only allow space or dash after every 3rd character
				if (nDigits % 3 != 0 || "- ".indexOf(ch, 0) < 0)
					break;
			} else {
				nDigits = nDigits + 1;
            }
		}
		if (i < nLen)
			bValid = false;
	}

	return bValid;
}

function check_mf1(oForm)
{
    if (oForm.Name.value == "") {
		alert("A name is required.");
		oForm.Name.focus();
		return false;
	}

	var strAddress = "" + oForm.Address.value;
	if (strAddress == "") {
		alert("Street address is required.");
		oForm.Address.focus();
		return false;
	}

	var strPCode = "" + oForm.PCode.value;
	if (strPCode == "") {
		alert("Postal code is required.");
		oForm.PCode.focus();
		return false;
	} else if (checkPCode(strPCode) == false) {
		alert("Postal codes have the form ANA-NAN.");
		oForm.PCode.focus();
		return false;
	} else {
        oForm.PCode.value = strPCode.toUpperCase();
    }

	if (oForm.City.value  == "") {
		alert("City is required.");
		oForm.City.focus();
		return false;
	}

	if (checkFormPhone(oForm.dayPhoneArea, oForm.dayPhonePrefix, oForm.dayPhoneNum, oForm.dayphone) == false)
		return false;
		
	if (checkFormPhone(oForm.nightPhoneArea, oForm.nightPhonePrefix, oForm.nightPhoneNum, oForm.nightphone) == false)
		return false;

    var strDay = "" + oForm.dayphone.value;
    var strNight = "" + oForm.nightphone.value;
    if (strDay == "" && strNight == "") {
		alert("A phone number is required.");
		oForm.dayPhoneArea.focus();
		return false;
    }
    
	var strSIN ="" +  oForm.SIN.value;
	if (strSIN == "") {
		alert("Social Insurance Number is required.");
		oForm.SIN.focus();
		return false;
	} else if (checkSIN(strSIN) == false) {
		alert("Format of Social Insurance Number is nnn-nnn-nnn");
		oForm.SIN.focus();
		return false;
	}
	
	if (oForm.BDate.value == "") {
		alert("Birth date is required.");
		oForm.BDate.focus();
		return false;
	}
	
	if (oForm.HowLong.value == "") {
		alert("How long at " + strAddress + " is required.");
		oForm.HowLong.focus();
		return false;
	}

	if (oForm.JointName.value != "") {
		var strJointAddr = "" + oForm.JointAddress.value;
		if (strJointAddr  == "")
		{
			alert("Street address is required for joint applicant.");
			oForm.JointAddress.focus();
			return false;
		}

		var strJointPCode = "" + oForm.JointPCode.value;
		if (strJointPCode == "") {
			alert("Postal code is required for joint applicant.");
			oForm.JointPCode.focus();
			return false;
		} else if (checkPCode(strJointPCode) == false) {
			alert("Postal codes have the form ANA-NAN.");
			documemnt.oForm.JointPCode.focus();
			return false;
		} else {
            oForm.JointPCode.value = strJointPCode.toUpperCase();
        }

		if (oForm.JointCity.value  == "") {
			alert("City is required for joint applicant.");
			oForm.JointCity.focus();
			return false;
		}

	    if (checkFormPhone(oForm.jointdayPhoneArea, oForm.jointdayPhonePrefix, oForm.jointdayPhoneNum, oForm.jointdayphone) == false)
		    return false;
    		
	    if (checkFormPhone(oForm.jointnightPhoneArea, oForm.jointnightPhonePrefix, oForm.jointnightPhoneNum, oForm.jointnightphone) == false)
		    return false;

        var strDay = "" + oForm.jointdayphone.value;
        var strNight = "" + oForm.jointnightphone.value;
        if (strDay == "" && strNight == "") {
		    alert("A phone number is required for joint applicant.");
		    oForm.jointdayPhoneArea.focus();
		    return false;
        }

		var strJointSIN ="" +  oForm.JointSIN.value;
		if (strJointSIN == "") {
			alert("Social Insurance Number is required for joint applicant");
			oForm.JointSIN.focus();
			return false;
		} else if (checkSIN(strJointSIN) == false) {
			alert("Format of Social Insurance Number is nnn-nnn-nnn");
			oForm.JointSIN.focus();
			return false;
		}
	
		if (oForm.JointBDate.value == "") {
			alert("Birth date is required for joint applicant.");
			oForm.JointBDate.focus();
			return false;
		}
	
		if (oForm.JointHowLong.value == "") {
			alert("How long at " + strJointAddr + " is required for joint applicant.");
			oForm.JointHowLong.focus();
			return false;
		}
	}	

	return true;
}
function check_mf2(oForm, strJointName)
{
    if (oForm.EmployerName.value == "") {
		alert("Employer name is required.");
		oForm.EmployerName.focus();
		return false;
	}
    if (oForm.Occupation.value == "") {
        alert("Occupation is required.");
        oForm.Occupation.focus();
        return false;
    }
    if (oForm.Salary.value == "") {
        alert("Gross annual salary is required.");
        oForm.Salary.focus();
        return false;
    }
	if (strJointName != "") {
	    if (oForm.JointEmployerName.value == "") {
			alert("Employer name is required for joint applicant.");
			oForm.JointEmployerName.focus();
			return false;
		}

	    if (oForm.JointOccupation.value == "") {
        	alert("Occupation is required for joint applicant.");
	        oForm.JointOccupation.focus();
    	    return false;
	    }

	    if (oForm.JointSalary.value == "") {
	        alert("Gross annual salary is required for joint applicant.");
        	oForm.JointSalary.focus();
    	    return false;
	    }
	}

	var strEmail = oForm.email.value;
    var bSignUp = false;
/*    if (oForm.signup.checked == true) {
        if (strEmail == "") {
            alert("A valid eMail address must be supplied in order to sign up for our FREE newsletter.");
            oForm.email.focus();
            return false;
        }
    }
*/
	if (strEmail != "" && checkEmailAddress(strEmail) == false) {
		alert("E-mail address must be of the form name@domain.xxx");
		oForm.email.focus();
		return false;
	}
    var str = ""+oForm.how_did_you_find_us.value;
    if (str == "" || str == "---- Select One ----") {
        alert("Please indicate how you found us.");
        oForm.how_did_you_find_us.focus();
        return false;
    }
	return true;
}

function check_oef1(oForm)
{
    if (oForm.FirstName.value == "") {
		alert("First Name is required.");
		oForm.FirstName.focus();
		return false;
	}

	if (oForm.LastName.value == "") {
		alert("Last Name is required.");
		oForm.LastName.focus();
		return false;
	}

	if (checkFormPhone(oForm.dayPhoneArea, oForm.dayPhonePrefix, oForm.dayPhoneNum, oForm.dayphone) == false)
		return false;
		
	if (checkFormPhone(oForm.nightPhoneArea, oForm.nightPhonePrefix, oForm.nightPhoneNum, oForm.nightphone) == false)
		return false;

    var strDay = "" + oForm.dayphone.value;
    var strNight = "" + oForm.nightphone.value;
    if (strDay == "" && strNight == "") {
		alert("A phone number is required.");
		oForm.dayPhoneArea.focus();
		return false;
   }
	
	if (oForm.Street.value == "") {
		alert("Property Address is required.");
		oForm.Street.focus();
		return false;
	}

	if (oForm.City.value  == "") {
		alert("City is required.");
		oForm.City.focus();
		return false;
	}
	
	return true;

	}
	
function check_oef2(oForm)
{
    if (oForm.HouseSize.value == "") {
		alert("Square footage of property is required.");
		oForm.HouseSize.focus();
		return false;
	}

    if (oForm.BedsUp.value == "" && oForm.BedsDown.value == "") {
        alert("Number of Bedrooms is required.");
        oForm.BedsUp.focus();
        return false;
    }

    if (oForm.HalfBaths.value == "" && oForm.FullBaths.value == "") {
        alert("Number of Bathrooms is required.");
        oForm.FullBaths.focus();
        return false;
    }

    if (oForm.TypeOfHome.value == " ") {
		alert("Property Type is required.");
		oForm.TypeOfHome.focus();
		return false;
	}

    if (oForm.Style.value == " ") {
		alert("Property Style is required.");
		oForm.Style.focus();
		return false;
	}

    if (oForm.BasementType.value == " ") {
		alert("Basement Type is required.");
		oForm.BasementType.focus();
		return false;
	}

	var strEmail = oForm.email.value;
    var bSignUp = false;
/*    if (oForm.signup.checked == true) {
        if (strEmail == "") {
            alert("A valid eMail address must be supplied in order to sign up for our FREE newsletter.");
            oForm.email.focus();
            return false;
        }
    }
*/    
	if (strEmail != "" && checkEmailAddress(strEmail) == false) {
		alert("E-mail address must be of the form name@domain.xxx");
		oForm.email.focus();
		return false;
	}
    var str = ""+oForm.how_did_you_find_us.value;
    if (str == "" || str == "---- Select One ----") {
        alert("Please indicate how you found us.");
        oForm.how_did_you_find_us.focus();
        return false;
    }
	return true;
}
