// stores the reference to the XMLHttpRequest object

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{  
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)


    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function checkValidUsername(username)
{
    var filter=/^([a-zA-Z0-9_\.])+$/;
    if (filter.test(username)) {
        return true;
    } else {
        return false;
    }
}
var xmlHttp = null;
// make asynchronous HTTP request using the XMLHttpRequest object 
function CheckUserName()
{
	var username = document.getElementById("user_name").value;
	if(checkValidUsername(username))
	{
		xmlHttp = createXmlHttpRequestObject(); 
		// proceed only if the xmlHttp object isn't busy
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
		{
		 xmlHttp.open("GET", "checkusername.php?username=" + username, true);  
		// define the method to handle server responses
		xmlHttp.onreadystatechange = ShowMessege;
		// make the server request
		xmlHttp.send(null);
		}
		else
		// if the connection is busy, try again after one second  
		setTimeout(CheckUserName,'1000');
	}
	else
	{
		document.getElementById("username_status").innerHTML = ErrorMsg("Please enter a valid username, only letters, numbers and symbols('_' and '.') are allowed.");
		return false;
	}
}

// executed automatically when a message is received from the server
function ShowMessege()
{
  // move forward only if the transaction has completed
   if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // update the client display using the data received from the server
	  	document.getElementById("username_status").innerHTML = xmlHttp.responseText ;
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}


// make the changes in the email address varification 
function CheckEmail()
{
	if(trim(document.getElementById("email").value)=="")
	{
		document.getElementById("emailStatus").innerHTML = ErrorMsg("Please enter your email address.");
		return false;
	}
	var str=document.getElementById("email").value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (!filter.test(str))
	{
		document.getElementById("emailStatus").innerHTML = ErrorMsg("Please enter a valid email address.");	
		return false;
	}
	else
	{
		document.getElementById("emailStatus").innerHTML = SuccessMsg("");	
		return true;
	}
}

function CheckEmailStatus()
{
	var emailstatus = document.getElementById("email").value;
	if(CheckEmail(emailstatus))
	{
		xmlHttp = createXmlHttpRequestObject(); 
		// proceed only if the xmlHttp object isn't busy
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
		{
		 xmlHttp.open("GET", "checkemail.php?emailstatus=" + emailstatus, true);  
		// define the method to handle server responses
		xmlHttp.onreadystatechange = ShowMess;
		// make the server request
		xmlHttp.send(null);
		}
		else
		// if the connection is busy, try again after one second  
		setTimeout(CheckEmailStatus,'1000');
	}
	else
	{
		document.getElementById("emailStatus").innerHTML = ErrorMsg("Please enter a valid email address.");
		return false;
	}
}

// executed automatically when a message is received from the server
function ShowMess()
{
  // move forward only if the transaction has completed
   if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // update the client display using the data received from the server
	  	document.getElementById("emailStatus").innerHTML = xmlHttp.responseText ;
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}