function echeck(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1) { return false; }
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) { return false; }
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) { return false; }
	if (str.indexOf(at,(lat+1))!=-1) { return false; }
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) { return false; }
	if (str.indexOf(dot,(lat+2))==-1) { return false; }
	if (str.indexOf(" ")!=-1) { return false; } 
	return true					
}
 
// Holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
		
		// try every prog id until one works
		for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e)
			{
				// ignore potential error
			}
		}
	}
	
	// return the created object or display an error message
	if(!xmlHttp)
	{
		alert("Error creating the XMLHttpRequest object.");
	}
	else
	{
		return xmlHttp;
	}
}

// Read a file from the server
function formSubmit(formName)
{
	// Only continue if xmlHttp isn't void
	if(xmlHttp)
	{
		// Try to connect to the server
		try
		{
			// Initiate the asynchronous HTTP request
			var rand = Math.floor(Math.random()*999999)
			// Process the form fields
			if(formName == "incident")
			{
				if(document.getElementById("General").checked == true) { var priority = "General"; }
				else if(document.getElementById("Standard").checked == true) { var priority = "Standard"; }
				else if(document.getElementById("Urgent").checked == true) { var priority = "Urgent"; }
				else { var priority = "System Down"; }
				var name = escape(document.getElementById("incident_name").value);
				var company = escape(document.getElementById("incident_company").value);
				var email = escape(document.getElementById("incident_email").value);
				var module = escape(document.getElementById("incident_module").value);
				var desc = escape(document.getElementById("incident_desc").value);
				var previous = escape(document.getElementById("incident_previous").value);
				var location = escape(document.getElementById("incident_location").value);
				
				var error = "";
				var ec = 0;
				if(name == "") { error += "<p class=\"toggle\">You failed to include your contact name</p>"; ec++; }
				if(company == "") { error += "<p class=\"toggle\">You failed to include your company</p>"; ec++; }
				if(email == "") { error += "<p class=\"toggle\">You failed to include your email</p>"; ec++; }
				else if(echeck(email) == false) { error += "<p class=\"toggle\">Your email does not appear to be valid</p>"; ec++; }
				if(module == "") { error += "<p class=\"toggle\">You failed to include your module</p>"; ec++; }
				if(desc == "") { error += "<p class=\"toggle\">You failed to include your problem</p>"; ec++; }
				
				if(ec > 0)
				{
					document.getElementById("incident_error").innerHTML = error;
				}
				else
				{
					document.getElementById("incident_error").innerHTML = "<p class=\"toggle\">Sending...</p>";
					//alert("/ajax-account-management-incident.php?rand=" + rand + "&priority=" + priority + "&name=" + name + "&module=" + module + "&desc=" + desc + "&previous=" + previous + "&location=" + location);
					xmlHttp.open("GET","/ajax-account-management-incident.php?rand=" + rand + "&priority=" + priority + "&name=" + name + "&company=" + company + "&email=" + email + "&module=" + module + "&desc=" + desc + "&previous=" + previous + "&location=" + location,true);
					xmlHttp.onreadystatechange = handleRequestStateChange;
					xmlHttp.send(null);
				}
			}
			else
			{
				var name = escape(document.getElementById("cr_name").value);
				var company = escape(document.getElementById("cr_company").value);
				var email = escape(document.getElementById("cr_email").value);
				var number = escape(document.getElementById("cr_number").value);
				var details = escape(document.getElementById("cr_details").value);
				
				var error = "";
				var ec = 0;
				if(name == "") { error += "<p class=\"toggle\">You failed to include your contact name</p>"; ec++; }
				if(company == "") { error += "<p class=\"toggle\">You failed to include your company</p>"; ec++; }
				if(email == "") { error += "<p class=\"toggle\">You failed to include your email</p>"; ec++; }
				else if(echeck(email) == false) { error += "<p class=\"toggle\">Your email does not appear to be valid</p>"; ec++; }
				if(number == "") { error += "<p class=\"toggle\">You failed to include your contact number</p>"; ec++; }
				if(details == "") { error += "<p class=\"toggle\">You failed to include your request details</p>"; ec++; }
				
				if(ec > 0)
				{
					document.getElementById("cr_error").innerHTML = error;
				}
				else
				{
					document.getElementById("cr_error").innerHTML = "<p class=\"toggle\">Sending...</p>";
					//alert("/ajax-account-management-cr.php?rand=" + rand + "&name=" + name + "&company=" + company + "&email=" + email + "&number=" + number + "&details=" + details);
					xmlHttp.open("GET","/ajax-account-management-cr.php?rand=" + rand + "&name=" + name + "&company=" + company + "&email=" + email + "&number=" + number + "&details=" + details,true);
					xmlHttp.onreadystatechange = handleRequestStateChange;
					xmlHttp.send(null);
				}
			}
			
			
		}
		catch(e)
		{
			// Display the error in case of failure
			//alert("Can't connect to server:\n" + e.toString());
		}
	}
}

// Functioncalled when the state of the HTTP request changes
function handleRequestStateChange()
{
	// When readystate is 4, we are ready to read the server response
	if(xmlHttp.readyState == 4)
	{
		// Continue only if HTTP status is OK
		if(xmlHttp.status == 200)
		{
			try
			{
				// Do something with the response from the server
				handleServerResponse();
			}
			catch(e)
			{
				// Display error message
				//alert("Error reading the response: " + e.toString());
			}
		}
		else
		{
			// Display status message
			//alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}

// Handles the response received from the server
function handleServerResponse()
{
	// Retrieve the server's response
	var errorReport = xmlHttp.responseText;
	
	if(errorReport == "1")
	{
		document.getElementById("incident_div").style.display = "none";
		document.getElementById("incident_error").innerHTML = "<p class=\"toggle\">Your incident request has been logged and a member of the team will be in contact shortly</p>";		
	}
	else
	{
		document.getElementById("cr_div").style.display = "none";
		document.getElementById("cr_error").innerHTML = "<p class=\"toggle\">Your call has been logged and a member of the team with be in contact within 24 hours.</p>";
	}
}