function acceptNum(evt){ 
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
	var key = nav4 ? evt.which : evt.keyCode; 
	return (key <= 13 || key == 46 || (key >= 48 && key <= 57));
}

function acceptNumComa(evt){ 
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
	var key = nav4 ? evt.which : evt.keyCode; 
	return (key <= 13 || key == 44 || (key >= 48 && key <= 57));
}

function acceptMay(evt){ 
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
	var key = nav4 ? evt.which : evt.keyCode;
	//alert (key);
	if (key <= 13 || (key >= 97 && key <= 122))
	  return evt.keyCode = key-32;
}


function acceptLetras(evt){ 
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
	var nav4 = window.Event ? true : false; 
	var key = nav4 ? evt.which : evt.keyCode;
	// solo acepto letras
	if (key <= 13 || (key >= 97 && key <= 122) || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || key == 45 || key == 95)
	  return evt.keyCode = key;
	else return false;
}

function cambio(d,v,f)
{
  if (d.options[d.selectedIndex].value == v) f.disabled = false;
  else f.disabled = true;
}

function setDisabled(id, disabled)
{
  if ( !document.getElementById
  || !document.getElementsByTagName) return;
  
  var nodesToDisable = {checkbox :'', input :'', span :'', strong : '',
  option :'', select :'', textarea :'', radio :'', text :''};
  
  var node, nodes;
  var div = document.getElementById(id);
  if (!div) return;
  
  nodes = div.getElementsByTagName('*');
  if (!nodes) return;
  
  var i = nodes.length;
  while (i--)
  {
    node = nodes[i];
    if ( node.nodeName
    && node.nodeName.toLowerCase() in nodesToDisable ){
      node.disabled = disabled;
    }
  }
}

function setDisabledT(id, disabled)
{
  if ( !document.getElementById
  || !document.getElementsByTagName) return;
  
  var nodesToDisable = {checkbox :'', input :'', span :'', strong : '',
  option :'', select :'', textarea :''};
  
  var node, nodes;
  var div = document.getElementById(id);
  if (!div) return;
  
  nodes = div.getElementsByTagName('*');
  if (!nodes) return;
  
  var i = nodes.length;
  while (i--)
  {
    node = nodes[i];
    if ( node.nodeName
    && node.nodeName.toLowerCase() in nodesToDisable ){
      if (node.disabled) node.disabled = false
      else node.disabled = true;
    }
  }
}


function cambia(c)
{
  if (c.value == "1") c.value = "0";
  else c.value = "1";
}

