// JavaScript Document

function valida_envia (formulario)
{
	if (formulario.departamento.value == 0)
	{
       //alert ("Debe seleccionar un departamento");
       formulario.departamento.focus();
       formulario.departamento.style.backgroundColor="rgb(251, 208, 208)";
       return false;
    }
	else
	{
		formulario.departamento.style.backgroundColor="rgb(238, 238, 238)";
	}
	
	//valido el nombre
    if (formulario.nombre.value.length==0)
	{
       //alert ("El campo 'Nombre y apellidos' es obligatorio");
       formulario.nombre.focus();
       formulario.nombre.style.backgroundColor="rgb(251, 208, 208)";
       return false;
    }
	else
	{
		formulario.nombre.style.backgroundColor="rgb(238, 238, 238)";
	}
	
	if (!emailCheck (formulario.email.value))
	{
		formulario.email.focus();
		formulario.email.style.backgroundColor="rgb(251, 208, 208)";
	    return false;
	}
	else
	{
		formulario.email.style.backgroundColor="rgb(238, 238, 238)";
	}
	if (!formulario.acepto.checked)
	{
		alert("La casilla de conformidad con la pol"+String.fromCharCode(237)+"tica de tratamiento de datos debe estar marcada");
		formulario.acepto.focus();
		formulario.acepto.style.backgroundColor="rgb(251, 208, 208)";
	    return false;
	}
	else
	{
		formulario.acepto.style.backgroundColor="rgb(238, 238, 238)";
	}	
}


function emailCheck (emailStr) {
    var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    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í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;
}
