// JavaScript Document
var res;

var error_start='<table width="200" border="0" cellspacing="0" cellpadding="0"><tr><td width="20" height="20"><img src="images/error.gif" width="16" height="16"></td><td width="5"></td><td class="div_error">';
var error_end='</td></tr></table>';

function pay_advance()
{
	var a1 = document.frm.a.value;
	var b1 = document.frm.b.value;
	var c1 = document.frm.c.value;
	document.getElementById('one').checked=false;
	document.getElementById('two').checked=true;	
	var c1 = c1-1;
	var r = (1+(b1/1200));
	var rate = (b1/1200);
	var p = Math.pow(r,c1);
	var x = (rate*p)/(p-1);
	var emi = (a1*x)/(1+x);	
	res=emi;
}

function pay_arrears()
{
	var a1 = document.frm.a.value;
	var b1 = document.frm.b.value;
	var c1 = document.frm.c.value;
	document.getElementById('one').checked=true;
	document.getElementById('two').checked=false;	
	var r = (1+(b1/1200));	
	var rate = (b1/1200);
	var p = Math.pow(r,c1);
	var r1 = a1*rate*p;
	var r2 = p-1;
	var emi = r1/r2;
	res=emi;
	
}

function calc_apr()
{
	
	var is_error=false;
	var emi_ty=true;
	//var div_array=Array("div_loanamt","div_rateofint","div_period","div_emipaytyp");
	var div_array=Array("div_loanamt","div_rateofint","div_period");
	/*var o = document.getElementById('one');
	var t = document.getElementById('two');
	if ((o.checked == false ) && (t.checked == false ))
	{
		emi_ty=false;
	}
	*/
		if (IsEmpty(document.frm.a,'text'))
		{
			//alert("Please Loan price");document.frm.a.focus();return false;
			is_error=true;
			thorw_error("Please enter the loan amount.","div_loanamt");
		}
		else if(ValidateNo(document.frm.a.value,"0"))
		{
			is_error=true;
			thorw_error("Please enter a valid loan amount.","div_loanamt");
			
		}
		else
		{
			if(!ValidateNo(document.frm.a.value,"1234567890."))
			{
				//alert("Invalid Loan price");document.frm.a.focus();return false;
				is_error=true;
				thorw_error("Please enter a valid loan amount.","div_loanamt");
			}
			else
			{
				hide_div("div_loanamt");	
			}
		}
		
		if (IsEmpty(document.frm.b,'text'))
		{
			is_error=true;
			//alert("Please enter an Rate of interest");document.frm.b.focus();return false;
			thorw_error("Please enter the rate of interest.","div_rateofint");
		}
		else if(ValidateNo(document.frm.b.value,"0"))
		{
			is_error=true;
			thorw_error("Please enter a valid rate of interest.","div_rateofint");
			
		}
		else
		{
			if(!ValidateNo(document.frm.b.value,"1234567890."))
			{
				is_error=true;
				thorw_error("Please enter a valid rate of interest.","div_rateofint");
				//alert("Invalid Rate of interest");document.frm.b.focus();return false;
			}
			else if (document.frm.b.value >= 100)
			{ 
				is_error=true;
				//alert("Invalid rate of interest");document.frm.b.focus(); return false;
				thorw_error("Please enter a valid rate of interest.","div_rateofint");
			}
			else
			{
				hide_div("div_rateofint");	
			}
		}

		if (IsEmpty(document.frm.c,'text'))
		{
			is_error=true;
			//alert("Please enter loan period ");document.frm.c.focus();return false;
			thorw_error("Please enter the period of loan.","div_period");
		}
		else if(ValidateNo(document.frm.c.value,"0"))
		{
			is_error=true;
			thorw_error("Please enter a valid period of loan.","div_period");
			
		}
		else
		{
			if(!ValidateNo(document.frm.c.value,"1234567890."))
			{
				is_error=true;
				//alert("Invalid loan period ");document.frm.c.focus();return false;
				thorw_error("Please enter a valid period of loan.","div_period");
			}
			else
			{
				hide_div("div_period");	
			}
		} 
		
		if (IsEmpty(document.frm.e,'text'))
		{
			//alert("Please Loan price");document.frm.a.focus();return false;
			is_error=true;
			thorw_error("Please enter the Extra cost.","div_extcost");
		}
		else if(ValidateNo(document.frm.e.value,"0"))
		{
			is_error=true;
			thorw_error("Please enter a valid Extra cost.","div_extcost");
			
		}
		else
		{
			if(!ValidateNo(document.frm.e.value,"1234567890."))
			{
				//alert("Invalid Loan price");document.frm.a.focus();return false;
				is_error=true;
				thorw_error("Please enter a valid Extra cost.","div_extcost");
			}
			else
			{
				hide_div("div_extcost");	
			}
		}
		if(is_error)
		{
			return false;
		}
		else{
				
				for(j=0;j<div_array.length;j++)	
				{
					if(document.getElementById(div_array[j]).style.display=='')	
					{
						document.getElementById(div_array[j]).style.display='none';
					}
				}
				var a = parseInt(document.frm.a.value);
				var b = parseFloat(document.frm.b.value);
				var c = parseInt(document.frm.c.value); 
				//var d = parseInt(document.frm.d.value); 
				var e = parseInt(document.frm.e.value); 
				//var url="apr_calculation.php?loan_amount="+a+"&interest="+b+"&periods="+c+"&points="+d+"&ocost="+e;
				var url="index.php?option=calculators&page=apr_calculation&notemplate=yes";
				url=url+"&loan_amount="+a+"&interest="+b+"&periods="+c+"&ocost="+e;
				http = new getHTTPObject();
				if (http) 
				{
					http.open("GET", url, true);
					http.send(null);
					http.onreadystatechange = handlerAPRHttpResponse;
				}	
				return false;
		}
				
		
		/*if(is_error==false){
			alert('no error');
		}
		*/
		/*if(o.checked==true)
		{
			 pay_arrears();
		}
		else if(t.checked=true)
		{
			pay_advance();	
		}
		document.frm.emi.value = parseInt(round_decimals(res,2));
		words('emi','formatedloanAmount3','wordloanAmount3');
		return false;
		*/
	//}
}