function cambia_idioma(idioma)
{

//  if (idioma == 2) alert("proximament");
//  if (idioma == 3) alert("coming soon");

//	if (document.location.href.match(/\?/))
//		car = "&"
//	else
//		car = "?"
	var url  = "";
	var url1 = "";
	if (document.location.href.match(/\#/))
    url = document.location.href.substring(0,document.location.href.length-1);
  else
    url = document.location.href

  if (url.match(/motor.php\?/))
  {
  	// estamos en una url del tipo motor.php?id_pagina=x
  	// hemos de añadir un parametro solo
		var items = url.split("&")
		if (items[items.length-1].match(/idioma/))
		{
			for (i=0;i<items.length-1;i++) {
				url1 = url1 + items[i] + "&"
			}
			url1 = url1 + "idioma=" + idioma;
			url = url1;
		}
		else
		{
			url = url + "&idioma=" + idioma;
		}
  }
	else
	{
	  // tengo que analizar la url y cambiar el primer elemento
	  // tengo la utl en formato /en/seccion/subseccion
	  var items = url.split("/");
		for (i=4;i<items.length;i++) {
			url1 = url1 + items[i] + "/"
		}
		if (url1 == "/" || url1 == "") url1 = "home/";
	  url = "/" + idioma + "/" + url1.substring(0,url1.length-1);
	}
  document.location.href = url;
}

function ajaxpage(param1, div2)
{
  $(div2).load("vota.php?" + param1);
}


function valida_registro_home()
{
	if (document.f1.login.value == "" || document.f1.login.value == "Escribe texto")
	{
		alert("El campo Nombre de usuario es obligatorio");
		document.f1.login.focus();
		return false;
	}
	if (document.f1.email.value == "" || document.f1.email.value == "Escribe texto")
	{
		alert("El campo email es obligatorio");
		document.f1.email.focus();
		return false;
	}
	
	valorEmail= document.f1.email.value;	
  regexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

  if (!regexp.test(valorEmail)){
    alert("El formato de email introducido es incorrecto");		
	  document.f1.email.focus();
    return false;
  }
	
	if (document.f1.password.value == "")
	{
		alert("El campo password es obligatorio");
		document.f1.password.focus();
		return false;
	}
	return true;
}

function valida_registro_home2(f)
{
	if (f.login.value == "" || f.login.value == "Escribe texto")
	{
		alert("El campo Nombre de usuario es obligatorio");
		f.login.focus();
		return false;
	}
	if (f.email.value == "" || f.email.value == "Escribe texto")
	{
		alert("El campo email es obligatorio");
		f.email.focus();
		return false;
	}
	
	valorEmail= f.email.value;	
  regexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

  if (!regexp.test(valorEmail)){
    alert("El formato de email introducido es incorrecto");		
	  f.email.focus();
    return false;
  }
	
	if (f.password.value == "")
	{
		alert("El campo password es obligatorio");
		f.password.focus();
		return false;
	}
	return true;
}

function valida_completa_registro()
{
	if (document.fc1.password.value == "")
	{
		alert("Debes escribir tu contraseña para validar el formulario");
		document.fc1.password.focus();
		return false;
	}
	if (document.fc1.password.value != document.fc1.password2.value)
	{
		alert("La confirmación de contraseña no coincide con la contraseña introducido");
		document.fc1.password2.focus();
		return false;
	}
	if (document.fc1.f_dia.value == 0 || document.fc1.f_mes.value == 0 || document.fc1.f_anyo.value == 0)
	{
		alert("El campo fecha de nacimiento es obligatoria");
		return false;
	}
	if (!document.fc1.condiciones.checked)
	{
		alert("Debes aceptar las condiciones para continuar");
		return false;
	}

	return true;
}

function valida_newsletter()
{
	if (document.fn1.email.value == "" || document.fn1.email.value == "Email")
	{
		alert("El campo email es obligatorio");
		document.fn1.email.focus();
		return false;
	}
	
	valorEmail= document.fn1.email.value;	
  regexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

  if (!regexp.test(valorEmail)){
    alert("El formato de email introducido es incorrecto");		
	  document.fn1.email.focus();
    return false;
  }
  return true

}

function ajaxpageG(param1, div2, url)
{
  $(div2).load(url + "?" + param1);
}

function enviaComentario()
{
  if (document.fm1.comentarios.value == '')
  {
    alert("Debes introducir un comentario");
    return false;
  }
  params = "id=" + document.fm1.id.value;
  params = params + "&comentario=" + escape(document.fm1.comentarios.value);
  ajaxpageG(params,"#mensajes","resComentario.php");
}

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function enviaAmigo()
{
  if (document.fe1.direcciones.value == "")
  {
    alert("Debes escribir alguna dirección de email");
    return false;
  }
  if (document.fe1.mensaje.value == "")
  {
    alert("Debes escribir un mensaje");
    return false;
  }
  params = "id=" + document.fe1.id.value;
  params = params + "&tipo=" + escape(document.fe1.tipo.value);
  if (isdefined(document.fe1.tipo_archivo))
    params = params + "&tipo_archivo=" + escape(document.fe1.tipo_archivo.value);
  params = params + "&mensaje=" + escape(document.fe1.mensaje.value);
  params = params + "&direcciones=" + escape(document.fe1.direcciones.value);
  ajaxpageG(params,"#mensajes","resEnviarAmigo.php");
}

function enviaAmigo2()
{
  if (document.fe1.direcciones.value == "")
  {
    alert("Debes escribir alguna dirección de email");
    return false;
  }
  if (document.fe1.mensaje.value == "")
  {
    alert("Debes escribir un mensaje");
    return false;
  }
  params = "id=" + document.fe1.id.value;
  params = params + "&id_serie=" + escape(document.fe1.id_serie.value);
  params = params + "&tipo=" + escape(document.fe1.tipo.value);
  params = params + "&mensaje=" + escape(document.fe1.mensaje.value);
  params = params + "&direcciones=" + escape(document.fe1.direcciones.value);
  ajaxpageG(params,"#mensajes","resEnviarAmigo.php");
}

function valida_cambia_password()
{
	if (document.fcp1.newpassword.value == "")
	{
		alert("Debes escribir tu contraseña nueva");
		document.fc1.newpassword.focus();
		return false;
	}
	if (document.fcp1.newpassword.value != document.fcp1.cnewpassword.value)
	{
		alert("La confirmación de contraseña no coincide con la contraseña introducida");
		document.fcp1.cnewpassword.focus();
		return false;
	}
	return true;
}

function denuncia_comentario(id)
{
  params = "id_comentario=" + id;
  ajaxpageG(params,"#mensajes","resDenunciaComentario.php");
}

function denuncia_archivo(id)
{
  params = "id_contenido=" + id;
  ajaxpageG(params,"#mensajes","resDenunciaArchivo.php");
}

function valida_canje()
{

	if (document.fk2.nombre.value.length == 0 || (document.fk2.nombre.value.length > 0 && document.fk2.nombre.value.length < 3))
	{
		alert("El campo Nombre debe rellenarse correctamente");
		return false;
	}
	if (document.fk2.apellido1.value.length == 0 || (document.fk2.apellido1.value.length > 0 && document.fk2.apellido1.value.length < 3))
	{
		alert("El campo Apellido 1 debe rellenarse correctamente");
		return false;
	}
	if (document.fk2.telefono.value.length == 0 || (document.fk2.telefono.value.length > 0 && document.fk2.telefono.value.length < 3))
	{
		alert("El campo Teléfono debe rellenarse correctamente");
		return false;
	}
	if (document.fk2.tipo_via.value.length == 0)
	{
		alert("El campo Tipo vía debe rellenarse");
		return false;
	}
	if (document.fk2.calle.value.length == 0 || (document.fk2.calle.value.length > 0 && document.fk2.calle.value.length < 3))
	{
		alert("El campo Calle debe rellenarse correctamente");
		return false;
	}
	if (document.fk2.numero.value.length == 0)
	{
		alert("El campo Número debe rellenarse");
		return false;
	}
	if (document.fk2.cp.value.length == 0)
	{
		alert("El campo Código postal debe rellenarse");
		return false;
	}
	if (document.fk2.poblacion.value.length == 0 || (document.fk2.poblacion.value.length > 0 && document.fk2.poblacion.value.length < 3))
	{
		alert("El campo Localidad debe rellenarse correctamente");
		return false;
	}

	return true;
}