// ------------------------------------------------------------------------
// Inicio FUNCIONES GENERALES
// ------------------------------------------------------------------------

//
// Función general que valida un login
//

function validateLogin(l){
	var res;
	reg = /[^-_a-zA-Z0-9]/;
	return !reg.test(l); // TRUE si es correcto, FALSE en caso contrario
	
}


function checkPassword(p){
	var error = document.getElementById('error_pwd');
	if (p.value.length<5 || p.value.length>12){
		if (error)
		{
			error.innerHTML = _JS_TEXTOS['REGISTRO_PASSWORD_INCORRECTO'];
		}
		else {
	    	alert(_JS_TEXTOS['REGISTRO_PASSWORD_INCORRECTO']); 
		}
		p.value="";
		return false;
	}
	else{
		reg = /[^-_a-zA-Z0-9]/;
		res=reg.test(p);
		if(!res){
		  if (error)
		  {
		    error.innerHTML = _JS_TEXTOS['REGISTRO_PASSWORD_INCORRECTO_FORMATO'];
		  }
		  else {
		  	alert(_JS_TEXTOS['REGISTRO_PASSWORD_INCORRECTO_FORMATO']); 
		  }
		  p.value="";
		  return false;
		}else{
			if (error)
			{
				error.innerHTML = '';
			}
			return true;
		}
	}
}

function checkRespassword(p,r){
	var repwdError = document.getElementById('error_repwd');
	if (p.value != r.value){
	  if (repwdError)
	  {
	  	repwdError.innerHTML = _JS_TEXTOS['REGISTRO_PASSWORDS_DISTINTOS'];
	  }
	  else {
	  	alert(_JS_TEXTOS['REGISTRO_PASSWORDS_DISTINTOS']); 
	  }
	  r.value="";
	  p.value="";
	  return false;
	}else{
		if (repwdError) {
		  repwdError.innerHTML = '';
		}
		return true;
	}
}




//
// Función general que valida un codigo
//

function validateCode(c){
	reg = /[^0-9]/;
	return !reg.test(c); // TRUE si es correcto, FALSE en caso contrario
	
}

//
// Función general que valida un teléfono
//

function validateTelephone(t,country){
	country = country || 'es';
	var handlers = { es:  function(t) { 
		var re = /^6[0-9]{7}[0-9]$/;
		return re.test(t);
	}, ar: function(t) {
		var re = /[^0-9]/;
		return !re.test(t) && t.length == 10;
	}, pt: function(t) {
		var re = /^9[0-9]{7}[0-9]$/;
		return re.test(t);
	}, mx: function(t) {
		var re = /[^0-9]/;
		return !re.test(t) && t.length == 10;
	}
	};
	if (!(country in handlers))
	{
		country = 'es';
	}
	return handlers[country](t);
}


//
// Función general que valida un email
//

function validateEmail(e){
	
	reg = /^[-_.a-zA-Z0-9]+@[-_.a-zA-Z0-9]+\.[a-zA-Z]+(\.[a-zA-Z]+)*$/
	return reg.test(e); // TRUE si es correcto, FALSE en caso contrario
	
}

//
// Función general que valida un antispam
//

function validateAntiSpam(a, k){
	
	if (a == k){
		return true;
	} else {
		return false;
	}
	
}

// ------------------------------------------------------------------------
// Fin FUNCIONES GENERALES
// ------------------------------------------------------------------------


// ------------------------------------------------------------------------
// Inicio FUNCIONES SECCION CONTACTOS
// ------------------------------------------------------------------------

//
//	Funcion de la sección CONTACTOS que muestra u oculta los TR no necesarios en ese momento
//

function hiddeContactsTR(opcion){
	
	TR_Incidencia = document.getElementById('ID_TR_Incidencia');
	TR_Sugerencia = document.getElementById('ID_TR_Sugerencia');
	
	idMotivo      = document.getElementById('idTextoMotivo');

	if (opcion.value == 'inc'){
		TR_Incidencia.style.display = '';
		TR_Sugerencia.style.display = 'none';
		idMotivo.value = opcion.options[0].text;
	} else if (opcion.value == 'sug'){
		TR_Incidencia.style.display = 'none';
		TR_Sugerencia.style.display = '';
		idMotivo.value = opcion.options[1].text;
	} else {
		TR_Incidencia.style.display = 'none';
		TR_Sugerencia.style.display = 'none';
		idMotivo.value = opcion.options[2].text;
	}

}


//
// Función que valida los campos del formulario CONTACTOS
//

function validateContactForm(f){
		
	// Validación relleno de todos los campos

	if (!f.login.value.length || !f.email.value.length || !f.comentarios.value.length){ // Comprobación de campos vacios
		
		alert(_JS_TEXTOS['CONTACTA_CAMPO_VACIO']);
		return false;
		
	}
	
	// Validación del login
	
	if (!validateLogin(f.login.value)){
		
		alert(_JS_TEXTOS['CONTACTA_LOGIN_INCORRECTO']);
		document.getElementById('contacto_login_error').style.display = '';
		document.getElementById('contacto_span_login_error').style.visibility = 'visible';
		
		f.login.focus();
		
		return false;
		
	} else {
		
		document.getElementById('contacto_login_error').style.display = 'none';
		document.getElementById('contacto_span_login_error').style.visibility = 'hidden';
	}
	
	// Validación de email
		
	if (!validateEmail(f.email.value)){
		
		alert(_JS_TEXTOS['CONTACTA_EMAIL_INCORRECTO']);
		document.getElementById('contacto_email_error').style.display = '';
		document.getElementById('contacto_span_email_error').style.visibility = 'visible';
		
		f.email.focus();
		
		return false;
		
	} else {
		
		document.getElementById('contacto_email_error').style.display = 'none';
		document.getElementById('contacto_span_email_error').style.visibility = 'hidden';
	}
	
	// Validación de teléfono
	
	if (!validateTelephone(f.telefono.value)){ // Comprobación de espacios en blanco
		
		alert(_JS_TEXTOS['CONTACTA_TELEFONO_INCORRECTO']);
		document.getElementById('contacto_telefono_error').style.display = '';
		document.getElementById('contacto_span_telefono_error').style.visibility = 'visible';
		
		f.telefono.focus();
		
		return false;
		
	} else {
		
		document.getElementById('contacto_telefono_error').style.display = 'none';
		document.getElementById('contacto_span_telefono_error').style.visibility = 'hidden';
		
	}
		
	document.getElementById('contacto_boton').style.display    = 'none';
	document.getElementById('contacto_enviando').style.display = '';
	
	return true;
}

