var lbto;
function AJAXInteraction(url, callback, params) {
    var req = init();
    //req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.onreadystatechange = processRequest;
    function init() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
          //hideloading();
          if (callback) callback(req.responseText);
        }
      }
    }
    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }
    this.doPost = function() {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
      req.send(params);
    }
    this.doGetNow = function() {
      req.open("GET", url, false);
      req.send(null);
      return(req.responseText);
    }
}
function ajax(url,resp,noload){
	var temp = new AJAXInteraction(url,resp, '');
	temp.doGet();
	//hideloading();
	//if(!noload) showloading();
}
function ajaxPost(url,resp,noload, params){
	var temp = new AJAXInteraction(url,resp, params);
	temp.doPost();
}
function simpleajax(url){
	var temp = new AJAXInteraction(url);
	return(temp.doGetNow());
}
function selectwait(ws){
	ws.length = 0;
	ws.options[0] = new Option('Please Wait...','');
	ws.options[0].style.color = '#A60000';
	ws.options[0].style.fontWeight = 'bold';
}
function hideloading(){
	if(document.getElementById('loadingbar')){
		document.getElementById('loadingbar').style.display = 'none';
		if(lbto) clearTimeout(lbto);
	}
}
function showloading(){
	if(document.getElementById('loadingbar')){
		document.getElementById('loadingbar').style.display = '';
	}else{
		temp = document.createElement('div');
		temp.setAttribute('id','loadingbar');
		//temp.innerHTML += '<table width=50 cellspacing=0 cellpadding=3 border=0 bordercolor=black><tr><td bgcolor=yellow><font face="Arial">Loading...</font></td></tr></table>';
		temp.innerHTML += '<center style="padding-top:200px;"><img src="images/loader.gif?5" border="0" hspace="5" vspace="5"></center>';
		temp.style.position = 'absolute';
		temp.style.backgroundImage = 'url(images/wait1.gif?4)';
		temp.style.width = "100%";
		temp.style.height = "100%";
		temp.style.top = document.body.scrollTop + 0;
		temp.style.left = document.body.scrollLeft + 0;
		document.body.appendChild(temp);
	}
	fixloadingpos();
}
function fixloadingpos(){
	if(document.getElementById('loadingbar')){
		document.getElementById('loadingbar').style.top = document.body.scrollTop + 0;
		document.getElementById('loadingbar').style.left = document.body.scrollLeft + 0;
	}
	if(lbto) clearTimeout(lbto);
	lbto = setTimeout('fixloadingpos()',300);
}
function results_to_select(tr,tl,ag){
	// remove all options and groups
	for(r=tl.childNodes.length-1;r>=0;r--){
		tl.removeChild(tl.childNodes[r]);
	}
	// go through each item seperated by the carrot
	tresults = tr.split("^");
	var lastgroup = null;
	for(r=0;r<tresults.length;r++){
		if(tresults[r].match(/\|/)){
			ops = tresults[r].split("|");
			ops[0] = ops[0].replace(/^\s+/g,'').replace(/\s+$/g,'');
			ops[1] = ops[1].replace(/^\s+/g,'').replace(/\s+$/g,'');
			if(ops[0] == 'optgroup'){
				lastgroup = document.createElement('optgroup');
				lastgroup.label = ops[1];
				tl.appendChild(lastgroup);
			}else{
				var newopt = document.createElement('option');
				newopt.value = ops[0];
				newopt.innerHTML = ops[1];
				if(lastgroup){
					lastgroup.appendChild(newopt);
				}else{
					tl.appendChild(newopt);
				}
				// Some special colored options based on the value
				if(ops[0].match(/^AID\d+$/)) newopt.style.color = '#398D10';
				if(ops[0].match(/^EID\d+$/) || ops[0].match(/^EID(.*)~(.*)$/)) 
					newopt.style.color = '#A60000';
				if(ops[0].match(/^RID\d+$/)) newopt.style.color = '#101081';
				if((ops[0].match(/^\d+$/))&&(ag)) newopt.style.color = '#398D10';
			}
		}
	}
	return true;
}
function su(tt){
	if(tt == '') return tt;
	var tf = new Array();
	for(tz=0;tz<=tt.length-1;tz++){tf[tf.length] = tt.charCodeAt(tz);}
	return tf.join('A');
}
