var http = getHTTPObject();
var timeout;
function getHTTPObject(){
	if(window.XMLHttpRequest){
        req = new XMLHttpRequest();
    }
	else if(window.ActiveXObject){
        req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return req;
}

/*
 * send the ajax request
 * sendpage: page we're sending to
 * params: parameters to pass
 * fname: function to call when call is completed
 * method: get/post
 * timed: whether or not to alert a timeout message
 */
function sendme(sendpage, params, fname, method, timed){
	if(!callInProgress(http)){
		if(method == 'POST' || method == 'post'){
			http.open('POST', sendpage, true);
			timeout = window.setTimeout(
				function(){
					if(!checkHTTPStatus()){
						http.abort();
						if(timed){
							callFailed();
						}
						removeLoader();
					}
				}, 
				5000);
			http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');			
			http.onreadystatechange = eval(fname);
			http.send(params);
		}
		else{
			http.open('GET', sendpage + params, true);
			timeout = window.setTimeout(
				function(){
					if(!checkHTTPStatus()){
						http.abort();
						if(timed){
							callFailed();
						}
						removeLoader();
					}
				}, 
				5000);
			http.onreadystatechange = eval(fname);
			http.send(null);
		}
	}
	else{
		removeLoader();
	}
}

// check to see if http is done with call and returned correct code
function checkHTTPStatus(){
	if(!callInProgress(http) && http.status == 200){
		window.clearTimeout(timeout);
		return true;
	}
	else{
		return false;
	}
}

// check to see if an http call is still in progress
function callInProgress(obj){
	switch(obj.readyState){
		case 1:
		case 2:
		case 3:
		return true;
		break;
		
		default:
		return false;
		break;
	}
}

// show a timeout message
function callFailed(){
	alert('Call failed, please try again.');
}

// write a "loading" image to the center of the screen
function addLoader(){
	/*
	 * check to see if the loader exists
	 */
	if(!document.getElementById('http_loader')){
		var w = screen.width;
		var h = screen.height;
		var loader = document.createElement('div');
		loader.setAttribute('id', 'http_loader');
		loader.setAttribute('style', 'position:fixed;top:50px;left:50px;background-color:#fff;padding:2px;border;1px solid #999');
		loader.innerHTML = '<div style="padding:2px;border:1px solid #999;background-color:#fff;"><b>LOADING DATA</b><br /><img src="/images/loading.gif" alt="loading data" /></div>';
		document.body.appendChild(loader);
	}
	else{
		document.getElementById('http_loader').style.display = 'block';
	}
}
function removeLoader(){
	if(document.getElementById('http_loader')){
		document.getElementById('http_loader').style.display = 'none';
	}
}
// null function
function foo(){}