// JavaScript Document

function valida_form_contacto(form){
	
	if ( form.nombre ){
		if ( form.nombre.value == '' ){
			//alert ("El campo 'Nombre' no puede estar vacío");
			form.nombre.focus();
			form.nombre.style.backgroundColor="rgb(255, 255, 153)";
			return false;
		}
		else{
			form.nombre.style.backgroundColor="rgb(255, 255, 255)";
		}
	}
	if ( form.email1 ){
		if ( form.email1.value == "" ){
			//alert ("El campo 'Email' no puede estar vacío");
			form.email1.focus();
			form.email1.style.backgroundColor="rgb(255, 255, 153)";
			return false;
		}
		else if (!emailCheck (form.email1.value))
		{
			//alert ("El email no es correcto");
			form.email1.focus();
			form.email1.style.backgroundColor="rgb(255, 255, 153)";
			return false;	
		}
		else{
			form.email1.style.backgroundColor="rgb(255, 255, 255)";
		}
	}
	if ( form.email2 ){
		if ( form.email1.value != form.email2.value ){
			if ( form.email2.value == "" ){
				//alert ("Debes confimar el email " + form.email1.value);
				form.email2.focus();
				form.email2.style.backgroundColor="rgb(255, 255, 153)";
				return false;
			}
			else if ( form.email2.value != form.email1.value ){
				//alert ("El email de confirmaci"+String.fromCharCode(243)+"n no es correcto");
				form.email2.focus();
				form.email2.style.backgroundColor="rgb(255, 255, 153)";
				return false;
			}
			else{
				form.email2.style.backgroundColor="rgb(255, 255, 255)";
			}
		}
	}
	if ( form.asunto ){
		if ( form.asunto.value == '' ){
			//alert ("El campo 'Asunto' no puede estar vacío");
			form.asunto.focus();
			form.asunto.style.backgroundColor="rgb(255, 255, 153)";
			return false;
		}
		else{
			form.asunto.style.backgroundColor="rgb(255, 255, 255)";
		}
	}
	
	return true;
}

function valida_form_reserva(form){
	
	if ( form.nombre ){
		if ( form.nombre.value == '' ){
			//alert ("El campo 'Nombre' no puede estar vacío");
			form.nombre.focus();
			form.nombre.style.backgroundColor="rgb(255, 255, 153)";
			return false;
		}
		else{
			form.nombre.style.backgroundColor="rgb(255, 255, 255)";
		}
	}
	if ( form.email1 ){
		if ( form.email1.value == "" ){
			//alert ("El campo 'Email' no puede estar vacío");
			form.email1.focus();
			form.email1.style.backgroundColor="rgb(255, 255, 153)";
			return false;
		}
		else if (!emailCheck (form.email1.value))
		{
			//alert ("El email no es correcto");
			form.email1.focus();
			form.email1.style.backgroundColor="rgb(255, 255, 153)";
			return false;	
		}
		else{
			form.email1.style.backgroundColor="rgb(255, 255, 255)";
		}
	}
	if ( form.email2 ){
		if ( form.email1.value != form.email2.value ){
			if ( form.email2.value == "" ){
				//alert ("Debes confimar el email " + form.email1.value);
				form.email2.focus();
				form.email2.style.backgroundColor="rgb(255, 255, 153)";
				return false;
			}
			else if ( form.email2.value != form.email1.value ){
				//alert ("El email de confirmaci"+String.fromCharCode(243)+"n no es correcto");
				form.email2.focus();
				form.email2.style.backgroundColor="rgb(255, 255, 153)";
				return false;
			}
			else{
				form.email2.style.backgroundColor="rgb(255, 255, 255)";
			}
		}
	}
	if ( form.fecha ){
		if ( form.fecha.value == '' ){
			//alert ("El campo 'Fecha' no puede estar vacío");
			form.fecha.focus();
			form.fecha.style.backgroundColor="rgb(255, 255, 153)";
			return false;
		}
		else{
			form.fecha.style.backgroundColor="rgb(255, 255, 255)";
		}
	}
	if ( form.plazas ){
		if ( form.plazas.value == '' ){
			//alert ("El campo 'Número de plazas' no puede estar vacío");
			form.plazas.focus();
			form.plazas.style.backgroundColor="rgb(255, 255, 153)";
			return false;
		}
		else{
			form.plazas.style.backgroundColor="rgb(255, 255, 255)";
		}
	}
	
	return true;
}

function emailCheck (emailStr) {
    var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|es)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);
    
	if (matchArray==null) {
    	//alert("El campo 'Email' está vacio o es incorrecto");
    	return false;
    }
    
	var user=matchArray[1];
    var domain=matchArray[2];
    
	for (i=0; i<user.length; i++) {
    	if (user.charCodeAt(i)>127) {
    		//alert("El campo 'Email' contiene caracteres no validos");
    		return false;
       }
    }
	
    for (i=0; i<domain.length; i++) {
    	if (domain.charCodeAt(i)>127) {
    		//alert("El campo 'Email' contiene caracteres no validos");
    		return false;
       }
    }

	if (user.match(userPat)==null) {
    	//alert("El campo 'Email' es incorrecta");
    	return false;
    }
    
	var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
    	for (var i=1;i<=4;i++) {
    		if (IPArray[i]>255) {
    			//alert("La dirección IPInternet Protocol de destino no es correcta!");
    			return false;
       		}
    	}
    	return true;
    }
	
    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) {
    	if (domArr[i].search(atomPat)==-1) {
    		//alert("El campo 'Email' es incorrecto, por favor compruebalo, incluyendo el uso incorrecto de signos de puntuación, comas , o puntos [.] al final de la dirección.");
    		return false;
       	}
    }
	
    if (checkTLD && domArr[domArr.length-1].length!=2 &&
    domArr[domArr.length-1].search(knownDomsPat)==-1) {
    	//alert("El campo 'Email' debe terminar en un dominio o dos letras " + "pa"+String.fromCharCode(237)+"s.");
    	return false;
    }
    
	if (len<2) {
    	//alert("Falta el nombre del host en tu dirección de correo - compruebalo. O debes de haber añadido un espacio en blanco al final de la dirección - corrije el error e intentalo de nuevo.");
    	return false;
    }
    
	return true;
}
