$(function() {  
	$('label').hide();
	$("#enviar").click(function() { 
		$('label').hide();								
  		var destino="send_contacto.php";  

		var nombre = $("input#nombre").val();  
        if (nombre == "") {   
			$("label#nombre_error").show();   
			$("input#nombre").focus();   
		    return false;   
		}   
		
		var empresa = $("input#empresa").val();  
		
        var email = $("input#email").val();   
        if (email == "") {   
			$("label#email_error").show();   
			$("input#email").focus();   
			return false;   
	    }
        if ( !esValidoElCorreo(email) ) {   
			$("label#email_error").show();   
			$("input#email").focus();   
			return false;   
	    }		
		
        var telefono = $("input#telefono").val();   
        if ( (telefono == "") || (telefono.length!=9) || (!isNumeric(telefono))  ) {
					$("label#telefono_error").show();   
					$("input#telefono").focus();   
					return false;   
	    }
		if (     !((telefono>=600000000) && (telefono<=699999999)) &&
				 ! ((telefono>=900000000) && (telefono<=999999999))	&& 
				 ! ((telefono>=700000000) && (telefono<=799999999))    
			) {		
				 
					$("label#telefono_error").show();   
					$("input#telefono").focus();   
					return false;   
	    }				 
				 
			 
        var consulta = $("textarea#consulta").val();   
        if (consulta == "") {   
			$("label#consulta_error").show();   
			$("textarea#consulta").focus();   
			return false;   
	    }

		if (!$('#privacidad').attr('checked'))
		{
			$("label#privacidad_error").show();   
 		    return false; 			
		}

  var dataString = 'nombre='+ nombre + '&empresa=' + empresa + '&email=' + email + '&telefono=' + telefono + '&consulta=' + consulta;

  $.ajax({
    type: "POST",
    url: "/ws/" + destino,
    data: dataString,
    success: function() {
      $('#formulario').html("<div id='mensaje' style='text-align:center; margin-top:60px; margin-right:80px'></div>");
      $('#mensaje').html("<h2><span class='azul'>Formulario enviado correctamente</span></h2>")
      .append("<p><br/><br/>En breve contactaremos con usted.<br/><br/>Muchas gracias por todo.</p><div style='height:180px'></div>")
      .hide()
      .fadeIn(1500);
    }
  });
  		
	return false; 
  });   
});


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

function esValidoElCorreo(mail) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(mail);

}
