// JavaScript Document
/*var isValid = "false"
function sinCheck() {
	var sin1 = document.form1.sin1.value;
	var sin2 = document.form1.sin2.value;
	var sin3 = document.form1.sin3.value;
	
	if (sin1.length < 3 || sin2.length < 3 || sin3.length < 3) {
		d = document.getElementById("sin_status");
		d.innerHTML = "<font color=\"red\">Invalid S.I.N.</font>";
	}	else {
	
		var num1 = sin1.charAt(0);
		var num2 = sin1.charAt(1);
		var num3 = sin1.charAt(2);
		var num4 = sin2.charAt(0);
		var num5 = sin2.charAt(1);
		var num6 = sin2.charAt(2);
		var num7 = sin3.charAt(0);
		var num8 = sin3.charAt(1);
		var num9 = sin3.charAt(2);
		
		var a = 2 * (num2 + num4 + num6 + num8);
		var b = 0;
		var tmpVar = Math.floor(a / 10000);
		b += tmpVar;                       
		a -= (tmpVar * 10000);
		tmpVar = Math.floor(a / 1000);
		b += tmpVar;                       
		a -= (tmpVar * 1000);
		tmpVar = Math.floor(a / 100);
		b += tmpVar;                       
		a -= (tmpVar * 100);                       
		tmpVar = Math.floor(a / 10);
		b += tmpVar;                       
		a -= (tmpVar * 10);
		tmpVar = Math.floor(a / 1)
		b += tmpVar;
		
		var c = num1 + num3 + num5 + num7;                          
		tmpVar = Math.floor(c / 1000);
		b += tmpVar;
		c -= (tmpVar * 1000);                      
		tmpVar = Math.floor(c / 100);
		b += tmpVar;                                       
		c -= (tmpVar * 100);                       
		tmpVar = Math.floor(c / 10);
		b += tmpVar;                                               
		c -= (tmpVar * 10);
		tmpVar = Math.floor(c / 1)
		b += tmpVar;
		tmpVar = Math.floor(b / 10);
		valid = 10 - (b - (tmpVar * 10));          
		
		var msg = "The Social Insurance Number " + num1 + num2 + num3 +
				" " + num4 + num5 + num6 + " " + num7 + num8 + num9 + " is ";
		
		if (valid == 10) {
			valid -= 10;
		}
								
		if (valid == num9) {
			d = document.getElementById("sin_status");
			d.innerHTML = "<font color=\"green\">Valid S.I.N.</font>";
			isValid = "true";
		}	else {
			d = document.getElementById("sin_status");
			d.innerHTML = "<font color=\"red\">Invalid S.I.N.</font>";
			isValid = "false";
		}
		//alert(msg);
	}
}*/


