// repayment types
// 0 - interest only
// 1 - normal repayment

function calculateRepayments(){
		loan = parseInt(document.calcform.loan.value);
		rate = document.calcform.rate.value;
//		mtype = document.calcform.mortgagetype.selectedIndex; 
		
		if (!parseInt(loan) > 0){
			alert("Please enter a value for the loan. Must be whole pounds.");
			return false;
		}

		if (!parseInt(rate) > 0 | isNaN(rate)){
			alert("Please enter an interest rate.");
			return false;
		}

		term = parseInt(document.calcform.period.value);
		rate100 = (rate / 100)
		// (loan amount * decimal rate) / 12
		monthlyrepayments = ((loan * rate100) / 12);
		
		//if (mtype == 0) 
		{ 
			monthlyrepayments = (Math.round(monthlyrepayments * 100)) / 100;
			document.calcform.answer1.value = monthlyrepayments;
		//	return false; 
		//} 
		//else if (mtype == 1)  { //Full Repayment 
			
			var topline = (loan * rate * 12);
			var midline = Math.pow((1+(rate/1200)), (-term * 12));
			midline = (100 * 12 * (1 - midline));
			var answer = ((topline / midline) / 12);
			answer = (Math.round(answer * 100)) / 100;
			document.calcform.answer2.value = answer; 
		//return false;
		}			

	//return false;			
}


function calculate_maxloan()
	{
	document.maxborrowcalc.firstincome.value = ForceNumeric(document.maxborrowcalc.firstincome.value);
	document.maxborrowcalc.secondincome.value = ForceNumeric(document.maxborrowcalc.secondincome.value);
	var firstincome = document.maxborrowcalc.firstincome.value;
	var secondincome = document.maxborrowcalc.secondincome.value;
	if (firstincome > 0) 
		{
		if (secondincome == 0)
			{
			averageloanamount = firstincome * 3.5;	
			highestloanamount = firstincome * 5;
			}
		else
			{
			var jointincome = Math.abs(firstincome) + Math.abs(secondincome);
			averageloanamount = 3 * jointincome;
			highestloanamount = 4 * jointincome;
			}
		document.maxborrowcalc.average.value = FormatNumber(averageloanamount,0);
		document.maxborrowcalc.highest.value = FormatNumber(highestloanamount,0);
		}
	else
		{
		document.maxborrowcalc.average.value = "*ERROR*";
		document.maxborrowcalc.highest.value = "*ERROR*";
		}
	}


function FormatNumber(Number,Decimals,Separator)
{
 // **********************************************************
 // Placed in the public domain by Affordable Production / March 21, 1998
 // Web site: http://www.aptools.com/
 //
 // November 24, 1998 -- Error which allowed a null value
 // to remain null fixed. Now forces value to 0.
 //
 // This function accepts a number to format and number
 // specifying the number of decimal places to format to. May
 // optionally use a separator other than '.' if specified.
 //
 // If no decimals are specified, the function defaults to
 // two decimal places. If no number is passed, the function
 // defaults to 0. Decimal separator defaults to '.' .
 //
 // If the number passed is too large to format as a decimal
 // number (e.g.: 1.23e+25), or if the conversion process
 // results in such a number, the original number is returned
 // unchanged.
 // **********************************************************
 Number += ""          // Force argument to string.
 Decimals += ""        // Force argument to string.
 Separator += ""       // Force argument to string.
 if((Separator == "") || (Separator.length > 1))
  Separator = "."
 if(Number.length == 0)
  Number = "0"
 var OriginalNumber = Number  // Save for number too large.
 var Sign = 1
 var Pad = ""
 var Count = 0
 // If no number passed, force number to 0.
 if(parseFloat(Number)){
  Number = parseFloat(Number)} else {
  Number = 0}
 // If no decimals passed, default decimals to 2.
 if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0)){
  Decimals = parseInt(Decimals,10)} else {
  Decimals = 2}
 if(Number < 0)
 {
  Sign = -1         // Remember sign of Number.
  Number *= Sign    // Force absolute value of Number.
 }
 if(Decimals < 0)
  Decimals *= -1    // Force absolute value of Decimals.
 // Next, convert number to rounded integer and force to string value.
 // (Number contains 1 extra digit used to force rounding)
 Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)
 if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
  return(OriginalNumber) // Number too large to format as specified.
 // If length of Number is less than number of decimals requested +1,
 // pad with zeros to requested length.
 if(Number.length < Decimals +1) // Construct pad string.
 {
  for(Count = Number.length; Count <= Decimals; Count++)
   Pad += "0"
 }
 Number = Pad + Number // Pad number as needed.
 if(Decimals == 0){
  // Drop extra digit -- Number is formatted.
  Number = Number.substring(0, Number.length -1)} else {
  // Or, format number with decimal point and drop extra decimal digit.
 Number = Number.substring(0,Number.length - Decimals -1) +
          Separator +
          Number.substring(Number.length - Decimals -1,
          Number.length -1)}
 if(Sign == -1)
  Number = "-" + Number  // Set sign of number.
 if(Number.length == 0)
  Number="0"
 return(Number)
}


function ForceNumeric(nValue)
	{
	validChars = "0123456789.";
	newValue="";
	for(k = 0; k < nValue.length; k++)
		{
		thisChar = nValue.charAt(k);
		if(validChars.indexOf(thisChar) != -1) newValue += thisChar;
		}
	return newValue;
	}