<!-- TCO calculator-I Script -->
var intRate;
var mnthRepay;
var monthsClearDebt;
var yearsClearDebt;

// round a number to X decimals
function round(number, plcs) 
{ 
  plcs = (!plcs ? 2 : plcs); 
  return Math.round(number * Math.pow(10, plcs)) / Math.pow(10, plcs); 
} 

// get the values from the Repay table into JS variables 
function getValsRepay()
{
   crdBalance = new Number(document.frmCard.crdBalanceRepay.value);
   intRate = new Number(document.frmCard.intRateRepay.value);
   mnthRepay = new Number(document.frmCard.mnthRepay.value);
}

// check if a given variable is a valid number. isNAN tests if a variable is Not A Number
function isValidNum(num)
{
   var isValidNum = false;

   if ( !isNaN(num) && num != "" && num != undefined){
      isValidNum = true; 
   }
   
   return isValidNum;
}

// does all the work
function CalcResultsRepay(clickCalc)
{	
	var remainingBalance = 0; 
	var mnIntRate = null;
	var monthsPaid = 0;
	var monthlyIntPay = null;
	var totIntPaid = 0;
	var paidOff = true; // can the card be paid off based on the repayment and interest amounts
	var yearsStr = 'años';
	var monthsStr = 'meses';
	
	// calc the monthly interest rate
	mnIntRate = GetMnthIntRate();
	
	// simulate making payments each month
	remainingBalance = crdBalance;
	
	while (remainingBalance > 0)
	{
		monthsPaid++;
		
		// calc monthly int payment
		monthlyIntPay = remainingBalance * mnIntRate;
		
		// add int payment and subtract payment from remaining balance
		remainingBalance = remainingBalance + monthlyIntPay - mnthRepay;
					
		// keep runnning total of interested paid
		totIntPaid = totIntPaid + monthlyIntPay;
		
		// is the base amount going up instead of down because of interest
		if (remainingBalance >= crdBalance)
		{
			paidOff = false;
			
			if (clickCalc)
			{
				alert('El interés a abonar es superior a la cuota mensual deseada.');
			}
			break;
		}
	}
	
	// if the card was able to be paid off, update the result boxes
	if (!paidOff)
	{
		ResetFieldsRepay();
	}
	else
	{
		if (monthsPaid >= 12)
		{
			var years = Math.floor(monthsPaid/12);
			var months = monthsPaid-(years*12);
			
			if(years==1){
				yearsStr = 'year';
			}
			if(months==1){
				monthsStr = 'month';
			}
			
			document.frmCard.resultRepay.value = years+' '+yearsStr+', '+months+' '+monthsStr;
		}
		else
		{
			if(monthsPaid==1){
				monthsStr = 'month';
			}
			document.frmCard.resultRepay.value = monthsPaid+' '+monthsStr;
		}
		// total cost -> document.frmCard.resultBox2.value = (crdBalance+totIntPaid).toFixed(2);
	}
}

// calc the monthly interest rate
function GetMnthIntRate()
{
	return (parseFloat(intRate)/100)/12;
}

function ResetFieldsRepay()
{
	document.frmCard.resultRepay.value = '';
}

function ResetFieldsMonths()
{
	document.frmCard.resultMonths.value = '';
}

function UpdateRepay()
{
	// get the forms variables
	getValsRepay();
	
	if (!isValidNum(crdBalance))
	{
		alert('Por favor introduzca el saldo correctamente');
		document.frmCard.crdBalanceRepay.focus();
	}
	else if (!isValidNum(intRate))
	{
		alert('Por favor introduzca un tipo de interés válido');
		document.frmCard.intRateRepay.focus();
	}
	else if (!isValidNum(mnthRepay))
	{	
		alert('Por favor introduzca una cuota mensual correcta');
	    document.frmCard.mnthRepay.focus();
	}
	else
	{
		CalcResultsRepay(true);
	}
}


