var num=0;
var txtloanamount=0;
var rateofinterest=0;
var months=0;
var nls="";
var rr="\r";
var b="                               ";
var d="-------------------------------------------------------------------\r";
var s="Rs.";
var done=0;
var EMItoCalculate;
var emiType;

function iA()
{
	this.length=iA.arguments.length;
	for (var i=0;i<this.length;i++)
	{
		this[i]=iA.arguments[i];
	}
}

var pwr=new iA( 10 );
var dec=new iA( 16 );

pwr[0]=1;

for (var i=0;i<9;i++)
{
	pwr[i+1] = pwr[i] * 10;
}


dec[0]=.1;
dec[1]=.01;
dec[2]=.001;
dec[3]=.0001;
dec[4]=.00001;
dec[5]=.000001;
dec[6]=.0000001;
dec[7]=.00000001;
dec[8]=.000000001;
dec[9]=.0000000001;
dec[10]=.00000000001;
dec[11]=.000000000001;
dec[12]=.0000000000001;
dec[13]=.00000000000001;
dec[14]=.000000000000001;
dec[15]=.0000000000000001;

var ns ="01234567890";
var cr="";
var str="";

function stn()
{
	num=0;
	pos=str.indexOf(".");
	sfx="";
	if (pos>-1)
	{
		sfx=str.substring(pos+1,str.length);
		str=str.substring(0,pos);
  	}
	strl=str.length;
	
	for (var i=strl-1;i>-1;i--)
	{
		cr=str.substring(i,i+1);
		pos=ns.indexOf(cr);
		num+=pos*pwr[strl-i-1];
	}
	
	if (sfx!=""&&sfx.length>dp)
	{
		pos=ns.indexOf(sfx.charAt(dp+1));
		if (pos>4)
		{
			pos=ns.indexOf(sfx.charAt(dp));
			sfx=sfx.substring(0,dp-1)+(pos+1);
		}
	}

	if (sfx!="")	
	{
		for (var i=0;i<dp;i++)
		{
			cr=sfx.substring(i,i+1);
			pos=ns.indexOf(cr);
			num+=pos*dec[i];
		}
	}
 }



function testIt( form )
{
	str=document.frmEmi.txtcarcost.value;
	fmtIt();
	bl=str.length+3;
	dp=2;
	stn();
	txtcarcost=num;
	
	
	str=document.frmEmi.txtloanamount.value;
	fmtIt();
	bl=str.length+3;
	dp=2;
	stn();
	txtloanamount=num;
	
	str=document.frmEmi.interestRate.value;
	dp=4;
	stn();
	rateofinterest=num;
	
	months = document.frmEmi.hdnMonths.value;
			
	if (txtcarcost == "")
	{
		alert("Enter Car Cost");
		document.frmEmi.txtcarcost.focus();
		return;
	}
	else 
	{
		if (txtcarcost<1||txtcarcost>pwr[9])
		{
			alert("Invalid Car Cost");
			document.frmEmi.txtcarcost.select();
			document.frmEmi.txtcarcost.focus();
			return;
		}
	}
	
	if (txtloanamount == "")
	{
		alert("Enter Loan Amount");
		document.frmEmi.txtloanamount.focus();
		return;
	}
	else
	{
		if (txtloanamount<1||txtloanamount>pwr[9])
		{
			alert("Invalid Loan Amount");
			document.frmEmi.txtloanamount.select();
			document.frmEmi.txtloanamount.focus();
			return;
		}
	}
	if (txtloanamount > txtcarcost)
	{
		alert("Loan amount can not be more than Cost of Car");
		document.frmEmi.txtcarcost.select();
		document.frmEmi.txtcarcost.focus();
		return;
	}
	
	if (rateofinterest == "")
	{
		alert("Enter Rate of Interest");
		document.frmEmi.rateofinterest.focus();
		return;
	}
	else
	{
		if (rateofinterest<.0001||rateofinterest>99)
		{
			alert("Invalid Interest Rate");
			document.frmEmi.rateofinterest.select();
			document.frmEmi.rateofinterest.focus();
			return;
		}
	}
	
		computeForm();
	 
}


function computeForm()
{
	ls = "";
	frmEminum = 1;
	i = rateofinterest/12/100;
	fpv = 0;
	for (var j = 0; j < months; j++)
	{
		frmEminum = frmEminum * ( 1 + i );
	}
	tmp = ( txtloanamount * frmEminum * i ) / ( frmEminum - 1 );
	fpv += tmp;
	fcalc = ( months * fpv ) - txtloanamount;
	prtSched();
}
 

