$(document).ready(function(){


// Validar formulario de contacto

var options = { 
  target:        '#mensaje_ok',   // target element(s) updated with server response 
  beforeSubmit:  validateForm,  // pre-submit callback 
  success:       showResponse  // post-submit callback 
}; 

// bind form using 'ajaxForm' 
$('#form_paso1').ajaxForm(options);
// pre-submit callback 
function validateForm(formData, jqForm, options) { 
  
  $("#form_errores").empty();

  var nombre                = $("#nombre").val(); 
  var tipo_cliente          = $("#tipo_cliente").val(); 
  var email_contacto        = $("#email_contacto").val(); 
  var pais_cliente          = $("#pais_cliente").val();
  var ciudad_cliente        = $("#ciudad_cliente").val();
  var acepto                = $("#acepto_envio:checked").val();
 
 
   var errors                = 0; 
   
  //alert(acepto);


  if (acepto == null || acepto == '') 
  { 
        $("#form_errores").append("No has aceptado las condiciones del servicio.<br/>"); 
        errors++; 
  } 


   if (tipo_cliente == null || tipo_cliente == '' || tipo_cliente == '0') 
  { 
        $("#form_errores").append("Selecciona una categoria.<br/>"); 
        errors++; 
  } 
  if (nombre == null || nombre == '') 
  { 
        $("#form_errores").append("Escribe el nombre de tu web.<br/>"); 
        errors++; 
  } 

  if (email_contacto == null || email_contacto == '') 
  { 
        $("#form_errores").append("Escribe tu email de contacto.<br/>"); 
        errors++; 
  } 
  else if ( email_contacto.length > 0 )
  {
    var filter=/^[A-Za-z.-][A-Za-z0-9_.-]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
    if (!filter.test(email_contacto))
    {
      $("#form_errores").append("El email de contacto no es v&#225;lido.<br/>"); 
      errors++;
    }
  }
  
  if (pais_cliente == null || pais_cliente == '' || pais_cliente == '0') 
  { 
        $("#form_errores").append("Selecciona el pais donde te encuentras.<br/>"); 
        errors++; 
  } 
  
  

  if (ciudad_cliente == null || ciudad_cliente == '') 
  { 
        $("#form_errores").append("Escribe la ciudad donde vives.<br/>"); 
        errors++; 
  } 
  

  if (errors > 0) 
  { 
        $("#form_errores").fadeIn(1000);
        return false; 
  } 

  return true;      
} 



function isNumeric(form_value) 
{ 
    if (form_value.match(/^\d+$/) == null) 
        return false; 
    else 
        return true; 
} 


//---------------


// post-submit callback 

function showResponse(responseText, statusText)  { 

//alert(responseText);

  if (responseText == "-1")
  {
    $("#form_errores").append("Ups! El nombre elegido no es v&aacute;lido. Evita los espacios, los signos de puntuaci&oacute;n, la letra e&ntilde;e y cualquier otro tipo de caracter especial distinto a letras min&uacute;sculas.<br/>"); 
    $("#form_errores").fadeIn(1000);
  }
  if (responseText == "-2")
  {
    $("#form_errores").append("Vaya! Este nombre ya est&aacute; siendo usado. Prueba con otro.<br/>"); 
    $("#form_errores").fadeIn(1000);
  }


  if (responseText == "-3")
  {
    $("#form_errores").append("<p><h1><strong>Hack attempt!</strong></h1></p><p>Your IP has been tracked. Thank you!</strong></p>"); 
    $("#form_errores").fadeIn(1000);
  }

  if (responseText == true || responseText > 0)
  {
   //bloquear el formulario
   //$('#tipo_cliente').attr("disabled", true); 
   //$("#nombre").attr("disabled", true);
   //$("#email_contacto").attr("disabled", true);
   //$("#pais_cliente").attr("disabled", true);
   //$("#ciudad_cliente").attr("disabled", true);
  

    $("#boton_enviar").hide(); 
    $("#form_errores").fadeOut(500);
  
    $("#mensaje_ok").html("<div style='float:left;'><img src='./images/check-OK.jpg' /></div><div style='float:left;width:410px;'><strong>Tu solicitud ha sido recibida correctamente. <br />Entra en tu email para consultar los datos de acceso al configurador de tu web. No olvides revisar la Bandeja de Correo no deseado.<br />Gracias!</strong></div>");

    $("#mensaje_ok").fadeIn(500);
    $("#capa_botones").fadeIn(500); 
  }
} 


});

