var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...
	XMLHttpRequestObject = new XMLHttpRequest();
	if (XMLHttpRequestObject.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		XMLHttpRequestObject.overrideMimeType('text/html');
	}
}
else if (window.ActiveXObject) { // IE
	try {
		XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try {
			XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
}

function loadAJAXQueue() {
	AJAXQueue = new AJAXQueueClass();
}

function AJAXQueueFinish( result ) {
	return AJAXQueue.finishInstruction( result );
}

addEvent( window, "load", loadAJAXQueue, false );

function AJAXInstruction() {
	this.type = null;
	this.update = false;
	this.source = null;
	this.callback = false;
	this.data = null;
	this.divID = null;

	this.execute = function( callback ) {
		//alert('executing' + this.type + ', ' + this.source + ', ' + this.divID + ', ' + this.callback );

		if( this.type == "post" ) {
			if( this.update == true ) {
				postDataUpdateCallback( this.source, this.data, this.divID, callback );
			}
			else {
				callback( postData( this.source, this.data ) );
			}
		}
		else if( this.type == "get" ) {
			if( this.update == true ) {
				getDataUpdateCallback( this.source, this.divID, callback );
			}
			else {
				getData( this.source, callback );
			}
		}
	}
}

function AJAXQueueClass() {
	this.active = false;
	this.current_instruction = false;

	this.instructions = new Array();

	this.addGetInstruction = function( source, divID, callback ) {
		var instruction = new AJAXInstruction();
		instruction.type = "get";
		instruction.source = source;

		if( typeof(callback) == 'function' ) {
			instruction.callback = callback;
		}
		
		if( divID ) {
			instruction.update = true;
			instruction.divID = divID;
		}

		this.instructions.push(instruction);
		this.nextInstruction();
	}

	this.addPostInstruction = function( source, data ) {
		var instruction = new AJAXInstruction();
		instruction.type = "post";
		instruction.source = source;
		instruction.data = data;

		this.instructions.push(instruction);
		this.nextInstruction();
	}

	this.addPostInstruction = function( source, data, callback ) {
		var instruction = new AJAXInstruction();
		instruction.type = "post";
		instruction.source = source;
		instruction.data = data;
		instruction.callback = callback;

		this.instructions.push(instruction);
		this.nextInstruction();
	}
	
	this.addPostInstruction = function( source, data, divID, callback ) {
		var instruction = new AJAXInstruction();
		instruction.type = "post";
		instruction.source = source;
		instruction.data = data;

		if( typeof(callback) == 'function' ) {
			instruction.callback = callback;
		}

		if( divID ) {
			instruction.update = true;
			instruction.divID = divID;
		}

		this.instructions.push(instruction);
		this.nextInstruction();
	}

	this.finishInstruction = function( result ) {
		this.active = false;

		if( typeof( this.current_instruction.callback ) == 'function' ) {
			var callback = this.current_instruction.callback;
			this.current_instruction = null;
			callback( result );
		}

		this.nextInstruction();
	}
	
	this.nextInstruction = function() {
		if( this.active == false ) {
			if( this.instructions.length > 0 ) {
				this.current_instruction = this.instructions.shift();

				this.active = true;
				this.current_instruction.execute( AJAXQueueFinish );
			}
		}
	}
}


function getData(dataSource,callback)
{
        if(XMLHttpRequestObject) {
                XMLHttpRequestObject.open("GET", dataSource);

                XMLHttpRequestObject.onreadystatechange = function()
                {
                        if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
							callback(XMLHttpRequestObject.responseText);
                        }
                }

                XMLHttpRequestObject.send(null);
        }
}

function postData(dataSource,data)
{        
        if(XMLHttpRequestObject) {
                XMLHttpRequestObject.open("POST", dataSource);
                XMLHttpRequestObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

                XMLHttpRequestObject.onreadystatechange = function()
                {
                        if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                                return XMLHttpRequestObject.responseText;
                        }
                }

		for(var i = 0; i< data.length; i++)
		{
			XMLHttpRequestObject.send(data[i].name + "=" + data[i].value);
		}
        }
}


function getDataUpdate(dataSource, divID)
{
	if(XMLHttpRequestObject) {
		var obj = getRef( divID );
		XMLHttpRequestObject.open("GET", dataSource);

		XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				obj.innerHTML = XMLHttpRequestObject.responseText;
			}
		}
		
		XMLHttpRequestObject.send(null);
	}
}

function getDataUpdateCallback(dataSource, divID, callback)
{
	if(XMLHttpRequestObject) {
		var obj = getRef( divID );
		XMLHttpRequestObject.open("GET", dataSource);

		XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				obj.innerHTML = XMLHttpRequestObject.responseText;
				callback( XMLHttpRequestObject.responseText );
			}
		}
		
		XMLHttpRequestObject.send(null);
	}
}

function encodeFormData( form ) {
	var formob = getRef( form );

	var poststr = '';

	var e = formob.elements;
	for ( var elem, i = 0; ( elem = e[i] ); i++ )
	{
		if( elem.name.length > 0 ) {
			if( elem.type == 'checkbox' ) {
				if( elem.checked == true ) {
					poststr = poststr + elem.name + "=" + encodeURI( elem.value ) + "&";
				}
				else {
					poststr = poststr + elem.name + "=" + "&";
				}
			}
			else if( elem.type == 'radio' ) {
				if( elem.checked == true ) {
					poststr = poststr + elem.name + "=" + encodeURI( elem.value ) + "&";
				}
			}
			else if( elem.type == 'select-multiple' ) {
				var k = 0;
				for( var j = 0; j < (elem.options.length); j++ ) {
					if( elem.options[j].selected == true ) {
						poststr = poststr + elem.name + "=" + encodeURI( elem.options[j].value ) + "&";
						k++;
					}
				}
				if( k == 0 ) {
					poststr = poststr + elem.name + "=" + "&";
				}
			}
			else {
				poststr = poststr + elem.name + "=" + encodeURI( elem.value ) + "&";
			}
		}
	}

	return poststr;
}

function postDataUpdate(dataSource,data,divID)
{
	if(XMLHttpRequestObject) {
		var obj = getRef( divID );

		XMLHttpRequestObject.open("POST", dataSource);
		XMLHttpRequestObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		XMLHttpRequestObject.setRequestHeader("Content-length", data.length);
		XMLHttpRequestObject.setRequestHeader("Connection", "close");

		XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				obj.innerHTML = XMLHttpRequestObject.responseText;
			}
		}

		XMLHttpRequestObject.send(data);
	}
}

function postDataUpdateCallback(dataSource,data,divID,callback)
{
	if(XMLHttpRequestObject) {
		var obj = getRef( divID );

		XMLHttpRequestObject.open("POST", dataSource);
		XMLHttpRequestObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		XMLHttpRequestObject.setRequestHeader("Content-length", data.length);
		XMLHttpRequestObject.setRequestHeader("Connection", "close");

		XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				obj.innerHTML = XMLHttpRequestObject.responseText;
				callback( XMLHttpRequestObject.responseText );
			}
		}

		XMLHttpRequestObject.send(data);
	}
}
