function validateForm()
{
	name=document.getElementById("name").value;
	email=document.getElementById("email").value;
	if (document.getElementById("location_0").checked)
	{
		ipRegistration=document.getElementById("location_0").value;
	} else
	{
		ipRegistration=document.getElementById("location_1").value;
	}
	ip=document.getElementById("IP").value;
	department=document.getElementById("department").value;
	comments=document.getElementById("comments").value;
	noError = true;
	if (!name)
	{
		document.getElementById("nameAlert").innerHTML="Name required";
		noError = false;
	} else {
		document.getElementById("nameAlert").innerHTML="";
	}
	var emailMatch = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if (!email || !(email.match(emailMatch)))
	{
		document.getElementById("emailAlert").innerHTML="Invalid e-mail";
		noError = false;
	} else {
		document.getElementById("emailAlert").innerHTML="";
	}
	var ipMatch = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	if (!ip || !(ip.match(ipMatch)))
	{
		document.getElementById("ipAlert").innerHTML="Invalid IP";
		noError = false;
	} else {
		document.getElementById("ipAlert").innerHTML="";
	}															   
	return noError;
}
function getPage()
{
	url="AccessRequest.php";
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4)
		{
			document.getElementById("main").innerHTML = xmlhttp.responseText;			
		}
	}
	xmlhttp.send();
}
function submitForm()
{
	loadingText='<center><img src="loading.gif" width="32" height="32" alt="loading..." /></center>';
	if (validateForm())
	{
		name=document.getElementById("name").value;
		email=document.getElementById("email").value;
		if (document.getElementById("location_0").checked)
		{
			ipRegistration=document.getElementById("location_0").value;
		} else
		{
			ipRegistration=document.getElementById("location_1").value;
		}
		ip=document.getElementById("IP").value;
		department=document.getElementById("department").value;
		comments=document.getElementById("comments").value;
		postData="name=" + name + "&email=" + email + "&ipRegistration=" + ipRegistration + "&ip=" + ip + "&department=" + department + "&comments=" + escape(comments);
		document.getElementById("main").innerHTML=loadingText;
		url="AccessRequest.php";
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open("POST",url,true);
		
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", postData.length);
		xmlhttp.setRequestHeader("Connection", "close");
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4)
			{
				document.getElementById("main").innerHTML=xmlhttp.responseText;
			}
		}
		xmlhttp.send(postData);
	}
}
document.onload=getPage();