var xmlHttp = null;
var content_area = null;

// GET Content from URL and insert it into Element
function GetContent(element, url) {
	// Get the reference to the HTML Element the content will be placed in
	if (element.length > 0) { 
		content_area = document.getElementById(element);
	} else {
		return false;
	}

	xmlHttp = GetXmlHttpObject();

	if (xmlHttp === null) {
		alert ('Please Enable Javascript in your browser, and make sure your Browser is up to date.');
		return false;
	} 

	xmlHttp.onreadystatechange = StateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


// POST Content from URL and insert it into Element
function POSTContent(element, url, form_object) {
	// Get the reference to the HTML Element the content will be placed in
	if (element.length > 0) { 
		content_area = document.getElementById(element);
		
		if (content_area === null) {
			//alert('The Content Area could not be found...');
			return false;
		}
	} else {
		return false;
	}

	xmlHttp = GetXmlHttpObject();

	if (xmlHttp === null) {
		alert ('Please Enable Javascript in your browser, and make sure your Browser is up to date.');
		return false;
	}
	
	// Prepare Form Data
	post_data = PrepareFormPOST(form_object);

	xmlHttp.onreadystatechange = StateChanged;
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", post_data.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(post_data);
}


// This function is run every time OnReadyState changes
function StateChanged() {
	switch (xmlHttp.readyState) {
		case 0:
			// Object is not initialized with data
			break;
		case 1:
			// Object is loading its data
			//content_area.innerHTML = '<p>Please Wait. Loading...</p>';
			break;
		case 2:
			// Object has finished loading its data.
			break;
		case 3:
			// User can interact with the object even though it is not fully loaded.
			break;
		case 4:
			// The request is complete
			content_area.innerHTML = xmlHttp.responseText;
			break;
		default:
			// Unknown ReadyState
	}
}


// Get instance of XMLHTTPRequest Object
function GetXmlHttpObject() {
	var xmlHttp = null;

	try {
		// Most Browsers: Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e1) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e2) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}


// Prepare Form Data for POST
function PrepareFormPOST(form_object) {
	var pairs = new Array();

	for (var i = 0; i < form_object.elements.length; i++) {
		pairs.push(form_object.elements[i].name + "=" + encodeURIComponent(form_object.elements[i].value));
	}
	
	// Return Formatted POST String
	return pairs.join('&');
}