var viewportwidth;
 var viewportheight;

 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }

// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }

 // older versions of IE

 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }

function overlay_hide() {
    document.getElementById('overlay2').style.display = 'none';
}

function overlay_show() {
    document.getElementById('overlay2').style.height = viewportheight+'px';
    document.getElementById('overlay2').style.display = 'block';
}


function togll(id) {
    if (document.getElementById(id).style.display != 'block') {
        document.getElementById(id).style.display = 'block';
        document.getElementById(id+'-link').className = 'minus';
    }
    else {
        document.getElementById(id).style.display = 'none';
        document.getElementById(id+'-link').className = 'plus';
    }
    document.getElementById('f2').focus();
}


function calcComSav() {
    document.getElementById('dispCalcSav').innerHTML = '<img src = \"/images/layout/loading.gif\" alt = \"loading\" />';
    setTimeout("calcComSav2()", 1000);

}

function calcComSav2() {
  x = (document.getElementById('propValue').value.replace(/[^\d\.-]/g,'') * 0.05) * 1.05; // 1.05 is for GST
  document.getElementById('dispCalcSav').innerHTML = '$'+addCommas(x.toFixed(2));
}


function calcMortgage() {
    var rate = document.mortgageCalc.interestRate.value.replace(/[^\d\.-]/g,'');
    var mortgageAmount = document.mortgageCalc.purchasePrice.value.replace(/[^\d\.-]/g,'') - document.mortgageCalc.downPayment.value.replace(/[^\d\.-]/g,'');
    var amortizationPeriod = document.mortgageCalc.amortizationPeriod.value.replace(/[^\d\.-]/g,'');
    var payment = (mortgageAmount * Math.pow((1+rate/100/12),(12*amortizationPeriod)) * (rate/100/12)) / (Math.pow((1+rate/100/12),(12*amortizationPeriod)) - 1);
    document.getElementById('monthly').innerHTML = '$' + addCommas(payment.toFixed(2));
}
function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function quickSearch() {
    if (!document.getElementById('qSearch').value.length) { alert('Please select a value to search for.');  return; }
    document.location  = '/property-listing.php?quickSearch='+document.getElementById('qSearch').value;
}

function findAHome() {
    if (!document.getElementById('fState').value.length) { alert('Please choose a province.');  return; }
    if (!document.getElementById('fCity').value.length) { alert('Please choose a city.'); return; }
//    if (!document.getElementById('fPropertyType').value.length) { alert('Please choose a property type.'); return; }
//    if (!document.getElementById('fPriceFrom').value.length) { alert('Please choose a starting price.'); return; }
//    if (!document.getElementById('fPriceTo').value.length) { alert('Please choose a maximum price.'); return; }

    document.location  = '/property-listing.php?'
        +'city='+document.getElementById('fCity').value
        +'&state='+document.getElementById('fState').value
        +'&propertyType='+document.getElementById('fPropertyType').value
        +'&startPrice='+document.getElementById('fPriceFrom').value
        +'&endPrice='+document.getElementById('fPriceTo').value
        +'&showResults='+document.getElementById('showResults').value;
}



function findAHome() {
    if (!document.getElementById('fState').value.length) { alert('Please choose a province.');  return; }
    if (!document.getElementById('fCity').value.length) { alert('Please choose a city.'); return; }
//    if (!document.getElementById('fPropertyType').value.length) { alert('Please choose a property type.'); return; }
//    if (!document.getElementById('fPriceFrom').value.length) { alert('Please choose a starting price.'); return; }
//    if (!document.getElementById('fPriceTo').value.length) { alert('Please choose a maximum price.'); return; }

    document.location  = '/property-listing.php?'
        +'city='+document.getElementById('fCity').value
        +'&state='+document.getElementById('fState').value
        +'&propertyType='+document.getElementById('fPropertyType').value
        +'&startPrice='+document.getElementById('fPriceFrom').value
        +'&endPrice='+document.getElementById('fPriceTo').value
        +'&showResults='+document.getElementById('showResults').value;
}


