
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function quitaMillares(temp){
        var cadena=""+temp;
        for(var n=0;n<=cadena.length;n++){
                if (cadena.charAt(n)=="."){
                        cadena=cadena.substring(0,n)+cadena.substring(n+1,cadena.length);
                }
        }
        return cadena;
}
function esNumero(temp){
        cadenaInput=""+temp;
        if (cadenaInput=="NaN"){
                return false;
        }
        for ( var j=0 ; j <= cadenaInput.length-1 ; j++ ){
                var caracter=cadenaInput.charAt(j);
                if (caracter <"0" || caracter >"9"){
                        if (caracter!="."){
                                return false;
                        }
                }
        }
        return true;
}
function formatoMoneda(temp){
        var cifra=""+temp;
        var miCadena="";
        var tres=0;
        for (var n=cifra.length; n>=1;n--){
                tres++
                if (tres==3) {
                        miCadena=("."+cifra.charAt(n-1))+miCadena;
                        tres=0;
                }
                else {
                        miCadena=cifra.charAt(n-1)+miCadena;
                }
        }
        if (miCadena.charAt(0)=="."){
                miCadena=miCadena.substring(1,miCadena.length);
        }
        return miCadena;
}
function entreLimites(inf,sup,temp,cadena){
        resultado=true
        if(temp.value=="") {
                temp.value = 0
        }
        cantidad=quitaMillares(temp.value);
        if (esNumero(cantidad)){
                if (cantidad < inf || cantidad > sup){
                        alert(" Value must be "+formatoMoneda(inf,".")+" or "+formatoMoneda(sup,".")+cadena);
                        temp.value="";
                        resultado=false;
                }
        } else {
                alert("Please enter a number");
                temp.value="";
                resultado=false;
        }
        return resultado;
}
function calculaPrestamo(temp){
        if (validas == 1) {
        temp.formCapital.value=quitaMillares(temp.formCapital.value);
        var capital=temp.formCapital.value;
        var tiempo=temp.formTiempo.value;
        var interes=temp.formInteres.value/1200;
        factor=Math.pow((1+interes),tiempo)
        cuota=capital * ( (factor*interes) / (factor-1) );
        temp.formCapital.value=formatoMoneda(temp.formCapital.value);
        temp.formTiempo.value=formatoMoneda(temp.formTiempo.value);

        if("NaN"==""+cuota || "Infinity"==""+cuota){
                cuota=0;
        }
        temp.formResultado.value=formatoMoneda(Math.round(cuota));
        }
        else{
        temp.formResultado.value=""
        }
}
function compruebaEntradas(temp){

        validas=1;
        if(!esNumero(quitaMillares(temp.formCapital.value))){
                alert("Please enter a number");
                temp.formCapital.value="";
                validas=0;
        }
        if(!entreLimites(1000000,30000000,temp.formCapital,' of pesetas.')){
                validas=0;
        }
        if(!entreLimites(1,300,temp.formTiempo,' months.')){
                validas=0;
        }
}
function ponInteres(temp){
        var inte=0;
        if (temp.Rmeses[0].checked){
                inte=3.75;
        }
        if (temp.Rmeses[1].checked){
                inte=4.25;
        }
        if (temp.Rmeses[2].checked){
                inte=6.;
        }
        temp.formInteres.value=inte;
}
function ponInteres2(temp){
        var t_inte=0;
        if (temp.periodo_inicial.options[0].selected == true){
                t_inte=3.75;
        }
        if (temp.periodo_inicial.options[1].selected == true){
                t_inte=4.25;
        }
        if (temp.periodo_inicial.options[2].selected == true){
                t_inte=6.;
        }
        temp.formInteres.value=t_inte;
}
var elem=new Array(6,9,11,12,13,17,19)
var campos=new Array("Importe a financiar","Plazo","Apellidos","Nombre","D.N.I.","E-mail","Teléfono")
function obligatorios(){
        var cont=0
        //document.forms['PrestamoPersonal'].action="http://www.ctv.es/cgi-bin/CLIENTES/mailtocli2.cgi?ptm@ctv.es,Prestamo_Personal,http://www.cisa.es/catresp.htm.htm"
        var paso=true
        for (var i=0;i<elem.length;i++){
        cont+=1
                if(document.forms['PrestamoPersonal'].elements[elem[i]].value==""){
					alert("Debe rellenar el campo \""+campos[i]+"\"")
					document.forms['PrestamoPersonal'].elements[elem[i]].focus()
					paso=false
					break
                }
        }
        if (paso) document.forms['PrestamoPersonal'].submit()
}

var datos = new Array(4)
var nomdatos =new Array("Capital inicial","Tipo de interés","Plazo de amortización","Mensualidad")
var capitemp=0
var intetemp=0
var plazotemp=0
var mensutemp=0

function borradato(num){
	document.forms[0].elements[num].value=""
}