// ------------------------------------------------------------------------
// Fin FUNCIONES SECCION CONTACTOS
// ------------------------------------------------------------------------


// ------------------------------------------------------------------------
// Inicio FUNCIONES SECCION REGISTRO
// ------------------------------------------------------------------------

//
// Función que valida los campos del formulario REGISTRO
//

function validateRegisterForm(f){
	// Validación relleno de todos los campos

	if (!f.nombre.value.length || !f.apellidos.value.length || !f.login.value.length || !f.password.value.length || !f.repassword.value.length){ // Comprobación de campos vacios
		
		alert(_JS_TEXTOS['REGISTRO_CAMPO_VACIO']);
		return false;
		
	}
	

	// Validación del password y repetición de password
	
	if (f.password.value != f.repassword.value || f.password.value.length<5){
		
		if (f.password.value != f.repassword.value){
			alert(_JS_TEXTOS['REGISTRO_PASSWORDS_DISTINTOS']);
		} else {
			alert(_JS_TEXTOS['REGISTRO_PASSWORD_INCORRECTO']);
		}
		document.getElementById('registro_password_error').style.display = '';
		document.getElementById('registro_span_password_error').style.visibility = 'visible';
		document.getElementById('registro_span_repassword_error').style.visibility = 'visible';
		f.password.value = '';
		f.repassword.value = '';
		f.password.focus();
		
		return false;
		
	} else {
		
		document.getElementById('registro_password_error').style.display = 'none';
		document.getElementById('registro_span_password_error').style.visibility = 'hidden';
		document.getElementById('registro_span_repassword_error').style.visibility = 'hidden';
		
	}
	
	// Validación de email
		
	if (f.email.value){
		if (!validateEmail(f.email.value)){
			
			alert(_JS_TEXTOS['REGISTRO_EMAIL_INCORRECTO']);
			document.getElementById('registro_email_error').style.display = '';
			document.getElementById('registro_span_email_error').style.visibility = 'visible';
			
			f.email.focus();
			
			return false;
			
		} else {
			
			document.getElementById('registro_email_error').style.display = 'none';
			document.getElementById('registro_span_email_error').style.visibility = 'hidden';
		}
	}
		
	// Validación de antispam
			
	if (!validateAntiSpam(f.antispam.value, f.antispamkeys.value)){
		
		alert(_JS_TEXTOS['REGISTRO_ANTISPAM_INCORRECTO']);
		document.getElementById('registro_antispam_error').style.display = '';
		document.getElementById('registro_span_antispam_error').style.visibility = 'visible';
		
		f.antispam.focus();
		
		return false;
		
	} else {
		
		document.getElementById('registro_antispam_error').style.display = 'none';
		document.getElementById('registro_span_antispam_error').style.visibility = 'hidden';
	}
	
	// Validación del login
	
	if (!checkRegisterLogin(f.login.value, 0, 1)){
		return false;
	}

	return true;
}


//
// Función que valida los campos del formulario REGISTRO
//

function validateRegisterActualizar(f){
	if (!f.email.value.length || !f.telefono.value.length || !f.password.value.length || !f.repassword.value.length){ // Comprobación de campos vacios		
		alert(_JS_TEXTOS['REGISTRO_CAMPO_VACIO']);
		return false;		
	}
	
	// Validación del password y repetición de password
	if (f.password.value != f.repassword.value || f.password.value.length<5){
		
		if (f.password.value != f.repassword.value){
			alert(_JS_TEXTOS['REGISTRO_PASSWORDS_DISTINTOS']);
		} else {
			alert(_JS_TEXTOS['REGISTRO_PASSWORD_INCORRECTO']);
		}
		f.password.value = '';
		f.repassword.value = '';
		f.password.focus();
		return false;
	}
	
	if (f.email.value){
		if (!validateEmail(f.email.value)){
			alert(_JS_TEXTOS['REGISTRO_EMAIL_INCORRECTO']);
			f.email.focus();
			return false;
		}
	}
		
	if (!validateTelephone(f.telefono.value)){ 
	  alert(_JS_TEXTOS['REGISTRO_TELEFONO_INCORRECTO']);
	  f.telefono.focus();
	  return false;
	}

	return true;
}


function radioIsChecked(radio)
{
	var anyChecked = false;
	for(var i = 0; i < radio.length; i++)
	{
		if ( radio[i].checked)
		{
			anyChecked = true;
		}
	}
	return anyChecked;
}


function checkCode(code) {
	var re = /[0-9]$/;
	var ret = code.length == 4 && re.test(code);
	if (!ret)
	{
		alert("No es el formato de codigo correcto");
	}
	return ret;
}

function checkRegisterCodeForm()
{
	var frm = document.registro;
	if (checkCode(document.getElementById('f-codigo').value))
	{
		document.registro.submit();
	}
}

