// Funciones para la validacion del formulario del buscador
function validarPuesto(formulario,campo)
{
	var texto=formulario[campo].value;
	var prov=formulario["provincia"].value;
	if(texto!=null && texto!="")
		return true;
	else if(prov!=null && prov!="" && prov!="0") {
		formulario[campo].value = "*";
		return true;	}
	else {
		alert("Debe completar puesto buscado");
		return false;
	}
}

function validarProvincia(formulario,campo)
{
	var texto=formulario[campo].value;
	if(texto!=null && texto!="" && texto!="0")
		return true;
	else
		{
			alert("Debe seleccionar una provincia de busqueda");
			return false;
		}
}

function existeEmail(formulario,campo)
{
	var texto=formulario[campo].value;
	if(texto!=null && texto!="")
		return true;
	else
		{
			return false;
		}
}

function emailCheck (emailStr) {
	/* Verificar si el email tiene el formato user@dominio. */
	var emailPat=/^(.+)@(.+)$/ 
	
	/* Verificar la existencia de caracteres. ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]" 
	
	/* Verifica los caracteres que son válidos en una dirección de email */
	var validChars="\[^\\s" + specialChars + "\]" 
	
	var quotedUser="(\"[^\"]*\")" 
	
	/* Verifica si la dirección de email está representada con una dirección IP Válida */ 
	
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	
	
	/* Verificar caracteres inválidos */ 
	
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	alert("La direccion de email es incorrecta")
	return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// Si el user "user" es valido 
	if (user.match(userPat)==null) {
	// Si no
	alert("La direccion de email es incorrecta")
	return false
	}
	
	/* Si la dirección IP es válida */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	alert("La direccion de email es incorrecta")
	return false
	}
	}
	return true
	}
	
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	alert("La direccion de email es incorrecta")
	return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	domArr[domArr.length-1].length>3) { 
	
	alert("La direccion de email es incorrecta")
	return false
	}
	
	if (len<2) {
	var errStr="La direccion de email es incorrecta"
	alert(errStr)
	return false
	}
	
	// La dirección de email ingresada es Válida
	return true;
}


function validacionFormulario(formulario)
{
	if(!validarPuesto(formulario,"puesto") || !validarProvincia(formulario,"provincia"))
	 	return false;
	//else if(existeEmail(formulario,"email") && !emailCheck (formulario["email"].value))
	//	return false;
	else
		return true;
	
}

function validacionFormularioConEmail(formulario)
{
	if(!validarPuesto(formulario,"puesto") || !validarProvincia(formulario,"provincia"))
	 	return false;
	else if(!existeEmail(formulario,"email")) {
		alert("Debe introducir su email para poder guardar su busqueda")
		return false;
	}
	else if(!emailCheck (formulario["email"].value))
		return false;
	else
		return true;
	
}

function procesarFormulario()
{
	var objf=document.forms[0];
		
	if(validacionFormulario(objf))
		objf.submit();	
}

function procesarFormularioConEmail()
{
	var objf=document.forms[0];
		
	if(validacionFormularioConEmail(objf))
		objf.submit();	
}

// Funciones para la validacion de formulario de Contacto
function validarComentarios(formulario,campo)
{
	var texto=formulario[campo].value;
	if(texto!=null && texto!="")
		return true;
	else
		{
			alert("Por favor introduzca sus comentarios");
			return false;
		}
}

function validacionFormularioContacto(formulario)
{
	if(!existeEmail(formulario,"emailContacto")) {
		alert("Debe introducir su email para que podamos contestarle a sus comentarios")
		return false;
	}
	else if (!validarComentarios(formulario,"comentarios"))
	 	return false;
	else if(!emailCheck (formulario["emailContacto"].value))
		return false;
	else
		return true;
	
}

function procesarFormularioContacto()
{
	var objf=document.forms[0];
		
	if(validacionFormularioContacto(objf))
		objf.submit();	
}
