if (window.location.href.search(/\/es\//)>0)
   var idioma = "es";
else if (window.location.href.search(/\/en\//)>0)
   var idioma = "en";
else if (window.location.href.search(/\/de\//)>0)
   var idioma = "de";
else if (window.location.href.search(/\/fr\//)>0)
   var idioma = "fr";
else if (window.location.href.search(/\/it\//)>0)
   var idioma = "it";

jQuery(document).ready(function() {
   jQuery('#cnt_telefono,#cnt_dias').TextField({type:"natural"});
   
   jQuery('#cnt_entrada').datepicker({
      changeFirstDay: false,
      showOtherMonths: true,
      minDate: +0,
      changeMonth: false, 
      changeYear: false
   }).attr("readonly","readonly");
   
   jQuery('#cnt_enviar').click(function() {
      var nombre = jQuery.trim(jQuery('#cnt_nombre').val());
      var telefono = jQuery.trim(jQuery('#cnt_telefono').val());
      var email = jQuery.trim(jQuery('#cnt_email').val());
      var comentario = jQuery.trim(jQuery('#cnt_comentario').val());
      
      if (nombre=="")
         showError("nombre");
      else if (telefono=="") 
         showError("telefono");
      else if (email=="")
         showError("email");
      else if (!checkEmail(email))
         showError("email2");
      else if (comentario=="")
         showError("comentario");
      else {
         this.disabled = true;
         this.value = texto['enviando'];
         jQuery.ajax({
            url: '/include/contacto.php?f=1',
            type: 'post',
            dataType: 'text',
            data: {
               inm: jQuery('#cnt_inm').val(),
               nombre: jQuery('#cnt_nombre').val(),
               pais: jQuery('#cnt_pais').val(),
               telefono: jQuery('#cnt_telefono').val(),
               email: jQuery('#cnt_email').val(),
               comentario: jQuery('#cnt_comentario').val(),
               entrada: jQuery('#cnt_entrada').size() ? jQuery('#cnt_entrada').val(): 0,
               dias: jQuery('#cnt_dias').size() ? jQuery('#cnt_dias').val() : 0,
               idioma: idioma
            },
            error: cntError,
            success: cntSuccess
         });
      }
      return false;
   });
});

function cntSuccess(str, status) {
   var errno = parseInt(str);
   if (errno<0)
      jQuery.prompt(jQuery('#cnt_success').html(), {buttons:{Aceptar:true}});
   else if (errno&32)
      showError("email2");
   else
      cntError(0,0,0);
   jQuery("#cnt_enviar").get(0).value=texto['enviado'];
}

function cntError(XMLHttpRequest, textStatus, errorThrown) {
   var msg = jQuery('#cnt_error').html();
   jQuery.prompt(msg, {buttons:{Aceptar:true}});
}

function showError(elem) {
   if (elem == "email2")
      var elemf="email";
   else
      var elemf = elem;
   jQuery.prompt(texto[elem], {
      buttons:{Aceptar:true},
      callback: function() { jQuery('#cnt_'+elemf).focus(); }
   });
};


