/*
Form field Limiter script- By Dynamic Drive
For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
This credit MUST stay intact for use
*/

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
if (window.event&&event.srcElement.value.length>=maxlength)
return false
else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
if (pressedkey.test(String.fromCharCode(e.which)))
e.stopPropagation()
}
}

function countlimit(maxlength,e,placeholder){
var theform=eval(placeholder)
var lengthleft=maxlength-theform.value.length
var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
if (window.event||e.target&&e.target==eval(placeholder)){
if (lengthleft<0)
theform.value=theform.value.substring(0,maxlength)
placeholderobj.innerHTML=lengthleft
}
}

function displaylimit(theform,thelimit){
var limit_text='<strong><span id="'+theform.toString()+'">'+thelimit+'</span></strong>'
if (document.all||ns6)
document.write(limit_text)
if (document.all){
eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
}
else if (ns6){
document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
}
}
//////////////////////////////
function dothis(obj,anterior)
{
	if(obj.value == ""){
	obj.value = anterior;
	}
}
//////////////////////////////
function checkCheckbox (f,name,require) {
var checked = 0;
var e;
var i = 0;
while (e = f.elements[i++]) {if (e.type == 'checkbox' && e.className == name && e.checked) checked++;}
if (checked < require) { 
//alert(name + " error"); 
}
return require <= checked

}

function validar(theForm){
// VALIDACION DEL NICK	
		
        if(theForm.nick.value == "") {
        alert("Indica tu título del anuncio.");
        theForm.nick.focus();
        return(false);
        }
		var largoNick = theForm.nick.value;
// el nick minimo 4 y maximo 16	
		if (largoNick.length < 4 || largoNick.length > 20){
		theForm.nick.focus();
		alert("Nombre del título del anuncio incorrecto.");
		return (false);
		}

// el nick bien escrito
		if(theForm.nick.value != "") {
			var parsed2 = true;
			var largo2 = theForm.nick.value;
			var validchars2 = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890áéíóúÁÉÍÓÚàèìpòùÀÈÌÒÙäëïöüÄËÏÖÜ .:-_";
				for (var z=0; z < largo2.length; z++) {
				var letter2 = largo2.charAt(z);
				if (validchars2.indexOf(letter2) != -1)
				continue;
				alert("Por favor, no escriba caracteres especiales en el campo Título del anuncio.");
				parsed2 = false;				
				theForm.nick.focus();
				return(false);
				break;
				
				}
		}		
// Desplegable Soy/somos			
        if(theForm.yo.value == 0 || theForm.yo.value == "null") {
        alert("Indica el estado del desplegable Soy/somos.");
        theForm.yo.focus();
        return(false);
        }
// Si selecciono pareja debe estar la fecha de nacimiento en pareja		
        if(theForm.yo.value == "Pareja") {
			if(theForm.f_date_d.value == "") {
			alert("Indicar la fecha de nacimiento de la pareja.");
			theForm.f_date_d.focus();
			return(false);
			}
        }
		
// Comprobacion checkbox Buscando		
		var estadobusco = checkCheckbox (theForm,"busco_",1)		
		if (estadobusco === false){
			alert("Debes marcar una opción de busqueda");
			return(false);
		}
// Desplegable provincias		
        if(theForm.provincias.value == "" || theForm.provincias.value == 0) {
        alert("Indique la provincia.");
        theForm.provincias.focus();
        return(false);
        }
// Valida fecha		
		if(theForm.f_date_c.value == "") {
        alert("Indicar la fecha de nacimiento.");
        theForm.f_date_c.focus();
        return(false);
        }
		if(theForm.email_1.value == "") {
		alert("Por favor, tu email.");
		theForm.email_1.focus();
		return(false);
		}
		
// Validacion email		
		if(theForm.email_1.value != "") {	
		var largoV = theForm.email_1.value;
			if (largoV.length < 6){
			theForm.email_1.focus();alert("Email incorrecto.");return (false);
			}else{
			var valor1v = "false";
			var valor2v = "false";
				for(x = 0;x<largoV.length;x++){
				var oneChar2v = largoV.charAt(x);
				if (oneChar2v == "@"){valor1v = "true";}
				if (oneChar2v == "."){valor2v = "true";}
				}
			if (valor1v == "true"){if(valor2v == "true"){}else{theForm.email_1.focus();alert("Email incorrecto.");return (false);}} else {theForm.email_1.focus();alert("Email incorrecto.");return (false);}
			}
		}		
// Validacion repetir email
		if(theForm.email_1.value != theForm.email_2.value) {
		alert("Por favor, tu email y repetir email no coinciden.");
		theForm.email_1.focus();
		return(false);
		}
// Validacion si marco alguna opción de contacto
//alert(theForm.email_3.checked);
		if( (theForm.wphone.value == "" || theForm.wphone.value == " " ) &&  (theForm.correo.value == "" || theForm.correo.value == " " ) &&  ( theForm.email_3.checked === false ) ) {
        alert("Debes indicar un método de contacto.");
        theForm.wphone.focus();
        return(false);
        }		
// Validacion si telefon es distinto de nada		
		if(theForm.wphone.value != "") {
			var parsed2 = true;
			var largo2 = theForm.wphone.value;
			if (largo2.length <9){alert("Es correcto el teléfono? \rEscriba sólo números.\rRecuerde escribir el prefijo.");theForm.wphone.focus();return(false);}
			var validchars2 = "0123456789 .+";
				for (var z=0; z < largo2.length; z++) {
				var letter2 = largo2.charAt(z);
				if (validchars2.indexOf(letter2) != -1)
				continue;
				alert("Por favor, escriba sólo números espacio en el campo Teléfono.");
				parsed2 = false;
				theForm.wphone.focus();
				return(false);
				break;
				}
		}
// Validacion está el archivo de Imagen	
		if(theForm.file_0.value == "") {
		alert("Por favor, la foto principal es obligatoria.");
		theForm.file_0.focus();
		return(false);
		}
	
		if(theForm.mathguard_answer.value == "" || theForm.mathguard_answer.value == " ") {
		alert("Por favor, responde a la sencilla suma de seguridad. Así evitamos spam y un mal uso de la web. Gracias.");
		theForm.mathguard_answer.focus();
		return(false);
		}
// Validacion politica de privacidad	
var politica = checkCheckbox (theForm,"politica",1)		
		if (politica === false){
			alert("Debes leer y aceptar la política de privacidad para registrarte");
			return(false);
		}
		
}// function validar



