var nume=1;

/* ABRIR VENTANA AL TAMAÑO DE IMAGEN Y DE LA PANTALLA*/
var titulopordefecto = " "; //Si no se especifica un título al llamar a la función colocará el que se especifique aquí
var ventana;
var cont=0;

function afoto(cual,titulo){
	if(cont==1){ventana.close();ventana=null}
	if(titulo==null){titulo=titulopordefecto}
	ventana=window.open('','ventana','resizable=no,scrollbars=no')
	ventana.document.write('<html><head><title>' + titulo + '</title></head><body style="overflow:hidden" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" scroll="no" onUnload="opener.cont=0" resizable="no"><table border=0 cellpadding=0 cellspacing=0 width="100%" height="100%"><tr><td align="center" valign="middle">');
	foto=new Image();
	foto.src=cual;
	var elalto;
	var elancho;
	if(foto.height>=(screen.height-300)){
		elalto=parseInt(screen.height-300);
		elancho=parseInt(((screen.height-300)*foto.width)/foto.height);
	}else{
		elalto=parseInt(foto.height);	
		elancho=parseInt(foto.width);	
	}
	ventana.document.write('<img src="' + cual + '" onLoad="opener.redimensionar('+ elancho +', '+ elalto +')" height="'+ elalto +'">');	
	ventana.document.write('</td></tr></table></body></html>');
	ventana.document.close();
	cont++;
}

function afoto2(cual,titulo){
	if(cont==1){ventana.close();ventana=null}
	if(titulo==null){titulo=titulopordefecto}
	ventana=window.open('','ventana','resizable=no,scrollbars=no')
	ventana.document.write('<html><head><title>' + titulo + '</title><link rel="STYLESHEET" type="text/css" href="css/styles.css"></head><body style="overflow:hidden" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" scroll="no" onUnload="opener.cont=0" resizable="no"><table border=0 cellpadding=0 cellspacing=0 width="100%" height="100%"><tr><td align="center" width="50%">');
	ventana.document.write('<a href="#" onClick="javascript: print();return false;"><img src="img/print.gif" border="0"> <strong>imprimir</strong></a></td><tr><td align="center" valign="middle">');
	foto=new Image();
	foto.src=cual;
	var elalto;
	var elancho;
	if(foto.height>=(screen.height-300)){
		elalto=parseInt(screen.height-300);
		elancho=parseInt(((screen.height-300)*foto.width)/foto.height);
	}else{
		elalto=parseInt(foto.height);	
		elancho=parseInt(foto.width);	
	}
	ventana.document.write('<img src="' + cual + '" onLoad="opener.redimensionar('+ elancho +', '+ elalto +')" height="'+ elalto +'">');	
	ventana.document.write('</td></tr></table></body></html>');
	ventana.document.close();
	cont++;
}

/*function afoto2(cual,titulo){
	//fotoguardar=cual;
	if(cont==1){ventana.close();ventana=null}
	if(titulo==null){titulo=titulopordefecto}
	ventana=window.open('','ventana','resizable=no,scrollbars=no')
	ventana.document.write('<html><head><title>' + titulo + '</title><link rel="STYLESHEET" type="text/css" href="css/styles.css"><script src="js/funciones.js" type="text/javascript"></script></head><body onload="isReady=true" style="overflow:hidden" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" scroll="no" onUnload="opener.cont=0" resizable="no"><table border=0 cellpadding=5 cellspacing=0 width="100%" height="100%">');
	ventana.document.write('<tr><td align="right" width="50%"><div id="tres"><a href="#" onClick="javascript: impre();return false;"><img src="img/print.gif" border="0"> <strong>imprimir</strong></a></div></td>');
	//ventana.document.write('<td align="left" width="50%"><div id="dos"><a href="#"><img src="img/save.gif" border="0"> <strong>guardar</strong></a></div></td></tr>');
	ventana.document.write('<td align="left" width="50%"><div id="dos"></div></td></tr>');
	ventana.document.write('<tr><td align="center" valign="middle" colspan=2>');
	foto=new Image();
	foto.src=cual;
	var elalto;
	var elancho;
	if(foto.height>=(screen.height-300)){
		elalto=parseInt(screen.height-300);
		elancho=parseInt(((screen.height-300)*foto.width)/foto.height);
	}else{
		elalto=parseInt(foto.height);	
		elancho=parseInt(foto.width);	
	}
	ventana.document.write('<div id="uno"><img id="fotoguardar" src="' + cual + '" onLoad="opener.redimensionar('+ elancho +', '+ elalto +')" height="'+ elalto +'"></div>');	
	ventana.document.write('</td></tr></table></body></html>');
	ventana.document.close();
	cont++;
}
*/

