var res1;
var res2;
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"><p style="color:#FF8E27; font-size:11px;">';
var error_end='</p></td></tr></table>';	
var payoffdate;
var res;
var totpay1;
var t2;		
var balan;
var tab1,tab2;
var Year = new Array();
var end_pay_mon;
var end_pay_year;
Year[1] = "Jan";
Year[2] = "Feb";
Year[3] = "Mar";
Year[4] = "Apr";
Year[5] = "May";
Year[6] = "Jun";
Year[7] = "Jul";
Year[8] = "Aug";
Year[9] = "Sep";
Year[10] = "Oct";
Year[11] = "Nov";
Year[12] = "Dec";	

var exd = new Date();
var y=exd.getFullYear();	
var m=(exd.getMonth() + 1);
var d1=exd.getDate();			

function FocusOnSelect(frm,val)
{
	for(var i=0; i<frm.options.length; i++)
	{
		if(frm.options[i].value==val)
		{
			frm.selectedIndex = i;
		}
	}
}

function Init()
{
	cmbDay = document.getElementById("day");
	cmbMonth = document.getElementById("month");
	cmbYear = document.getElementById("year");	
	FocusOnSelect(cmbDay,d1);
	FocusOnSelect(cmbMonth,m);
	FocusOnSelect(cmbYear,y);	
}

function pay_advance(a1,b1,c1)
{  
	var c1 = c1-1;
	var r1 = (b1/1200);
	var r = (1+(b1/1200));
	var p = Math.pow(r,c1);			
	var x = (r1*p)/(p-1);
	var emi = (a1*x)/(1+x);	
	//emi = Math.round(emi);
	return res=emi;
}

function pay_arrears(a1,b1,c1)
{ 
	var r = (1+(b1/1200));
	var r1 = (b1/1200);
	var p = Math.pow(r,c1);
	var r1 = a1*r1*p;
	var r2 = p-1;
	var emi = r1/r2;
	//emi = Math.round(emi);
	return res=emi;		
}

function round_decimals(original_number, decimals)
{
	var result1 = original_number * Math.pow(10, decimals)
	var result2 = Math.round(result1)
	var result3 = result2 / Math.pow(10, decimals)
	return (result3)
}

