function ClearInput(form){
form.price.value="";
form.year.value="";
form.rate.value="";
this.document.result.down0.value="";
this.document.result.down_1.value="";
this.document.result.down_3.value="";
this.document.result.down1.value="";
this.document.result.down2.value="";
this.document.result.down3.value="";
this.document.result.down4.value="";
this.document.result.month0.value="";
this.document.result.month_1.value="";
this.document.result.month_3.value="";
this.document.result.month1.value="";
this.document.result.month2.value="";
this.document.result.month3.value="";
this.document.result.month4.value="";
return true;
}

function help(helponwhat) {
if (helponwhat == "value")
alert("Enter the amount you need to borrow.")
else
if (helponwhat == "year")
alert("Enter the number of years you want to repay your home loans. ")
else
if (helponwhat == "rate")
alert("Parrish & Associates offers a variety of home loan products to suit your needs.");
}

function CheckForm(form){
if (form.price.value==null||form.price.value.length==0){
alert("Please enter the amount financed for your purchase");
return false;}
else
{ if (form.price.value<20000){
alert("Please enter a price greater than 20000. Please reenter");
return false;}
else
{ if (form.year.value==null||form.year.value.length==0){
alert("Please enter the length of your mortgage.");
return false;}
else
{ if (form.rate.value<2||form.rate.value>20){
alert("The interest rate should be between 5% 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)){
down0=form.price.value*0.00;
down_1=form.price.value*0.01;
down_3=form.price.value*0.03;
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.down0.value=Math.round(down0);
this.document.result.down_1.value=Math.round(down_1);
this.document.result.down_3.value=Math.round(down_3);
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);
var base_amount=form.price.value-down0;
this.document.result.month0.value=Math.round(base_amount/monthly_factor);
var base_amount=form.price.value-down_1;
this.document.result.month_1.value=Math.round(base_amount/monthly_factor);
var base_amount=form.price.value-down_3;
this.document.result.month_3.value=Math.round(base_amount/monthly_factor);
var base_amount=form.price.value-down1;
this.document.result.month1.value=Math.round(base_amount/monthly_factor);
base_amount=form.price.value-down2;
this.document.result.month2.value=Math.round(base_amount/monthly_factor);
base_amount=form.price.value-down3;
this.document.result.month3.value=Math.round(base_amount/monthly_factor);
base_amount=form.price.value-down4;
this.document.result.month4.value=Math.round(base_amount/monthly_factor);
//form.price.value=MakeMoney(form.price);
this.document.result.down0.value=MakeMoney(this.document.result.down0);
this.document.result.down_1.value=MakeMoney(this.document.result.down_1);
this.document.result.down_3.value=MakeMoney(this.document.result.down_3);
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.month0.value=MakeMoney(this.document.result.month0);
this.document.result.month_1.value=MakeMoney(this.document.result.month_1);
this.document.result.month_3.value=MakeMoney(this.document.result.month_3);
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);
return true;
}

}