var ajax = false;
var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADING       = 1;
var READY_STATE_LOADED        = 2;
var READY_STATE_INTERACTIVE   = 3;
var READY_STATE_COMPLETE      = 4;

function AjaxCall(url, params, httpMethod, callBack) {
	httpMethod = (httpMethod == 'POST')? 'POST' : 'GET';
	ajax = false;

	if (!params) {
		params = null;
	}

	// create the XMLHttp object (Cross-Browsers)
	if (window.XMLHttpRequest) {       // Mozilla, Safari,...
		ajax = new XMLHttpRequest();
		if (ajax.overrideMimeType) {
			ajax.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // Internet Explorer
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	// unable to create XMLHTTP objects
	if (!ajax) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	if (httpMethod == 'GET') {
		url = url + '?' + params;
	}

	ajax.onreadystatechange = eval(callBack);
	ajax.open(httpMethod, url, true);

	if (httpMethod == 'POST') {
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	ajax.send(params);
}

function alertContents() {
	if (ajax.readyState != READY_STATE_COMPLETE) {
		document.getElementById('loadingBox').top = screen.availHieght / 2;
		document.getElementById('loadingBox').left = screen.availWidth / 2;
		document.getElementById('loadingBox').style.display = '';
		document.getElementById('contentBody').innerHTML = '';
	}
	if (ajax.readyState == READY_STATE_COMPLETE) {
		document.getElementById('loadingBox').style.display = 'none';
		if (ajax.status == 200) {
			document.getElementById('contentBody').innerHTML = ajax.responseText;
			document.getElementById('contentBody').style.display = '';
		} else {
			alert('There was a problem with the request. Error: ' + ajax.status);
		}
	}
}

function toggleDivGroup(div, active) {
	children = document.getElementById(div).childNodes;
	for(i=0; i<=children.length-1; i++) {
		if (children[i].id == active) {
			children[i].style.display = '';
		} else {
			children[i].style.display = 'none';
		}
	}
}

function toggleForm(formName, focusElement) {		
	if (document.getElementById(formName).style.display=='') {
		document.getElementById(formName).style.display='none';
	} else {
		document.getElementById(formName).style.display='';
		document.getElementById(focusElement).focus();
	}
}