function openHouseSearch() {
    if (!document.getElementById('fState_oh').value.length) { alert('Please choose a province.');  return; }
    if (!document.getElementById('fCity_oh').value.length) { alert('Please choose a city.'); return; }
//    if (!document.getElementById('fPropertyType').value.length) { alert('Please choose a property type.'); return; }
//    if (!document.getElementById('fPriceFrom').value.length) { alert('Please choose a starting price.'); return; }
//    if (!document.getElementById('fPriceTo').value.length) { alert('Please choose a maximum price.'); return; }

    document.location  = '/property-listing.php?'
        +'city='+document.getElementById('fCity_oh').value
        +'&state='+document.getElementById('fState_oh').value
        +'&openHouse=future'
        +'&showResults=list';
}

 




function checkEmail(email){
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if (email.search(emailRegEx) == -1) { return false; }
    else { return true; }
}



function validateEmailFriendForm() {
    if(document.emailFriendForm.fromName.value.length == 0)			{ alert("Please enter your name.");			document.emailFriendForm.fromName.focus();			return; }
    if (!checkEmail(document.emailFriendForm.fromEmail.value)) { alert('Please provide a valid email address'); document.emailFriendForm.fromEmail.focus(); return; }
    if(document.emailFriendForm.toName.value.length == 0)			{ alert("Please enter your name.");			document.emailFriendForm.toName.focus();			return; }
    if (!checkEmail(document.emailFriendForm.toEmail.value)) { alert('Please provide a valid email address'); document.emailFriendForm.toEmail.focus(); return; }

    sendEmailFriendForm();
}
function sendEmailFriendForm() {
    var http = false;
    if(navigator.appName == "Microsoft Internet Explorer") { http = new ActiveXObject("Microsoft.XMLHTTP"); }
    else { http = new XMLHttpRequest(); }

    var url = "/property-email-friend-send.php";
    var params = "ihatespam="+document.emailFriendForm.ihatespam.value+
                "&fromName="+document.emailFriendForm.fromName.value+
                "&fromEmail="+document.emailFriendForm.fromEmail.value+
                "&toName="+document.emailFriendForm.toName.value+
                "&toEmail="+document.emailFriendForm.toEmail.value+
                "&subject="+document.emailFriendForm.subject.value;
    http.open("POST", url, true);
	//Send the proper header infomation along with the reque
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
    http.send(params);

    http.onreadystatechange=function() {
      if(http.readyState == 4) {
          var res = http.responseText;
            document.getElementById('email_friend_content').innerHTML = res;
      }
    }
}








function validateProRegisterForm() {
    if(document.emailProRegisterForm.companyName.value.length == 0)			{ alert("Please enter your company name.");			document.emailProRegisterForm.companyName.focus();			return; }
    if(document.emailProRegisterForm.industry.value.length == 0)			{ alert("Please choose an industry.");			document.emailProRegisterForm.industry.focus();			return; }
    if(document.emailProRegisterForm.name.value.length == 0)			{ alert("Please enter a contact name.");			document.emailProRegisterForm.name.focus();			return; }
    if(document.emailProRegisterForm.title.value.length == 0)			{ alert("Please enter your title.");			document.emailProRegisterForm.title.focus();			return; }
    if(document.emailProRegisterForm.phone.value.length == 0)			{ alert("Please enter your phone number.");			document.emailProRegisterForm.phone.focus();			return; }
    if (!checkEmail(document.emailProRegisterForm.email.value)) { alert('Please provide a valid email address'); document.emailProRegisterForm.email.focus(); return; }
    if(document.emailProRegisterForm.address.value.length == 0)			{ alert("Please enter your address.");			document.emailProRegisterForm.address.focus();			return; }
    if(document.emailProRegisterForm.city.value.length == 0)			{ alert("Please enter your city.");			document.emailProRegisterForm.city.focus();			return; }
    if(document.emailProRegisterForm.province.value.length == 0)			{ alert("Please enter your province.");			document.emailProRegisterForm.province.focus();			return; }
    if(document.emailProRegisterForm.postalCode.value.length == 0)			{ alert("Please enter your postal code.");			document.emailProRegisterForm.postalCode.focus();			return; }

    if (!checkEmail(document.emailProRegisterForm.email.value)) { alert('Please provide a valid email address'); document.emailProRegisterForm.email.focus(); return; }

    sendRegisterForm();
}
function sendRegisterForm() {
    var http = false;
    if(navigator.appName == "Microsoft Internet Explorer") { http = new ActiveXObject("Microsoft.XMLHTTP"); }
    else { http = new XMLHttpRequest(); }

    var url = "/send_pro_register.php";
    var params = "ihatespam="+document.emailProRegisterForm.ihatespam.value+
                "&companyName="+document.emailProRegisterForm.companyName.value+
                "&industry="+document.emailProRegisterForm.industry.value+
                "&name="+document.emailProRegisterForm.name.value+
                "&title="+document.emailProRegisterForm.title.value+
                "&phone="+document.emailProRegisterForm.phone.value+
                "&email="+document.emailProRegisterForm.email.value+
                "&address="+document.emailProRegisterForm.address.value+
                "&city="+document.emailProRegisterForm.city.value+
                "&province="+document.emailProRegisterForm.province.value+
                "&postalCode="+document.emailProRegisterForm.postalCode.value;
    http.open("POST", url, true);
	//Send the proper header infomation along with the reque
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
    http.send(params);

    http.onreadystatechange=function() {
      if(http.readyState == 4) {
          var res = http.responseText;
            document.getElementById('pro_register_content').innerHTML = res;
      }
    }
}




