function confirma(mensaje,destino) {
	if ( confirm(mensaje) ) { 
		self.location =destino;
	}
}

function openWinClean(fichero,nombre,tam) {
	ventana=window.open(fichero,nombre,tam+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes");	
}

function openWinScroll(fichero,nombre,tam) {
	ventana=window.open(fichero,nombre,tam+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars,resizable=no,copyhistory=yes");	
}

function openWin(fichero,nombre,tam) {
	ventana=window.open(fichero,nombre,tam+",toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes");	
}

function irA(destino) {
	self.location =destino;
}

function toggleDisplay(e) {
	imageX="image"+e;
	tableX="table"+e;

	element = document.getElementById(tableX).style;
	if (element.display=='none') {
		element.display='block';
		document.getElementById(imageX).src='minus.gif';
	} else {
		element.display='none';
		document.getElementById(imageX).src='plus.gif';
	}
}


//Control del formulario

// Simulador hipotecas

var tablaFormulario = [
	['R', 			"Tipo de interés", 				"_OBLIGATORIO_REALPTO_"],
 	['N', 		"Número de años", 			"_ENTERO_"],	
 	['I', 		"Importe Deseado", 				"_REALPOSPTO_"],
	['C', 		"Cuota mensual", 				"_REALPOSPTO_"]
 		
];
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function updateFields() {
  R = document.Form.R.value; // Tipo de interes anual 
  N = document.Form.N.value; // Numero de a&#8211;os
  I = document.Form.I.value; // Importe deseado
  C = document.Form.C.value; // Cuota mensual
  if(isNaN(R) || R<=0) {
    alert('Es obligatorio introducir un valor válido en el campo Tipo de Interés Anual. Use el punto decimal "." en lugar de la coma "," (ejemplo: 3.5%)'); 
  }
  else if(!isNaN(N) && N>0 && !isNaN(I) && I>0) {

    C = I*(R/1200)/(1-Math.pow(1+R/1200,-N*12));
    document.Form.C.value = rounding(C); 
  }

  else if(!isNaN(I) && I>0 && !isNaN(C) && C>0) {

    N = Math.round(Math.log(1-(I*R/(C*1200)))/(-12*Math.log(1+R/1200)));
    document.Form.N.value = N;
    // recalculamos C
    C = I*(R/1200)/(1-Math.pow(1+R/1200,-N*12));
    document.Form.C.value = rounding(C); 
  
  }

  else if(!isNaN(N) && N>0 && !isNaN(C) && C>0) {

    I = C*(1-Math.pow(1+R/1200,-N*12))/(R/1200);
    document.Form.I.value = rounding(I);
  
  }
  else {

  alert('Es preciso introducir el Tipo de interés y dos de los tres restantes campos correctamente');
  }

}

function updateFields2() {
  R = document.Form.R.value; // Tipo de interes anual 
  N = document.Form.N.value; // Numero de a&#8211;os
  I = document.Form.I.value; // Importe deseado
  C = document.Form.C.value; // Cuota mensual
  if(isNaN(R) || R<=0) {
    alert('Type in annual interest rate field. Use dot punctuation "." instead of coma "," (Example: 3.5%)'); 
  }
  else if(!isNaN(N) && N>0 && !isNaN(I) && I>0) {

    C = I*(R/1200)/(1-Math.pow(1+R/1200,-N*12));
    document.Form.C.value = rounding(C); 
  }

  else if(!isNaN(I) && I>0 && !isNaN(C) && C>0) {

    N = Math.round(Math.log(1-(I*R/(C*1200)))/(-12*Math.log(1+R/1200)));
    document.Form.N.value = N;
    // recalculamos C
    C = I*(R/1200)/(1-Math.pow(1+R/1200,-N*12));
    document.Form.C.value = rounding(C); 
  
  }

  else if(!isNaN(N) && N>0 && !isNaN(C) && C>0) {

    I = C*(1-Math.pow(1+R/1200,-N*12))/(R/1200);
    document.Form.I.value = rounding(I);
  
  }
  else {

  alert('Typing the Interest rate and two of the three remaining fields correctly is required');
  }

}

function rounding(anynum) {
    // redondeamos a 2 decimales
    retvalue = Math.round(anynum*100)/100;
	return retvalue;
}


function enviar() {
	/*if (ValidarFormulario (document.Form, tablaFormulario)) {
		updateFields();
	}*/
	updateFields();
}

function enviar2() {
	/*if (ValidarFormulario (document.Form, tablaFormulario)) {
		updateFields();
	}*/
	updateFields2();
}

// Buscador de inmuebles
function disable1(disableIt)
{
	document.listingsearch.cityID.disabled = disableIt;
	document.listingsearch.typeID.disabled = disableIt;
	document.listingsearch.priceMin.disabled = disableIt;
}

function disable2(disableIt)
{
	document.listingsearch.locationID.disabled = disableIt;
	document.listingsearch.typeID.disabled = disableIt;
	document.listingsearch.priceMin.disabled = disableIt;
}

function disable3(disableIt)
{
	document.listingsearch.cityID.disabled = disableIt;
	document.listingsearch.locationID.disabled = disableIt;
	document.listingsearch.priceMin.disabled = disableIt;
}

function disable4(disableIt)
{
	document.listingsearch.cityID.disabled = disableIt;
	document.listingsearch.typeID.disabled = disableIt;
	document.listingsearch.locationID.disabled = disableIt;
}

function changeColor(){
		if(navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) >= 4){
				ie4 = true;
		}
		else {
				ie4 = false;
		}	
			if(ie4){
                what = window.event.srcElement;
                if(what.className == "link"){					
                        if(what.style.color == "white"){
                                what.style.color = "black";
                        }
						else if(what.style.color == "orange"){
                                what.style.color = "black";
                        }
                        else {
                                what.style.color = "orange";
                        }
                }
                else {}
        }
}

function ruta(form){
	var destino = form.direccion.value;
	var var_url = "http://maps.google.es/maps?f=d&hl=es&saddr=" +  form.origen.value + "&daddr=" + destino + "&om=1";
	window.open(var_url,'Ruta');
}

//Control del formulario

function blinky(delay){
     var el = document.body.getElementsByTagName('SPAN');
     for (var i = 0; i < el.length; i++){
	if (el[i].className == 'blink'){
	  el[i].style.visibility = el[i].style.visibility == 'hidden' ? 'visible' : 'hidden';
	}
     }
     setTimeout('blinky(' + delay + ')', delay);
   }
