/* ---------------------------------------- Ajax Object */
function oAjax(dnis, sessionId, dbc) {
	this.staticArguments='dnis=' + dnis + '&sessionId=' + sessionId + '&dbc=' + dbc;
	this.dynamicArguments=new Object();
	//this.active=false;
	this.delay=1;}

oAjax.prototype.createRequestObject=function() {
    try {return new ActiveXObject('Msxml2.XMLHTTP');}
    catch(e) {try {return new ActiveXObject('Microsoft.XMLHTTP');}
				catch(e) {return new XMLHttpRequest();}}}

oAjax.prototype.sendRequest=function(request,response,oneTimeDelay) {
	//if (this.active) {
	//	alert('Please wait, a request is currently being processed. If you continue to get this message, please refresh the page.');
	//	return;}
	//else this.active=true;
	
	var delay=oneTimeDelay==undefined?this.delay:oneTimeDelay;
	this.start=new Date(new Date().getTime() + delay).getTime();
	var tempThis=this;
	var http=this.createRequestObject();
	//get('debug').innerHTML='/com/ufltek/core/gui/html/ajax.jsp' + '?' + request;
	//alert('Request=' + request);
	
	function handleResponse() {
		if (http.readyState==4) {
			
			if (http.status==200) {
				//ajax.active=false;
				var interval=tempThis.start-new Date().getTime();
				if (interval<1) interval=1;			
				tempThis.start=0;
				//alert('Response=' + http.responseText);
				//divAlert('Response=' + http.responseText.replace(/\\/g,'\\\\').replace(/\'/g,'\\\'').replace(/&amp;/g,'&amp;amp;').replace(/&lt;/g,'&amp;lt;').replace(/&gt;/g,'&amp;gt;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/&quot;/g,'&amp;quot;'));
				if (response==undefined) response=tempThis.response;
				//alert(http.responseText);
				var responseObject=oXml.parseXml(http.responseText);
				if (responseObject.ajax && responseObject.ajax.error) {window.onerror('ajax');}
				else if (responseObject.ajax && responseObject.ajax.reload) browser.reload();
				else if (response!=undefined) {
					tempThis.responseObject=responseObject.ajax;
					setTimeout(response + '(ajax.responseObject)',interval);}}}}

	http.onreadystatechange = handleResponse;
	http.open('POST', '/com/ufltek/core/gui/html/ajax.jsp');
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", request.length);
	http.setRequestHeader("Connection", "close");
	http.send(request);
	return true;}

oAjax.prototype.ajax=function(action, arguments, callback, oneTimeDelay) {
	var args=this.staticArguments;
	for (var p in this.dynamicArguments) {
		args+='&' + p + "=" + this.dynamicArguments[p];}
	if (action) this.action=action;
	if (this.action) args +='&action=' + this.action;	
	this.sendRequest(args + '&method=ajax' + (arguments?arguments:''),callback,oneTimeDelay);}

oAjax.xmlToHtml=function(xml) {
	return xml.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&apos;/g,'"');}

/* ---------------------------------------- Catch any javascript error and report back Via Ajax */
window.onerror=function(error, file, line) {
	var message='Sorry, there has been an error on this page. ';
	try {
		//if (error!='ajax') ajax.sendRequest('/index.jsp?' + ajax.staticArguments + '&error=javascript&javascriptError=' + error + '&file=' + file + '&line=' + line);
		if (error!='ajax') ajax.ajax('&error=javascript&javascriptError=' + encodeURIComponent(error) + '&file=' + encodeURIComponent(file) + '&line=' + line);
		message+='An administrator has been notified. Please try to continue. ' +
			'If the same problem persists, you will need to wait for us to address the issue. ';}
	catch(err) {
		message+='If the problem persists, please let us know and we will try and rectify the situation as quickly as possible. ';}
	if (error!='ajax') message+='Once again, sorry for any inconvenience.\n\nError: ' + error + '\nFile: ' + file + '\nLine: ' + line;
	alert(message);
	return true;}