function impre() {
	document.getElementById('uno').className="nover";
    document.getElementById('uno').className="ver";
    print();
    document.getElementById('uno').className="nover";
}

//var fotoguardar;

var isReady = false;
function doSaveAs(){
	//alert(document.getElementById('fotoguardar').src);
	if (document.execCommand){
		if (isReady){
		//	document.execCommand("SaveAs", null, document.getElementById('fotoguardar').src);
		document.execCommand("SaveAs", true, "aaa.jpg");
		}
	}else{
		alert('Feature available only in Internet Exlorer 4.0 and later.');
	}
}





function redimensionar(ancho, alto){
	ventana.resizeTo(ancho+30,alto+60);
	ventana.moveTo((screen.width-(ancho+30))/2,(screen.height-(alto+60))/2); //centra la ventana. Eliminar si no se quiere centrar el popup
}


/****************************************/




function muestra(cual, cuantos){
	for (i=1;i<=cuantos;i++){
		//eval("bloque"+i+".style.display='none'");
		document.getElementById("bloque"+i).style.display='none';
	}
	//eval(cual+".style.display='block'");
	document.getElementById(cual).style.display='block';
}

//SOLO NÚMEROS, ENTERO
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
function onlyDigits(e,decReq) {
	var key = (isIE) ? window.event.keyCode : e.which;
	var obj = (isIE) ? event.srcElement : e.target;
	var isNum = (key > 47 && key < 58) ? true:false;
	var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;
	window.event.keyCode = (!isNum && !dotOK && isIE) ? 0:key;
	e.which = (!isNum && !dotOK && isNS) ? 0:key;
	return (isNum || dotOK);
}


//CON DOS DECIMALES
function currencyFormat(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}




function verificar_numero(numero,formulario){
	if (numero.value==''){
	}
	var noes=0;
	var str = numero.value;
	for (var i = 0; i < str.length; i++){
		var ch = str.substring(i, i + 1)
		if ((ch < "0" || "9" < ch) && ch != '.'){
			numero.value=''
			numero.focus()
			numero.select()
		   	noes=1
		}               
	}	  
	if (noes==1){
		alert('El valor introducido "' + str + '" no es un numero.')
	  	return 1
	}
	return 0;
}

function hay_cambio(){
	if (document.forms[0].medida.options[0].selected){
		if ( document.forms[0].medida_anterior.value != 12.0){
			return 1
		}
	}
	else{
		if ( document.forms[0].medida_anterior.value == 1.0){
			return 1
		}
	}
	
	if ( document.forms[0].importe.value != document.forms[0].importe_anterior.value){
		return 1
	}
	if ( document.forms[0].plazo.value != document.forms[0].plazo_anterior.value){
		return 1
	}
	if ( document.forms[0].tipo.value != document.forms[0].tipo_anterior.value){
		return 1
	}
	if ( document.forms[0].cuota.value != document.forms[0].cuota_anterior.value){
		return 1
	}
	return 0
}

