var errColor = "#FFCC00";
var validColor = "#33CC00";
function checkLoad()
{
	checkValidate();
	document.getElementById("SubmitReg").value  = 'Create my account';
	if (document.getElementById("frm2a").value.trim() != "" || document.getElementById("frm2b").value.trim() != "") { checkEmail(); };
	if (document.getElementById("frm3a").value.trim() != "" || document.getElementById("frm3b").value.trim() != "") { checkPass(); };
	if (document.getElementById("frm1").value.trim() != "") { checkNick(); };
	checkValidate();
}

function checkEmail()
{
	var emailTmp1 = document.getElementById("frm2a").value.trim();
	var emailTmp2 = document.getElementById("frm2b").value.trim();
	
	if (emailTmp1 == emailTmp2)
	{
		if (emailTmp1 != "")
		{
			var xmlHttp;
			try
			{
				// Firefox, Opera 8.0+, Safari
				xmlHttp=new XMLHttpRequest();
			}
			catch (e)
			{
				// Internet Explorer
				try
				{
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
					try
					{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e)
					{
						isEmailValid = false;
						checkValidate();
						alert("Your browser does not support AJAX!");
						return false;
					}
				}
			}
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState==4)
				{
					//document.frmReg.frm0.value=xmlHttp.responseText;
					if (xmlHttp.responseText == "100")
					{
						isEmailValid = false;
						document.getElementById("imgE1CheckReason").innerHTML = "<br /><strong>This email is already registered</strong>";
						document.getElementById("imgE2CheckReason").innerHTML = "<br /><strong>This email is already registered</strong>";
						document.getElementById("imgE1CheckReason").style.color = errColor;
						document.getElementById("imgE2CheckReason").style.color = errColor;
						checkValidate();
					}
					else if (xmlHttp.responseText == "200")
					{
						isEmailValid = false;
						document.getElementById("imgE1CheckReason").innerHTML = "<br /><strong>This email contains an error</strong>";
						document.getElementById("imgE2CheckReason").innerHTML = "<br /><strong>This email contains an error</strong>";
						document.getElementById("imgE1CheckReason").style.color = errColor;
						document.getElementById("imgE2CheckReason").style.color = errColor;
						checkValidate();
					}
					else
					{
						isEmailValid = true;
						document.getElementById("imgE1CheckReason").innerHTML = " <strong>Valid</strong>";
						document.getElementById("imgE2CheckReason").innerHTML = " <strong>Valid</strong>";
						document.getElementById("imgE1CheckReason").style.color = validColor;
						document.getElementById("imgE2CheckReason").style.color = validColor;
						checkValidate();
					}
				}
			}
			xmlHttp.open("GET","/_ajax/checkEmail.asp?v=" + emailTmp1,true);
			xmlHttp.send(null);
		}
		else
		{
			isEmailValid = false;
			document.getElementById("imgE1CheckReason").innerHTML = "<br /><strong>Enter your email address</strong>";
			document.getElementById("imgE2CheckReason").innerHTML = "<br /><strong>Enter your email address</strong>";
			document.getElementById("imgE1CheckReason").style.color = errColor;
			document.getElementById("imgE2CheckReason").style.color = errColor;
			checkValidate();
		}
	}
	else
	{
		isEmailValid = false;
		document.getElementById("imgE1CheckReason").innerHTML = "<br /><strong>Addresses does not match</strong>";
		document.getElementById("imgE2CheckReason").innerHTML = "<br /><strong>Addresses does not match</strong>";
		document.getElementById("imgE1CheckReason").style.color = errColor;
		document.getElementById("imgE2CheckReason").style.color = errColor;
		checkValidate();
	}
}

