function creaAjax()
{
	var objetoAjax = false;
	
	try
	{
		// Para navegadores distintos a internet explorer
		objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			// Para explorer
			objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			objetoAjax = false;
		}
	}
	
	if (!objetoAjax && typeof XMLHttpRequest!='undefined')
	{
		objetoAjax = new XMLHttpRequest();
	}
	
	return objetoAjax;
}

function CharInCorrectosRegistro(cadena)
{
	validos = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	
	document.form_registro.usuario.value = cadena.toLowerCase();
	
	for (i=0;i<cadena.length;i++)
	{
		if (validos.indexOf(cadena.charAt(i))==-1)
		{
			document.form_registro.usuario.value = cadena.substring(0,cadena.length-1);
			break;
		}
		
	}
}

function validar_usuario(e)
{
    tecla = (document.all) ? e.keyCode : e.which;
	
    if (tecla==8) return true;
	
	if (tecla==0) return true;

    patron = /\w/;

    te = String.fromCharCode(tecla);

    return patron.test(te);
}

function validarEmail(valor)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

function CompruebaLogin()
{
	var divresultado = document.getElementById("divlogin");
	
	if (ajax_login.readyState == 4)
	{
		if (ajax_login.responseText == "ok")
		{
			document.login.submit();
		}
		else
		{
			divresultado.innerHTML = ajax_login.responseText;
		}
	}
	else
	{
		divresultado.innerHTML = "Cargando..."
	}
}

function Login()
{
	var divresultado = document.getElementById("divlogin");
	
	var aleatorio = Math.random();
	
	if (document.login.usuario.value == "" || document.login.clave.value == "")
	{
		divresultado.innerHTML = "Rellena todos los campos";
		
		return false;
	}
	else
	{
		var usuario = document.login.usuario.value;
		
		var clave = document.login.clave.value;
		
		ajax_login = creaAjax();
		
		ajax_login.open("GET", "/comprobar_login.php?aleatorio="+aleatorio+"&usuario="+usuario+"&clave="+clave);
		
		ajax_login.onreadystatechange = CompruebaLogin;
		
		ajax_login.send(null);
		
		return false;
	}
}


function CompruebaRegistro()
{
	var divresultado = document.getElementById("divregistro");
	
	if (ajax_registro.readyState == 4)
	{
		if (ajax_registro.responseText == "ok")
		{
			document.form_registro.submit();
		}
		else
		{
			divresultado.innerHTML = ajax_registro.responseText;
		}
	}
	else
	{
		divresultado.innerHTML = "Cargando..."
	}
}


function Registro()
{
	var divresultado = document.getElementById("divregistro");
	
	var aleatorio = Math.random();
	
	if (document.form_registro.usuario.value == "" || document.form_registro.email.value == "" || document.form_registro.clave.value == "" || document.form_registro.clave2.value == "" || document.form_registro.imagen.value == "")
	{
		divresultado.innerHTML = "Rellena todos los campos";
		
		return false;
	}
	else if (document.form_registro.usuario.value.length > 12)
	{
		divresultado.innerHTML = "El usuario es demasiado largo";
		
		return false;
	}
	else if (validarEmail(document.form_registro.email.value) == 0)
	{
		divresultado.innerHTML = "El email no es correcto";
		
		return false;
	}
	else if (document.form_registro.clave.value != document.form_registro.clave2.value)
	{
		divresultado.innerHTML = "Las contrase&ntilde;as no coinciden";
		
		return false;
	}
	else if (document.form_registro.clave.value.length < 6)
	{
		divresultado.innerHTML = "La contrase&ntilde;a es demasiado corta";
		
		return false;
	}
	else
	{
		var usuario = document.form_registro.usuario.value;
	
		var email = document.form_registro.email.value;
		
		var imagen = document.form_registro.imagen.value;
	
		ajax_registro = creaAjax();
	
		ajax_registro.open("GET", "comprobar_registro.php?aleatorio="+aleatorio+"&usuario="+usuario+"&email="+email+"&imagen="+imagen);
		
		ajax_registro.onreadystatechange = CompruebaRegistro;
		
		ajax_registro.send(null);
		
		return false;
	}
}

function CompruebaRecordar()
{
	var divresultado = document.getElementById("divlogin");
	
	if (ajax_recordar.readyState == 4)
	{
		divresultado.innerHTML = ajax_recordar.responseText;
		
		document.recordar.imagen.value = "";
	}
	else
	{
		divresultado.innerHTML = "Cargando...";
	}
}

function Recordar()
{
	var divresultado = document.getElementById("divlogin");
	
	var aleatorio = Math.random();
	
	if (document.recordar.email.value == "" || document.recordar.imagen.value == "")
	{
		divresultado.innerHTML = "Rellena todos los campos";
		
		return false;
	}
	else if (validarEmail(document.recordar.email.value) == 0)
	{
		divresultado.innerHTML = "El email no es correcto";
		
		return false;
	}
	else
	{
		var email = document.recordar.email.value;
		
		var imagen = document.recordar.imagen.value;
		
		ajax_recordar = creaAjax();
		
		ajax_recordar.open("GET", "/comprobar_recordar.php?aleatorio="+aleatorio+"&email="+email+"&imagen="+imagen);
		
		ajax_recordar.onreadystatechange = CompruebaRecordar;
		
		ajax_recordar.send(null);
		
		return false;
	}
}

function RefreshImg()
{
	var aleatorio = Math.random();
	
	document.getElementById("img_captcha").src = "/captcha.php?aleatorio="+aleatorio;
}
