// JavaScript Document
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
// Mortgage Calculator

function calculate(myForm) {
   var errors='';
   if(myForm.amount.value<1)
      errors += "Invalid mortgage amount\nPlease enter a non-zero value.\n\n";
	else if(IsNumeric(myForm.amount.value)==false){
   			errors += "Entered mortgage amount is not numeric. \n\n";
   			} 
	  
   if(myForm.rate.value<1 || myForm.rate.value>100)
     errors += "Invalid Interest Rate\nPlease enter a value between 1 and 100.\n\n";
   else if(IsNumeric(myForm.rate.value)==false){
	  		errors += "Entered interest rate is not numeric. \n\n";
			}
   if((myForm.mtype[1].checked == true)&&(errors.length == 0)) {
      if(myForm.term.value<1) {
         errors += "Invalid term of mortgage\nPlease enter a non-zero value.\n\n";
         
      }
	  else if (IsNumeric(myForm.term.value)==false){
	  		errors += "Entered term of mortgage not numeric. \n\n";
      		}
	  else {
         //do the calc for repayment mortgages in here
         var myPayment=0;
         myRate=1+(myForm.rate.value/100);
         myRate=Math.pow(myRate,(1/12));
         myTerm=myForm.term.value*12;
         myMortgage=myForm.amount.value;
         var x = Math.pow(myRate,myTerm);
         //fix dividing by zero where interest rate is set at 0 by if else
         if(x==1) { myPayment = (myMortgage/myTerm); }
         else { myPayment=((myMortgage*x)*(1-myRate))/(1-x) }
         myForm.interest.value=moneyFormat(Math.round(myPayment*100)/100);
      }
   } else if((myForm.mtype[0].checked == true) && (errors.length == 0)) {
      //do calculation for interest only mortages here
      myForm.interest.value = moneyFormat((myForm.amount.value*(myForm.rate.value/100))/12);
   } else { alert(errors); }
}

function calculate_yield(myForm) {
   var errors='';
   if(myForm.rent.value<1)
      errors += "Invalid rent amount\nPlease enter a non-zero value.\n\n";
   if(myForm.costs.value<1 || myForm.costs.value>10000)
      errors += "Invalid Annual Costs\nPlease enter a value between 1 and 10000.\n\n";
   if(this.mortgage_calc.interest.value<1 || myForm.costs.value>1000000)
     errors = "Please calculate your mortgage first.\n\n";
   
   if(errors != '')
   {
    alert(errors); 
	}
	else
	{
	  var monthly_profit = 0;
	  var monthly_mortgage = 0;
	  monthly_mortgage = moneyFormat(this.mortgage_calc.interest.value);
	  monthly_profit = (myForm.rent.value-monthly_mortgage);
	  
	  myForm.against_mortgage.value = moneyFormat(myForm.rent.value * 12);
	  
	  yearly_profit = (monthly_profit * 12) - myForm.costs.value;
	  myForm.profit.value = moneyFormat(yearly_profit);
	}
}

function moneyFormat(expr) {
   var decplaces = 2;
   var myString = "" + Math.round((eval(expr)) * Math.pow(10,decplaces));
   while (myString.length <= decplaces) { myString = "0" + myString; }
   var decpoint = myString.length - decplaces;
   return myString.substring(0,decpoint) + "." + myString.substring(decpoint, myString.length);
}
