function esDigito(sChr){
	var expreg = new RegExp ("^[1-9]\\d{0,50}$");
	return (expreg.test(sChr))
}

function esPrecio(sChr){
	var expreg = new RegExp ("^[1-9]+(\.?)+\\d{0,50}$");
	return (expreg.test(sChr))
}


function esAno(sChr){
	var expreg = new RegExp ("^[1-9]\\d{0,3}$");
	return (expreg.test(sChr))
}


function esMail(sChr){
	var expreg = new RegExp ("(^[0-9a-zA-Z]+(?:[._][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,3})$");
	return (expreg.test(sChr))
}



function esTexto(sChr){
	if (sChr.length<2)
	return false;
	else
	return true;
}

function esTexto2(sChr){
	if (sChr.length<1)
	return false;
	else
	return true;
}

function esPass(sChr){
	if (sChr.length<6)
	return false;
	else
	return true;
}


function openpop(URL,Name,W,H){
	var posx=(screen.width/2)-262;
	var posy=(screen.height/2)-182;
	window.open(URL,Name,'left='+posx+',top='+posy+',toolbar=no,location=no,status=no,directories=no,menubar=no,scrollbars=no,resizable=no,width='+W+',height='+H) 
}


//--------------------------------------------------------------------------------------------------------------------------------
function objetoAjax(){
	var xmlhttp=false;
    try {
    	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    	try {
        	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
        	xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	xmlhttp = new XMLHttpRequest();
	}
    return xmlhttp;
}

//Comprovar que el email te l'estructura correcta
function check_mail(texto){

    var mailres = true;            
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
    
    var arroba = texto.indexOf("@",0);
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
    
    var punto = texto.lastIndexOf(".");
                
     for (var contador = 0 ; contador < texto.length ; contador++){
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
            mailres = false;
            break;
     }
    }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
     mailres = true;
    else
     mailres = false;
                
    return mailres;
}

function datos_suscriptor(quin){
	divResultado = document.getElementById("datos");
	ajax=objetoAjax();
	ajax.open("GET", "datos_form.php?opcio="+quin);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divResultado.innerHTML = ajax.responseText;
		}
	}
	ajax.send(null);
}

function isEmailAddress(email){
	var s = email;
	var filter=/^[A-Za-z][-.\w]*[A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ){
		return true;
	}
	
	if (filter.test(s)){
		return true;
	}else{
		//alert("Ingrese una dirección de correo válida");
		return false;
	}
}

function registrar(){
	error="Se han encontrado los siguientes errores:\n\n";
	hihaerror=0;
	
	if(document.miform.pass.value==""){
		hihaerror=1;
		error=error+"- No has introducido una contraseña.\n";
	}else if(document.miform.pass.value!=document.miform.pass2.value){
		hihaerror=1;
		error=error+"- No has introducido la misma contraseña en los dos campos.\n";
	}
	
	if(document.miform.nombre.value=="" || document.miform.apellido1.value=="" || document.miform.apellido2.value==""){
		hihaerror=1;
		error=error+"- No has introducido el nombre i/o los apellidos.\n";
	}
	
	var i, suscriptor;
	suscriptor="";
	
    for (i=0;i<document.miform.suscriptor.length;i++){
       if (document.miform.suscriptor[i].checked){
          suscriptor=document.miform.suscriptor[i].value;
		  break;
	   }
    }
	
	//alert(suscriptor);
	if(suscriptor=="si"){
		if(document.miform.revista.value==""){
			hihaerror=1;
			error=error+"- No has seleccionado una revista.\n";	
		}
	}else if(suscriptor=="no"){
		if(document.miform.direccion.value==""){
			hihaerror=1;
			error=error+"- No has introducido una dirección.\n";	
		}
		if(document.miform.cp.value==""){
			hihaerror=1;
			error=error+"- No has introducido un código postal.\n";	
		}
		if(document.miform.poblacion.value==""){
			hihaerror=1;
			error=error+"- No has introducido una poblacion.\n";	
		}
		if(document.miform.provincia.value==""){
			hihaerror=1;
			error=error+"- No has seleccionado una provincia.\n";	
		}
		if(document.miform.pais.value==""){
			hihaerror=1;
			error=error+"- No has introducido un pais.\n";	
		}
		if(document.miform.telefono.value==""){
			hihaerror=1;
			error=error+"- No has introducido un teléfono.\n";	
		}
	}else{
		hihaerror=1;
		error=error+"- Debes escoger si eres o no un suscriptor.\n";	
	}
	
	if(document.miform.profesion.value==""){
		hihaerror=1;
		error=error+"- No has seleccionado una profesion.\n";
	}
	
	if(!document.miform.clausula.checked){
		hihaerror=1;
		error=error+"- Debes aceptar la clausula de privacidad.\n";
	}
	
	if(document.miform.email.value==""){
		hihaerror=1;
		error=error+"- No has introducido un E-mail.\n";
		
		alert(error);
	}else{
		if(isEmailAddress(document.miform.email.value)){
			//comprobar que no existeix.
			ajax=objetoAjax();
			ajax.open("GET", "comprova_mail.php?email="+document.miform.email.value);
			ajax.onreadystatechange=function() {
				if (ajax.readyState==4) {
					if(ajax.responseText=="no"){
						hihaerror=1;
						error=error+"- El E-mail introducido ya existe.\n";
						
						alert(error);
					}else{
						if(hihaerror==1){
							alert(error);	
						}else{
							ajax2=objetoAjax();
							ajax2.open("GET", "comprueba_captcha.php?codigo="+document.miform.tmptxt.value);
							ajax2.onreadystatechange=function() {
								if (ajax2.readyState==4){
									if(ajax2.responseText=="no"){
										alert("Debes introducir el código de letras y números.");
									}else{
										document.miform.submit();
									}
								}
							}
							ajax2.send(null);
							//alert("Enviamos el form");
						}
					}
				}
			}
			ajax.send(null);
		}else{
			hihaerror=1;
			error=error+"- El E-mail que has introducido no tiene un formato válido.\n";
			
			alert(error);
		}
	}
}

