handleResponse = function(result)
{
	document.getElementById("loading").style.display = "none";
	document.getElementById("complete").style.display = "block";
    document.getElementById("completeText").innerHTML = result;            
}

get = function(obj) 
{
	var ajax = new Ajax();
    var poststr = "email=" + encodeURI( document.getElementById("email").value ) +
		"&name=" + encodeURI(document.getElementById("name").value ) +
        "&message=" + encodeURI( document.getElementById("message").value ) + 
		"&Hidden=" + encodeURI( document.getElementById("Hidden").value );
	document.getElementById("overlay").style.display = "block";
    ajax.doPost('sendmail.php', handleResponse, poststr);
}

function Ajax()
{
	//Class properties
	this.req = null;
	this.url = null;
	this.method = "GET";
	this.async = true;
	this.status = null;
	this.statusText = '';
	this.postData = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXML = null;
	this.handleResp = null;
	this.responseFormat = 'text'; //'text', 'xml', or 'object'
	this.mimeType = null;
	
	//Class methods
	this.init = function()
	{
		if (!this.req)
		{
			try
			{
				//Try to create an object for Firefox, Safari, Opera, IE7, etc.
				this.req = new XMLHttpRequest();
			}
			catch(e)
			{
				try
				{
					//Try to create an object for later versions of IE
					this.req = new ActiveXObject('MSXML2.XMLHTTP');
				}
				catch(e)
				{
					try
					{
						//Try to create an object for early versions of IE
						this.req = new ActiveXObject('Microsoft.XMLHTTP');
					}
					catch(e)
					{
						//Could not create an XMLHttp request object
						return false;
					}
				}
			}
		}
		return this.req;
	};
	
	this.doReq = function()
	{
		if (!this.init())
		{
			alert('Could not create XMLHttpRequest Object.');
			return;
		}
		
		this.req.open(this.method, this.url, this.async);
		var self = this; //Fix loss of scope in inner function
		
		if (this.method = "POST")
		{
			this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.req.setRequestHeader("Content-length", this.postData.length);
			this.req.setRequestHeader("Connection", "close");
		}
		
		this.req.onreadystatechange = function()
		{
			var resp = null;
			if (self.req.readyState == 4)
			{
				switch(self.responseFormat)
				{
					case 'text':
						resp = self.req.responseText;
						break;
					case 'xml':
						resp = self.req.responseXML;
						break;
					case 'object':
						resp = req;
						break;
				}
				
				if (self.req.status >= 200 && self.req.status <= 299)
				{
					self.handleResp(resp);
				}
				else
				{
					self.handleErr(resp);
				}
			}
		};
		this.req.send(this.postData);
	};
	
	this.handleErr = function()
	{
		var errorWin;
		try
		{
			errorWin = window.open('', 'errorWin');
			errorWin.document.body.innerHTML = this.responseText;
		}
		catch(e)
		{
			alert('An error occurred, but the error message cannot be '
			+ 'displayed. This is probably because of your browser\'s '
			+ 'pop-up blocker.\n'
			+ 'Please allow pop-ups from this web site if you want to '
			+ 'see the full error messages.\n'
			+ '\n'
			+ 'Status Code: ' + this.req.status + '\n'
			+ 'Status Description: ' + this.req.statusText);
		}
	};
	
	//use a custom error handling method
	this.setHandlerErr = function(funcRef)
	{
		this.handleErr = funcRef;
	};
	
	//use a single handler for both error and success
	this.setHandlerBoth = function(funcRef)
	{
		this.handleErr = funcRef;
		this.handleResp = funcRef;
	};
	
	//Stop the ajax function
	this.abort = function()
	{
		if (this.req)
		{
			this.req.onreadystatechange = function() {};
			this.req.abort();
			this.req = null;
		}
	};
	
	//Set properties and start request
	//Required parameters: URL of the page to request, and the event handler function to handle the succesfull response from the server
	//Optional parameters: format which can be set to text, xml, or object.  If not set it will default to text
	this.doGet = function(url, hand, format)
	{
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.doReq();
	};
	
	//Same as above except for POST instead of GET
	this.doPost = function(url, hand, postData, format)
	{
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.postData = postData;
		this.method = "POST";
		this.doReq();
	};
}
