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

		var nombre = $("input#demo-nombre").val();  
        if (nombre == "") {   
			$("label#demo-nombre_error").show();   
			$("input#demo-nombre").focus();   
		    return false;   
		}   
		
		var empresa = $("input#demo-empresa").val();  
        if (empresa == "") {   
			$("label#demo-empresa_error").show();   
			$("input#demo-empresa").focus();   
		    return false;   
		}   		
		
        var email = $("input#demo-email").val();   
        if (email == "") {   
			$("label#demo-email_error").show();   
			$("input#demo-email").focus();   
			return false;   
	    }
        if ( !esValidoElCorreo(email) ) {   
			$("label#demo-email_error").show();   
			$("input#demo-email").focus();   
			return false;   
	    }		
		
        var codtelefono = $("input#demo-cod-telefono").val();
        if ( (codtelefono == "") || (codtelefono.length==0) || (!isNumeric(codtelefono))  ) {
					$("label#demo-telefono_error").show();   
					$("input#demo-cod-telefono").focus();   
					return false;   
	    }

        var telefono = $("input#demo-telefono").val();   

		if (codtelefono=="34"){
			        if ( (telefono == "") || (telefono == "912792370") || (telefono.length!=9) || (!isNumeric(telefono))  ) {
						$("label#demo-telefono_error").show();   
						$("input#demo-telefono").focus();   
						return false;   
		    }
			if (     !((telefono>=600000000) && (telefono<=999999999))    ) {		
	 					$("label#demo-telefono_error").show();   
						$("input#demo-telefono").focus();   
						return false;   
		    }				 
		}else{
			if ( (telefono == "") || (telefono.length<8) || (!isNumeric(telefono))  ) {
						$("label#demo-telefono_error").show();   
						$("input#demo-telefono").focus();   
						return false;   
		    }			
		}

        var skype = $("input#demo-skype").val();  

        var consulta = $("textarea#demo-consulta").val();   
        if (consulta == "") {   
			$("label#demo-consulta_error").show();   
			$("textarea#demo-consulta").focus();   
			return false;   
	    }

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

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

  $.ajax({
    type: "POST",
    url: "/ws/" + destino,
    data: dataString,
    success: function() {
      $('#demo-formulario').html("<div id='demo-mensaje' style='text-align:center; margin-top:60px; margin-right:80px'></div>");
      $('#demo-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);

}
