var xmlHttp;
var cmt;

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		document.getElementById(cmt).innerHTML=xmlHttp.responseText;
	}
}

function sendData(url, fnc, str)
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var params="str="+str;
	xmlHttp.onreadystatechange=fnc;
	xmlHttp.open("POST",url,true);

	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.send(params);
}

function checkUser(str)
{ 
	une = new RegExp("^[a-zA-Z0-9_.]{4,12}$");

	if(une.test(str) || !str)
	{
		cmt = "c3";
		sendData("getuser.php", stateChanged, str);
		return true;
	}
	else
	{
		document.getElementById("c3").innerHTML="Invalid username.";
		return false;
	}
}

function checkEmail(str)
{ 
	eme = new RegExp("^[a-zA-Z0-9_.]+[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)+$");

	if(eme.test(str) || !str)
	{
		cmt = "c6";
		sendData("getmail.php", stateChanged, str);
		return true;
	}
	else
	{
		document.getElementById("c6").innerHTML="Invalid email.";
		return false;
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}

function checkPword()
{
	pwe = new RegExp("^[a-zA-Z0-9]{6,12}$");

	if(pwe.test(document.frm1.pword.value) || !document.frm1.pword.value)
	{
		document.getElementById("c4").innerHTML="";
		return true;
	}
	else
	{
		document.getElementById("c4").innerHTML="Invalid password.";
		return false;
	}
}

function checkConfirm()
{
	if(document.frm1.pword.value == document.frm1.cpword.value)
	{
		document.getElementById("c5").innerHTML="";
		return true;
	}
	else if(!document.frm1.cpword.value || !document.frm1.pword.value)
	{
		return false;
	}
	else
	{
		document.getElementById("c5").innerHTML="Passwords do not match.";
		return false;
	}
}

function checkNotEmpty()
{
	if(document.frm1.fname.value && document.frm1.sname.value
	&& document.frm1.uname.value && document.frm1.pword.value
	&& document.frm1.cpword.value && document.frm1.email.value)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function checkName(str, cmt)
{
	nme = new RegExp("^[a-zA-Z0-9_.]{1,16}$");

	if(nme.test(str) || !str)
	{
		document.getElementById(cmt).innerHTML="";
		return true;
	}
	else
	{
		document.getElementById(cmt).innerHTML="Invalid name.";
		return false;
	}
}

function checkSubmit()
{
	if(!checkConfirm())
	{
		alert("Passwords do not match.");
		document.frm1.cpword.focus();
	}
	else if(!checkEmail())
	{
		alert("Invalid Email.");
		document.frm1.email.focus();
	}
	else if(!checkPword())
	{
		alert("Invalid password.");
		document.frm1.pword.focus();
	}
	else if(!checkNotEmpty())
	{
		alert("Fill in all fields.");
	}
	else if(!checkName(document.frm1.fname.value, "c5"))
	{
		alert("Invalid first name.");
		document.frm1.fname.focus();
	}
	else if(!checkName(document.frm1.sname.value, "c6"))
	{
		alert("Invalid surname.");
		document.frm1.sname.focus();
	}
	else
	{
		document.frm1.submit();
	}
}