jQuery(document).ready(function() {
   jQuery('img.img_close').hover(
      function() { this.src='/imas/comunes/cl_h.gif'; },
      function() { this.src='/imas/comunes/cl.gif'; }
   );
   jQuery('img.img_open').hover(
      function() { this.src='/imas/comunes/op_h.gif'; },
      function() { this.src='/imas/comunes/op.gif'; }
   );
   jQuery('img.img_close, img.img_open').click(function() {
      data = this.id.split("_");
      toggle_block(data[2]);
   });
   jQuery('.blk_title').dblclick(function() {
      data = this.id.split("_");
      toggle_block(data[2]);
   });
   jQuery('.box1').focus(function() { this.className='box2'; } ).blur(function() { this.className='box1'; } );
});

function toggle_block(n) {
   var $content = jQuery('#blk_content_'+n);
   var $title = jQuery('#blk_title_'+n);
   if ($content.eq(0).css("display") == "none") {
      $content.slideDown('normal');
      jQuery('img.img_open',$title).css("display","none");
      jQuery('img.img_close',$title).css("display","");
   }
   else {
      $content.slideUp('normal');
      jQuery('img.img_close',$title).css("display","none");
      jQuery('img.img_open',$title).css("display","");
   }
}

function changeloc(prov,id) {
   jQuery.get("/include/funcXHR.php?op=17&prov="+prov,function(data) { fillSelect(data,id); });
}

function changedist(loc,id) {
   jQuery.get("include/funcXHR.php?op=18",{tbl:'distritos',key:'cod_dist',val:'nombre_dist',whr:'cod_loc='+loc+' and cod_bar is null',ord:'nombre_dist'},function(data) {
      fillSelect(data,id);
      if (/==/.test(data))
         jQuery('.c_'+id).css("display","");
      else
         jQuery('.c_'+id).css("display","none");
      
   });
}

function changebarr(dist,id) {
   jQuery.get("include/funcXHR.php?op=18",{tbl:'distritos',key:'cod_bar',val:'nombre_bar',whr:'cod_dist='+dist,ord:'nombre_bar'},function(data) {
      fillSelect(data,id);
   });
}

