function PrintAmort(form){
	ambasicedit(form);

	pass=1;	
	ir = replacePct(form.ir.value);
	str=replaceChars(form.p.value);
	str = replaceChar(str);
	p = replaceChars(form.p.value);
	p = replaceChar(p);
	if (str > 10000000){
		alert ("La cantidad del préstamo debe ser menor de $1,000,000");
		pass = 0;
	}
	if (str < 1000){
		alert ("La cantidad del préstamo debe ser mayor de $1,000");
		pass = 0;
		}	
 	str=replacePct(form.ir.value);
	if (str > 20){
		alert ("La tasa de interés no debe ser mayor de 20%");
		form.ir.value = 20;
		pass = 0;
	}
	if (str < 1 ){
		alert ("La tasa de interés debe ser mayor de 1%");
		form.ir.value = 1;
		pass = 0;
	}
	str=form.y.value;
	if (str > 360){
		alert ("El número de pagos no puede ser mayor de 360") 
		pass = 0;
	}
	if (str < 70){
		alert ("El número de pagos debe ser mayor de 70")
		pass = 0;
}

	form.y.value=parseInt(str);

	if (checkForm(p) && checkForm(ir)
		&& checkForm(form.y.value) && (pass==1)) {
	amortize(form); }
}		
    function amortize(form) {
         var top="<div align='center'>";
             top=top+"<table border='0' cellpadding='1' width='340'><tr>"; 
         with (Math) {
         var yr=parseFloat(form.ir.value);
         var tm=parseInt(form.y.value);
		 var amt=parseFloat(p);		 
         var amt1=amt;
		 var amtd = formatCurrency(amt);
         var mr=yr/1200;
         var r=yr/12;
         var mr1=pow((parseFloat(1)+parseFloat(mr)),tm);
       
		 var tempAmortStr = "<html><head></head>";
		 tempAmortStr += "<body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>";  
         tempAmortStr += "<table align='center' width='364' border='0' cellspacing='0' cellpadding='0' height='51'><tr>";
		 tempAmortStr += "<td  align='center'><img src='App_Images/calculadora/calcheader.gif'></td>";
		 tempAmortStr += "</tr></table>";
		 tempAmortStr += "<form id=form1 name=form1>";
         tempAmortStr += "<table border=0 align=center cellpadding=0 cellspacing=0>";
		 tempAmortStr += "<tr><td colspan=5 align=center><a href='javascript:window.print();'><img src='App_Images/calculadora/btn_print.gif' border=0></a></td></tr>";
		 tempAmortStr += "<tr><th colspan=5 align=center><font size=2 color=#000000>Plan de Pago</font></th></tr>";
		 tempAmortStr += "<tr><th colspan=5 align=center><font size=2 color=#000000>"+tm+" meses para saldar " + amtd +" al " + yr + "%</font></th></tr>";
		 tempAmortStr += "<tr><th align=center><font size=1>Pago</font></th>";
		 tempAmortStr += "<td align=center><font size=1>Pago</font></td>";
		 tempAmortStr += "<th align=center><font size=1>Interés</font></th>";
		 tempAmortStr += "<td align=center><font size=1>Principal</font></td>";
		 tempAmortStr += "<th align=center><font size=1>Balance</font></th></tr>";		
		 tempAmortStr += "<tr><th align=center><font size=1>Número</font></th>";
		 tempAmortStr += "<td align=center><font size=1>Cantidad</font></td>";
		 tempAmortStr += "<th align=center><font size=1>Cantidad</font></th>";
		 tempAmortStr += "<td align=center><font size=1>Reducción</font></td>";
		 tempAmortStr +="<th align=center><font size=1>Deuda</font></th></tr>";
		 for (var j=1; j<=tm; j++) 
               {
				 m2=""+j;                 
				 int1=amt1*(r/100);
                 p=(mr1*amt*mr)/(mr1-1);
                 q=(amt1+int1)-p;
				 amt1=p-int1; 
                 	if(q<1) { q="0.00";}

  				 p2=""+p;
				 pos=p2.indexOf(".");
				 	if(pos != "-1") {p2=p2.substring(0,pos+3);
				 } else
					 { p2 = p2 + ".00";} 
               
 				 int2=""+int1;pos=int2.indexOf(".");
				 	if(pos != "-1") {int2=int2.substring(0,pos+3);
				 }else 
					{ int2=int2+".00";}

                 amt2=""+amt1;pos=amt2.indexOf(".");
				 	if(pos != "-1"){amt2=amt2.substring(0,pos+3);
				 }else
					{ amt2=amt2+".00";}
                 q2=""+q;pos=q2.indexOf(".");

					if(pos != "-1") {q2=q2.substring(0,pos+3);
				 } else
					 {q2=q2+".00";}
					p2 = formatCurrency(p2);
					int2 = formatCurrency(int2);
					amt2 = formatCurrency(amt2);
					q2 = formatCurrency(q2);
                 tempAmortStr += "<tr><td align=center><font size=2>"+m2+"</font></td>";
				 tempAmortStr += "<td align=center><font size=1>"+p2+"</font></td>";
				 tempAmortStr += "<td align=center><font size=1>"+int2+"</font></td>";
				 tempAmortStr += "<td align=center><font size=1>"+amt2+"</font></td>";
				 tempAmortStr += "<th align=center><font size=1>"+q2+"</font></th></tr>";
            
				 amt1=q;				
               }
		   
		 tempAmortStr += "<tr><th colspan=5 align=center><font size='1'>Un cierto redondeo de valores pudo haber ocurrido </font></th></tr>";     
         tempAmortStr +="</table></body></form></html>";
		 newWindow=window.open("","","height=500,width=380,scrollbars")
		 newWindow.document.write(tempAmortStr);	
		 newWindow.document.close();
                  return false;
                        }
                            }
         
