function checkMail(x){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)){
		return true;
	}else{
		return false;
	}
}

var url = 'inc/captcha/captcheck.php?captcha=';
var captchaOK = 2;  // 2 - not yet checked, 1 - correct, 0 - failed
        
        function getHTTPObject()
        {
        try {
        req = new XMLHttpRequest();
          } catch (err1)
          {
          try {
          req = new ActiveXObject("Msxml12.XMLHTTP");
          } catch (err2)
          {
          try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err3)
            {
	req = false;
            }
          }
	}
        return req;
	}
        
        var http = getHTTPObject(); // We create the HTTP Object        
        
        function handleHttpResponse() {
        if (http.readyState == 4) {
            captchaOK = http.responseText;
            if(captchaOK != 1) {
              alert('El código ingresado es incorrecto.');
              document.formulario.captcha.value='';
              document.formulario.captcha.focus();
              return false;
              }
              document.formulario.submit();
           }
        }

        function checkcode(thecode) {
        http.open("GET", url + escape(thecode), true);
        http.onreadystatechange = handleHttpResponse;
        http.send(null);
        }
        
        function checkform() {
        // First the normal form validation
        if(document.formulario.nombre.value=='') {
          alert('\n\tEl campo "Nombre" es obligatorio.');
          document.formulario.nombre.focus();
          return false;
          }
        if(document.formulario.email.value=='') {
          alert('\n\tEl campo "E-mail" es obligatorio.');
          document.formulario.email.focus();
          return false;
          }		  
		if(document.formulario.consulta.value=='') {
          alert('\n\tEl campo "Consulta" es obligatorio.');
          document.formulario.consulta.focus();
          return false;
          }		 
        if(!checkMail(document.formulario.email.value)) {
          alert('\n\tEl "E-mail" no es válido.');
          document.formulario.mail.focus();
          return false;
          }
          // Now the Ajax CAPTCHA validation
          checkcode(document.formulario.captcha.value);
          return false;
        }
