// JavaScript Document
function formValidate()
{

	var mess = document.getElementById("errorMess2").innerHTML;
	var usernameCheck = document.frmReg.username.value;
	if(document.frmReg.username.value == "" )
	{
		document.getElementById("username").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "Username can't blank.";
		document.frmReg.username.focus();
		return false;
	}
	else if(usernameCheck.indexOf(" ") != -1){
		document.getElementById("username").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "Username must be without spaces.";
		document.frmReg.username.focus();
		return false;
	}
	else{
		document.getElementById("username").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	if(document.frmReg.password1.value=="")
	{
		document.getElementById("password").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "Password can't blank.";
		document.frmReg.password1.focus();
		return false;
	}
	else{
		document.getElementById("password").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	if(document.frmReg.password2.value=="")
	{
		document.getElementById("password2").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "Confirm password can't blank.";
		document.frmReg.password2.focus();
		return false;
	}else{
		document.getElementById("password2").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	if(document.frmReg.password1.value != document.frmReg.password2.value)
	{
		document.getElementById("password").style.backgroundColor = "yellow";
		document.frmReg.password1.focus();
		document.frmReg.password2.value = '';
		document.frmReg.password1.value = '';
		document.getElementById("errorMess2").innerHTML = "Both password must be same.";
		return false;
	}
	else{
		document.getElementById("password").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	if(document.frmReg.email.value=="")
	{
		document.getElementById("email").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "Email can't be blank.";
		document.frmReg.email.focus();
		return false;
	}
	else{
		document.getElementById("email").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	if(document.frmReg.name.value=="")
	{
		document.getElementById("fullname").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "Name can't be blank.";
		document.frmReg.name.focus();
		return false;
	}else{
		document.getElementById("fullname").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	if(document.frmReg.year.value=="")
	{
		document.getElementById("year").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "Year can't be blank.";
		document.frmReg.year.focus();
		return false;
	}
	else{
		document.getElementById("year").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	if(document.frmReg.profession.value=="")
	{
		document.getElementById("prof").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "Profession can't be blank.";
		document.frmReg.profession.focus();
		return false;
	}
	else{
		document.getElementById("prof").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	if(document.frmReg.country.value=="")
	{
		document.getElementById("country").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "Country can't be blank.";
		document.frmReg.country.focus();
		return false;
	}else{
		document.getElementById("country").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	if(document.frmReg.city.value=="")
	{
		document.getElementById("city").style.backgroundColor = "yellow";
		document.getElementById("errorMess2").innerHTML = "City can't be blank.";
		document.frmReg.city.focus();
		return false;
	}
	else{
		document.getElementById("city").style.backgroundColor = "white";
		document.getElementById("errorMess2").innerHTML = "";
	}

	return true;
}

// focus on first text box on load
function focus(){
	document.frmReg.username.focus();
}


// Validate username 
function validateUsername(username){
	
	xmlHttp = GetXmlHttpObject();
	
	if(xmlHttp == null) { 
		alert("Browser does not support HTTP Request");
		return;
	}
	
	var url = "validateUsername.php"
	url = url+"?username="+username
	xmlHttp.onreadystatechange = stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)	
}

//validate email id
function validateEmail(emailId){
	xmlHttp = GetXmlHttpObject();
	
	if(xmlHttp == null) { 
		alert("Browser does not support HTTP Request");
		return;
	}
	
	var url = "validateEmail.php"
	url = url+"?emailId="+emailId
	xmlHttp.onreadystatechange = stateChangedEmail
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)	
}

// Checking browser compatabile
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

// State Changed 
function stateChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		document.getElementById("errorMess").innerHTML = xmlHttp.responseText 

	}
	else{
		document.getElementById("errorMess").innerHTML = "Loading.....";
	} 
}


// State Changed 
function stateChangedEmail() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		document.getElementById("errorMess").innerHTML = xmlHttp.responseText 

	}
	else{
		document.getElementById("errorMess").innerHTML = "Loading.....";
	} 

} 