function change_inmuebles(val) {
   jQuery.get("/include/funcXHR.php?op=19&cod="+val+"&idi="+idioma,function(data) {
      fillSelect(data,'casa');
      if (/==/.test(data))
         jQuery('.c_casa').css("display","");
      else
         jQuery('.c_casa').css("display","none");
   });
   
   if (val&511)
      jQuery('.c_ascensor').show();
   else {
      jQuery('.c_ascensor').hide();
      document.f.ascensor.checked=false;
   }
   if (val&3583)
      jQuery('.c_garaje').show();
   else {
      jQuery('.c_garaje').hide();
      document.f.garaje.value=0;
      document.f.plazas.value=0;
   }
   if (val&4304)
      jQuery('.c_patio').show();
   else {
      jQuery('.c_patio').hide();
      document.f.patio.checked=false;
   }
   if (val&4351)
      jQuery('.c_amueblado,.c_comunidad,.c_armarios,.c_cocina,.c_salon,.c_electrodomesticos,.c_lavadero,.c_chimenea,.c_despensa,.c_parabolica,.c_calefaccion').show();
   else {
      jQuery('.c_amueblado,.c_comunidad,.c_armarios,.c_cocina,.c_salon,.c_electrodomesticos,.c_lavadero,.c_chimenea,.c_despensa,.c_parabolica,.c_calefaccion').hide();
      document.f.amueblado.checked=false;
      document.f.comunidad.value=0;
      document.f.armarios.value=0;
      document.f.cocina.value=0;
      document.f.salon.value=0;
      document.f.electrodomesticos.checked = false;
      document.f.lavadero.checked = false;
      document.f.chimenea.checked = false;
      document.f.despensa.checked = false;
      document.f.parabolica.checked = false;
      document.f.calefaccion.value=0;
   }
   if (val&4607)
      jQuery('.c_portero,.c_orientacion,.c_terrazas,.c_trastero').show();
   else {
      jQuery('.c_portero,.c_orientacion,.c_terrazas,.c_trastero').hide();
      document.f.portero.value=0;
      document.f.orientacion.value=0;
      document.f.terrazas.value=0;
      document.f.supTerraza.value=0;
      document.f.trastero.checked = false;
   }
   if (val&6399)
      jQuery('.c_dorm,.c_jardin,.c_piscina').show();
   else {
      jQuery('.c_dorm,.c_jardin,.c_piscina').hide();
      document.f.dormDobles.value=0;
      document.f.dormSimples.value=0;
      document.f.jardin.checked=false;
      document.f.piscina.checked=false;
   }
   if (val&6911)
      jQuery('.c_soleria,.c_aire').show();
   else {
      jQuery('.c_soleria,.c_aire').hide();
      document.f.soleria.value=0;
      document.f.aire.value=0;
   }
   if (val&7376)
      jQuery('.c_nPlantas').show();
   else {
      jQuery('.c_nPlantas').hide();
      document.f.nPlantas.value=0;
   }
   if (val&8191)
      jQuery('.c_supUtil,.c_supConstruida,.c_banos,.c_aseos,.c_carpinteria,.c_gas,.c_cerradura,.c_blindada').show();
   else {
      jQuery('.c_supUtil,.c_supConstruida,.c_banos,.c_aseos,.c_carpinteria,.c_gas,.c_cerradura,.c_blindada').hide();
      document.f.supUtil.value=0;
      document.f.supConstruida.value=0;
      document.f.banos.value=0;
      document.f.aseos.value=0;
      document.f.carpinteria.value=0;
      document.f.gas.value=0;
      document.f.cerradura.checked=false;
      document.f.blindada.checked=false;
   }
   if (val&28688)
      jQuery('.c_luz,.c_agua,.c_telefono,.c_acceso').show();
   else {
      jQuery('.c_luz,.c_agua,.c_telefono,.c_acceso').hide();
      document.f.luz.value=0;
      document.f.agua.value=0;
      document.f.telefono.checked=false;
      document.f.acceso.value=0;
   }
   if (val&31984)
      jQuery('.c_supParcela').show();
   else {
      jQuery('.c_supParcela').hide();
      document.f.supParcela.value=0;
   }
   if (val&61439)
      jQuery('.c_campoGolf,.c_entorno').show();
   else {
      jQuery('.c_campoGolf,.c_entorno').hide();
      document.f.campoGolf.checked=false;
      document.f.comercios.checked=false;
      document.f.culturales.checked=false;
      document.f.verdes.checked=false;
      document.f.deportivas.checked=false;
      document.f.escolares.checked=false;
      document.f.metro.checked=false;
      document.f.golf.checked=false;
      document.f.autobuses.checked=false;
      document.f.renfe.checked=false;
   }
   if (val&106495)
      jQuery('.c_alarma,.c_serVig').show();
   else {
      jQuery('.c_alarma,.c_serVig').hide();
      document.f.alarma.checked=false;
      document.f.serVig.checked=false;
   }
 
}

function fillSelect(data,id) {
   var elem = jQuery('#'+id).get(0);
   if (elem.tagName == 'SELECT') {
      elem.options.length = 1;
      if (/==/.test(data)) {
         var arr = data.split('<>');
         for (var i=0; i<arr.length; i++) {
            var dt = arr[i].split('==');
            elem.options[i+1] = new Option(dt[1],dt[0]);
         }
      }
   }
}

function enviar() {
   if (comprobar()) {
      document.f.accion.value=2;
      document.f.submit();
   }
}



