/**************************************************************************
* Document	: ajax_util.js
* Author	: Wayne J. Earl
* Created	: 2007-03-06
* Purpose	: JavaScript utility functions for ajax
* Comments	: Taken from ING
*************************************************************************/
/*************************************************************************
* Revised by	: 
* Revised date	: 
* Description	: 
*************************************************************************/

//var xmlHttpArray = new Array();
//var xmlStatus = new Array();

//global var for single call
var xmlHttp;

//multiple xmlHttp container
function XmlHttpContainer(xmlHttp, status)
{
	if (xmlHttp !== undefined) this.xmlHttp = xmlHttp;
	if (status !== undefined) this.status = status;

	this.xmlHttp;
	this.status;
	
	//getters
	this.getXmlHttp = function() { return this.xmlHttp; }
	this.getStatus = function() { return this.status; }
	
	//setters
	this.setStatus = function(status) { this.status = status; }
}

var xmlHttpArray = new Array();

function getXmlHttpObject()
{ 
	var objXMLHttp = null;
	if (window.XMLHttpRequest) {
		
		objXMLHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}


//single call only
function ajaxCall(url, handler)
{
	xmlHttp = getXmlHttpObject();
	if (xmlHttp == null) {

		alert ("Browser does not support HTTP Request");
		return;
	}
	
	xmlHttp.onreadystatechange = handler;
	
	//show loader
	if (typeof(bb_fader) == "object") {
		
		if (bb_fader.loader_id === null) bb_fader.show(ajax_loader_name, 5, 75, 100);
	}

	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


//can handle multiple simultaneous calls
function ajaxCallMult(url, handler)
{
	var xmlHttp = getXmlHttpObject();
	if (xmlHttp == null) {

		alert ("Browser does not support HTTP Request");
		return;
	}
	
	xmlHttp.onreadystatechange = handler;
	
	//show loader
	if ((typeof(bb_fader) == "object") && (getVisibility(panel_cont_id))) {
		
		if (bb_fader.loader_id === null) bb_fader.show(ajax_loader_name, 5, 75, 100);
	}

	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	
	//xmlHttpArray.push(xmlHttp);
	//xmlStatus.push(true);
	xmlHttpArray.push(new XmlHttpContainer(xmlHttp, true));
}


function processResponseText(resp_str)
{
	debug = 0;
	
	//remove the html header and footer in QA, staging, etc
	resp_str = resp_str.replace(/<bbcom_banner>.*?bbcom_banner>/g, '');
	
	var var_str = "alert('Return Vars Not Set');";
	//interpret string and create local vars
	var re = /^(var ajax_resp = \d+\;[^\x00]*)/;
	var matches = resp_str.match(re);
	//perform any necessary string replacements
	if (matches) {
		
		if (typeof(matches[1]) != 'undefined') { //
		
			var_str = matches[1];
			if (debug) alert(var_str);
			var test_val;
			var repl_val;
			var test_str;
			var repl_str;
			var re_repl;
			var test_array = [];
			var repl_array = [];
			for (idx = 0; idx < test_array.length; idx ++) {
			
				test_val = test_array[idx];
				repl_val = repl_array[idx];
				test_str = String.fromCharCode(test_val);
				repl_str = String.fromCharCode(repl_val);
				re_repl = new RegExp(test_str, "gm");
				var_str = var_str.replace(re_repl, repl_str);
			}
		}
	}
	return var_str;
}