function validarSugerencias(theForm){
// VALIDACION DEL NICK	
		
        if(theForm.sugerencias_asunto.value == "") {
        alert("Debes indicar el asunto.");
        theForm.sugerencias_asunto.focus();
        return(false);
        }
        if(theForm.sugerencias_email.value == "") {
        alert("Debes indicar el email.");
        theForm.sugerencias_email.focus();
        return(false);
        }
// Validacion email		
		if(theForm.sugerencias_email.value != "") {	
		var largoV = theForm.sugerencias_email.value;
			if (largoV.length < 6){
			theForm.sugerencias_email.focus();alert("Email incorrecto.");return (false);
			}else{
			var valor1v = "false";
			var valor2v = "false";
				for(x = 0;x<largoV.length;x++){
				var oneChar2v = largoV.charAt(x);
				if (oneChar2v == "@"){valor1v = "true";}
				if (oneChar2v == "."){valor2v = "true";}
				}
			if (valor1v == "true"){if(valor2v == "true"){}else{theForm.sugerencias_email.focus();alert("Email incorrecto.");return (false);}} else {theForm.sugerencias_email.focus();alert("Email incorrecto.");return (false);}
			}
		}		
		
        if(theForm.sugerencias_mensaje.value == "") {
        alert("Debes indicar el mensaje.");
        theForm.sugerencias_mensaje.focus();
        return(false);
        }	
        if(theForm.sugerencias_destino.value == "null") {
        alert("Debes indicar el departamento.");
        theForm.sugerencias_destino.focus();
        return(false);
        }		
}