function almacena() {
	document.forms[0].importe_anterior.value = document.forms[0].importe.value
	document.forms[0].plazo_anterior.value = document.forms[0].plazo.value
	document.forms[0].tipo_anterior.value = document.forms[0].tipo.value
	document.forms[0].cuota_anterior.value = document.forms[0].cuota.value
	if (document.forms[0].medida.options[0].selected){
		document.forms[0].medida_anterior.value = 12.0
	}else{
		document.forms[0].medida_anterior.value = 1.0
	}
}

function calculacuota() {
	// CALCULA LA CUOTA A PAGAR CONOCIENDO:
	// 	EL IMPORTE DEL PRESTAMO
	//	EL NUMERO DE PLAZOS A PAGAR
	//	EL TIPO DE INTERES QUE SE LE APLICA

	if ( hay_cambio() == 0 ) {
		return
	}

	imp = parseInt(document.forms[0].importe.value)
	plazo_meses = parseInt(document.forms[0].plazo.value)
	tipo_mensual = parseFloat(document.forms[0].tipo.value)


	if (document.forms[0].medida.options[0].selected){
		medida = 12.0
	}else{
		medida = 1.0
	}

	if ( isNaN(imp) || imp == 0 ){
		alert("Debe rellenar el campo 'Importe prestamo'")
	 	return
	}
	if ( isNaN(plazo_meses) || plazo_meses == 0 ){
		alert("Debe rellenar el campo 'Plazo'")
	  	return
	}
	if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ){
		alert("Debe rellenar el campo 'Tipo Nominal'")
	 	return
	}

	if ( tipo_mensual > 20.0 ){
		alert("El Tipo Nominal es superior al 20% y no es aceptable para un prestamo hipotecario.")
	  	return
	}


	plazo_meses = plazo_meses * medida

	// EL TIPO DE INTERES VIENE EN AñOS Y LOS PASAMOS A TIPO INTERES MENSUAL
	tipo_mensual = tipo_mensual / 1200.0

	y  = 1.0 + tipo_mensual
	cuota = imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ) 
	document.forms[0].cuota.value = Math.round(cuota *100.00) / 100.00
	almacena()
}

function calculaimporte() {
	// CALCULA EL IMPORTE DEL PRESTAMO A SOLICITAR CONOCIENDO:
	//	EL NUMERO DE PLAZOS A PAGAR
	//	EL TIPO DE INTERES QUE SE LE APLICA
	//	LA CUOTA A PAGAR EN CADA PLAZO

	if ( hay_cambio() == 0 ) {
		return
	}

	plazo_meses = parseInt(document.forms[0].plazo.value)
	tipo_mensual = parseFloat(document.forms[0].tipo.value)
	cuota = parseInt(document.forms[0].cuota.value)

	if (document.forms[0].medida.options[0].selected){
		medida = 12.0
	}else{
		medida = 1.0
	}

	if ( isNaN(cuota) || cuota == 0 ){
		alert("Debe rellenar el campo 'Importe cuota'")
		return
	}
	if ( isNaN(plazo_meses) || plazo_meses == 0 ){
		alert("Debe rellenar el campo 'Plazo'")
		return
	}
	if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) {
		alert("Debe rellenar el campo 'Tipo Nominal'")
		return
	}

	if ( tipo_mensual > 20.0 ){
		alert("El Tipo Nominal es superior al 20% y no es aceptable para un prestamo hipotecario.")
		return
	}


	plazo_meses = plazo_meses * medida

	// EL TIPO DE INTERES VIENE EN A?OS Y LOS PASAMOS A TIPO INTERES MENSUAL
	tipo_mensual = tipo_mensual / 1200.0

	y  = 1.0 + tipo_mensual
	imp = cuota / ( tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ) )
	document.forms[0].importe.value = Math.round(imp *100.00) / 100.00

	almacena()
}

