// JavaScript Document
function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		alert("Email incorrecto");
		return false;
	}
}

function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

function validate_form(thisform)
{
	with (thisform) {
		if (validate_required(nombre,"Debe rellenar el nombre")==false)
		  {nombre.focus();return false;}
		if ((validate_required(email,"Debe rellenar el email")==false)
			|| (checkEmail(email) == false))
		  	{email.focus();return false;}
		if (validate_required(telefono,"Debe rellenar el telefono")==false)
	  	    {telefono	.focus();return false;}  	
	}
var urlpost = "comprobar_captcha.php";
var valorCaptcha = document.getElementById("captchacode").value;
if (valorCaptcha == "") {
 	document.getElementById("captchacode").focus();
	alert("Debe introducir el valor que existe en la imagen");	 	
	return false;
} else {
 	//comprobamos captcha
	 http.open("POST", urlpost, true); 
	 http.onreadystatechange = function () {
		return handleHttpResponseCaptcha();		 		 
	 }
	 http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 http.send('captchacode=' + escape(valorCaptcha));
	 return false;
 }
}

function handleHttpResponseCaptcha() { 
    if (http.readyState == 4) { 
			resultado = http.responseText;
			if (resultado == 'OK') {
				document.getElementById('form1').submit();					
			} else {
				alert("No es correcto el valor de la imagen introducido");
				document.getElementById("captcha").src = 'captcha/securimage/securimage_show.php?' + Math.random();
			 	document.getElementById("captchacode").focus();				 
				return false;
			}
	}
}
function getHTTPObject() {
	   var xmlhttp;
	   /*@cc_on
	   @if (@_jscript_version >= 5)
	     try {
	       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	     } catch (e) {
	       try {
	         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	       } catch (E) {
	         xmlhttp = false;
	       }
	     }
	   @else
	   xmlhttp = false;
	   @end @*/
	   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	     try {
	       xmlhttp = new XMLHttpRequest();
	     } catch (e) {
	       xmlhttp = false;
	     }
	   }
	   return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object

function validar() {
	var doc = document.form;
	doc.texto_localidad.value = doc.localidad.options[doc.localidad.selectedIndex].text;
	doc.texto_precio.value = doc.precio_medio.options[doc.precio_medio.selectedIndex].text;	
	doc.texto_tipo_cocina.value = doc.tipo_comida.options[doc.tipo_comida.selectedIndex].text;	
	return true;
}
