// JavaScript Document
var objXML;
var objTXT;
var idIntervalDespliege;
AlturaFormulario=0;
LimiteFormulario=268;

function despliega_formulario(){
    AlturaFormulario=0;
	var divTL=document.getElementById("FormularioDeHipoteca");
	divTL.style.visibility="visible";
	idIntervalDespliege = setInterval("MuestraFormulario()",1);
}

function MuestraFormulario(){
	AlturaFormulario=AlturaFormulario+10;
	var divTL=document.getElementById("FormularioDeHipoteca");
	divTL.style.height=AlturaFormulario+'px';
	if(AlturaFormulario>LimiteFormulario){
	   clearInterval(idIntervalDespliege);
	}
}

function Repliega_formulario(){
	var divTL=document.getElementById("FormularioDeHipoteca");
	idIntervalDespliege = setInterval("repliegalo()",1);
}

function repliegalo(){
	AlturaFormulario=AlturaFormulario-10;
	var divTL=document.getElementById("FormularioDeHipoteca");
	divTL.style.height=AlturaFormulario+'px';
	if(AlturaFormulario==0){
	   clearInterval(idIntervalDespliege);
	   divTL.style.visibility="hidden";
	}
}
//Comprobación de formularios y envío del mismo
function enviaFormulario(frmDatos){
   //compruebo la introducción de datos
   if (check_form(frmDatos)){
	  var iIndice;
      var strContenido="";
      var strSeparador="";
      //Recorremos el formulario	 
      for (iIndice=0;iIndice<10;iIndice++){
         //Si se trata de un checkbox
		 if(frmDatos.elements[iIndice].type=="checkbox"){
             if (frmDatos.elements[iIndice].checked){
			     strContenido=strContenido+strSeparador+frmDatos.elements[iIndice].name+"=1";		 
             }
			 else{
			     strContenido=strContenido+strSeparador+frmDatos.elements[iIndice].name+"=0"; 
			 }
		 } //Si no se trata de un checkbox
		 else{
		     strContenido=strContenido+strSeparador+frmDatos.elements[iIndice].name+"="+encodeURIComponent(frmDatos.elements[iIndice].value);
		 }
	     strSeparador="&";
      }
	  objXML=CreaXHR();
	  //alert(strContenido);
      //Si tenemos el objeto
      if(objXML){     
	    //Enviamos la solicitud al servidor		
		var url_xml='./Mail/enviaFinanciacion.php';
		objXML.onreadystatechange=ProcesaRespuesta;
		objXML.open('POST',url_xml);
		//con las cabeceras apropiadas
	    objXML.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	    objXML.setRequestHeader('Content-Length',strContenido.length);
	    objXML.send(strContenido);
      }
	  else{
	    alert('Objeto XMLHttpRequest no activo.Actualize su navegador');
	  }	  
	  return false;
   }
   else{
	 return false;
   }	  
}

function ProcesaRespuesta(){
  if (objXML.readyState==4){
	 //limpio el intervalo de tiempo
	 var XMLCo=objXML.responseXML;
	 var respuesta=XMLCo.getElementsByTagName('resultado');
     if (respuesta[0].getAttribute('resul')=="SI"){
         alert(respuesta[0].getAttribute('descrip'));
     }
  }
}

function check_form(frm){
	if(validanom(frm.fldNombre.value,'Nombre y Apellidos','fldNombre')==false){return false;};			
	if(validanom(frm.fldTelefono.value,'Teléfono','fldTelefono')==false){return false;};
	if(validanom(frm.fldEdad.value,'Edad','fldEdad')==false){return false;};	
	if(frm.fldEstadoCivil.selectedIndex==0){alert("Seleccione estado civil");return false;}
	if(frm.fldTipoContrato.selectedIndex==0){alert("Seleccione tipo de contrato");return false;}
	if(validanom(frm.fldPrecio.value,'Precio de vivienda','fldPrecio')==false){return false;};
	if(validanom(frm.fldIngresos.value,'Ingresos netos anuales','fldIngresos')==false){return false;};
	return true;
}

//función que comprueba la existencia de al menos un carácter
function validanom(nomtxt,nombrecampo,nomcasilla){
	frm=document.getElementById('formFinanciacion');
	if (nomtxt.length==0){
	    alert("La longitud del campo " + nombrecampo + " no puede ser 0");
        frm[nomcasilla].focus(); 
		return false;
	}
	var i;
	var suma=0;
	for (i=0;i<nomtxt.length;i++){
		if (nomtxt.charAt(i) !=" "){
		   suma++;
		}
	}
	if (suma==0){
     	alert("Es necesario completar el dato " + nombrecampo);
		//frm[nomcasilla].focus(); 
		return false; 
	}
	else {
	    //var txtmayus = nomtxt.toUpperCase();
		//frm(nomcasilla).value=txtmayus;
		return true;
	}
}

function cargaHipotecasRSS(){
	  var strContenido='';
	  objTXT=CreaXHR();
	  //Si tenemos el objeto
      if(objTXT){     
	    //Enviamos la solicitud al servidor		
		var url_xml='./rss/NewRSSHipotecas.php';
		objTXT.onreadystatechange=ProcesaRSSHipoteca;
		objTXT.open('POST',url_xml);
		//con las cabeceras apropiadas
	    objTXT.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	    objTXT.setRequestHeader('Content-Length',strContenido.length);
	    objTXT.send(strContenido);
      }
	  else{
	    alert('Objeto XMLHttpRequest no activo.Actualize su navegador');
	  }	  
	  return false;
}

function ProcesaRSSHipoteca(){
  if (objTXT.readyState==4){
	 //limpio el intervalo de tiempo
	 var XMLRSS=objTXT.responseText;
	 var divRSS=document.getElementById("RSSHipoteca");
     divRSS.innerHTML=XMLRSS;
  }
}