// JavaScript Document
function messageWindow(title, strcuota,strvalor,strplazo)
{
  var width="300", height="150";
  var left = (screen.width/2) - width/2;
  var top = (screen.height/2) - height/2;
  var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
  var msgWindow = window.open("","", styleStr);
  //var msgWindow = window.open("","msgWindow", styleStr);
  var head = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Dann Regional - Cuota mensual</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link href="estilos.css" rel="stylesheet" type="text/css"></head><body>'
  //var body = '<center>Para un valor de '+strvalor+' y un plazo de '+strplazo+' meses, el valor de la cuota mensual es:<br> '+strcuota+'<br><br><hr>';
  var body = '<center>Para un valor de '+strvalor+' y un plazo de '+strplazo+' meses, el valor aproximado de la cuota mensual es:<br> '+strcuota+'<br><br><hr><form><input type="button" value="   Cerrar   " onClick="self.close()"></form>';
  msgWindow.document.write(head + body);
}

function calcular(){
	var interes,valor,plazo
	var TEA,TNMV
	var pago,seguro,cuota
	var DTF
	var strcuota,strvalor,strplazo
	
	interes =	document.getElementById("interes").value;
	valor = 	document.getElementById("valor").value;
	plazo =		document.getElementById("plazo").value;
	
	if (valor == ""){
		valor = 0;
	}
	if (plazo == ""){
		plazo = 12;
	}
	if (interes == ""){
		interes = 7.04;
	}
	
	DTF = interes/100;
	TEA =	(Math.pow((1/(1-((0.14 + DTF))/4)),4)-1);
	TNMV=	Math.pow((1+TEA),(1/12))-1;
	pago = 	valor *((TNMV*Math.pow((1+TNMV),plazo)))/(((Math.pow((1+TNMV),plazo))-1)) ;
	seguro = (valor * 0.0156)/12 ;
	cuota =  pago + seguro ;
	cuota = Math.round(cuota);
	strcuota = "$" + formatear(cuota);
	strvalor = "$" + formatear(valor);
	strplazo = formatear(plazo);

	messageWindow("Valor de la cuota mensual",strcuota,strvalor,strplazo);
}

function formatear(valor){
	var temp;
	var valortemporal;
	if (valor == ""){
		temp = 0;
	}
	if (valor < 1000 && valor >= 0){
		temp = valor;
	}
	if (valor < 1000000 && valor >= 1000){
		temp = Math.floor(valor/1000) + ".";
		valortemporal = (valor - Math.floor(valor/1000)*1000);
		if (valortemporal == 0){
			valortemporal = "000";
		} 
		else
		{
			if (valortemporal <= 9){
				valortemporal = "00" + valortemporal;
			}
			else
			{
				if (valortemporal <= 99){
					valortemporal = "0" + valortemporal;
				}
			}
		
		}
		temp = temp + valortemporal;
	}
	if (valor < 1000000000 && valor >= 1000000){
		temp = Math.floor(valor/1000000)+ "'";
		valortemporal = Math.floor((valor-Math.floor(valor/1000000)*1000000)/1000);
		if (valortemporal == 0){
			valortemporal = "000";
		} 
		else
		{
			if (valortemporal <= 9){
				valortemporal = "00" + valortemporal;
			}
			else
			{
				if (valortemporal <= 99){
					valortemporal = "0" + valortemporal;
				}
			}
		
		}
		temp = temp + valortemporal + ".";
		valortemporal = (valor - Math.floor(valor/1000)*1000);
		if (valortemporal == 0){
			valortemporal = "000";
		} 
		else
		{
			if (valortemporal <= 9){
				valortemporal = "00" + valortemporal;
			}
			else
			{
				if (valortemporal <= 99){
					valortemporal = "0" + valortemporal;
				}
			}
		
		}
		temp = temp + valortemporal;
	}
	return temp;
}