function replaceChar(entry) {
	out = "$"; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function replacePct(entry) {
	out = "%"; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}	
	
function replaceChars(entry) {
	out = ","; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function formatCurrency(num) {
			num=replaceChar(num);
			num=replaceChars(num);
		if ( checkForm(num));
{			
			num = num.toString().replace(/$|,/g,'');
			if(isNaN(num)) num = "0";
			cents = Math.floor((num*100+0.5)%100); 
			num = Math.floor(num).toString();
			if(cents < 10) cents = "0" + cents; 
			for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3)); 
			return ('$' + num + '.' + cents);

}
}


function ambasicedit(form)
{		
	if 	(!IsMoney(form.p.value) || form.p.value.length ==0)
		form.p.value='$125,000';
								
	if 	(!IsNumber(form.y.value) || form.y.value.length ==0)
		form.y.value='360';		
	
	if 	(!IsPct(form.ir.value) || form.ir.value.length ==0)
		form.ir.value='7.5%';	
}	


function IsMoney(val)
	{
		var number="0123456789$,.";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}

function IsNumber(val)
	{
		var number="0123456789.";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}
	
function IsPct(val)
	{
		var number="0123456789.%,";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}

 

function addPct(pct) {
			pct=replacePct(pct);
			pct=parseFloat(pct);		
		if (checkForm(pct));
	{
			return (pct + '%');
		}
}

function checkForm(toCheck) {	
  isNum = true;
  for (j = 0; j < toCheck.length; j++) {
    if ((toCheck.substring(j,j+1) < "0") && (toCheck.substring(j,j+1) !=".") || (toCheck.substring(j,j+1) > "9")) 
      isNum = false;{	
      }
    }
  if ((isNum == false) || (toCheck.length == 0) || (toCheck == null)) {
  alert("Favor utilizar datos numéricos solamente en todos los campos.");
  return false;
  }
  else {
  return true;
  }
}