function checkRegisterLoginWithImg(login, showAlert, enviar,imgId){
	var error = document.getElementById('error_login');
	if (login==''){
		if (error) {
			error.innerHTML = _JS_TEXTOS['REGISTRO_LOGIN_VACIO'];
		}
		if (imgId)
		{
			document.getElementById(imgId).src = '/img/v3/public/delete.png';
		}
		return false;
	}
	else {

		if (!validateLogin(login)){
			if (error) {
				error.innerHTML = _JS_TEXTOS['REGISTRO_LOGIN_F_INCORRECTO'];
			}
			if (imgId)
			{
				document.getElementById(imgId).src = '/img/v3/public/delete.png';
			}
			return false;
		}

		var options = {
			url: '/sections/register/checkLogins.php',
			queryVars: {login: login},
			method: 'POST',
			showLoading: true,
			loadingImg: '/img/v2/public/ajax_loader.gif',
			async: false,
			handler: showIsRegisteredWithImg,
			handlerVars: [showAlert, enviar, imgId],
			responseType: 'text'
		};

		ajaxSendRequest(options);

	}
}

function showIsRegisteredWithImg(response, showAlert, enviar,imgId){
	var error = document.getElementById('error_login');
	if (response == 'KO1'){
		error.innerHTML = _JS_TEXTOS['REGISTRO_LOGIN_F_INCORRECTO'];
		document.getElementById('f-login').value = '';
		if (imgId)
		{
			document.getElementById(imgId).src = '/img/v3/public/delete.png';
		}
		setTimeout("document.getElementById('f-login').focus()",75);
	} else if(response == 'KO2'){
		error.innerHTML = _JS_TEXTOS['REGISTRO_LOGIN_CORTO'];
		document.getElementById('f-login').value = '';
		if (imgId)
		{
			document.getElementById(imgId).src = '/img/v3/public/delete.png';
		}
		setTimeout("document.getElementById('f-login').focus()",75);
	} else {
		if (response){
			error.innerHTML = _JS_TEXTOS['REGISTRO_LOGIN_EXISTENTE'];
			document.getElementById('f-login').value = '';
			if (imgId)
			{
				document.getElementById(imgId).src = '/img/v3/public/delete.png';
			}
			setTimeout("document.getElementById('f-login').focus()",75);
		} else {
			if (showAlert){
				alert(_JS_TEXTOS['REGISTRO_LOGIN_NO_EXISTENTE']);
			}
			if (imgId)
			{
				document.getElementById(imgId).src = '/img/v3/public/checked.png';

			}
			error.innerHTML = '';
			if (enviar){
				parent.document.getElementById('registro_boton').style.display  = 'none';
				parent.document.getElementById('registro_enviando').style.display = '';
				parent.document.getElementById('idenviar').value='submit';
				parent.document.registro.submit();
			}
		}
	}
}

function checkPasswordWithImg(pwd,imgId)
{
	if (!checkPassword(pwd))
	{
		document.getElementById(imgId).src = '/img/v3/public/delete.png';
	}
	else
	{
		document.getElementById(imgId).src = '/img/v3/public/checked.png';
	}
}

function checkRepasswordWithImg(pwd,repwd,pwdImgId,repwdImgId)
{
	if (!checkRespassword(pwd,repwd))
	{
		document.getElementById(repwdImgId).src = '/img/v3/public/delete.png';
		document.getElementById(pwdImgId).src = '/img/v3/public/delete.png';
	}
	else
	{
		document.getElementById(repwdImgId).src = '/img/v3/public/checked.png';
		document.getElementById(pwdImgId).src = '/img/v3/public/checked.png';
	}
}

//
// Función que valida los campos del formulario REGISTRO
//

function validateRegisterForm2(f,country){
	// Validación relleno de todos los campos
	var isValide = true;
	
	if (!f.login.value.length) {
		document.getElementById('error_login').innerHTML = _JS_TEXTOS['REGISTRO_LOGIN_VACIO'];
		document.getElementById('imagen_login').src = '/img/v3/public/delete.png';
		isValide = false;	
	}

	if (!f.password.value.length ){ // Comprobación de campos vacios		
		document.getElementById('error_pwd').innerHTML = _JS_TEXTOS['REGISTRO_CAMPO_VACIO'];
		document.getElementById('imagen_pwd').src = '/img/v3/public/delete.png';
		isValide = false;
	}
	
	if(!f.repassword.value.length)
	{
		document.getElementById('error_repwd').innerHTML = _JS_TEXTOS['REGISTRO_CAMPO_VACIO'];
		document.getElementById('imagen_repwd').src = '/img/v3/public/delete.png';
		isValide = false;
	}
	
	if (country == 'ar')
	{
		f.telefono.value = f.telefono1.value + f.telefono2.value;
	}
	else {
		f.telefono.value = f.telefono.value;
	}
	
	checkRegisterMobile(f.telefono.value,0,0,'imagen_tlf',country);
	if (document.getElementById('imagen_tlf').src.indexOf('delete') >= 0)
	{
		isValide = false;
	}
	
	// Validación del password y repetición de password
	if (f.password.value != f.repassword.value || f.password.value.length<5){
		
		if (f.password.value != f.repassword.value){
			 document.getElementById('error_repwd').innerHTML = _JS_TEXTOS['REGISTRO_PASSWORDS_DISTINTOS']; 
		
		} else {
			 document.getElementById('error_pwd').innerHTML =_JS_TEXTOS['REGISTRO_PASSWORD_INCORRECTO']; 
		}
		document.getElementById('imagen_pwd').src = '/img/v3/public/delete.png';
		document.getElementById('imagen_repwd').src = '/img/v3/public/delete.png';
		isValide = false;		
		f.password.value = '';
		f.repassword.value = '';
		f.password.focus();
	}
	
	if (!validateEmail(f.email.value))
	{  
		document.getElementById('error_email').innerHTML = _JS_TEXTOS['REGISTRO_EMAIL_INCORRECTO']; 
	    f.email.focus();
	    isValide = false;
	}
	
	var radioError = document.getElementById('error_sexo');
	if (!radioIsChecked(f.sexo)) {
		var msg = "Debe selecccionar su sexo";
		if (radioError) {
			radioError.innerHTML = msg;
		}
		else {
			alert(msg);
		}
		isValide = false;
	}
	else {
		radioError.innerHTML = '';		
	}
		// Validación del login
	if (checkRegisterLoginWithImg(f.login.value, 0, 0,'imagen_login')){
		isValide = false;
	}
	
	var condicionesCheck = document.getElementById('f-condiciones');
	
	if (condicionesCheck && !condicionesCheck.checked)
	{
		var msg = "Para continuar con el registro debe aceptar los términos y condiciones.";
		if (isValide) {
			alert(msg);
			isValide = false;
		}
	}

	return isValide;
}

