function checkfields()
{
	if (document.theform.username.value=="" || document.theform.password.value=="")
	{
		alert ("Not all fields were filled by you. Please go back and re enter all information.");
		return false;
	}
	else
	{
			processlogin(document.theform.username.value,document.theform.password.value);
	
	}
}

function alertkey(e) {
  if( !e ) {
    if( window.event ) {
     //Internet Explorer
      e = window.event;
    } else {
      return;
    }
  }
  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
      e = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
      e = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
      e = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }
  if(e==13) {
      processlogin(document.theform.username.value,document.theform.password.value);
     return false;
  }
}


var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		for (var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e) { }
		}
	}
	if (!xmlHttp)
		alert("Error Creating the XML HttpRequest Object");
		else
			return xmlHttp;
}

function processlogin(username,password)
{
	document.getElementById('loginform').innerHTML='<p>Logging in. Please be patient...</p>';
	if (xmlHttp)
	{
		try
		{
			
			var divelement = "loginform";
				xmlHttp.open("GET", '/site/processors/processLogin.php?username='+username+'&password='+password, true);
		
			xmlHttp.onreadystatechange = 
			
			function handleRequestStateChange()
{
	
	myDiv = document.getElementById(divelement);
	if (xmlHttp.readyState == 1)
	{
		myDiv.innerHTML = "<p>Sending your request...</p>";
	}
	else if (xmlHttp.readyState==2)
	{
		myDiv.innerHTML = "<p>Sending your request...</p>";

	}
	else if (xmlHttp.readyState==4)
	{
		if (xmlHttp.status == 200)
		{
			try
			{

				var response = xmlHttp.responseText;
				myDiv.innerHTML = ""+response+"";
				var checksuccess = response.substring(0,8);
				if (checksuccess=='<!--1-->')
					location.href='index.php';
				
			}
			catch(e)
			{
				alert("Technical Error when logging in. Contact Donal: donaloconnor@gmail.com");
			}
		}
		else
		{
		alert("There was a problem retrieving content:\n" + xmlHttp.statusText);
		}
	}


}
			
			xmlHttp.send(null);
		}
		catch(e)
		{
			alert ("Can't connect to server:\n" + e.toString());
		}
	}
}
