document.write('<script language="javascript" src="ajax/lithos.js"></script>');

function jsValidateUser()
{
	var xmlHttp = GetXmlHttpObject()
	
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	var user = document.frmRegister.txtUsername.value;
	
	if (user.length == 0)
		return;
		
	var url = "ajax/register.php?q=validateUser" + "&user=" + user + "&sid=" + NumberMS();
	
	SetHTMLColor("divUsernameDesc", "Validating user name...", colorNormal);
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			switch (xmlHttp.responseText)
			{
				case "1": // success!
					SetHTMLColor("divUsernameDesc", "Your chosen user name is available!", colorValid);
					break;
				
				case "-1":
					SetHTMLColor("divUsernameDesc", "User name must be at least 3 characters long.", colorInvalid);
					break;
					
				case "-2":
					SetHTMLColor("divUsernameDesc", "User name must be less than 16 characters long.", colorInvalid);
					break;
				
				case "-3":
					SetHTMLColor("divUsernameDesc", "The user name may only contain numbers or letters.", colorInvalid);
					break;
				
				case "-4":
					SetHTMLColor("divUsernameDesc", "Your desired user name is already chosen. Please choose another one!", colorInvalid);
					break;
			}
		}
	}
	
	//jsValidateUsernameChanged
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function jsCheckRealname()
{
	var realname = document.frmRegister.txtRealname.value;
	
	// si pone caracteres < > que son invalidos entonces no!
	var invalidChars = /[<>]/;
	
	if (invalidChars.test(realname))
	{
		SetHTMLColor("divRealnameDesc", "The < and > characters are not permitted!", colorInvalid);
		return;
	}
}

function jsCheckReferredBy()
{
	var xmlHttp = GetXmlHttpObject()
	
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	var user = document.frmRegister.txtReferrerUsername.value;
	
	if (user.length == 0)
	{
		SetHTML("divReferredBy", "&nbsp;");
		return;
	}
	
	var url = "ajax/register.php?q=checkRef" + "&user=" + user + "&sid=" + NumberMS();
	
	//document.getElementById("divEmailDesc").innerHTML = "Validating email..."
	SetHTMLColor("divReferredBy", "Checking referral user name...", colorNormal);
	
	xmlHttp.onreadystatechange = function() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.responseText == "1") // valid
				SetHTMLColor("divReferredBy", "The referral user name you entered is valid!", colorValid);
				
			else
				SetHTMLColor("divReferredBy", "The referral user name does not exist.", colorInvalid);
		}
	}
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function jsValidateEmail()
{	
	var xmlHttp = GetXmlHttpObject()
	
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	var email = document.frmRegister.txtEmail.value;
	
	if (email.length == 0)
		return;
		
	var url = "ajax/register.php?q=validateEmail" + "&email=" + email + "&sid=" + NumberMS();
	
	SetHTMLColor("divEmailDesc", "Validating email...", colorNormal);
	
	xmlHttp.onreadystatechange = function() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			var response = xmlHttp.responseText;
			
			if (response == "1") // valid
				SetHTMLColor("divEmailDesc", "Your email appears to be valid!", colorValid);
			
			else if (response == "-1") // invalido
				SetHTMLColor("divEmailDesc", "Your email is invalid. Please enter a valid email.", colorInvalid);
				
			else if (response == "-2") // ya ta en uso!
				SetHTMLColor("divEmailDesc", "The email you selected is already registered to another user.", colorInvalid);
		} 
	}

	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function checkPassword1()
{
	var pw1 = document.frmRegister.txtPW.value;
	
	var legalChars = /^[a-zA-Z0-9]+$/; // allow only letters and numbers
	
	if (pw1 == "")
		return;
	//	document.getElementById("divPWDesc").innerHTML = "Don't forget to enter your password!";

	else if ((pw1.length < 5))
	  	SetHTMLColor("divPWDesc", "The password must contain at least 5 characters.", colorInvalid);
    
	else if (pw1.length > 32)
      	SetHTMLColor("divPWDesc", "The password is too long! It must contain less than 32 characters.", colorInvalid);
    
	else if (!legalChars.test(pw1))
		SetHTMLColor("divPWDesc", "The password may only contain numbers and letters.", colorInvalid);
	
	else // ok!
		SetHTMLColor("divPWDesc", "Your password is valid!", colorValid);
}
	
function jsCheckPasswords()
{
	var pw1 = document.frmRegister.txtPW.value;
	var pw2 = document.frmRegister.txtPW2.value;
	
	// primero pw1
		var legalChars = /^[a-zA-Z0-9]+$/; // allow only letters and numbers
	
	if (pw1 == "")
		return;
	//	document.getElementById("divPWDesc").innerHTML = "Don't forget to enter your password!";

	else if ((pw1.length < 5))
	{
		SetHTMLColor("divPWDesc", "The password must contain at least 5 characters.", colorInvalid);
    	return;
	}
	
	else if (pw1.length > 32)
    {
		SetHTMLColor("divPWDesc", "The password is too long! It must contain less than 32 characters.", colorInvalid);
    	return;
	}
	
	else if (!legalChars.test(pw1))
	{
		SetHTMLColor("divPWDesc", "The password may only contain numbers and letters.", colorInvalid);
		return;
	}
	
	// si pw2 esta vacio entonces no escribir nada
	if (pw2.length == 0)
		return;
		
	if (pw1 != pw2)
		SetHTMLColor("divPW2Desc", "Your passwords don't match! Please re-type them.", colorInvalid);

	else
		SetHTMLColor("divPW2Desc", "Your passwords match. Don't forget it!", colorValid);
	
	// borrar la desc del PW1
	SetHTML("divPWDesc", "&nbsp;");
}