//
// Función que comprueba si el LOGIN es único
//

function checkRegisterLogin(login, showAlert, enviar,imgId){
	
	if (login==''){
		//alert(_JS_TEXTOS['REGISTRO_LOGIN_VACIO']);
	} else {
		
		if (!validateLogin(login)){
		
			//alert(_JS_TEXTOS['REGISTRO_LOGIN_F_INCORRECTO']);
			document.getElementById('registro_login_error').style.display = '';
			//document.getElementById('registro_span_login_error').style.visibility = 'visible';
			
			return false;
		}

		var myHandlerVars = [showAlert, enviar];
		if (imgId)
		{
			myHandlerVars.push(imgId);
		}
		
		var options = {
			url: '/sections/register/checkLogins.php',
			queryVars: {login: login},
			method: 'POST',
			showLoading: true,
			loadingImg: '/img/v2/public/ajax_loader.gif',
			async: false,
			handler: showIsRegistered,
			handlerVars: myHandlerVars ,
			responseType: 'text'
		};
			
		ajaxSendRequest(options);
						
	}
}


//
// Función que comprueba si el MOVIL es único
//

function checkRegisterMobile(mobile, showAlert, enviar,imgId,country)
{
	var error = document.getElementById('error_tlf');	
	if (mobile==''){
		if (error) {
			error.innerHTML = _JS_TEXTOS['REGISTRO_TELEFONO_VACIO'];
		}
		else {
			alert(_JS_TEXTOS['REGISTRO_TELEFONO_VACIO']);
		}
		document.getElementById(imgId).src = '/img/v3/public/delete.png';
		return false;
	} else {
		if (!validateTelephone(mobile,country)) {
			if (error) {
				error.innerHTML = _JS_TEXTOS['REGISTRO_MOVIL_F_INCORRECTO'];
			}
			else {
				alert(_JS_TEXTOS['REGISTRO_MOVIL_F_INCORRECTO']); 
			}
			document.getElementById(imgId).src = '/img/v3/public/delete.png';
			return false;
		}

		var myHandlerVars = [showAlert, enviar];
		if (imgId)
			myHandlerVars.push(imgId);
		
		var options = {
			url: '/sections/register/checkMobile.php',
			queryVars: {movil: mobile},
			method: 'POST',
			showLoading: true,
			loadingImg: '/img/v2/public/ajax_loader.gif',
			async: false,
			handler: showIsRegisteredMobile,
			handlerVars: myHandlerVars,
			responseType: 'text'
		};
			
		ajaxSendRequest(options);
						
	}
}

function checkRegisterMobileUpdate(mobile, showAlert, enviar, iduser, pais){
	
	if (mobile==''){
		alert(_JS_TEXTOS['REGISTRO_TELEFONO_VACIO']);
		document.images["imagen_mobile"].src = "/img/v3/public/delete.png";
	} else {
		
		if (!validateTelephone(mobile)){
			alert(_JS_TEXTOS['REGISTRO_MOVIL_F_INCORRECTO']);
			document.images["imagen_mobile"].src = "/img/v3/public/delete.png";
			return false;
		}
		
		
		
		

		var options = {
			url: '/sections/register/checkMobile.php',
			queryVars: {movil: mobile, iduser: iduser, pais: pais},
			method: 'POST',
			showLoading: true,
			loadingImg: '/img/v2/public/ajax_loader.gif',
			async: false,
			handler: showIsRegisteredMobile,
			handlerVars: [showAlert, enviar],
			responseType: 'text'
		};
			
		ajaxSendRequest(options);			
	}
}


//
// Función que comprueba si el EMAIL es único
//

function checkRegisterEmail(email, showAlert, enviar,imgId){
	
	var error = document.getElementById('error_email');
	
	if (email){
		if (!validateEmail(email)){
			if (error) {
				error.innerHTML = _JS_TEXTOS['REGISTRO_EMAIL_F_INCORRECTO'];
			}
			else {
				alert(_JS_TEXTOS['REGISTRO_EMAIL_F_INCORRECTO']);
			}
			if (imgId)
			{	
				document.getElementById('imagen_email').src = '/img/v3/public/delete.png';			
			}
			return false;
			
		} 		
		
		var myHandlerVars = [showAlert, enviar];
		if (imgId)
		{
			myHandlerVars.push(imgId);
		}
		
		var options = {
			url: '/sections/register/checkEmail.php',
			queryVars: {email: email},
			method: 'POST',
			showLoading: true,
			loadingImg: '/img/v2/public/ajax_loader.gif',
			async: false,
			handler: showIsRegisteredEmail,
			handlerVars: myHandlerVars,
			responseType: 'text'
		};
			
		ajaxSendRequest(options);				
	}
}

