
// bcbcAjax static class/object/namespace
// do not try to create instance of this object, access directly (instance reference bug with statechange function assignment in XMLHTTPREQUEST)

var bcbcAjax = new Object();

bcbcAjax.Initialise = function() {
	// defaults
	bcbcAjax.defaultMethod = 'POST';

	// var setup
	bcbcAjax.state = 0;
	bcbcAjax.jobs = new Array();
	bcbcAjax.jobcount = 0;
	bcbcAjax.HTTPReqObj = null;
	bcbcAjax.currentJob = null;
}
bcbcAjax.Start = function() {
	if (bcbcAjax.state == 0) {
		// has been initialised only
		bcbcAjax.state = 1;
		bcbcAjax.nextJob();
	} else if (bcbcAjax.state == 1) {
		// has been initialised and started (waiting for jobs)
	} else if (bcbcAjax.state == 2) {
		// working on a job
	} else if (bcbcAjax.state == 3) {
		// processing state change
	} else if (bcbcAjax.state == 4) {
		// processing response
	} else {
		// uh oh
	}
}

bcbcAjax.AddJob = function(url, method, params, htmlid) {
	if ( (htmlid == null) || ( (htmlid != null) && (document.getElementById(htmlid) != null) ) ) {
		// only add job to queue if it is an alert (null htmlid) or htmlid exists in DOM
		var newJob = new Array();
		newJob[0] = url;
		if ( (method != "GET") && (method != "POST") ) { 
			newJob[1] = bcbcAjax.defaultMethod; 
		} else { newJob[1] = method; }
		newJob[2] = params;
		newJob[3] = htmlid;
		bcbcAjax.jobcount = bcbcAjax.jobs.push(newJob);
	}
	if (bcbcAjax.state == 1) { bcbcAjax.nextJob(); } // if started and waiting, goto next job
}
bcbcAjax.getJob = function() {
	if (bcbcAjax.jobcount > 0) { // outstanding jobs in array?
		var tmpJob = bcbcAjax.jobs.shift();
		bcbcAjax.jobcount = bcbcAjax.jobs.length;
		return tmpJob;
	} else {
		bcbcAjax.jobcount = bcbcAjax.jobs.length;
		return false;
	}
}
bcbcAjax.nextJob = function() {
	if (bcbcAjax.currentjob = bcbcAjax.getJob()) { // try to GET a job
		// got job ok, procede
		bcbcAjax.state = 2;
		if (window.XMLHttpRequest) {
			bcbcAjax.HTTPReqObj = new XMLHttpRequest();
		} else if (window.ActiveXObject) { // IE
			bcbcAjax.HTTPReqObj = new ActiveXObject("Microsoft.XMLHTTP");
		}
		bcbcAjax.HTTPReqObj.onreadystatechange = bcbcAjax.stateChange;
		bcbcAjax.HTTPReqObj.open(bcbcAjax.currentjob[1], bcbcAjax.currentjob[0], true);
		bcbcAjax.HTTPReqObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		bcbcAjax.HTTPReqObj.send(bcbcAjax.currentjob[2]);
		
	} else { // no jobs
	}
}
bcbcAjax.stateChange = function() {
	var response = "";
	if (bcbcAjax.HTTPReqObj.readyState == 3) {
		response = "Working.....\n";
		if ((navigator.userAgent.toLowerCase().indexOf("firefox") != -1)) { // detect firefox (ajax impementation support for following)
			if (bcbcAjax.HTTPReqObj.status == 200) { // feeds output to html element (if specified) - stream still open
				if (bcbcAjax.HTTPReqObj.responseText) { response += bcbcAjax.HTTPReqObj.responseText; }
			}
		}
		bcbcAjax.handleResponse(response, bcbcAjax.currentjob[3], true); // render partial response
		
	} else if (bcbcAjax.HTTPReqObj.readyState == 4) { // response complete
		bcbcAjax.state = 3;
		if (bcbcAjax.HTTPReqObj.status == 200) {
			response = bcbcAjax.HTTPReqObj.responseText;
		} else { // http or server error (404, 500 etc)
			response = "ERROR: " + bcbcAjax.HTTPReqObj.status + "\n\n";
		}
		bcbcAjax.handleResponse(response, bcbcAjax.currentjob[3], false); // render final response
		bcbcAjax.HTTPReqObj = null;
		bcbcAjax.nextJob(); //recurse to next job
		bcbcAjax.state = 1;
	} else {
		// other state, do nothing for now
	}
}
bcbcAjax.handleResponse = function(response, htmlid, partial) {
	if (partial != true) { partial = false; }
	bcbcAjax.state = 4;
	if  ( (htmlid == null) || (document.getElementById(htmlid) == null) )  { // either no htmlid has been set, or bad one specified
		// perform default action with response
		if (partial == false) {
			if (response != "") {
				alert( "bcbcAjax Response: \n" + response );
			}
		}
	} else {
		// put response into innerHTML of specified element (htmlid)
		var theElement = document.getElementById(htmlid);
		if (response != "") { // is response empty?
			theElement.innerHTML = response;
			theElement.style.display = "block";
		} else {
			theElement.style.display = "none";
		}
	}
}
