// GMC Template Scripts.

var gmc_template = {
	
	initialize:
		[function() {
			$('input.formpost').click(function() { $(this).parents("form").submit(); });
			$('.calcpost').click(function() {
				$('#calculator').toggle();
				$('#calcresult').toggle();
			});
		}, 'addToJquery']
}

function extendJQ(source) {//adds array functions to jQuery namespace
	for (var i in source) {
		if (source[i][1] == 'addToJquery') {
			jQuery.fn[i] = source[i][0];
		}
	}
}
extendJQ(gmc_template);

// ie functions
var ie6 = ($.browser.msie && $.browser.version == "6.0");
var ie7 = ($.browser.msie && $.browser.version == "7.0");

$(document).ready(function() {
	$('.page').initialize();
});

function ContactValidate(){
    if (document.sendme.first_name.value == "") {
        alert("Please enter First Name.");
        document.sendme.first_name.focus();
        return false;
        }

    if (document.sendme.last_name.value == "") {
        alert("Please enter Last Name.");
        document.sendme.last_name.focus();
        return false;
        }

   if (document.sendme.address.value == "") {
        alert("Please enter Address.");
        document.sendme.address.focus();
        return false;
        }   

   if (document.sendme.city.value == "") {
        alert("Please enter City.");
        document.sendme.city.focus();
        return false;
        }  

   if (document.sendme.state.value == "State") {
        alert("Please select State.");
        document.sendme.state.focus();
        return false;
    }

    if (document.sendme.zip.value == "") {
        alert("Please enter Zip code.");
        document.sendme.zip.focus();
        return false;
    } 

    HomeIsZipNum = validateZIP(document.sendme.zip.value);
    if (HomeIsZipNum == false) {

    document.sendme.zip.focus();
    return false;
    }

    if (document.sendme.email.value == "" ) {
        alert("Please enter EMail.");
        document.sendme.email.focus();
        return false;
    }

       if (document.sendme.phone.value == "" ) {
        alert("Please enter Phone.");
        document.sendme.phone.focus();
        return false;
    }

   
    if (document.sendme.dob.value == "") {
        alert("Please enter Date of Birth.");
        document.sendme.dob.focus();
        return false;
    }

dtvalidate = isDate(document.sendme.dob.value);
    if (dtvalidate == false) {
        alert("Please enter a valid date of birth (mm/dd/yyyy).");
        document.sendme.dob.focus();
       return false;  
    }

    now = new Date();

    born = new Date(document.sendme.dob.value);

    years = Math.floor((now.getTime() - born.getTime()) / (365 * 24 * 60 * 60 * 1000));

    if ( years < 60 || years > 120) {
        alert("FHA Insured Reverse Mortgages are for homeowners who are 62 years of age and older. If you are not currently 62 years of age or inquiring for someone who is, please call me, your local Licensed Reverse Mortgage Professional, at my number listed below to answer all your questions.");
        document.sendme.dob.focus();
        return false;
        }

    if (document.sendme.sendme.value == "Choose") {
        alert("Please select the information you would like");
        document.sendme.sendme.focus();
        return false;
   }

    alert("Thank you. We have received your referral information. You will now be re-directed to our website. Have a great day.");


}



function ContactValidate2() {

    



    if (document.freeDVD1.first_name.value == "") {

        alert("Please enter First Name.");

        document.freeDVD1.first_name.focus();

        return false;

    }

    if (document.freeDVD1.last_name.value == "") {

        alert("Please enter Last Name.");

        document.freeDVD1.last_name.focus();

        return false;

    }

  

    if (document.freeDVD1.phone.value == "" || document.freeDVD1.phone.value == "999-999-9999") {

        alert("Please enter Phone.");

        document.freeDVD1.phone.focus();

        return false;

    }

    alert("Thank you. We have received your referral information. You will now be re-directed to our website. Have a great day.");

     

    }

    

    function validate_cal(){
	if (document.leftcalculator.homevalue.value == "") {
        alert("Please enter Home Value.");
        document.leftcalculator.homevalue.focus();
        return false;
    }

    if (document.leftcalculator.debt.value == "") {
        document.leftcalculator.debt.value = 0; 
        return false;
    }

	if (document.leftcalculator.dob.value == "") {
        alert("Please enter Date of Birth.");
        document.leftcalculator.dob.focus();
        return false;
    }

	if (document.leftcalculator.zip.value == "") {
        alert("Please enter Zip code.");
        document.leftcalculator.zip.focus();
        return false;
    } 
	
    dtvalidate = isDate(document.leftcalculator.dob.value);
    if (dtvalidate == false) {
        alert("Please enter a valid date of birth (mm/dd/yyyy).");
        document.leftcalculator.dob.focus();
       return false;  
    }
    IsHomeValueNum = IsNumeric(document.leftcalculator.homevalue.value);
    IsDebtNum = IsNumeric(document.leftcalculator.debt.value);
    HomeIsZipNum = validateZIP(document.leftcalculator.zipcode.value);

    if (IsHomeValueNum==false) {
    alert("Home value is not valid"); 
    document.leftcalculator.homevalue.focus();
    return false;
    }

        if (IsDebtNum==false) {
    alert("Debt value is not valid"); 
    document.leftcalculator.debt.focus();
    return false;
    }



    if (HomeIsZipNum == false) {
        /*alert("The zip code you entered is not valid. Please confirm and re-enter."); */
    document.leftcalculator.zipcode.focus();
    return false;
    }

    now = new Date();
    born = new Date(document.leftcalculator.dob.value);
    years = Math.floor((now.getTime() - born.getTime()) / (365 * 24 * 60 * 60 * 1000));

    if ( years < 60 || years > 120) {
        alert("You must be between 62 and 120 years of age to qualify. If you have questions, please contact me to discuss.");
        document.leftcalculator.dob.focus();
        return false;
        }
     }

function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
/*alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");*/
return false;
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;
}



if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
return true; // date is valid
}





function IsNumeric(sText)
{
   var ValidChars = "0123456789,.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;

   

   }

   

   function validateZIP(field) {

var valid = "0123456789-";

var hyphencount = 0;



if (field.length!=5 && field.length!=10) {

alert("The zip code you entered is not valid. Please confirm and re-enter.");

return false;

}

for (var i=0; i < field.length; i++) {

temp = "" + field.substring(i, i+1);

if (temp == "-") hyphencount++;

if (valid.indexOf(temp) == "-1") {

alert("The zip code you entered is not valid. Please confirm and re-enter.");

return false;

}

if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {

alert("The zip code you entered is not valid. Please confirm and re-enter.");

return false;

   }

}

return true;

}