function comprobar() {
   var errores = new Array();
   
   if (document.f.prop_nombre.value=='') {
      errores[errores.length] = erroresStr['prop_nombre'];
      jQuery('#prop_nombre').addClass("error");
      jQuery('#prop_nombre').one("focus",function() { jQuery(this).removeClass("error"); });
   }
   if (document.f.prop_email.value=='' || checkMail(document.f.prop_email.value)) {
      errores[errores.length] = erroresStr['prop_email'];
      jQuery('#prop_email').addClass("error");
      jQuery('#prop_email').one("focus",function() { jQuery(this).removeClass("error"); });
   }
   if (document.f.prop_telefono.value=='' || document.f.prop_telefono.value.length<9) {
      errores[errores.length] = erroresStr['prop_telefono'];
      jQuery('#prop_telefono').addClass("error");
      jQuery('#prop_telefono').one("focus",function() { jQuery(this).removeClass("error"); });
   }
   if (!document.f.chk_privacidad.checked) {
      errores[errores.length] = erroresStr['chk_privacidad'];
      jQuery('#chk_privacidad').addClass("error");
      jQuery('#chk_privacidad').one("focus",function() { jQuery(this).removeClass("error"); });
   }
   if (document.f.inmuebles.value==0) {
      errores[errores.length] = erroresStr['inmuebles'];
      jQuery('#inmuebles').addClass("error");
      jQuery('#inmuebles').one("focus",function() { jQuery(this).removeClass("error"); });
   }
   if (document.f.oferta.value==0) {
      errores[errores.length] = erroresStr['oferta'];
      jQuery('#oferta').addClass("error");
      jQuery('#oferta').one("focus",function() { jQuery(this).removeClass("error"); });
   }
   if (document.f.provincia.value==0) {
      errores[errores.length] = erroresStr['provincia'];
      jQuery('#provincia').addClass("error");
      jQuery('#provincia').one("focus",function() { jQuery(this).removeClass("error"); });
   }
   if (document.f.localidad.value==0) {
      errores[errores.length] = erroresStr['localidad'];
      jQuery('#localidad').addClass("error");
      jQuery('#localidad').one("focus",function() { jQuery(this).removeClass("error"); });
   }
   if ((document.f.oferta.value==1 || document.f.oferta.value==2 || document.f.oferta.value==7 || document.f.oferta.value==8 || document.f.oferta.value==3 || document.f.oferta.value==9)) {
      if (!document.f.precio.value || document.f.precio.value==0)  {
         errores[errores.length] = erroresStr['precio'];
         jQuery('#precio').addClass("error");
         jQuery('#precio').one("focus",function() { jQuery(this).removeClass("error"); });
      }
      if(document.f.oferta.value==9){
         if (!document.f.precioCompra.value || document.f.precioCompra.value==0)  {
            errores[errores.length] = erroresStr['precio'];
            jQuery('#precioCompra').addClass("error");
            jQuery('#precioCompra').one("focus",function() { jQuery(this).removeClass("error"); });
         }
      }      
   }
   else if (!document.f.preciopordia.value && !document.f.precioporfinde.value && !document.f.precioporsemana.value && !document.f.precioporquincena.value && !document.f.preciopormes.value) {
      errores[errores.length] = erroresStr['precioAlq'];
      jQuery('.c_precioAlq input').addClass("error");
      jQuery('.c_precioAlq input').one("focus",function() { jQuery('.c_precioAlq input').removeClass("error") });
   }
      
   if (errores.length) {
      var msg = erroresStr['inicio']+"\n\n";
      for (var i=0; i<errores.length; i++)
         msg += errores[i]+"\n";
      msg += "\n"+erroresStr['final'];
      alert(msg);
      return false;
   }
   else
      return true;
}

function marcar(val,id) {
   var elem = document.getElementById(id);
   for (var i=0; i<elem.options.length; i++) {
      var op = elem.options[i];
      if (op.value == val) 
         op.selected = true;
   }
}