function compruebadatos(){
sigo = true
	for (i=0;i<4;i++){

		if (isNaN(document.forms[0].elements[i].value)){
			sigo=false
			alert(""+nomdatos[i]+" must be a number")
			break
		}
	}
	if (sigo){
		calculaprestamo()
	}
}

function calculaprestamo(){
var suma=0
correcto=true
	for (i=0;i<4;i++){
		datos[i]=document.forms[0].elements[i].value
		if (datos[i]!=""){
			suma+=1
		}
	}
	if (suma==4){
		correcto=false
		alert("One of the data fields must be left empty")
	}else{
		if (suma<3){
			correcto=false
			alert("Three of the data fields must be populated")
		}
	}
	if (correcto){
		if (datos[1]==""){
			mtemp=datos[0]/datos[2]
			if (datos[3]<mtemp){
				correcto=false
				alert("This is not a valid combination of principal, duration and monthly payment.")
			}else{
				calculainteres()
			}
		}
		if (datos[0]==""){
			id=datos[1]/1200

			//******************Operación espejo***********************
			if (plazotemp==datos[2] && intetemp==id && parseInt(mensutemp)==datos[3]){
				res=mensutemp/((Math.pow(1+intetemp,plazotemp)*intetemp)/(Math.pow(1+intetemp,plazotemp)-1))
			}else{
				res=datos[3]/((Math.pow(1+id,datos[2])*id)/(Math.pow(1+id,datos[2])-1))
			}
			res=parseInt(res)

			document.forms[0].elements[0].value=res
		}
		if (datos[2]==""){
			id=datos[1]/1200
			//res=-Math.log((datos[0]*i/datos[3])-1)/Math.log(1+id)
			dividendo1=Math.log(datos[3])
			suma1=(-datos[0]*id)

			suma2=datos[3]

			sumando=parseFloat(suma1)+parseFloat(suma2)

			dividendo2=Math.log(sumando)
			divisor=Math.log(1+id)

			res=(dividendo1-dividendo2)/divisor

			res=Math.ceil(res)
			document.forms[0].elements[2].value=res
			//**********Modificamos el capital inicial******************
			cinicial=datos[3]/((Math.pow(1+id,res)*id)/(Math.pow(1+id,res)-1))
			if (document.forms[0].elements[0].value!=parseInt(cinicial)){
				document.forms[0].elements[0].value=parseInt(cinicial)
				alert("The downpayment has been adjusted to produce an even number of payments")
			}

		}
		if (datos[3]==""){
			id=datos[1]/1200
			//res=datos[0]/((1-Math.pow(1+id,2)-datos[2])/id)
			res=datos[0]*((Math.pow(1+id,datos[2])*id)/(Math.pow(1+id,datos[2])-1))
			intetemp=id
			capinitemp=datos[0]
			plazotemp=datos[2]
			mensutemp=res
			dato=res+""
			indi=dato.indexOf(".")
			if (dato!=-1){

				deci=dato.substring(indi+1,indi+3)

				if (parseInt(deci)>=50){
					document.forms[0].elements[3].value=Math.ceil(res)
				}else{
					document.forms[0].elements[3].value=parseInt(res)
				}
			}else{
				res=parseInt(res)
				document.forms[0].elements[3].value=res
			}
		}
	}

}

function calculainteres(){
	inter=4.50

	//******************Operación espejo***********************************
			if (plazotemp==datos[2] && capinitemp==datos[0] && parseInt(mensutemp)==datos[3]){
				d3=mensutemp
				d2=plazotemp
				d0=capinitemp
			}else{

				d0=datos[0]
				d3=datos[3]
				d2=datos[2]
			}
	//**********************************************************************
	do{
	id=inter/1200
	res=d0*((Math.pow(1+id,d2)*id)/(Math.pow(1+id,d2)-1))
	if (parseInt(res)>d3){
			inter=inter-0.01
	}
	if (parseInt(res)<d3){
			inter= inter+0.01
	}

	comparo=(parseInt(d3))+parseInt(31)

	comparo1=(parseInt(d3))-parseInt(31)
	//alert(comparo1)
	//alert("paso="+parseInt(res)<parseInt(comparo) && parseInt(res)>parseInt(comparo1))


	}
	while (parseInt(res)>parseInt(comparo) || parseInt(res)<parseInt(comparo1))
	inter=inter*100
	inter=parseInt(inter)
	inter=inter/100
	document.forms[0].elements[1].value=inter
	//**********Modificamos el capital inicial******************
			id=inter/1200
			cinicial=datos[3]/((Math.pow(1+id,datos[2])*id)/(Math.pow(1+id,datos[2])-1))
			if (document.forms[0].elements[0].value!=parseInt(cinicial)){
				document.forms[0].elements[0].value=parseInt(cinicial)
				alert("The downpayment has been adjusted to produce a rounded off result")
			}

	capinitemp=0
	plazotemp=0

}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
