
/* Start AJAX functions */
var xmlhttp;

//AJAX call to ensure that username/email is not already in DB
function checkReg(param, str)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
		  alert ("Browser does not support HTTP Request");
		  return;
	  }
	 
	var url="check_regparams.php";

	if(param == "username")
	{
		var username = str;
		url=url+"?username="+str;
		url=url+"&sid="+Math.random();
		xmlhttp.onreadystatechange=stateChangedUsername;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	
	if(param == "email")
	{
		var email = str;
		url=url+"?email="+str;
		url=url+"&sid="+Math.random();
		xmlhttp.onreadystatechange=stateChangedEmail;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}

}

//Get response for username error
function stateChangedUsername()
{
	
	if (xmlhttp.readyState==4)
	{		
		document.getElementById("error_username_unavailable").innerHTML=xmlhttp.responseText;
		if(xmlhttp.responseText == "That username is not available.  Please select a different username.")
		{
			errorBox("reg_username");
			document.getElementById("nextInput").style.display = "none";
		}
		else
		{
			restoreBox("reg_username");
			document.getElementById("nextInput").style.display = "";
		}
	}
}

//Get response for div error
function stateChangedEmail()
{
	
	if (xmlhttp.readyState==4)
	{				
		document.getElementById("error_email_unavailable").innerHTML=xmlhttp.responseText;
		if(xmlhttp.responseText == "You are already registered.  Please login.")
		{
			errorBox("reg_email");
			document.getElementById("nextInput").style.display = "none";
			window.location = "login.php"

		}
		else
		{
			restoreBox("reg_email");
			document.getElementById("nextInput").style.display = "";
		}
	}
}


function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {
		  // code for IE7+, Firefox, Chrome, Opera, Safari
		  return new XMLHttpRequest();
	  }
	if (window.ActiveXObject)
	  {
		  // code for IE6, IE5
		  return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return null;
}
/* End AJAX functions */

//Verfies field info onblur
function checkField(div1, value, param, errordiv1)
{
	if(checkRegSetValue(div1, errordiv1))
	{
		checkReg(param, value);
	}
}
	
//Shows error div for empty value
function checkRegSetValue(value, errordiv)
{
	if(document.getElementById(value).value.length <=  1)
	{
		document.getElementById(errordiv).style.display = "";
		return false;
	}
	else
	{
		document.getElementById(errordiv).style.display = "none";
		return true;
	}
}
	
//Highlights border red for field with errors
function errorBox(id)
{
	document.getElementById(id).style.borderColor = "red";
	document.getElementById(id).style.borderStyle = "solid";
	document.getElementById(id).style.borderWidth = "2px";
}

//Restore text input border
function restoreBox(id)
{
	document.getElementById(id).style.borderColor = "#7F9DB9";
	document.getElementById(id).style.borderWidth = "1px";
}

//Validates directory user registration form
function validateDirRegForm()
{
	var error = false;
	
	if(document.getElementById("reg_username").value.length == 0)
	{
		errorBox("reg_username");
		error = true;
	}
	if(document.getElementById("error_username_unavailable").value == "That username is not available. Please select a different username,")
	{
		errorBox("reg_username");
		error = true;
	}
	else
	{
		restoreBox("reg_username")
	}
		
	if(document.getElementById("reg_pw").value.length == 0)
	{
		errorBox("reg_pw");
		error = true;
	}
	else
	{
		restoreBox("reg_pw")
	}
	
	if(document.getElementById("reg_email").value.length == 0)
	{
		errorBox("reg_email");
		error = true;
	}
	else
	{
		restoreBox("reg_email")
	}
	
	if(error == true)
	{
		return false;
	}
}
