//Create a boolean variable to check for a valid Internet Explorer instance.
var xmlhttp = false;
//Check if we are using IE.
	try {
				//If the Javascript version is greater than 5.
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				//alert ("You are using Microsoft Internet Explorer.");
				} catch (e) {
									//If not, then use the older active x object.
									try {
										//If we are using Internet Explorer.
											xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
											//alert ("You are using Microsoft Internet Explorer");
											} catch (E) {
											//Else we must be using a non-IE browser.
											xmlhttp = false;
											}
			}
	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();

		//alert ("You are not using Microsoft Internet Explorer");
	}

function checkemail(){
		//var url= document.location.href.toString();
		//document.form1.prevurl.value = url;
		
		if(document.getElementById('namebox').value == ""){
			alert("Por favor escriba su nombre");
			document.form1.namebox.focus();
			document.form1.namebox.selectionStart = 0;
			return false;
		}
		else if(document.getElementById('emailbox').value == ""){
			alert("Por favor escriba su dirección de correo electrónico");
			document.form1.emailbox.focus();
			document.form1.emailbox.selectionStart = 0;
			return false;
		}
		else if(document.getElementById('mensaje').value == ""){
			alert("Por favor escriba un mensaje");
			document.form1.mensaje.focus();
			document.form1.mensaje.selectionStart = 0;
			return false;
			}
		else if(document.getElementById('captchacode').value == ""){
			alert("Por favor escriba el codigo que aparece en la imagen");
			document.form1.captchacode.focus();
			document.form1.captchacode.selectionStart = 0;
			return false;
			}
		else if(document.getElementById('code').value == 0){
			alert("El codigo es incorrecto. Por favor intente de nuevo");
			document.getElementById('captcha').src = 'captcha/securimage_show.php?' + Math.random();
			document.form1.captchacode.focus();
			document.form1.captchacode.selectionStart = 0;
			return false;
			}

		
		else {
				
				if(!comprobar_sintaxis(document.getElementById('emailbox').value)){
					alert("Por favor escriba una dirección de correo electrónico válida.");
					document.form1.emailbox.focus();
					document.form1.emailbox.selectionStart = 0;
					return false;
				}
				else
					return true;	
		}

}

function comprobar_sintaxis(email){
	
					var mail_correcto = 0;
					//compruebo unas cosas primeras

					if(substr_count(email, '@') == 1){ // si solo tiene un @
						if (email.length >= 6 && email.substr(0,1) != "@" && email.substr(email.length-1,1) != "@"){ // si la cadena es mayor a 6 caracteres y no empieza ni termina en @
							 //busco que no tenga algunos caracteres especiales.
							if (email.indexOf("'") == -1 && email.indexOf("\\") == -1  && email.indexOf("$") == -1  && email.indexOf("!") == -1  && email.indexOf(' ') == -1 ) {
							 	if (substr_count(email.substr(email.lastIndexOf('@'),email.length),".") >= 1){ //si contiene al menos un '.' despues del arroba
								 //obtengo la terminacion del dominio
								 var term_dom = email.substr(email.lastIndexOf('.')+1,email.length);
								 //compruebo que la terminación del dominio sea correcta
								 	if (term_dom.length>1 && term_dom.length<5 ){ // si la terminacion del dominio es mayor a 1 y menor que 5
									//compruebo que lo de antes del dominio sea correcto
										var term = email.substr(email.lastIndexOf('@'),email.length);
										if(term.indexOf('@.') == -1){
											
											mail_correcto = 1;	
										}
										
									}
								}
							}
						}
					}

					if (mail_correcto){
						 //alert("email correcto");
						 return true;
					}
					else{
						 //alert("email incorrecto");
						 return false;
					}
} 

function substr_count(cadena, caracter){
	var temp = new Array();
	temp = cadena.split(caracter);
	return temp.length -1;
}

function checkSubmit(boton) {
    document.getElementById(boton).value = "Enviando...";
    document.getElementById(boton).disabled = true;
    return true;
}



function validar(){
	
 var isOK = false;
				if(checkemail()){
					isOK=true;
					/*isOK = validaCaptcha();
					if(!isOK){
						document.form1.captchacode = "";
						document.form1.captchacode.focus();
						document.form1.captchacode.selectionStart = 0;
						
					}*/
					checkSubmit('sndbtn');
			
				}
	return isOK;
						
}

function validaCaptcha(){
		code = document.getElementById('captchacode').value;
		


		var obj = document.getElementById('captcha_response_div');
		xmlhttp.open("GET", 'captval.php?code='+code);
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				if(xmlhttp.responseText == '0'){
					obj.innerHTML = "<img src='assets/warning_16x16.gif' /> C&oacute;digo Incorrecto. Por favor intente de nuevo";
					document.form1.code.value = 0;
					document.form1.captchacode.focus();
					//document.getElementById('sndbtn').disabled = true;
					//return false;
					
				}
				else if(xmlhttp.responseText == '1'){
					document.form1.code.value = 1;
					obj.innerHTML = "";
					document.getElementById('sndbtn').disabled = false;
					//return true;

				}
				
			}
			if(xmlhttp.readyState == 1){
				obj.innerHTML = "<img src='assets/icn_loading.gif' alt='Cargando' />";
			}

		}
	
	xmlhttp.send(null);



}

function msgbox(texto){
	alert(texto);
	return 0;	
}

function recaptcha(){
	document.getElementById('code').value = 0;
	document.getElementById('captcha').src = 'captcha/securimage_show.php?' + Math.random(); 
	return false
}