var numerr = 0;
var errmsg = "The following errors were detected in the form:\n\r\n\r";
function CheckText(fieldname, tmin, tmax, err) {
	if (fieldname.value.length < tmin || fieldname.value.length > tmax) {
		alert("Invalid "+err+".  Field must be at least "+tmin+" characters and no more than "+tmax+" characters.\n\rPlease fix the error and try again.\n\r\n\r");
		fieldname.focus();
		return false;
	} else {
		return true;
	}
}
function CheckNumber(fieldname, tmin, tmax, err) {
	if (fieldname.value.length < tmin || fieldname.value.length > tmax || isNaN(fieldname.value)) {
		alert("Invalid "+err+".  Field must be at least "+tmin+" numerical characters and no more than "+tmax+" numerical characters.\n\rPlease fix the error and try again.\n\r\n\r");
		fieldname.focus();
		return false;
	} else {
		return true;
	}
}
function CheckDropDown(fieldname, err) {
	if (fieldname.options[fieldname.selectedIndex].value == "null") {
		alert("Invalid "+err+".  You must selected a "+err+".\n\rPlease fix the error and try again.\n\r\n\r");
		fieldname.focus();
		return false;
	} else {
		return true;
	}
}
function CheckMinPayment (range_from, range_to, min_range) {
	if (range_from < min_range || range_to < min_range) {
		alert("Our minimum monthly loan payments start as low as $250.  Please correct the Minimum Requested Payment Range and try again.");
		return false;
	} else {
		return true;
	}
}
function validateForm(step) {
	var f = document.form1;
	numerr=0;
	switch (step) {
		case 1:
			if (CheckText(f.lastname, 2, 50, "Last Name") == false) { return false; }
			if (CheckText(f.firstname, 2, 50, "First Name") == false) { return false; }
			if (CheckText(f.emailadd, 6, 255, "Email Address") == false) { return false; }
			if (f.confirmemail.value != f.emailadd.value) {
				alert("Your email address and confirmation email address do not match.\n\rPlease double-check and try again.");
				f.confirmemail.focus();
				return false;
			}
			if (CheckNumber(f.sin1, 3, 3, "Social Insurance Number (Box #1)") == false) { return false; }
			if (CheckNumber(f.sin2, 3, 3, "Social Insurance Number (Box #2)") == false) { return false; }
			if (CheckNumber(f.sin3, 3, 3, "Social Insurance Number (Box #3)") == false) { return false; }
			if (CheckNumber(f.hphone1, 3, 3, "Home Phone Number (Box #1)") == false) { return false; }
			if (CheckNumber(f.hphone2, 3, 3, "Home Phone Number (Box #2)") == false) { return false; }
			if (CheckNumber(f.hphone3, 4, 4, "Home Phone Number (Box #3)") == false) { return false; }
			if (CheckNumber(f.wphone1, 3, 3, "Work Phone Number (Box #1)") == false) { return false; }
			if (CheckNumber(f.wphone2, 3, 3, "Work Phone Number (Box #2)") == false) { return false; }
			if (CheckNumber(f.wphone3, 4, 4, "Work Phone Number (Box #3)") == false) { return false; }
			if (CheckDropDown(f.dbMonth, "Date of Birth (Month)") == false) { return false; }
			if (CheckDropDown(f.dbDay, "Date of Birth (Day)") == false) { return false; }
			if (CheckDropDown(f.dbYear, "Date of Birth (Year)") == false) { return false; }
			if (CheckDropDown(f.fcontact, "Best Contact Time (From time)") == false) { return false; }
			if (CheckDropDown(f.fcontactampm, "Best Contact Time (From AM or PM)") == false) { return false; }
			if (CheckDropDown(f.tcontact, "Best Contact Time (To time)") == false) { return false; }
			if (CheckDropDown(f.tcontactampm, "Best Contact Time (To AM or PM)") == false) { return false; }
			break;
		case 2:
			if (CheckDropDown(f.residencetype, "Residence Type") == false) { return false; }
			if (CheckText(f.landlordbank, 2, 50, "Landlord Name or Bank Name") == false) { return false; }
			//CheckNumber(f.phone1, 3, 3, "Landlord or Bank Phone Number (Box #1)");
			//CheckNumber(f.phone2, 3, 3, "Landlord or Bank Phone Number (Box #2)");
			//CheckNumber(f.phone3, 4, 4, "Landlord or Bank Phone Number (Box #3)");
			// CHECK RESIDENCE DURATION
			if (f.resdurationyears.value < 1 && f.resdurationmonths.value < 1) {
				alert("Invalid Residence Duration.  You must have lived at this residence for at least  1 month.\n\rPlease correct the error and try again.");
				f.resdurationyears.focus();
				return false;
			}
			// CHECK RENT/MORTGAGE
			if (f.rentmortgage.value == "") {
				alert("Invalid Rent or Mortgage.  If you do not pay any rent/mortgage, simply enter 0.\n\rPlease correct the error and try again.");
				f.rentmortgage.focus();
				return false;
			}
			if (CheckText(f.address1, 2, 50, "Address (Line #1)") == false) { return false; }
			if (CheckText(f.city, 2, 50, "City") == false) { return false; }
			if (CheckText(f.province, 2, 50, "Province") == false) { return false; }
			if (CheckText(f.country, 2, 50, "Country") == false) { return false; }
			// CHECK POSTAL CODE
			//f.postalcode.value = f.pc1.value + "" + f.pc2.value;
			if (CheckText(f.postalcode, 6, 6, "Postal Code")) {
				pc1 = f.postalcode.value.substr(0,1);
				pc2 = f.postalcode.value.substr(1,1);
				pc3 = f.postalcode.value.substr(2,1);
				pc4 = f.postalcode.value.substr(3,1);
				pc5 = f.postalcode.value.substr(4,1);
				pc6 = f.postalcode.value.substr(5,1);
				if (!isNaN(pc1) || isNaN(pc2) || !isNaN(pc3) || isNaN(pc4) || !isNaN(pc5) || isNaN(pc6)) {
					alert("Invalid Postal Code Format.  Please ensure the correct postal code format (ex. A5A5A5).\n\rPlease correct the error and try again.");
					f.postalcode.focus();
					return false;
				}
			}
			break;
		case 3:
			if (CheckText(f.employername, 2, 50, "Employer Name") == false) { return false; }
			// CHECK WORK DURATION
			if (f.workdurationyears.value < 1 && f.workdurationmonths.value < 1) {
				alert("Invalid Work Duration.  You must have worked for this employer for at least  1 month.\n\rPlease correct the error and try again.");
				f.workdurationyears.focus();
				return false;
			}
			if (CheckNumber(f.grossmonthlyincome, 2, 8, "Gross Monthly Income") == false) { return false; }
			break;
		case 4:
			if (CheckNumber(f.pmtrangefrom, 2, 4, "Requested Payment Range (From amount)") == false) { return false; }
			if (CheckNumber(f.pmtrangeto, 2, 4, "Requested Payment Range (To amount)") == false) { return false; }
			if (CheckNumber(f.down_payment, 3, 6, "Down-Payment") == false) { return false; }
			if (f.user_agreement.checked == false) {
				alert("You must agree the End User Agreement before submitting your application.");
				f.user_agreement.focus();
				return false;
			}
			if (CheckMinPayment (f.pmtrangefrom.value, f.pmtrangeto.value, 250) == false) { return false; }
			break;
	}
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}


