/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(function() {
   $('.error').hide();
   /*
 *Funcion que valida el recomienda a un amigo
 *
 */
   $('#btn_reco').click(function () {
      // validate and process form here
      $('.error').hide();
      var nombre = $("input#txtnombre1").val();
      if (nombre == "" || nombre ==null || nombre == "Su nombre") {
         $('#txtnombre1_error').fadeIn(500);
         $("input#txtnombre1").focus();
         return false;
      }

      var apellidos = $("input#txtnombre2").val();
      if (apellidos == "" || apellidos ==null || apellidos == "Nombre de su amigo") {
         $("label#txtnombre2_error").fadeIn(500);
         $("input#txtnombre2").focus();
         return false;
      }

      var correo = $("input#txtemail2").val();
      var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;
      if (correo == "" || !b.test(correo)) {
         $("label#txtemail2_error").fadeIn(500);
         $("input#txtemail2").focus();
         return false;
      }
      return true;
   });


   /*
    * Funcion para validad los comentarios
    */
   $('#btn_come').click(function () {
      // validate and process form here

      $('.error').hide();
      var nombre = $("input#nombre").val();
      if (nombre == "" || nombre ==null) {
         $("label#nombre_error").fadeIn(2000);
         $("input#nombre").focus();
         return false;
      }

      var apellidos = $("input#apellidos").val();
      if (apellidos == "" || apellidos ==null) {
         $("label#apellidos_error").fadeIn(2000);
         $("input#apellidos").focus();
         return false;
      }

      var dni = $("input#dni").val();
      if (dni == "" || dni ==null) {
         $("label#dni_error").fadeIn(2000);
         $("input#dni").focus();
         return false;
      }

      var edad = $("input#edad").val();
      if (edad == "" || edad ==null) {
         $("label#edad_error").fadeIn(2000);
         $("input#edad").focus();
         return false;
      }


      var correo = $("input#correo").val();
      var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;
      if (correo == "" || !b.test(correo)) {
         $("label#correo_error").fadeIn(2000);
         $("input#correo").focus();
         return false;
      }


      var ocupacion = $("input#ocupacion").val();
      if (ocupacion == "" || ocupacion ==null) {
         $("label#ocupacion_error").fadeIn(2000);
         $("input#ocupacion").focus();
         return false;
      }

      var comentario = $("textarea#comentario").val();
      if (comentario == "" || comentario ==null) {
         $("label#comentario_error").fadeIn(2000);
         $("textarea#comentario").focus();
         return false;
      }


      return true;
   });


   // Interceptamos el evento submit del form miComentario
   $('#miComentario').submit(function() {
      // Enviamos el formulario usando AJAX
      $.ajax({
         type: 'POST',
         url: $(this).attr('action'),
         data: $(this).serialize(),
         beforeSend: function(){
            $('#loading').show();
            $('#miComentario').hide();
            $('#result').hide();
         },
         // Mostramos un mensaje con la respuesta del Servelet
         success: function(data) {
            $('#result').html(data);
            $('#loading').hide();
            $('#result').show();
            $('#result').fadeOut(3000, function(){
               $('#miComentario').fadeIn(1000);
            });
            formResetCome();

         }
      })
      return false;
   });


   /*
    * Funcion para Recomienda a un Amigo ajax
    */
   $('#form_reco').submit(function() {
      // Enviamos el formulario usando AJAX
      $.ajax({
         type: 'POST',
         url: $(this).attr('action'),
         data: $(this).serialize(),
         beforeSend: function(){
            $('#loading_reco').show();
            $('#resuReco').hide();
         },
         // Mostramos un mensaje con la respuesta del Servelet
         success: function(data) {
            $('#resuReco').html(data);
            $('#loading_reco').hide();
            $('#resuReco').show();
            $('#resuReco').fadeOut(3000);
            formResetReco();
         }
      })
      return false;
   });


})


function valida_texto_null(){
   if(document.getElementById('txtnombre1').value == ''){
      document.getElementById('txtnombre1').value = 'Su Nombre';
   }
   if(document.getElementById('txtnombre2').value == ''){
      document.getElementById('txtnombre2').value = 'Nombre de su Amigo';
   }
   if(document.getElementById('txtemail2').value == ''){
      document.getElementById('txtemail2').value = 'Email de su Amigo';
   }
}

function formResetCome()
{
   document.getElementById("miComentario").reset();
}
function formResetReco()
{
   document.getElementById("form_reco").reset();
}