function calculaplazo() {
	// CALCULA EL NUMERO DE PLAZOS NECESARIOS CONOCIENDO:
	// 	EL IMPORTE DEL PRESTAMO
	//	EL TIPO DE INTERES QUE SE LE APLICA
	//	LA CUOTA A PAGAR EN CADA PLAZO

	if ( hay_cambio() == 0 ) {
		return
	}

	imp = parseInt(document.forms[0].importe.value)
	cuota = parseInt(document.forms[0].cuota.value)
	tipo_mensual = parseFloat(document.forms[0].tipo.value)

	if ( isNaN(imp) || imp == 0 ){
		alert("Debe rellenar el campo 'Importe prestamo'")
		return
	}
	if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) {
		alert("Debe rellenar el campo 'Tipo Nominal'")
		return
	}
	if ( isNaN(cuota) || cuota == 0 ){
		alert("Debe rellenar el campo 'Importe cuota'")
		return
	}

	if ( tipo_mensual > 20.0 ) {
		alert("El Tipo Nominal es superior al 20% y no es aceptable para un prestamo hipotecario.")
		return
	}

	tipo_mensual = tipo_mensual / 1200.0

	y  = 1.0 + tipo_mensual

	plazo_meses = 1;
	cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))
	while (cuota_actual > cuota){
		ultima_cuota = cuota_actual;
		cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))
		plazo_meses++
		if (plazo_meses > 600){
			alert("El numero de plazos necesarios excede el limite admitido por un banco (50 años)")
			almacena()
			return;
		}
	}
	document.forms[0].plazo.value = Math.round(plazo_meses - 1) 
	document.forms[0].medida.options[1].selected = true
	// document.forms[0].medida.selectedindex = 1
	almacena()
}

function calculatipo() {
	// CALCULA EL TIPO DE INTERES CONOCIENDO:
	// 	EL IMPORTE DEL PRESTAMO
	//	EL NUMERO DE PLAZOS A PAGAR
	//	LA CUOTA A PAGAR EN CADA PLAZO

	if ( hay_cambio() == 0 ) {
		return
	}

	imp = parseInt(document.forms[0].importe.value)
	cuota = parseInt(document.forms[0].cuota.value)
	plazo_meses = parseInt(document.forms[0].plazo.value)

	if (document.forms[0].medida.options[0].selected){
		medida = 12.0
	}else{
		medida = 1.0
	}

	if ( isNaN(imp) || imp == 0 ){
		alert("Debe rellenar el campo 'Importe prestamo'")
		return
	}
	if ( isNaN(cuota) || cuota == 0 ){
		alert("Debe rellenar el campo 'Importe cuota'")
		return
	}
	if ( isNaN(plazo_meses) || plazo_meses == 0 ){
		alert("Debe rellenar el campo 'Plazo'")
		return
	}

	plazo_meses = plazo_meses * medida

	tipo_mensual = 20.0 / 1200.0
	inc_tipo_mensual = 10.0 / 1200.0
	y  = 1.0 + tipo_mensual

	cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))
	while ( cuota_actual != cuota){
		y  = 1.0 + tipo_mensual
		cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))

		if (cuota_actual > cuota){
			// se aplica un interes muy alto, hay que bajarlo
			tipo_mensual= tipo_mensual - inc_tipo_mensual
		}else{
			// se aplica un interes muy bajo, hay que subirlo
			tipo_mensual= tipo_mensual + inc_tipo_mensual
		}
		if ((tipo_mensual * 1200.0) > 20.0)	{
			alert("El tipo de interes a aplicar es superior al interes permitido por un banco (20%)")
			almacena()
			return
		}
		inc_tipo_mensual = inc_tipo_mensual / 2.0
	}
	tipo_mensual = tipo_mensual * 1200.0
	document.forms[0].tipo.value = Math.round(tipo_mensual * 100.00) / 100.00
	almacena()
}

function nada (){
  return
}



function confirmDel(){
	var agree=confirm("Eliminar? ");
	if (agree) return true ;
	else return false ;
}
