function newLocation(location2){
	//location="/edmonton_communities.asp?location="+location2.value;
	location="edmonton_communities.asp?location="+location2.value;
}

function ClearInput(form){
	form.price.value="";
	form.year.value="";
	form.rate.value="";
	this.document.result.down1.value="";
	this.document.result.down2.value="";
	this.document.result.down3.value="";
	this.document.result.down4.value="";
	this.document.result.month1.value="";
	this.document.result.month2.value="";
	this.document.result.month3.value="";
	this.document.result.month4.value="";
	this.document.result.fortnight1.value="";
	this.document.result.fortnight2.value="";
	this.document.result.fortnight3.value="";
	this.document.result.fortnight4.value="";
	this.document.result.week1.value="";
	this.document.result.week2.value="";
	this.document.result.week3.value="";
	this.document.result.week4.value="";
	return true;
}



function CheckForm(form){
    if (form.price.value==null||form.price.value.length==0){
        alert("Please enter the price of your house");
        return false;
		}
    else { 
		if (form.price.value<10000){
			alert("Your purchase price is too low. Please enter a higher value.");
            return false;
		} else { 
			if (form.year.value==null||form.year.value.length==0){
				alert("Please enter the length of your mortgage in years.");
				return false;
			} else {
				if (form.rate.value<1||form.rate.value>20){
					alert("The interest rate should be between 1% and 20%.");
					return false;
				} else {
					return true;
				}
			}
		}
	}
}

function MakeMoney(numb){
	money='$';
	for (var i=0;i<numb.value.length; i++) {
		j=i+1;
		money+=numb.value.substring(i,j);
		if (numb.value==Math.round(numb.value)) {
			pos=numb.value.length-j;
		} else { 
			pos=numb.value.length-3-j;
		}
        tmp=Math.round(pos/3);
		if (pos == tmp*3 && pos != 0) money+=",";
	}
    return money;
}

function PVIFA(annual_rate, years, period){
	var monthly_rate=annual_rate/period;
	var factor=0;
	var base_rate=1+monthly_rate;
	var denominator=base_rate;
	for (var i=0;i<(years*period);i++){
		factor+=(1/denominator);
        denominator*=base_rate;
	}
	return factor;
}

function Calculate(form){
	if (CheckForm(form)){
		down1=form.price.value*0.05;
        down2=form.price.value*0.10;
        down3=form.price.value*0.15;
        down4=form.price.value*0.20;
        this.document.result.down1.value=Math.round(down1);
        this.document.result.down2.value=Math.round(down2);
        this.document.result.down3.value=Math.round(down3);
        this.document.result.down4.value=Math.round(down4);
        interest_rate=(form.rate.value/100);
        monthly_factor=PVIFA(interest_rate, form.year.value, 12);
	    fortnightly_factor=PVIFA(interest_rate, form.year.value, 26);
        weekly_factor=PVIFA(interest_rate, form.year.value, 52);
        var base_amount=form.price.value-down1;
        this.document.result.month1.value=Math.round(base_amount/monthly_factor);
		this.document.result.fortnight1.value=Math.round(base_amount/fortnightly_factor);
        this.document.result.week1.value=Math.round(base_amount/weekly_factor);
        base_amount=form.price.value-down2;
        this.document.result.month2.value=Math.round(base_amount/monthly_factor);
        this.document.result.fortnight2.value=Math.round(base_amount/fortnightly_factor);
        this.document.result.week2.value=Math.round(base_amount/weekly_factor);
        base_amount=form.price.value-down3;
        this.document.result.month3.value=Math.round(base_amount/monthly_factor);
        this.document.result.fortnight3.value=Math.round(base_amount/fortnightly_factor);
        this.document.result.week3.value=Math.round(base_amount/weekly_factor);
        base_amount=form.price.value-down4;
        this.document.result.month4.value=Math.round(base_amount/monthly_factor);
        this.document.result.fortnight4.value=Math.round(base_amount/fortnightly_factor);
        this.document.result.week4.value=Math.round(base_amount/weekly_factor);
		form.price.value=MakeMoney(form.price);
        this.document.result.down1.value=MakeMoney(this.document.result.down1);
		this.document.result.down2.value=MakeMoney(this.document.result.down2);
		this.document.result.down3.value=MakeMoney(this.document.result.down3);
        this.document.result.down4.value=MakeMoney(this.document.result.down4);
        this.document.result.month1.value=MakeMoney(this.document.result.month1);
        this.document.result.month2.value=MakeMoney(this.document.result.month2);
        this.document.result.month3.value=MakeMoney(this.document.result.month3);
        this.document.result.month4.value=MakeMoney(this.document.result.month4);
		this.document.result.fortnight1.value=MakeMoney(this.document.result.fortnight1);
        this.document.result.fortnight2.value=MakeMoney(this.document.result.fortnight2);
		this.document.result.fortnight3.value=MakeMoney(this.document.result.fortnight3);
        this.document.result.fortnight4.value=MakeMoney(this.document.result.fortnight4);
        this.document.result.week1.value=MakeMoney(this.document.result.week1);
		this.document.result.week2.value=MakeMoney(this.document.result.week2);
        this.document.result.week3.value=MakeMoney(this.document.result.week3);
		this.document.result.week4.value=MakeMoney(this.document.result.week4);
        return true;
	}
}