function checkUpdateEmail(email,login, showAlert, enviar){
	if (email){
		if (!validateEmail(email)){
		
			alert(_JS_TEXTOS['REGISTRO_EMAIL_F_INCORRECTO']);	
			document.images["imagen_email"].src = "/img/v3/public/delete.png";					
			return false;
			
		} 		
		var options = {
			url: '/sections/register/checkEmail.php',
			queryVars: {email: email, login: login},
			method: 'POST',
			showLoading: true,
			loadingImg: '/img/v2/public/ajax_loader.gif',
			async: false,
			handler: showIsRegisteredEmailImage,
			handlerVars: [showAlert, enviar],
			responseType: 'text'
		};
			
		ajaxSendRequest(options);				
	}
}



function showIsRegistered(response, showAlert, enviar,imgId){

	if (response == 'KO1'){
		 alert(_JS_TEXTOS['REGISTRO_LOGIN_F_INCORRECTO']); 
		document.getElementById('f-login').value = '';
		if (imgId)
		{
			document.getElementById(imgId).src = '/img/v3/public/delete.png';
		}
		setTimeout("document.getElementById('f-login').focus()",75);  //esto hay que hacerlo así pq hay un bug con firefox y netscape sino no haria falta el setTimeout
	} else if(response == 'KO2'){
		 alert(_JS_TEXTOS['REGISTRO_LOGIN_CORTO']); 
		document.getElementById('f-login').value = '';
		if (imgId)
		{
			document.getElementById(imgId).src = '/img/v3/public/delete.png';
		}
		setTimeout("document.getElementById('f-login').focus()",75);  //esto hay que hacerlo así pq hay un bug con firefox y netscape sino no haria falta el setTimeout
	} else {
		if (response){
			 alert(_JS_TEXTOS['REGISTRO_LOGIN_EXISTENTE']); 
			document.getElementById('f-login').value = '';			
			setTimeout("document.getElementById('f-login').focus()",75); 		
		} else {
			if (showAlert){
				 alert(_JS_TEXTOS['REGISTRO_LOGIN_NO_EXISTENTE']); 
			}
			if (imgId)
			{
				document.getElementById(imgId).src = '/img/v3/public/checked.png';
			}
			if (enviar){
				parent.document.getElementById('registro_boton').style.display  = 'none';
				parent.document.getElementById('registro_enviando').style.display = '';
				parent.document.getElementById('idenviar').value='submit';
				parent.document.registro.submit();
			}
		}
	}
}

function showIsRegisteredEmailImage(response, showAlert, enviar){
	if (response!=0){
		parent.document.getElementById('f-email').value="";		
		alert(_JS_TEXTOS['REGISTRO_EMAIL_EXISTENTE']);
		setTimeout("document.getElementById('f-email').focus()",75);  //idem que en la función de arriba 
		
	} else {
		if (showAlert){
			alert(_JS_TEXTOS['REGISTRO_EMAIL_NO_EXISTENTE']);
		}
		document.images["imagen_email"].src = "/img/v3/public/checked.png";
	}
}

function showIsRegisteredEmail(response, showAlert, enviar,imgId){
	var error = document.getElementById('error_email');
	if (response!=0){
		parent.document.getElementById('f-email').value="";		
		if (error) {
			error.innerHTML = _JS_TEXTOS['REGISTRO_EMAIL_EXISTENTE'];
		}
		else {
			alert(_JS_TEXTOS['REGISTRO_EMAIL_EXISTENTE']); 	
		}
		setTimeout("document.getElementById('f-email').focus()",75);  //idem que en la función de arriba 
		if (imgId)
			document.getElementById(imgId).src = '/img/v3/public/delete.png';
	} else {
		if (showAlert){
			 alert(_JS_TEXTOS['REGISTRO_EMAIL_NO_EXISTENTE']); 
		}
		if (imgId) {
			document.getElementById(imgId).src = '/img/v3/public/checked.png';
		}
		if (error) {
			error.innerHTML = '';
		}
	}
}

function showIsRegisteredMobile(response, showAlert, enviar,imgId){
	var error = document.getElementById('error_tlf');
	if (response!=0){
		parent.document.getElementById('f-telefono').value="";		
		if (error) {
		  error.innerHTML = _JS_TEXTOS['REGISTRO_MOVIL_EXISTENTE'];
		}else {
		  alert(_JS_TEXTOS['REGISTRO_MOVIL_EXISTENTE']); 
		}
		if (imgId)
		{
			document.getElementById(imgId).src = '/img/v3/public/delete.png';
		}
		setTimeout("document.getElementById('f-telefono').focus()",75);  //idem que en la función de arriba 
		
	} else {
		if (showAlert){
			alert(_JS_TEXTOS['REGISTRO_MOVIL_NO_EXISTENTE']); 
		}
		if (error)
		{
			error.innerHTML = '';
		}
		if (imgId) {
			document.getElementById('imagen_tlf').src = '/img/v3/public/checked.png';
		}
	}
}


// ------------------------------------------------------------------------
// Fin FUNCIONES SECCION REGISTRO
// ------------------------------------------------------------------------