// auto populate the 2nd card balance box
function PopCardBalRepay()
{
	if (document.frmCard.crdBalanceMonths.value == null || document.frmCard.crdBalanceMonths.value == ''){
		document.frmCard.crdBalanceMonths.value = document.frmCard.crdBalanceRepay.value;
	}
}

	function realityCheck1()
	{
		var imagePath= 'http://www.tarjeta-credito-online.es/creditcal/images';
		
		document.write('<style>.calcTD{font-family:Tahoma, Arial, Verdana;font-size:12px;font-weight:bold;text-decoration:none;color:#000000;}.box-border{border:1px solid #8C8C8C;}.calcubg{background:url('+imagePath+'/bg.jpg);background-repeat:repeat-x;background-color:#92BFFC;}.right-repeat{background:url('+imagePath+'/right-repeat.jpg);background-repeat:repeat-x;background-color:#DCEAFE;}.textbox{width:96px;height:18px;background-color:#FFFFFF;border:1px solid #81A699;font-family:Tahoma, Arial, Verdana;font-size:12px;font-weight:normal;text-decoration:none;color:#000000;}.dropdown{width:36px;height:20px;font-family:Tahoma, Arial, Verdana;font-size:12px;font-weight:normal;text-decoration:none;color:#000000;}.repeat-line{background:url('+imagePath+'/repeat-line.jpg);background-repeat:repeat-x;height:26px;}.bold-white{font-family:Tahoma, Arial, Verdana;font-size:17px;font-weight:bold;text-decoration:none;color:#ffffff;}a.calculartextlink{font-family:Tahoma, Arial, Verdana;font-size:12px;font-weight:bold;text-decoration:none;color:#000000;}a.calculartextlink:hover{font-family:Tahoma, Arial, Verdana;font-size:12px;font-weight:bold;text-decoration:underline;color:#000000;}.blacktext{font-family:Tahoma, Arial, Verdana;font-size:12px;font-weight:bold;text-decoration:none;color:#4B4B4B;}</style>');
		
		document.write('<FORM name=frmCard action=# method=post>');
		
		var cal1 = '<table width="476" border="0" align="center" cellpadding="0" cellspacing="0" class="box-border"><tr><td align="left" valign="top" class="calcubg" style="padding-top:16px; padding-bottom:20px; padding-left:7px; padding-right:5px;"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="left" valign="top"><img src="'+imagePath+'/cal-header2.jpg" alt="" width="462" height="64" /></td></tr><tr><td align="left" valign="top" style="padding-right:6px;"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="301" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="39" align="left" valign="top">&nbsp;</td></tr><tr><td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="139" height="29" align="left" valign="middle" class="calcTD">Saldo pendiente</td><td width="148" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="12" height="29" align="left" valign="middle" class="calcTD">&euro;</td><td width="136" align="left" valign="middle"><input name="crdBalanceRepay" type="text" class="textbox" id="crdBalanceRepay" /></td></tr></table></td></tr><tr><td height="29" align="left" valign="middle" class="calcTD">Tipo de inter&eacute;s</td><td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="12" height="29" align="left" valign="middle" class="calcTD">&nbsp;</td><td width="136" align="left" valign="middle"><input name="intRateRepay" type="text" class="textbox" id="intRateRepay" />%</td></tr></table></td></tr><tr><td align="left" valign="middle" class="calcTD">Pago mensual</td><td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="12" height="29" align="left" valign="middle" class="calcTD">&euro;</td><td width="136" align="left" valign="middle"><input name="mnthRepay" type="text" class="textbox" id="mnthRepay" /></td></tr></table></td></tr></table></td></tr><tr><td height="69" align="left" valign="middle"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="148">&nbsp;</td><td align="left" valign="top"><a href="javascript:UpdateRepay()" class="calculartextlink"><img src="'+imagePath+'/calc_btn.jpg" alt="" width="97" height="20" border="0" /></a></td></tr></table></td></tr></table></td></tr></table></td><td width="162" align="left" valign="top" class="right-repeat" style="padding-left:13px;"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="39" align="left" valign="middle"><img src="'+imagePath+'/icon2.jpg" alt="" width="24" height="22" /></td></tr><tr><td align="left" valign="top" class="blacktext">Esta calculadora le ense&ntilde;ara cuanto tiempo tardar&aacute; en saldar el importe total de la factura de su tarjeta de cr&eacute;dito, si abona &uacute;nicamente el m&iacute;nimo mensual.</td></tr></table></td></tr></table></td></tr><tr><td align="left" valign="top" style="padding-right:6px;"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="26%" align="left" valign="middle" class="bold-white">RESULTADO</td><td width="74%" align="left" valign="top" class="repeat-line">&nbsp;</td></tr></table></td></tr><tr><td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="193" align="left" valign="top" class="calcTD">N&uacute;mero de meses requeridos</td><td align="left" valign="top"><input name="resultRepay" type="text" class="textbox" id="resultRepay" /></td></tr></table></td></tr></table></td></tr></table>';
		document.write(cal1);
		document.write('</FORM>');
	}
	realityCheck1();