function postdata(){
	document.submit();
}

function alertt(string){
	alert(string);
}

function alertt2(string){
	alert(string);
}

function v_num_limit(value){
	if (value.length >=500 ) return false;
	return true;
}

function v_nb(value){
	if (value == '' || value == ' ') return false;
	return true;
}

function v_email(value){
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (value.indexOf(".") > 2) && (value.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(value) && r2.test(value));
}

function Validate(form){

if (!v_nb(form.LeadName.value)){alertt('Please enter your name.'); 
form.LeadName.focus();form.LeadName.select();return false;}

if (!v_email(form.LeadEmail.value)){alertt('Please add or check your email address.'); 
form.LeadEmail.focus();form.LeadEmail.select();return false;}

if (!v_num_limit(form.message.value)){alertt2('Please add a message to your email or ensure it\'s under 500 characters.');
return false;}

else

return true;
postdata();
}

function Validate1(form) {

	if (!v_nb(form.lgn.value)){
		alert('Please enter your name.'); 
		form.lgn.focus();
		form.lgn.select();
		return false;
	}

	if (!v_email(form.pwd.value)){
		alert('Please add or check your email address.'); 
		form.pwd.focus();
		form.pwd.select();
		return false;
	}
	else
		return true;
	postdata();
}

function ValidateContacts(form) {
	if (!form.itype[0].checked && !form.itype[1].checked) {
		alert('Please select the type of the contact either "General Inquiry" or "I am an existing Challenger Homes client".'); 
		return false;
	}

	if (!v_nb(form.LeadName.value)){
		alert('Please enter your name.'); 
		form.LeadName.focus();
		form.LeadName.select();
		return false;
	}

	if (!v_email(form.LeadEmail.value)){
		alert('Please add or check the Email Address.'); 
		form.LeadEmail.focus();
		form.LeadEmail.select();
		return false;
	}

	if (!v_nb(form.LeadPhone.value)){
		alert('Please enter the Phone Number.'); 
		form.LeadPhone.focus();
		form.LeadPhone.select();
		return false;
	}
	
	if (form.itype[1].checked) {
		if (!v_nb(form.LeadAddress.value)){
			alert('Please enter the Home\'s Address.'); 
			form.LeadAddress.focus();
			form.LeadAddress.select();
			return false;
		}
		
		if (!v_nb(form.LeadJob.value)){
			alert('Please enter the Job #.'); 
			form.LeadJob.focus();
			form.LeadJob.select();
			return false;
		}
		
	}
	
	//else
	return true;
	//postdata();
}

function openWindow(url) { popupWin = window.open(url, 'openWin', "width=400, height=250, scrollbars=yes, resizable=yes"); }