//Basic Ajax Routine- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated: Jan 15th, 06'

function createAjaxObj(){
	var httprequest=false
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		httprequest=new XMLHttpRequest()
		if (httprequest.overrideMimeType) 
			httprequest.overrideMimeType('text/xml')
				
	}else if (window.ActiveXObject){ // if IE
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");			
		}catch (e){
			try{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){}
		}
	}
	return httprequest
}
	
function ajaxPackObj(){
	oThis = this;	//to enable us to reference top level obj in methods
	this.bExecuting = false;
	this.basedomain="http://"+window.location.hostname;
	this.filetype="txt";
	this.addrandomnumber=0; //Set to 1 or 0. See documentation.
	this.ajaxobj = new createAjaxObj();
	this.executed = function(){
		return ! oThis.bExecuting;
	}
	this.getAjaxRequest=function(url, parameters, callbackfunc, filetype, oncallfunc){
		oThis.ajaxobj = new createAjaxObj(); //recreate ajax object to defeat cache problem in IE
		if (oThis.addrandomnumber==1) //Further defeat caching problem in IE?
			var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
		if (oThis.ajaxobj){
			if (oThis.bExecuting && parseInt(oThis.ajaxobj.readyState) < 2) {
				try {
					window.status = 'Aborting';
					oThis.ajaxobj.abort();
					}
				catch (e) {
					window.status = 'Abort failed';
				}
				oThis.bExecuting = false;
			}
			if(!oThis.bExecuting){
				oThis.bExecuting = true;
				oThis.filetype=filetype
				oThis.ajaxobj.onreadystatechange=function(){
					
					var sState = oThis.ajaxobj.readyState
					if(oThis.ajaxobj.readyState == 4){
						oThis.bExecuting = false;
						callbackfunc(oThis.ajaxobj);
					}

					if(oThis.ajaxobj.readyState == 0){  //something's gone wrong... 0 is for uninitialised. Repost the request.
						
						oThis.bExecuting = false;
						if (oncallfunc){
							ajaxpack.getAjaxRequest(url, parameters, callbackfunc, filetype, oncallfunc);}
						else {
							ajaxpack.getAjaxRequest(url, parameters, callbackfunc, filetype);}
					}
				}
				if (oncallfunc){
					oncallfunc();
				}
				//PS - check for ? in url already
				var aUrl = url.split("?");
				url = aUrl[0] + "?";
				if(aUrl.length == 2){
					if(aUrl[1].indexOf('ajax=Y') == -1){
						url += "ajax=Y&"
					}
					url += aUrl[1];		
				}
				oThis.ajaxobj.open('GET', url+parameters, true)
				oThis.ajaxobj.send(null);
			}
		}
	}
	this.postAjaxRequest = function(url, parameters, callbackfunc, filetype){
		oThis.ajaxobj = createAjaxObj() //recreate ajax object to defeat cache problem in IE
		if (oThis.ajaxobj){
			if(!oThis.bExecuting){
				oThis.filetype = filetype
				oThis.ajaxobj.onreadystatechange = function(){
					if(oThis.ajaxobj.readyState == 4){
						oThis.bExecuting = false;
					}
					callbackfunc;
				}
				oThis.ajaxobj.open('POST', url, true);
				oThis.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				oThis.ajaxobj.setRequestHeader("Content-length", parameters.length);
				oThis.ajaxobj.setRequestHeader("Connection", "close");
				oThis.ajaxobj.send(parameters);
			}
		}
	}
	
}

var ajaxpack=new ajaxPackObj()





//ACCESSIBLE VARIABLES (for use within your callback functions):
//1) ajaxpack.ajaxobj //points to the current ajax object
//2) ajaxpack.filetype //The expected file type of the external file ("txt" or "xml")
//3) ajaxpack.basedomain //The root domain executing this ajax script, taking into account the possible "www" prefix.
//4) ajaxpack.addrandomnumber //Set to 0 or 1. When set to 1, a random number will be added to the end of the query string of GET requests to bust file caching of the external file in IE. See docs for more info.

//ACCESSIBLE FUNCTIONS:
//1) ajaxpack.getAjaxRequest(url, parameters, callbackfunc, filetype)
//2) ajaxpack.postAjaxRequest(url, parameters, callbackfunc, filetype)

///////////END OF ROUTINE HERE////////////////////////


//////EXAMPLE USAGE ////////////////////////////////////////////
/* Comment begins here

//Define call back function to process returned data
function processGetPost(){
var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ if request was successful or running script locally
if (myfiletype=="txt")
alert(myajax.responseText)
else
alert(myajax.responseXML)
}
}
}

/////1) GET Example- alert contents of any file (regular text or xml file):

ajaxpack.getAjaxRequest("example.php", "", processGetPost, "txt")
ajaxpack.getAjaxRequest("example.php", "name=George&age=27", processGetPost, "txt")
ajaxpack.getAjaxRequest("examplexml.php", "name=George&age=27", processGetPost, "xml")
ajaxpack.getAjaxRequest(ajaxpack.basedomain+"/mydir/mylist.txt", "", processGetPost, "txt")

/////2) Post Example- Post some data to a PHP script for processing, then alert posted data:

//Define function to construct the desired parameters and their values to post via Ajax
function getPostParameters(){
var namevalue=document.getElementById("namediv").innerHTML //get name value from a DIV
var agevalue=document.getElementById("myform").agefield.value //get age value from a form field
var poststr = "name=" + encodeURI(namevalue) + "&age=" + encodeURI(agevalue)
return poststr
}

var poststr=getPostParameters()

ajaxpack.postAjaxRequest("example.php", poststr, processGetPost, "txt")
ajaxpack.postAjaxRequest("examplexml.php", poststr, processGetPost, "xml")

Comment Ends here */