function calc_emi()
{
	var is_error=false;
	var emi_ty=true;
	var div_array=Array("div_loanamt","div_rateofint","div_period");
	if (IsEmpty(document.frm.a,'text'))
	{
		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."))
		{
			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");
		}
		else if (document.frm.b.value >= 100)
		{ 
			is_error=true;
			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;
		thorw_error("Please enter the period of loan.","div_period");
	}
	else
	{
		if(!ValidateNo(document.frm.c.value,"0123456789."))
		{
			is_error=true;
			thorw_error("Please enter a valid period of loan.","div_period");
		}
		else
		{
			hide_div("div_period");	
		}
	} 
	if(is_error)
	{
		return false;
	}
	
	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 = parseFloat(document.frm.a.value);
	var d1 = parseInt(document.frm.b.value);
	var e = parseInt(document.frm.c.value); 
	
	if(document.getElementById("payment_arrears").checked==true)
	{
		pay_arrears(a,d1,e);
	}
	else
	{
		pay_advance(a,d1,e);	
	}
	
	balan = res;		 
	var t1 = balan*e;		
	totpay1 = t1;
	var tot = totpay1;
	t2 = tot - a; 		
	tab1 = '<p><span class="normtxt">EMI/Payment (Rs.):<img src="images/trans.gif" width="50" height="1" />'+Math.round(balan)+'<br/><img src="images/trans.gif" width="1" height="10" /><br/>'+'Total Payments (Rs.):<img src="images/trans.gif" width="38" height="1" />'+Math.round(totpay1)+'<br/><img src="images/trans.gif" width="1" height="10" /><br/>Total Interest (Rs.):<img src="images/trans.gif" width="50" height="1" />'+Math.round(t2)+'</span><br/>&nbsp;<br/></p>';	
	var m = parseInt(document.frm.month.value);
	var day1 = document.frm.day.selectedIndex+1;
	var y1=parseInt(document.frm.year.value);
	var yearValue = j = m;
	totCount=parseInt(e);
	
	for(k=1;k<totCount;k++)
	{
		m=m+1;
		if(m==13)
		{
			m=1;
			y1=y1+1;
		}
	}
	//alert(m);
	end_pay_mon=m;
	end_pay_year=y1;
	res1 = Year[m]+'-'+y1;
	tab2 = '<p><span class="normtxt">Pay off-Date: <img src="images/trans.gif" width="76" height="1" />'+res1+'</span></p><br/><br/>'
	return true;
}

function showEMI(mon,mval,yval)
{
	
	var year_wise=false;
	//getting calendar type
	var show_cal;
	var mon_year;
	for( i = 0; i < document.frm.payment.length; i++ )
	{
		if( document.frm.payment[i].checked == true ){
			show_cal = document.frm.payment[i].value;
		}
		
	}
	if(show_cal==2 || show_cal==3) year_wise=true;
	
	if(!calc_emi())	return;		 
	var tab='<table border=0 width=600 align=center>';		
	var exd = new Date();
	var y=yval;
	var m=parseInt(mval);
	var d1=exd.getDate();	
	day = (document.frm.day.selectedIndex)+1;			
	var i=0;					
	a1 = parseFloat(document.frm.a.value);
	b1 = parseInt(document.frm.b.value);
	c1 = parseInt(document.frm.c.value);			
	//if(m==12){m=m-12;y=parseInt(y)+1;}
	var due = new Array();			
	var principalamount = new Array();
	var balanceamount = new Array();
	var interestpay = new Array();
	var paymentdue=new Array();
	var bal = a1;
	var p = a1;
	var rate = b1;
	var interest1;
	var interest = balan;	
	var r=b1/1200;
	
	 var f_month=Year[document.getElementById("month").value];
   	 var f_month_num=document.getElementById("month").value;
	 var f_year=document.getElementById("year").value;
	 var cal_year_mon;
	// paymentdue[i]=res;
	for(i=1;i<=c1;i++)
	{ 				
		var f_month_y=Year[f_month_num];
		
		if(document.getElementById("payment_arrears").checked==true)
		{					
			due[i]= res;
			if(i==1)//month one
			{
				interestpay[i] = p*r;
				principalamount[i] = res-interestpay[i];
				balanceamount[i] = p-principalamount[i];
				paymentdue[i]=res;
			}
			else
			{
				
				if(show_cal==2 || show_cal==3){
					    
						if(i==c1)//last month
						{
							interestpay[i] = balanceamount[i-1]*r;
							principalamount[i] = res-interestpay[i];
							balanceamount[i] = 0;
							due[i]=interestpay[i]+principalamount[i];
							paymentdue[i]=paymentdue[i-1]+res;
						}
						else
						{
							interestpay[i] = balanceamount[i-1]*r;
							principalamount[i] = res-interestpay[i];
							balanceamount[i] = balanceamount[i-1]-principalamount[i];
							paymentdue[i]=paymentdue[i-1]+res;
						}
						
						//if(show_cal==3 || show_cal==2){
							
							if(show_cal==3)
								 cal_year_mon='Jan';
							else
								cal_year_mon='Apr';
							
							if(f_month_y==cal_year_mon){
								interestpay[i]=0+interestpay[i];
								principalamount[i]=0+principalamount[i];
								paymentdue[i]=res;
								//alert(principalamount[i]);
							}
							else{
								interestpay[i]=interestpay[i-1]+interestpay[i];
								principalamount[i]=principalamount[i-1]+principalamount[i];
								paymentdue[i]=paymentdue[i-1]+res;
							}
							
						//}
						//else{
							//interestpay[i]=interestpay[i-1]+interestpay[i];
							//principalamount[i]=principalamount[i-1]+principalamount[i];
							
						//}
						//alert(principalamount[i]);
				}
				else{
										
						if(i==c1)//last month
						{
							if(show_cal==3 || show_cal==2){
									if(show_cal==3)
										 cal_year_mon='Jan';
									else
										cal_year_mon='Apr';
									
									if(f_month_y==show_cal){
									interestpay[i]=0+interestpay[i];
									principalamount[i]=0+principalamount[i];
									paymentdue[i]=res;
									//alert(principalamount[i]);
									}
									else{
										interestpay[i]=interestpay[i-1]+interestpay[i];
										principalamount[i]=principalamount[i-1]+principalamount[i];
										paymentdue[i]=paymentdue[i-1]+res;
									}
							}
							else{
								interestpay[i] = balanceamount[i-1]*r;
								principalamount[i] = res-interestpay[i];
								balanceamount[i] = 0;
								due[i]=interestpay[i]+principalamount[i];
							}
						}
						else
						{
							interestpay[i] = balanceamount[i-1]*r;
							principalamount[i] = res-interestpay[i];
							balanceamount[i] = balanceamount[i-1]-principalamount[i];					
						}
				}
				
			}
		}
		else
		{	
			due[i]= res;
			if(i==1)
			{
				interestpay[i] = 0;
				principalamount[i] = res;
				balanceamount[i] = p-res;
				paymentdue[i]=res;
			}
			else
			{
						
					if(show_cal==2 || show_cal==3){
						if(i==c1)
						{
							interestpay[i] = balanceamount[i-1]*r;
							principalamount[i] = res-interestpay[i];
							balanceamount[i] = 0;
							due[i]=interestpay[i]+principalamount[i];
							paymentdue[i]=paymentdue[i-1]+res;
						}
						else
						{
							interestpay[i] = balanceamount[i-1]*r;
							principalamount[i] = res-interestpay[i];
							balanceamount[i] = balanceamount[i-1]-principalamount[i];
							paymentdue[i]=paymentdue[i-1]+res;
						}
						//if(show_cal==3 || show_cal==2){
							if(show_cal==3)
								 cal_year_mon='Jan';
							else
								cal_year_mon='Apr';
								
							if(f_month_y==cal_year_mon){
								interestpay[i]=0+interestpay[i];
								principalamount[i]=0+principalamount[i];
								paymentdue[i]=res;
								//alert(principalamount[i]);
							}
							else{
								interestpay[i]=interestpay[i-1]+interestpay[i];
								principalamount[i]=principalamount[i-1]+principalamount[i];
								paymentdue[i]=paymentdue[i-1]+res;
							}
							
						//}
						//else{
							//interestpay[i]=interestpay[i-1]+interestpay[i];
							//principalamount[i]=principalamount[i-1]+principalamount[i];
							
						//}
					}
					else{
						if(i==c1)
						{
							
							if(show_cal==3 || show_cal==2){
									if(show_cal==3)
										 cal_year_mon='Jan';
									else
										cal_year_mon='Apr';
									
									if(f_month_y==show_cal){
									interestpay[i]=0+interestpay[i];
									principalamount[i]=0+principalamount[i];
									paymentdue[i]=res;
									//alert(principalamount[i]);
									}
									else{
										interestpay[i]=interestpay[i-1]+interestpay[i];
										principalamount[i]=principalamount[i-1]+principalamount[i];
										paymentdue[i]=paymentdue[i-1]+res;
									}
							}
							else{
								interestpay[i] = balanceamount[i-1]*r;
								principalamount[i] = res-interestpay[i];
								balanceamount[i] = 0;
								due[i]=interestpay[i]+principalamount[i];
							}
							
						}
						else
						{
							interestpay[i] = balanceamount[i-1]*r;
							principalamount[i] = res-interestpay[i];
							balanceamount[i] = balanceamount[i-1]-principalamount[i];					
						}
					}
			}
		}
		
		if(f_month_num!=12)
			f_month_num++;
		else
			f_month_num=1;
	}
	var totCount = parseInt(mon);			
	var Sno=1;
	yearValue = j = m;
	tab ='<tr><td colspan="6" class="normtxt" style="padding:7px 0px;"><br/><p><b>Output</b></p><br /><br /></td></tr>'+					
	tab1+tab2+
	'<tr><td colspan="6" style="background-image:url(.../images/bg/grey&white.gif); background-repeat:repeat-x; height:2px;"><!-- --></td></tr>'+
	'<div class="pink_txtb12"><p>Amortization Schedule</p></div><br/>'+
	'<table cellpadding="0" cellspacing="1" border="0" align="center" class="amort_tbl">' +															
	'<tr class="rowHead">'+						
	'<td><b><p>No</p></b></td>' +
	'<td><b><p>Month/year</p></b></td>' +
	'<td><b><p>Payment Due (Rs.)</p></b></td>' +
	'<td><b><p>Interest (Rs.)</p></b></td>' +
	'<td><b><p>Principal (Rs.)<p></b></td>' +
	'<td><b><p>Balance (Rs.)</p></b></td>' +
	'</tr>' +
	'<tr><td colspan="5"><!--  --></td><td><p>'+a1+'</p></td></tr>';		
	p=1;
	var end_arr=new Array();
	end_arr=res1.split("-"); 
	var end_pay_char=end_arr[0];
	
	for(i=1;i<=totCount;i++)
	{ 
		
		if(year_wise){
			
			var chk_val;
			var prev_year;
			var disp_year;
			if(show_cal==2) chk_val='Mar'; else chk_val='Dec';
			//prev_year=y.substring(2,2);
			//Year[yearValue]
			
			prev_year=parseInt(y)-1;	
			if(Year[yearValue]==chk_val){
				
						var start_mon;
						var f_month=Year[document.getElementById("month").value];
						var f_month_num=document.getElementById("month").value;
						var f_year=document.getElementById("year").value;
				
				if(chk_val=='Mar'){ 
				   		if(p==1){
								if(f_month_num==3){
									start_mon=f_month+" - "+f_year; 
								}
								else if(f_month_num ==1 || f_month_num ==2){
							    	start_mon=f_month+" "+f_year+" - Mar"+" "+f_year;
								}
								else{
							 		start_mon=f_month+" "+f_year+" - Mar"+" "+(parseInt(f_year)+1);
								}
					    		disp_year=start_mon;
				   		}
				   		else{
				   			disp_year="Apr "+prev_year+" - Mar"+" "+y;  
				   		}
				}
				else{  
					
						if(p==1){
								if(f_month_num==12){
									start_mon=f_month+" - "+f_year; 
								}
								else{
							 		start_mon=f_month+" "+y+" - Dec"+" "+y;
								}
					    		disp_year=start_mon;
				   		}
				   		else{
				   			disp_year="Jan "+y+" - Dec"+" "+y;  
				   		}
						//disp_year="Jan "+y+" - "+chk_val+" "+y;
					
				}
				
				tab+='<tr>';			
				tab+= '<td><p>'+p+'</p></td>' +
				'<td><p>'+disp_year+'</p></td>' +								
				'<td><p>'+formatNumber(paymentdue[Sno],2)+'</p></td>' +
				'<td><p>'+formatNumber(interestpay[Sno],2)+'</p></td>' +
				'<td><p>'+formatNumber(principalamount[Sno],2)+'</p></td>' +
				'<td><p>'+formatNumber(balanceamount[Sno],2)+'</p></td>';
				tab+='</tr>'; 
				p++;
			 }
			 else if(Year[yearValue]!=chk_val && i==totCount){
				
				/*
				var ye=parseInt(y)+1;
				if(chk_val=='Mar'){
					 disp_year="Apr "+prev_year+" - "+chk_val+" "+y;
					 
				}
				else{
					var ye=parseInt(y);
					disp_year="Jan "+ye+" - "+chk_val+" "+ye;
				}
				*/
				if(chk_val=='Mar'){
						var ye;
						if(end_pay_mon==4){
										disp_year=end_pay_char+" - "+end_pay_year; 
						}
						else if(end_pay_mon ==1 || end_pay_mon ==2 ||end_pay_mon==3){
									ye=parseInt(y)-1;
									disp_year="Apr "+ye+" - "+end_pay_char+" "+end_pay_year;
						}
						else{
									 ye=parseInt(y);
									 disp_year="Apr"+" "+ye+" - "+end_pay_char+" "+end_pay_year;
						}
				}
				else{
						
						if(end_pay_mon==1){
								disp_year=end_pay_char+" - "+end_pay_year; 
						}
						else{
								disp_year="Jan"+" "+y+" - "+end_pay_char+" "+end_pay_year;
						}
				}
				tab+='<tr>';			
				tab+= '<td><p>'+p+'</p></td>' +
				'<td><p>'+disp_year+'</p></td>' +								
				'<td><p>'+formatNumber(paymentdue[Sno],2)+'</p></td>' +
				'<td><p>'+formatNumber(interestpay[Sno],2)+'</p></td>' +
				'<td><p>'+formatNumber(principalamount[Sno],2)+'</p></td>' +
				'<td><p>'+formatNumber(balanceamount[Sno],2)+'</p></td>';
				tab+='</tr>'; 
				p++;	
			 }
			 
		}
		else{
		
				tab+='<tr>';			
				tab+= '<td><p>'+Sno+'</p></td>' +
				'<td><p>'+Year[yearValue]+'-'+y+'</p></td>' +								
				'<td><p>'+formatNumber(due[Sno],2)+'</p></td>' +
				'<td><p>'+formatNumber(interestpay[Sno],2)+'</p></td>' +
				'<td><p>'+formatNumber(principalamount[Sno],2)+'</p></td>' +
				'<td><p>'+formatNumber(balanceamount[Sno],2)+'</p></td>';
				tab+='</tr>';
		}
				
				yearValue++;
				if(yearValue>=13)
				{
					yearValue=1;	
					y=parseInt(y)+1;
				}
		Sno++;				
	}	
	tab+='</td></tr></table>';
	document.getElementById("emi_table").innerHTML = tab;
	document.getElementById("emi_table").style.display="";				
}

function formatNumber(myNum, numOfDec)
{
	var decimal = 1
	for(i=1; i<=numOfDec;i++)
	decimal = decimal *10
	
	var myFormattedNum = (Math.round(myNum * decimal)/decimal).toFixed(numOfDec)
	return(myFormattedNum)
} 

function setDays()
{
	year = parseInt(document.frm.year.options[document.frm.year.selectedIndex].value); 
	month = document.frm.month.selectedIndex;
	day = document.frm.day.selectedIndex;
	document.frm.day.options.length = 0;
	var days = new Array(31, ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	for(i = 0; i < days[month]; i++)
	{
		document.frm.day.options.length = document.frm.day.options.length + 1;
		document.frm.day.options[i].value = i + 1;
		document.frm.day.options[i].text = i + 1;
	}
	document.frm.day.selectedIndex = (day < document.frm.day.options.length) ? day : document.frm.day.options.length - 1;
}

function show1()
{
	month1 = frm.year.options[frm.month1.selectedIndex].value; 
}

function thorw_error(error_txt,div_id)
{//display the error message if any
	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)
{//hides a particular div
	var DivObj = (document.getElementById(div_id)) ? document.getElementById(div_id) : new Object;
	DivObj.style.display='none';
}

function IsEmpty(obj, obj_type)
{
	if (obj_type == "text" || obj_type == "password" || obj_type == "textarea" || obj_type == "file")
	{
		var objValue;
		
		objValue = obj.value.replace(/\s+$/,"");
		
		if (objValue.length == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else if (obj_type == "select")
	{
		for (i=0; i < obj.length; i++)
		{
			if (obj.options[i].selected)
			{
				if(obj.options[i].value == "0")
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		return true;	
	}
	
	else if(obj_type == "radio" || obj_type == "checkbox")
	{
		if (!obj[0] && obj)
		{
			if (obj.checked)
			{
				return false;
			}
			else
			{
				return true;	
			}
		}
		else
		{
			for (i=0; i < obj.length-1; i++)
			{
				if (obj[i].checked)
				{
					return false;
				}
			}
			return true;
		}
	}
	else
	{
		return false;
	}
}

function ValidateNo( NumStr, String )
{
	for( var Idx = 0; Idx < NumStr.length; Idx ++ )
	{
		var Char = NumStr.charAt( Idx );
		var Match = false;
		
		for( var Idx1 = 0; Idx1 < String.length; Idx1 ++)
		{
			if( Char == String.charAt( Idx1 ) )
			Match = true;
		}
		if ( !Match )
		return false;
	}
	return true;
}
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;
   }