function checkPass()
{
	var passTmp1 = document.getElementById("frm3a").value.trim();
	var passTmp2 = document.getElementById("frm3b").value.trim();
	var isLngEnough = true;
	if (passTmp1.length < 6 && passTmp2.length > 0)
	{
		isLngEnough = false;
		document.getElementById("imgP1CheckReason").innerHTML = "<strong>Too short</strong>";
		document.getElementById("imgP1CheckReason").style.color = errColor;
	}
	
	if (passTmp2.length < 6 && passTmp1.length > 0)
	{
		isLngEnough = false;
		document.getElementById("imgP2CheckReason").innerHTML = "<strong>Too short</strong>";
		document.getElementById("imgP2CheckReason").style.color = errColor;
	}
	
	if (isLngEnough && passTmp1.length > 0 && passTmp2.length > 0)
	{
		if (passTmp1 != passTmp2)
		{
			isPassValid = false;
			document.getElementById("imgP1CheckReason").innerHTML = "<br /><strong>Password does not match</strong>";
			document.getElementById("imgP2CheckReason").innerHTML = "<br /><strong>Password does not match</strong>";
			document.getElementById("imgP1CheckReason").style.color = errColor;
			document.getElementById("imgP2CheckReason").style.color = errColor;
		}
		else
		{
			isPassValid = true;
			document.getElementById("imgP1CheckReason").innerHTML = " <strong>Valid</strong>";
			document.getElementById("imgP2CheckReason").innerHTML = " <strong>Valid</strong>";
			document.getElementById("imgP1CheckReason").style.color = validColor;
			document.getElementById("imgP2CheckReason").style.color = validColor;
		}
	}
	else
	{
		isPassValid = false;
		document.getElementById("imgP1CheckReason").innerHTML = "<strong>Too short</strong>";
		document.getElementById("imgP2CheckReason").innerHTML = "<strong>Too short</strong>";
		document.getElementById("imgP1CheckReason").style.color = errColor;
		document.getElementById("imgP2CheckReason").style.color = errColor;
	}
	checkValidate();
}

function checkNick()
{
	var nickTmp = document.getElementById("frm1").value.trim();
	
	if (nickTmp.length < 3)
	{
		isNickValid = false;
		document.getElementById("imgNCheckReason").innerHTML = "<strong>Too short</strong>";
		document.getElementById("imgNCheckReason").style.color = errColor;
		checkValidate();
	}
	else
	{
		var xmlHttp;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		{
			// Internet Explorer
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
					isNickValid = false;
					checkValidate();
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				//document.frmReg.frm0.value=xmlHttp.responseText;
				if (xmlHttp.responseText == "100")
				{
					isNickValid = false;
					document.getElementById("imgNCheckReason").innerHTML = "<br /><strong>Nickname already exists</strong>";
					document.getElementById("imgNCheckReason").style.color = errColor;
					checkValidate();
				}
				else if (xmlHttp.responseText == "200")
				{
					isNickValid = false;
					document.getElementById("imgNCheckReason").innerHTML = "<br /><strong>Nickname is too short</strong>";
					document.getElementById("imgNCheckReason").style.color = errColor;
					checkValidate();
				}
				else
				{
					isNickValid = true;
					document.getElementById("imgNCheckReason").innerHTML = " <strong>Valid</strong>";
					document.getElementById("imgNCheckReason").style.color = validColor;
					checkValidate();
				}
			}
		}
		xmlHttp.open("GET","/_ajax/checkNick.asp?v=" + nickTmp,true);
		xmlHttp.send(null);
	}
}


function checkValidate()
{
	if (isPassValid == true && isNickValid == true && isEmailValid == true && document.getElementById("subAgeValid").checked == true && document.getElementById("subTermsValid").checked == true)
	{
		document.getElementById("SubmitReg").disabled = false;
	}
	else
	{
		document.getElementById("SubmitReg").disabled = true;
	}
}



function showTOU()
{
	window.open('/termsofuse.html','termsofuse','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=800, height=600');
}


String.prototype.trim = function()
{
	return this.replace(/^\s+/, "").replace(/\s+$/, "");
}
