//Ajax = get_ajax();
var ajax_array = new Array();
var ajax_controller = new Array();
var element_controller = new Array();
ajax_controller[0] = 0; //0 = availiable, 1 = taken
//when a send ajax is called
//loop through the length of ajax_controller, 1 = in use, 0 = availiable
//if it cant find one thats 0, push to the end. when ajax send_complete ajax = 0 ajax_array[] - get_ajax()


var responce;
var overlay;

function get_ajax(){
	//incase the controller is full, the ajax id is the length
	var ajax_id = ajax_controller.length;
	
	for (i = 0; i < ajax_controller.length; i++) {
		if (ajax_controller[i] == 0) {
			ajax_id = i;
			break;
		}
	}
	
	ajax_controller[ajax_id] = 1;
	
	//create the ajax request
	if (window.XMLHttpRequest) { // Non-IE browsers
		ajax_array[ajax_id] = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // IE
		ajax_array[ajax_id] = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return ajax_id;
}


function send_ajax(params, url, type, get_reply, callback_function, element){
	//when a send ajax is called
	//loop through the length of ajax_controller, 1 = in use, 0 = availiable
	//if it cant find one thats 0, push to the end. when ajax send_complete ajax = 0 ajax[] - get_ajax()
	
	var ajax_id = get_ajax();
	ajax_id = parseInt(ajax_id);
	ajax_array[ajax_id].open(type, url, true);
	responce = "";
	element_controller[ajax_id] = element;
	
	params += "&ajax_id=" + ajax_id
	
	//Send the proper header information along with the request
	ajax_array[ajax_id].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax_array[ajax_id].setRequestHeader("Content-length", params.length);
	ajax_array[ajax_id].setRequestHeader("Connection", "close");
	
	
	if (get_reply) {
		if (callback_function) {
			ajax_array[ajax_id].onreadystatechange = function(event){
				if (ajax_ready(ajax_id)) {
					eval(callback_function + "(" + ajax_id + ")");
					ajax_null(ajax_id);
				}
			}
		}
		else {
			ajax_array[ajax_id].onreadystatechange = function(){//Call a function when the state changes.
				if (ajax_ready(ajax_id)) {
					responce = ajax_array[ajax_id].responseText;
					alert(responce);
					ajax_null(ajax_id);
				}
			}
		}
	}
	//send the request
	ajax_array[ajax_id].send(params);
}


function ajax_ready(ajax_id){
	if (ajax_array[ajax_id].readyState == 4 && ajax_array[ajax_id].status == 200) {
		return true;
	}
	else {
		return false;
	}
}

function ajax_null(ajax_id){
	ajax_controller[ajax_id] = 0;
}
