function call(){

calculate()

}

function calculate(){

var value=document.calculator.ammount.value;

var months=document.calculator.months.value;

var payment=0;

var total=0;

var roi=0;

var apr=0;

var reciprocal=0;

var result=0;

var mystr= /£/g

var mydec= /\./

var checkdecimal=0;

var remainder;

var checkremainder=0;

var point=0;

var pointpos=0;

var payment=0;

var totalpoint=0;

var totalpointpos=0;

if (value == "" ){alert("Please Enter An Amount To Borrow.");document.calculator.ammount.select();return false}



//check for leading pound sign and delete if found

if (value.charAt(0) == "£"){

value=value.replace(mystr, "");

}





// check if less than 3000 or more than 500000

if (value < 3000){alert("Minimum Loan Is £3000.");document.calculator.ammount.select();return false}

if (value > 500000){alert("Maximum Loan Is £500000.");document.calculator.ammount.select();return false}



//check for decimal point

checkdecimal=value.search(mydec)

if (checkdecimal > -1){alert("Please Enter A Whole Number , Without The Decimal Point Please.");document.calculator.ammount.select();return false}







if (isNaN(value) == true){alert("Please Correct The Amount You Wish To Borrow\nOnly Numbers 1-10 Please");document.calculator.ammount.select();return false}



// if you get this far its a number!!



// set interest rate (APR)

if (value<10000){roi=0.00944;apr="11.9%"}

if (value>=10000 && value<=19999){roi=0.00869;apr="10.9%"}

if (value>=20000 && value<=34999){roi=0.0069304;apr="8.6%"}

if (value>=35000){roi=0.00639;apr="7.9%"}


//check months field

if(months==""){alert("Please Enter The Length of The Loan");document.calculator.months.focus();return false}



checkdecimal=months.search(mydec)

if (checkdecimal > -1){alert("Please Enter A Whole Number of Months Between 12 and 300 , Without The Decimal Point Please.");document.calculator.months.select();return false}





if (isNaN(months) == true){alert("Please Correct The Length Of The Loan\nOnly Numbers 0-9 Please");document.calculator.months.focus();return false}

if (months<36){alert("Please Select At Least 36 Monthly Payments");document.calculator.months.select();return false}

if (months>300){alert("Maximum Monthly Repayments Are 300");document.calculator.months.select();return false}



// this is the clever bit!! that works out the monthly payment.

if (document.calculator.protection[0].checked){value=value/100*1.1993*100}

if (document.calculator.protection[1].checked){value=value/100*1.2499*100}

reciprocal=1/(1+roi);

reciprocal=Math.pow(reciprocal,months)

result=1-reciprocal

result=result/roi

payment=value/result



payment=payment+0.005

//total=payment*months

point =payment.toString()

pointpos = point.search(mydec)

if (point.charAt(pointpos+2) == ""){point=point+"0"}

payment=point.slice(0,pointpos+3)



//total payment commented out

//totalpoint = total.toString()

//totalpointpos=totalpoint.search(mydec)

//if (totalpoint.charAt(totalpointpos+2) == ""){totalpoint=totalpoint+"0"}

//total=totalpoint.slice(0,totalpointpos+3)



// set fields

payment="£"+payment

//total="£"+total

document.calculator.payment.value=payment

//document.calculator.total.value=total

document.calculator.apr.value=apr

//End of calculation bit.

}


