// JavaScript Document



function ValidateForm(f){

	var numericExpression = /^[^\+\-a-zA-Z]?([0-9]*\.[0-9]+|[0-9]+)$/;


	if(!f.loan.value.match(numericExpression)){		
		alert("Please enter valid loan amount.");
		f.loan.focus();
		return false;
	}	
	if(!f.interest.value.match(numericExpression)){		
		alert("Please enter valid Interest rate.");
		f.interest.focus();
		return false;
	}	
	if(!f.year.value.match(numericExpression)){		
		alert("Please enter valid Term in years.");
		f.year.focus();
		return false;
	}
	if(f.freq.value==0){		
		alert("Please choose the frequency.");
		f.freq.focus();
		return false;
	}
	
	if(f.loantype.value==0){		
		alert("Please choose the Loan Type.");
		f.loantype.focus();
		return false;
	}
	
	if(f.loantype.value==3){		
		if(!f.intperiod.value.match(numericExpression)){		
			alert("Please enter valid interest only years.");
			f.intperiod.focus();
			return false;
		}
	}
	if(!f.payment.value.match(numericExpression)){		
		alert("Please enter valid Mortgage payment.");
		f.payment.focus();
		return false;
	}	
	if(!f.income.value.match(numericExpression)){		
		alert("Please enter valid Monthly Income.");
		f.income.focus();
		return false;
	}	
	if(!f.expenses.value.match(numericExpression)){		
		alert("Please enter valid Monthly Expenses.");
		f.expenses.focus();
		return false;
	}
	if(!f.totalexpense.value.match(numericExpression)){		
		alert("Please enter valid Total Expenses & Debt.");
		f.totalexpense.focus();
		return false;
	}


return true;

}



	function checkNumeric(value){
	
		var anum=/(^\d+$)|(^\d+$)/   
		  if (anum.test(value))   
			 return true;
		
		return false;
	
	}

	function Calculate(){

		var years_left = 0;
		var freq	=	document.getElementById('freq').value;
		years_left	=	document.getElementById('year').value;
		
		// Set the periodicity
		if(freq==4){	var pay_periodicity	=	26;}
		if(freq==3){	var pay_periodicity	=	52;}
		if(freq==1){	var pay_periodicity	=	12;}
		
		document.vform2.noofpayment.value 	= pay_periodicity;
		if(document.vform2.loan.value!='' && document.vform2.interest.value!='' && document.vform2.year.value!=''){
			Get_payment();
		}

	}

	//For simple loan types: calculate schedule payment
	function Get_payment(){

		var loan_amt 		=	document.vform2.loan.value;
		var freq			=	document.getElementById('freq').value;
		var rate			=	document.vform2.interest.value;
		var years_left		=	document.vform2.year.value;
		//loan type
		var loanType		=	document.vform2.loantype.value;
		//interestonly 
		var interestPeriod		=	document.vform2.intperiod.value;
		var monthlyExpense = document.vform2.expenses.value -0;
		var monthlyIncome  = document.vform2.income.value -0;
		
		// Set the periodicity
		if(freq==4){	var pay_periodicity	=	26;}
		if(freq==3){	var pay_periodicity	=	52;}
		if(freq==1){	var pay_periodicity	=	12;}
		
		 
		if(loanType==3){
			//calculating total period in case of interest only
			var total_period 		=	(years_left - interestPeriod) *pay_periodicity;
		}else{
			//calculating total period for other cases 
			var total_period 		=	years_left*pay_periodicity;
		}
		
		var interest_percent	=	rate/100;
		
		
		if(loanType==1 || loanType==3){
			//fixed rate
			var period_interest		=	interest_percent/pay_periodicity;
		}else if(loanType==2){
			//canadian
			var compound_period		=	'2';
			var period_interest		=	( Math.pow((1+(interest_percent/compound_period)),2/pay_periodicity) ) -1;
		}
		
		//interest only period mortgage payemnt calculation
		if(interestPeriod > 0 ){
			var intOnlyPayment = loan_amt * (interest_percent/pay_periodicity);
		}else{
			var intOnlyPayment = 0;
		}
	
		//Current payment
		var c_period_payment 	= loan_amt * (period_interest/(1 - Math.pow((1 + period_interest), -(total_period))));
		var payment			 	=	c_period_payment.toFixed(2);	

		var month_payment	= 0.0;
		var intOnlyMonthPay = 0.0;
		if(freq==4){
			var month_payment	=	2.16666*(payment);
			var intOnlyMonthPay =   2.16666*(intOnlyPayment);
		}else if(freq==3){
			var month_payment	=	4.33333*(payment);
			var intOnlyMonthPay =   4.33333*(intOnlyPayment);
		}else if(freq==1){
			var month_payment	=	1*(payment);
			var intOnlyMonthPay =   1*(intOnlyPayment);
		}
		//interest only monthly payment 
		document.getElementById('ipayment').value		=	intOnlyMonthPay.toFixed(2);
		//Monthly payment
		document.getElementById('payment').value		=	month_payment.toFixed(2);
	
		//interest only monthly payment 
		var intTotalExpense = (intOnlyMonthPay + monthlyExpense);
		
		var intOnlyExp = document.getElementById('itotalexpense').value		=	intTotalExpense.toFixed(2);
		
		//Monthly payment
		var afterIntOnlyExp = document.getElementById('totalexpense').value		=	(month_payment + monthlyExpense).toFixed(2);
		
		if((intOnlyExp > monthlyIncome) || (afterIntOnlyExp > monthlyIncome)){
											
			alert("Your monthly expense is greater than your income");
		}
	
	}


	function GetHttp(){
		var xmlhttp;
		if (window.XMLHttpRequest)
		  {
		  // code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else if (window.ActiveXObject)
		  {
		  // code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		else
		  {
		  alert("Your browser does not support XMLHTTP!");
		  }
		  return xmlhttp;
		
	}
	function adv(){
		xmlhttp = GetHttp();
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4)
		  {
		  document.vform2.paymentondebts.value = xmlhttp.responseText;
		  }
		}
		xmlhttp.open("GET","totaldebts.php?t=0",true);
		xmlhttp.send(null);
		
		xmlhttp1 = GetHttp();
		xmlhttp1.onreadystatechange=function()
		{
		if(xmlhttp1.readyState==4)
		  {
		  document.vform2.totaldebts.value = xmlhttp1.responseText;
		  }
		}
		xmlhttp1.open("GET","totaldebts.php?t=1",true);
		xmlhttp1.send(null);
		
		setTimeout("Calculate()",1250);
		
	}
	
	
	function HomeDebt(){
		xmlhttp = GetHttp();
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4)
		  {
		  document.vform1.paymentondebts2.value = xmlhttp.responseText;
		  }
		}
		xmlhttp.open("GET","totaldebts_home.php?t=0",true);
		xmlhttp.send(null);
		
		xmlhttp1 = GetHttp();
		xmlhttp1.onreadystatechange=function()
		{
		if(xmlhttp1.readyState==4)
		  {
		  document.vform1.totaldebts2.value = xmlhttp1.responseText;
		  }
		}
		xmlhttp1.open("GET","totaldebts_home.php?t=1",true);
		xmlhttp1.send(null);
		
		setTimeout("CalTotalExp2",1250);
		
	}