// ------------------------------------------------------------------------
// Inicio FUNCIONES SECCION PROFILE
// ------------------------------------------------------------------------

	//
	// Función que valida el campos puntos del desafio
	//
	
	function validateDesafioForm(p, tus, sus,tus_cre,sus_cre,coste){
				
		if (p.value == '' || p.value==0) {
			alert(_JS_TEXTOS['DESAFIOS_ERROR_PUNTOS_VACIO']);
			p.value = '';
			p.focus();
			return false;
		}
		
		reg = /[^0-9]/;
		if (reg.test(p.value)) {
			alert(_JS_TEXTOS['DESAFIOS_ERROR_PUNTOS']);
			p.value = '';
			p.focus();
			return false;
		}
		
		if (p.value>tus){
			alert(_JS_TEXTOS['DESAFIOS_PUNTOS_INSUFICIENTES1']);
			p.value = '';
			p.focus();
			return false;
		}
		
		if (p.value>sus){
			alert(_JS_TEXTOS['DESAFIOS_PUNTOS_INSUFICIENTES2']);
			p.value = '';
			p.focus();
			return false;
		}
		if (coste>tus_cre){
			alert(_JS_TEXTOS['DESAFIOS_CREDITOS_INSUFICIENTES1']);
			p.value = '';
			p.focus();
			return false;
		}
		if (coste>sus_cre){
			alert(_JS_TEXTOS['DESAFIOS_CREDITOS_INSUFICIENTES2']);
			p.value = '';
			p.focus();
			return false;
		}
		
		return true;
	}

	//
	// Función que valida si tienes créditos para regalar
	//
	
	function validateRegalarForm(p, tus){
				
		if (p.value == '' || p.value==0) {
			alert(_JS_TEXTOS['PROFILE_ERROR_CREDITOS_VACIO']);
			p.value = '';
			p.focus();
			return false;
		}
		
		reg = /[^0-9]/;
		if (reg.test(p.value)) {
			alert(_JS_TEXTOS['PROFILE_ERROR_CREDITOS']);
			p.value = '';
			p.focus();
			return false;
		}
		
		if (p.value>tus){
			alert(_JS_TEXTOS['PROFILE_CREDITOS_INSUFICIENTES']);
			p.value = '';
			p.focus();
			return false;
		}
		
		return true;
	}
	
	
	//
	// Función muestra el POPUP del avatar
	//
	
	function avatarPopup(){
				
		if ((typeof(popup_avatar) != "undefined")){
	    	popup_avatar.close();
		}
			
		popup_avatar = window.open('/sections/profile/avatarPopup.php', 'name', 'height=160,width=450,left=100,top=100,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	}
	
	//
	// Función muestra el POPUP para subir imagenes al blog 
	//
	
	
	function cambiarImagen(num){		
		if ((typeof(popup_subirImagen) != "undefined")){
	    	popup_subirImagen.close();
		}
		popup_subirImagen = window.open('/sections/profile/subirImagen.php?num='+num, 'name', 'height=225,width=475,left=100,top=100,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	}
	
	
	//
	// Función muestra el POPUP para eliminar una imagen del blog
	//
	
	function eliminarImagen(num){		
		if ((typeof(popup_subirImagen) != "undefined")){
	    	popup_subirImagen.close();
		}
			
		popup_subirImagen = window.open('/sections/profile/eliminarImagen.php?num='+num, 'name', 'height=160,width=450,left=100,top=100,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	}
	
	//
	// Función muestra el POPUP para subir mensaje al blog
	//
	
	
	function SubirMensaje(num,user,op){		
		if ((typeof(popup_subirImagen) != "undefined")){
	    	popup_subirImagen.close();
		}
		popup_subirImagen = window.open('/sections/profile/subirMensaje.php?num='+num+'&iduser='+user+'&op='+op, 'name', 'height=225,width=475,left=100,top=100,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	}

	//
	// Función muestra el POPUP del reenvio de mail
	//
	
	function reenvioPopup(){
				
		if ((typeof(popup_reenvio) != "undefined")){
	    	popup_reenvio.close();
		}
			
		popup_reenvio = window.open('/sections/profile/reenviarPopup.php', 'name', 'height=160,width=450,left=100,top=100,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	}
	
	//
	// Función muestra el POPUP del avatar en la parte publica
	//
	
	function avatarpublicPopup(){
					
		popup_avatar = window.open('/sections/profile/avatarPopup.php', 'name', 'height=160,width=450,left=100,top=100,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	}

	//
	// Función muestra el POPUP del logout
	//
	
	function logoutPopup(url, sesion){
		window.location.href = '/sections/profile/cierraSesion.php?u=' + url + '&s=' + sesion;
	}

	//
	// Abre el popup para el TPV
	//
	
	function abrir_popup_TPV(url, ancho, alto)
	{
		var opciones="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width="+ancho+",height="+alto+",top=85,left=140";
		window.open(url,"",opciones);
	}
	
	//
	// Función que MUESTRA u OCULTA la imagen del avatar dependiendo del CHECKBOX
	//
	
	function checkAvatar(user, def, nodef){
			
		var now = new Date(); // Para evitar problemas de caché
		var img = document.getElementById('idProfileFoto');
		var a   = document.getElementById('idcheck');
		var chk = document.getElementById('idcheckdefault');
		var spa = document.getElementById('idspan');
			
		if (!chk.checked){
			if (!a.checked){
				img.style.backgroundImage = "url('" + nodef + "?" + now.getTime() + "')";
			} else {
				img.style.backgroundImage = "url('" + user + "?" + now.getTime() + "')";
			}
		} else {
			
			img.style.backgroundImage = "url('" + def + "?" + now.getTime() + "')";
	
			if (!a.checked){
				img.style.backgroundImage = "url('" + nodef + "?" + now.getTime() + "')";
				chk.checked = false;
			}
		}
	}
	
	
	//
	// Función que MUESTRA el texto de CARGANDO...
	//
	
	function avatarLoading(){
		
		document.getElementById('idtexto1').style.display = "none";
		document.getElementById('idtexto2').style.display = "none";
		document.getElementById('idtexto3').style.display = "";
		if (document.getElementById('idtexto4')){
			document.getElementById('idtexto4').style.display = "none";
		}
		if (document.getElementById('idtexto5')){
			document.getElementById('idtexto5').style.display = "none";
		}
		
		document.getElementById('idfichero1').style.display = "none";
		
		document.getElementById('idboton1').style.display = "none";
		document.getElementById('idboton2').style.display = "none";
		document.getElementById('idboton3').style.display = "";
		
		if (document.getElementById('idinput1')){
			document.getElementById('idinput1').style.display = "none";
		}
		
		if (document.getElementById('idinput2')){
			document.getElementById('idinput2').style.display = "none";
		}
		
		
		return true;
		
	}
	
	
	//
	// Función que MUESTRA el AVATAR actual
	//
	
	function changeAvatar(def){
				
		var now = new Date(); // Para evitar problemas de caché
		
		var img = window.opener.document.getElementById('idProfileFoto');
		if (img){
			img.style.backgroundImage = "url('" + def + "?" + now.getTime() + "')";
		}

		var img = window.opener.document.getElementById('idProfileHeadFoto');
		if (img){
			img.src = def + "?" + now.getTime();
		}
		
		var a = window.opener.document.getElementById('idcheck');
		if (a){
			a.checked = true;
		}
		
		var chk = window.opener.document.getElementById('idcheckdefault');
		if (chk){
			chk.checked = false;
		}		
		
		window.close();
	}
	
	
	
	//
	// Función muestra el AvisoLegal y la otra el copyright, las dos en un popup
	//
	
	function avisoLegal(){
				
		if ((typeof(popup) != "undefined")){
	    	popup.close();
		}
			
		popup = window.open('/sections/miscelany/avisoLegal.php', 'name', 'height=360,width=600,left=300,top=300,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no');
		
	}
	
	function copyright(){
				
		if ((typeof(popup) != "undefined")){
	    	popup.close();
		}
			
		popup = window.open('/sections/miscelany/copyright.php', 'name', 'height=360,width=600,left=300,top=300,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no');
		
	}
	
	//
	// Función muestra el POPUP del olvido password
	//
	
	function olvidoPopup(){
				
		if ((typeof(popup_olvido) != "undefined")){
	    	popup_olvido.close();
		}
			
		popup_olvido = window.open('/sections/profile/olvidoPopup.php', 'name', 'height=160,width=450,left=300,top=300,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
		
	}

	//
	// Función muestra el POPUP del olvido password
	//
	
	function recomendarPopup(){
				
		if ((typeof(popup_olvido) != "undefined")){
	    	popup_recomendar.close();
		}
			
		popup_recomendar = window.open('/sections/profile/recomendarPopup.php', 'name', 'height=160,width=450,left=300,top=300,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
		
	}
	
	//
	// Función muestra el POPUP del password
	//
	
	function passwordPopup(){
				
		if ((typeof(popup_password) != "undefined")){
	    	popup_password.close();
		}
			
		popup_password = window.open('/sections/profile/passwordPopup.php', 'name', 'height=160,width=450,left=300,top=300,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
		
	}
	
	//
	// Función muestra el POPUP del password para el admin
	//
	
	function passwordPopup_all(id){
				
		if ((typeof(popup_password) != "undefined")){
	    	popup_password.close();
		}
			
		popup_password = window.open('/sections/profile/passwordPopup_all.php?id='+id, 'name', 'height=160,width=450,left=300,top=300,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
		
	}
	
	//
	// Función que valida los campos del formulario REGISTRO
	//
	
	function validateProfileForm(f){
		
		// Validación de email
		
		if (f.email.value){
			if (!validateEmail(f.email.value)){
				alert(_JS_TEXTOS['REGISTRO_EMAIL_INCORRECTO']);
				//document.getElementById('registro_email_error').style.display = '';
				//document.getElementById('registro_span_email_error').style.visibility = 'visible';
				f.email.focus();
				return false;
			}
		}
		
		reg = /[^0-9]/;
		if (f.dia.value!=""){
			if (!reg.test(f.dia.value)){
				if (f.dia.value<1 || f.dia.value>31){
					
					alert(_JS_TEXTOS['PROFILE_FECHA_INCORRECTA']);
					//document.getElementById('registro_email_error').style.display = '';
					//document.getElementById('registro_span_email_error').style.visibility = 'visible';
					
					return false;
				}
			} else {
				alert(_JS_TEXTOS['PROFILE_FECHA_INCORRECTA']);
				return false;
			}
		}
		
		reg = /[^0-9]/;
		var curdate = new Date();
		var curyear = curdate.getFullYear();
		
		if (f.anyo.value!=""){
			if (!reg.test(f.anyo.value)){
				if (f.anyo.value<1900 || f.anyo.value>curyear){
					
					alert(_JS_TEXTOS['PROFILE_FECHA_INCORRECTA']);
					//document.getElementById('registro_email_error').style.display = '';
					//document.getElementById('registro_span_email_error').style.visibility = 'visible';
					
					return false;
				}
			} else {
				alert(_JS_TEXTOS['PROFILE_FECHA_INCORRECTA']);
				return false;
			}
		}
		
		var fecha_user   = new Date(parseInt(f.anyo.value), parseInt(f.mes.value)-1, parseInt(f.dia.value));
		var fecha_actual = new Date(parseInt(f.anyo_actual.value), parseInt(f.mes_actual.value)-1, parseInt(f.dia_actual.value));

		if (fecha_actual<fecha_user){
			alert(_JS_TEXTOS['PROFILE_FECHA_INCORRECTA']);
			return false;
		}
		
	}
	
	
	function checkingPasswords(f){
		
		// Validación del password y repetición de password
	
		if (f.password.value!=''){
			
			if (f.password.value.length>=5){
			
				if (f.password.value != f.repassword.value){
					alert(_JS_TEXTOS['PROFILE_PASSWORDS_DISTINTOS']);
					
					f.password.value = '';
					f.repassword.value = '';
					f.password.focus();
				
					return false;
				}
	
			} else {
				alert(_JS_TEXTOS['PROFILE_PASSWORD_INCORRECTO']);
				
				f.password.focus();
				return false;
			}
			
		} else {
			alert(_JS_TEXTOS['PROFILE_PASSWORD_VACIO']);
			
			f.password.focus();
			return false;
		}
		
		if (!confirm(_JS_TEXTOS['PROFILE_SEGURO_CAMBIAR_PASSWORD'])) {
        	return false;
    	} else {
			return true;
    	}
	}

// ------------------------------------------------------------------------
// Fin FUNCIONES SECCION PROFILE
// ------------------------------------------------------------------------



// ------------------------------------------------------------------------
// Inicio FUNCIONES HEAD
// ------------------------------------------------------------------------

	function isPlayingGame(){
	    
	    if (document.getElementById('idempiezadesafio') != null){
	        if (!document.getElementById('idempiezadesafio').value){
	            return false;
	        }
	    } else {
	        return false;
	    }
	    
	}

	function updateDivAmigos(response){

		if (response=='NOT_LOGGED'){
			window.location = '/sections/home/';
			exit;
		}
	
		if (response){
			var iddiv = document.getElementById('iddivamigos');
			
			if (iddiv!=null){
				iddiv.innerHTML = response;
			}
		}
	}
	
	function refrescaAmigos(iduser, pag){
		var options = {
			url: '/sections/profile/muestra_amigos.php',
			queryVars: {iduser: iduser, apag: pag},
			responseType: 'text',
			handler: updateDivAmigos
		};
		ajaxSendRequest(options);
	}
	
	function actictivaMinipuntos(codjuego, minipunto){
		var mini2 = document.getElementById(codjuego + '_2');
		var mini3 = document.getElementById(codjuego + '_3');
		var mini4 = document.getElementById(codjuego + '_4');
		var mini5 = document.getElementById(codjuego + '_5');
		
		var num   = document.getElementById(codjuego + '_div');
		
		var url_on  = '/img/v2/public/minipunto_on.jpg'; 
		var url_off = '/img/v2/public/minipunto_off.jpg';
		
		var link_f  = document.getElementById(codjuego + '_link_f');
		var link_m  = document.getElementById(codjuego + '_link_m');
		var link_d  = document.getElementById(codjuego + '_link_d');
		
		switch(minipunto){
			case 1:
					mini2.src = url_off;
					mini3.src = url_off;
					mini4.src = url_off;
					mini5.src = url_off;
					break;
			case 2:
					mini2.src = url_on;
					mini3.src = url_off;
					mini4.src = url_off;
					mini5.src = url_off;
					break;
			case 3:
					mini2.src = url_on;
					mini3.src = url_on;
					mini4.src = url_off;
					mini5.src = url_off;
					break;
			case 4:
					mini2.src = url_on;
					mini3.src = url_on;
					mini4.src = url_on;
					mini5.src = url_off;
					break;
			case 5:
					mini2.src = url_on;
					mini3.src = url_on;
					mini4.src = url_on;
					mini5.src = url_on;
					break;
		}
		
		num.innerHTML = minipunto;
		
		if (link_f){
			link_f.href = '../games/training.php?g=' + codjuego + '&d=1&p=' + minipunto;
		}
		
		if (link_m){
			link_m.href = '../games/training.php?g=' + codjuego + '&d=2&p=' + minipunto;
		}
		
		if (link_d){
			link_d.href = '../games/training.php?g=' + codjuego + '&d=3&p=' + minipunto;
		}
	}
	
	function update_alertas_salas(idd, dificultad){
		var divtext = document.getElementById('iddivsala' + dificultad);
		if (divtext){
			divtext.innerHTML = '<br><font color=#5F5F5F>' + _JS_TEXTOS['SALAS_AVISANDO_JUGAR'] + '</font>';
		}
		var options5 = {
			url: '/sections/jugar/update_alertas_salas.php',
			queryVars: {idd: idd, d: dificultad},
			responseType: 'text'
		};
		ajaxSendRequest(options5);
	}
	
	
// ------------------------------------------------------------------------
// Fin FUNCIONES HEAD
// ------------------------------------------------------------------------



// ------------------------------------------------------------------------
// Inicio FUNCIONES FOOT
// ------------------------------------------------------------------------

	function openCloseFoot() {
		var fc  = document.getElementById('idtrfootcontent');
		var me  = document.getElementById('idtrmenor');
		
		if (fc.style.display == ''){
			fc.style.display = 'none';
			me.style.display = '';		
		} else {
			fc.style.display = '';
			me.style.display = 'none';
		}
		
		var options = {
			url: '/sections/profile/muestra_desafios.php',
			queryVars: {display: fc.style.display},
			responseType: 'text'
		};
			
		ajaxSendRequest(options);
	}
	
	function openFoot(ancla) {
		var fc  = document.getElementById('idtrfootcontent');
		var me  = document.getElementById('idtrmenor');

		if (fc.style.display == 'none'){
			fc.style.display = '';
			me.style.display = 'none';
		} else {
			fc.style.display = 'none';
			me.style.display = '';
			
		}
		
//		if (ancla){
//			window.location.href = '#ancladesafios';
//		}
	}

// ------------------------------------------------------------------------
// Fin FUNCIONES FOOT
// ------------------------------------------------------------------------





// ------------------------------------------------------------------------
// FUNCIONES ROLLOVER IMAGENES
// ------------------------------------------------------------------------


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}




// ------------------------------------------------------------------------
// fin FUNCIONES ROLLOVER IMAGENES
// ------------------------------------------------------------------------