function thorw_error(error_txt,div_id)
{
	var DivObj = (document.getElementById(div_id)) ? document.getElementById(div_id) : new Object;
	DivObj.innerHTML = error_start+error_txt+error_end;
	DivObj.style.display="";
}

function hide_div(div_id)
{
	var DivObj = (document.getElementById(div_id)) ? document.getElementById(div_id) : new Object;
	//DivObj.innerHTML = error_start+error_txt+error_end;
	DivObj.style.display='none';
}
function IsNumeric(obj)
   //  check for valid numeric strings 
   {
   var strValidChars = "0123456789";
   var strChar;
   var strChar_int='';
   var blnResult = true;
   var strString=obj.value;	
   var dpos=strString.indexOf(".");
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
		 strChar_int=strString.substr(0,dpos);
		 obj.value=strChar_int;
		 break;
       }
	   
      }
   //return blnResult;
   }
 function IsNumeric123(obj)
	{
		//alert(obj.value);
		mystring = obj.value;
		if (mystring.match(/^\d+$/ ) ) {
		
			//alert("Valid number");
		}
		else
		{
			obj.value='';
			//alert("Only numeric values are allowed");
		}
	}  
function round_decimals(original_number, decimals) {
	//alert(original_number);
  var result1 = original_number * Math.pow(10, decimals)
  //alert(result1);
  var result2 = Math.round(result1)
  //alert(result2);
  var result3 = result2 / Math.pow(10, decimals)
  //alert(result3);
  return (result3)
}
function handlerAPRHttpResponse()
{
	
	if(http.readyState == 4)
	{
		//alert(http.responseText);	
		document.frm.emi.value = round_decimals(http.responseText,2);
		//words('emi','formatedloanAmount3','wordloanAmount3');
		
		/*var res_tab='<table cellpadding="0" cellspacing="0" border="0" width="330" bgcolor="#FFFFFF" style="border:3px solid #018BCC;">';
		res_tab+='<tr><td colspan="2" align="right"><a href="javascript:;"  class="mediumtxt" onclick="javascript:hide_delete_div();" style="font-size:12px;" title="Close"><b>x</b></a></td></tr><tr><td colspan="2" align="center" style="font-size:12px;"><font color="red">'+http.responseText+'</font></td></tr><tr><td colspan="2">&nbsp;</td></tr></table>';
		document.getElementById("div_aproutput").innerHTML=res_tab;
		*/
	}
}
function getHTTPObject()
{
	var xmlhttp;
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try 
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	
		}
		catch (E)
		{
			xmlhttp = false;
		}
	
	}
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlhttp = false;
		}
	}
	
	return xmlhttp;
}