function prtSched()
{
	var totalInterest = 0;
	fpv+=.01;
	str=""
	str+=fpv;
	fmtIt();
	dp=2;
	stn();
	fpv=num;
	pct=rateofinterest/12/100;
	if (bl<14)
	{
		bl=14;
	}
	
	if ( document.frmEmi.R1[1].checked )
	{
		emiType = "Arrears(i.e. Rear Ended EMI's).";
	}
	else
	{
		emiType = "Advance."
	}
	
	ls="Following  Schedule Is For : "+ s + document.frmEmi.txtloanamount.value
	  + " to repay in " + months
	  + " months." + rr
	  + "All calculations are based on EMI in " + emiType + rr + d + rr
	  +"Payment       Payment       Interest      Principal     Balance"
	  +rr
 	  +"Number        Amount        Amount        Reduction     Due"
	  +rr+d+rr;
	
	for (var j=0;j<months;j++)
	{
		ntr=(txtloanamount*pct);

		str="";
		str+=ntr;
		fmtIt();
		
		// Next Line is added by Banwari to calculate Total Interest Amount.
		totalInterest = Number( totalInterest ) + Number( str );
		
		ntr1=s+str;
		prp=fpv-ntr;
		
		if (prp>txtloanamount)
		{
			prp=txtloanamount;
		}
		
		str="";
		str+=prp;
		fmtIt();
		prp1=s+str;
		txtloanamount-=prp;
		str="";
		str+=txtloanamount;
		fmtIt();
		txtloanamount1=s+str;
		
		if (fpv>(ntr+prp))
		{
			fpv=ntr+prp;
		}
		
		str="";
		str+=fpv;
		fmtIt();
		fpv1=s+str;
		
		if((months-1)==0)
		{
			document.frmEmi.monthly_emi.value=fpv1;
		}
		else
		{
			if(j==(months-2))
			document.frmEmi.monthly_emi.value=fpv1;
		}

		str="";
		str+=(j+1)+".";
		ls+=b.substring(0,2)+str+b.substring(0,12-str.length)
		  +fpv1+b.substring(0,14-fpv1.length)+ntr1
		  +b.substring(0,14-ntr1.length)+prp1
		  +b.substring(0,14-prp1.length)+txtloanamount1
		  +rr;
	}

	if ( document.frmEmi.R1[0].checked && done==0 )
	{
		advanceEMI( Math.round( fpv ) );
		done=0;
		return;
	}
	
	document.frmEmi.sched.value = ls + d + rr
		+ ' Total Interest Amount : Rs. ' + Math.round( totalInterest * 100 ) / 100 + rr + d + rr
		+ ' Note: Interest calculated at 1/12th of annual interest rate on'
		+ rr + "    the remaining principal amount. (Rounding errors "
		+ "possible)"+rr+d+rr;
}

function advanceEMI( calculatedEMI )
{
	EMItoCalculate = calculatedEMI
	done=1;
	document.frmEmi.txtloanamount.value = document.frmEmi.txtloanamount.value - calculatedEMI;
	document.frmEmi.hdnMonths.value = document.frmEmi.hdnMonths.value - 1;
	//alert( months )
	testIt();
	document.frmEmi.txtloanamount.value = parseInt(document.frmEmi.txtloanamount.value) + parseInt(EMItoCalculate);
	document.frmEmi.hdnMonths.value++;
}

function fmtIt()
{
	//checking for decimal places
	pos=str.indexOf(".");
	
	if (pos==0)
	{
		str="0"+str;
		pos++;
	}
	
	if (pos<0)
	{
		str+=".00";
		pos=str.indexOf(".");
	}

	str+="0000";
	str=str.substring(0,pos+4);
	cr=str.charAt(str.length-1);
	pos=ns.indexOf(cr);
	str=str.substring(0,str.length-1);

	if (pos>5)
	{
		fmtIt1();
	}
 }




function fmtIt1()
{
	for (var k=str.length-1;k>-1;k--)
	{
		cr=str.charAt(k);
		posn=ns.indexOf(cr);

		if (posn<0)
		{
			k--;
		}
		else
		{
			str=str.substring(0,k)+ns.substring(posn+1,posn+2)
			   +str.substring(k+1,str.length);
			
			if (posn!=9)
			{
			     k=-1;
			}
		}
	}
}
	function printSchedule()
	{
		var myWindow = window.open('','','toolbars=no,width=500,height=400,scrollbars=yes');
		myWindow.document.write('<html><head><title>EMI Schedule</title></head><body>');
		myWindow.document.write('<pre>' + document.getElementById('txtSchedule').value + '</pre>');
		myWindow.document.write('</body></html>')
		myWindow.print();
		myWindow.document.close();
		myWindow.close();
	}
	
	function putMonthsInHiddenField()
	{
		var MonthOptions = document.frmEmi.months;
		
		for( var i = 0; i < MonthOptions.length; i++ )
		{
			if ( MonthOptions[i].checked ) 
			{
				document.forms[0].hdnMonths.value = MonthOptions[i].value;
			}
		}
	}