function change_oferta(val) {
   // 1. Actualizamos el estado.
   var antEstado = document.f.estado.value;
   if (val==1) // Venta a sexas
      jQuery.get("include/funcXHR.php?op=18",{tbl:'testado',val:'nombre'+idioma,whr:'cod in(2,3)'},function(data) { fillSelect(data,"estado"); marcar(antEstado,"estado"); });
   else if (val==2) // Venta en constriccion
      jQuery.get("include/funcXHR.php?op=18",{tbl:'testado',val:'nombre'+idioma,whr:'cod in(1)'},function(data) { fillSelect(data,"estado"); marcar(1,"estado"); });
   else if (val==7) // Inversiones
      jQuery.get("include/funcXHR.php?op=18",{tbl:'testado',val:'nombre'+idioma,whr:'cod in(1,2,3)'},function(data) { fillSelect(data,"estado"); marcar(antEstado,"estado"); });
   else if (val==8)
     jQuery.get("include/funcXHR.php?op=18",{tbl:'testado',val:'nombre'+idioma,whr:'cod in(1,2)'},function(data) { fillSelect(data,"estado"); marcar(antEstado,"estado"); });
   else {
      fillSelect("",'estado');
      marcar(0,"estado");
   }
   if (val==2)
      antEstado = 1;
   for (var i=0; i<document.f.estado.options.length; i++) {
      var op = document.f.estado.options[i];
      if (op.value == antEstado) 
         op.selected = true;
   }
    
   jQuery('.c_precioCompra').hide();
   jQuery('#euroMes').hide();
   jQuery('#soloEuro').show();  
   if (val==1 || val==2 || val==7 || val==8) { // VENTAS
      jQuery('.c_precio').show();
      jQuery('.c_precio td.column1').text('* '+labelsStr['85']);
      jQuery('.c_precioAlq').hide();
      jQuery('.c_alqLarga, .c_alqTuristico').hide();
      if (val==7)
         jQuery('.c_estado').show();
      else
         jQuery('.c_estado').hide();
      if (val==2)
         jQuery('.c_tespera').show();
      else
         jQuery('.c_tespera').hide();
      if (val==1)
         jQuery('.c_antiguedad').show();
      else {
         jQuery('.c_antiguedad').hide();
         document.f.antiguedad.value=0;
         document.f.conservacion.value=0;
      }
   }
   else if (val==3) { // ALQUILER LARGA TEMPORADA
      jQuery('.c_tespera, .c_antiguedad, .c_alqLarga').hide();
      jQuery('.c_alqTuristico').show();
      if (document.f.alqTuristico.checked) {
         jQuery('.c_precio').show();
         jQuery('.c_precio td.column1').text('* '+labelsStr['433']);
         jQuery('.c_precioAlq').show();
      } else {
         jQuery('.c_precio').show();
         jQuery('.c_precio td.column1').text('* '+labelsStr['85']);
         jQuery('.c_precioAlq').hide();
      }
   }
   else if (val==9) { // ALQUILER CON OPCIÓN A COMPRA
      jQuery('.c_tespera, .c_antiguedad, .c_alqLarga').hide();
      jQuery('.c_alqTuristico').hide();      
      jQuery('.c_precio').show();
      jQuery('.c_precio td.column1').text('* '+labelsStr['85']);
      jQuery('.c_precioAlq').hide();
      jQuery('.c_precioCompra').show();
      
      jQuery('#soloEuro').hide();
      jQuery('#euroMes').show();   
      
   }
   else if (val==4 || val==5) { // ALQUILER TURISTICO Y TURISMU RURAL
      jQuery('.c_tespera, .c_antiguedad, .c_alqTuristico').hide();
      jQuery('.c_alqLarga').show();
      if (document.f.alqLarga.checked) {
         jQuery('.c_precio').show();
         jQuery('.c_precio td.column1').text('* '+labelsStr['433']);
         jQuery('.c_precioAlq').show();
      } else {
         jQuery('.c_precio').hide();
         jQuery('.c_precio td.column1').text('* '+labelsStr['85']);
         jQuery('.c_precioAlq').show();
      }
   }
   else 
      jQuery('.c_alqLarga, .c_alqTuristico, .c_estado, .c_tespera').hide();
}

function change_estado(val) {
   if (val==3)
      jQuery('.c_antiguedad').show();
   else
      jQuery('.c_antiguedad').hide();
   if (val==1)
      jQuery('.c_tespera').show();
   else
      jQuery('.c_tespera').hide();
}

function change_alqLarga() {
   change_oferta(document.f.oferta.value);
}
function change_alqTuristico() {
   change_oferta(document.f.oferta.value);
}