function validateMortgageApp() {
    if(document.emailMortgageApp.applicant_first_name.value.length == 0)			{ alert("Please enter applicant first name.");			document.emailMortgageApp.applicant_first_name.focus();			return; }
    if(document.emailMortgageApp.applicant_last_name.value.length == 0)			{ alert("Please enter applicant last name.");			document.emailMortgageApp.applicant_last_name.focus();			return; }
    if(document.emailMortgageApp.applicant_phone.value.length == 0)			{ alert("Please enter applicant phone.");			document.emailMortgageApp.applicant_phone.focus();			return; }
    if(document.emailMortgageApp.applicant_email.value.length == 0)			{ alert("Please enter applicant email.");			document.emailMortgageApp.applicant_email.focus();			return; }
    if(document.emailMortgageApp.applicant_city.value.length == 0)			{ alert("Please enter applicant city.");			document.emailMortgageApp.applicant_city.focus();			return; }
    if(document.emailMortgageApp.applicant_province.value.length == 0)			{ alert("Please enter applicant province.");			document.emailMortgageApp.applicant_province.focus();			return; }
    if(document.emailMortgageApp.applicant_income_type.value.length == 0)			{ alert("Please enter applicant income type.");			document.emailMortgageApp.applicant_income_type.focus();			return; }
//    if(document.emailMortgageApp.applicant_annual_income.value.length == 0)			{ alert("Please enter applicant annual income.");			document.emailMortgageApp.applicant_annual_income.focus();			return; }

    if(document.emailMortgageApp.agree.length == 0)			{ alert("You must agree to the terms of use.");			document.emailMortgageApp.agree.focus();			return; }

    sendEmailMortgageApp();
}
function sendEmailMortgageApp() {
    var http = false;
    if(navigator.appName == "Microsoft Internet Explorer") { http = new ActiveXObject("Microsoft.XMLHTTP"); }
    else { http = new XMLHttpRequest(); }

    var url = "/send_mortgage_app.php";
    var params = "ihatespam="+document.emailMortgageApp.ihatespam.value+
                "&applicant_first_name="+document.emailMortgageApp.applicant_first_name.value+
                "&applicant_last_name="+document.emailMortgageApp.applicant_last_name.value+
                "&applicant_phone="+document.emailMortgageApp.applicant_phone.value+
                "&applicant_email="+document.emailMortgageApp.applicant_email.value+
                "&applicant_city="+document.emailMortgageApp.applicant_city.value+
                "&applicant_province="+document.emailMortgageApp.applicant_province.value+
                "&applicant_income_type="+document.emailMortgageApp.applicant_income_type.value+
//                "&applicant_annual_income="+document.emailMortgageApp.applicant_annual_income.value+

                "&coapplicant_first_name="+document.emailMortgageApp.coapplicant_first_name.value+
                "&coapplicant_last_name="+document.emailMortgageApp.coapplicant_last_name.value+
                "&coapplicant_income_type="+document.emailMortgageApp.coapplicant_income_type.value+
//                "&coapplicant_annual_income="+document.emailMortgageApp.coapplicant_annual_income.value;

    http.open("POST", url, true);
	//Send the proper header infomation along with the reque
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
    http.send(params);

    http.onreadystatechange=function() {
      if(http.readyState == 4) {
          var res = http.responseText;
            document.getElementById('pro_register_content').innerHTML = res;
      }
    }
}
