//
/////////////////////////////////////////// Form Validation *********************
//
////////////////////////////////// Empty fields ***
//
function isEmpty( str ){
    strRE = new RegExp( );
    strRE.compile( '^[\s ]*$', 'gi' );
    return strRE.test( str.value );
}
/////////////////////////////// Validate Email Address ***
function notValidEmail( str ){
    mailRE = new RegExp( );
    mailRE.compile( '^[\._a-z0-9-]+@[\.a-z0-9-]+[\.]{1}[a-z]{2,4}$', 'gi' );
    return !(mailRE.test( str.value ));
}
//
////////////////////////////////// Main Validation ***
//
function checkForm( form ){
    if( isEmpty( form.name ) ){
        alert( 'Name is a required field...' );
        document.forms.newsletter.name.focus();
        return false;
    }
    if( isEmpty( form.email ) ){
        alert( 'Email address is a required field...' );
        document.forms.newsletter.email.focus();
        return false;
    }
    if( notValidEmail( form.email ) ){
        alert( 'Email address is not valid - Please re-enter...' );
        document.forms.newsletter.email.focus();
        return false;
    }
}
//
//////////////////////////////// Pre-Approval Form Validator ***
//
function checkForm1( form ){
    if( isEmpty( form.Applicant_Name ) ){
        alert( 'Please provide your full name...' );
        document.forms.Pre_Approval_Application.Applicant_Name.focus();
	      return false;
    }
    if( isEmpty( form.Applicant_Home_Phone_Areacode ) ){
        alert( 'Please provide your home phone area code...' );
        document.forms.Pre_Approval_Application.Applicant_Home_Phone_Areacode.focus();
	      return false;
    }
    if( isEmpty( form.Applicant_Home_Phone_1 ) ){
        alert( 'Please provide your full home phone number...' );
        document.forms.Pre_Approval_Application.Applicant_Home_Phone_1.focus();
	      return false;
    }
    if( isEmpty( form.Applicant_Home_Phone_2 ) ){
        alert( 'Please provide your full home phone number...' );
        document.forms.Pre_Approval_Application.Applicant_Home_Phone_2.focus();
	      return false;
    }
    if( isEmpty( form.Applicant_Email ) ){
        return true;
    } else {
       if( notValidEmail( form.Applicant_Email ) ){
        alert( 'Email address is not valid - Please re-enter...' );
        document.forms.Pre_Approval_Application.Applicant_Email.focus();
        return false;
       }
    }
}

function checkForm2( form ){
    if( isEmpty( form.Co_Applicant_Name ) ){
        alert( 'Please provide your full name...' );
        document.forms.Pre_Approval_Application.Co_Applicant_Name.focus();
	      return false;
    }
    if( isEmpty( form.Co_Applicant_Home_Phone_Areacode ) ){
        alert( 'Please provide your home phone area code...' );
        document.forms.Pre_Approval_Application.Co_Applicant_Home_Phone_Areacode.focus();
	      return false;
    }
    if( isEmpty( form.Co_Applicant_Home_Phone_1 ) ){
        alert( 'Please provide your full home phone number...' );
        document.forms.Pre_Approval_Application.Co_Applicant_Home_Phone_1.focus();
	      return false;
    }
    if( isEmpty( form.Co_Applicant_Home_Phone_2 ) ){
        alert( 'Please provide your full home phone number...' );
        document.forms.Pre_Approval_Application.Co_Applicant_Home_Phone_2.focus();
	      return false;
    }
    if( isEmpty( form.Co_Applicant_Email ) ){
        return true;
    } else {
       if( notValidEmail( form.Co_Applicant_Email ) ){
        alert( 'Email address is not valid - Please re-enter...' );
        document.forms.Pre_Approval_Application.Co_Applicant_Email.focus();
        return false;
       }
    }
}

function agreement( form ) { 
// Checking for Terms and Conditions is checked 
    if(!document.forms.Pre_Approval_Application.Agree_to_Terms_and_Conditions.checked){
        alert("Please read the 'Terms and Conditions' and check the agreement box"); 
        document.forms.Pre_Approval_Application.Agree_to_Terms_and_Conditions.focus();
        return false;
    } else {
        document.forms.Pre_Approval_Application.action='https://www.karenhall.ca/preapproval/mortpa.php';
    }
}

function agreement2( form ) { 
// Checking for Terms and Conditions is checked 
    if(!document.forms.Pre_Approval_Application.Agree_to_Terms_and_Conditions_2.checked){
        alert("Please read the 'Terms and Conditions' and check the agreement box"); 
        document.forms.Pre_Approval_Application.Agree_to_Terms_and_Conditions_2.focus();
        return false;
    } else {
        document.forms.Pre_Approval_Application.action='https://www.karenhall.ca/preapproval/mortpa.php';
    }
}

//
//////////////////////////////////////// Melissa Rice Pre-App Agreement ************ 
//

function mragreement( form ) { 
// Checking for Terms and Conditions is checked 
    if(!document.forms.Pre_Approval_Application.Agree_to_Terms_and_Conditions.checked){
        alert("Please read the 'Terms and Conditions' and check the agreement box"); 
        document.forms.Pre_Approval_Application.Agree_to_Terms_and_Conditions.focus();
        return false;
    } else {
        document.forms.Pre_Approval_Application.action='https://www.karenhall.ca/preapproval/mrmortpa.php';
    }
}

function mragreement2( form ) { 
// Checking for Terms and Conditions is checked 
    if(!document.forms.Pre_Approval_Application.Agree_to_Terms_and_Conditions_2.checked){
        alert("Please read the 'Terms and Conditions' and check the agreement box"); 
        document.forms.Pre_Approval_Application.Agree_to_Terms_and_Conditions_2.focus();
        return false;
    } else {
        document.forms.Pre_Approval_Application.action='https://www.karenhall.ca/preapproval/mrmortpa.php';
    }
}


//
//////////////////////////////////////// Disable Enter Key for Forms ************ 
//
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
}
//
/////////////////////////////////// form focus - move to next field ************
//
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;
}