function recuperar(){
	var error;
	if(document.miform.email.value!=""){
		if(isEmailAddress(document.miform.email.value)){
			document.miform.submit();
		}else{
			error="- El E-mail que has introducido no tiene un formato válido.\n";			
			alert(error);
		}
	}else{
		error="- El E-mail que has introducido no tiene un formato válido.\n";			
		alert(error);
	}
}

function enviar_contacto(){
	error="Se han encontrado los siguientes errores:\n\n";
	hihaerror=0;
	
	if(document.miform.nombre.value==""){
		hihaerror=1;
		error=error+"- No has introducido un nombre.\n";	
	}
	if(document.miform.email.value==""){
		hihaerror=1;
		error=error+"- No has introducido un e-mail.\n";	
	}else{
		if(!isEmailAddress(document.miform.email.value)){
			hihaerror=1;
			error=error+"- El E-mail que has introducido no tiene un formato válido.\n";
		}
	}
	if(document.miform.tema.value==""){
		hihaerror=1;
		error=error+"- No has introducido un tema.\n";	
	}
	if(document.miform.comentarios.value==""){
		hihaerror=1;
		error=error+"- No has introducido un comentario.\n";	
	}
	
	if(hihaerror==1){
		alert(error);
	}else{
		ajax2=objetoAjax();
		ajax2.open("GET", "comprueba_captcha.php?codigo="+document.miform.tmptxt.value);
		ajax2.onreadystatechange=function() {
			if (ajax2.readyState==4){
				if(ajax2.responseText=="no"){
					alert("Debes introducir el código de letras y números.");
				}else{
					document.miform.submit();
				}
			}
		}
		ajax2.send(null);	
	}
}

function enviar_cv(){
	error="Se han encontrado los siguientes errores:\n\n";
	hihaerror=0;
	
	if(document.miform.nombre.value==""){
		hihaerror=1;
		error=error+"- No has introducido un nombre.\n";	
	}
	
	if(document.miform.email.value==""){
		hihaerror=1;
		error=error+"- No has introducido un e-mail.\n";
	}else{
		if(!isEmailAddress(document.miform.email.value)){
			hihaerror=1;
			error=error+"- El e-mail que has introducido no tiene un formato válido.\n";
		}
	}
	
	if(document.miform.comentarios.value==""){
		hihaerror=1;
		error=error+"- No has introducido por qué quieres trabajar con nosotros.\n";	
	}
	
	if(document.miform.cv.value==""){
		hihaerror=1;
		error=error+"- No has adjuntado tu curriculum.\n";	
	}else{
		var str,ext,total;
		str=document.miform.cv.value;
		total=str.length;
		inicio=total-3;
		ext=str.substr(inicio,total);
		if(ext!="pdf" && ext!="doc"){
			hihaerror=1;
			error=error+"- El formato del archivo no és válido..\n";
		}
		//alert(ext);
	}
	
	if(hihaerror==1){
		alert(error);
	}else{
		ajax2=objetoAjax();
		ajax2.open("GET", "comprueba_captcha.php?codigo="+document.miform.tmptxt.value);
		ajax2.onreadystatechange=function() {
			if (ajax2.readyState==4){
				if(ajax2.responseText=="no"){
					alert("Debes introducir el código de letras y números.");
				}else{
					document.miform.submit();
				}
			}
		}
		ajax2.send(null);
		//document.miform.submit();	
	}
}

function enviar_publicacion(){
	error="Se han encontrado los siguientes errores:\n\n";
	hihaerror=0;
	
	if(document.miform.nombre.value==""){
		hihaerror=1;
		error=error+"- No has introducido un nombre.\n";	
	}
	
	if(document.miform.email.value==""){
		hihaerror=1;
		error=error+"- No has introducido un e-mail.\n";
	}else{
		if(!isEmailAddress(document.miform.email.value)){
			hihaerror=1;
			error=error+"- El e-mail que has introducido no tiene un formato válido.\n";
		}
	}
	
	if(document.miform.comentarios.value==""){
		hihaerror=1;
		error=error+"- No has introducido una propuesta editorial.\n";	
	}
	
	if(document.miform.cv.value==""){
		hihaerror=1;
		error=error+"- No has adjuntado el índice o capítulo.\n";	
	}else{
		var str,ext,total;
		str=document.miform.cv.value;
		total=str.length;
		inicio=total-3;
		ext=str.substr(inicio,total);
		if(ext!="pdf" && ext!="doc"){
			hihaerror=1;
			error=error+"- El formato del archivo no és válido..\n";
		}
		//alert(ext);
	}
	
	if(hihaerror==1){
		alert(error);
	}else{
		ajax2=objetoAjax();
		ajax2.open("GET", "comprueba_captcha.php?codigo="+document.miform.tmptxt.value);
		ajax2.onreadystatechange=function() {
			if (ajax2.readyState==4){
				if(ajax2.responseText=="no"){
					alert("Debes introducir el código de letras y números.");
				}else{
					document.miform.submit();
				}
			}
		}
		ajax2.send(null);
		//document.miform.submit();	
	}
}

function logejarse(){
	if(document.login.user.value=="" || document.login.pwd.value==""){
		alert("Debes introducir usuario y contraseña.");
	}else{
		document.login.submit();
	}
}
