var lbto;
var ajax_status = 0;
function AJAXInteraction(url, callback) {
    var req = init();
    req.onreadystatechange = processRequest;
    function init() {
    	ajax_status++;
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    function processRequest () {
      if (req.readyState == 4) {
    	  ajax_status--;
        if (req.status == 200) {
          hideloading();
          if (callback){
          	callback(req.responseText);
          	if(req.responseText.match(/var js_([a-zA-Z0-9\-\_]+) = SortableTableTypes\('([a-zA-Z0-9\-\_]+)'\)/gi)){
          		window['js_'+RegExp.$1] = SortableTableTypes(RegExp.$2);
          	}
          }
        }
      }
    }
    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }
    this.doGetNow = function() {
      req.open("GET", url, false);
      req.send(null);
      return(req.responseText);
    }
    this.doPost = function(params) {
      req.open("POST", url, true);
	  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	  req.setRequestHeader("Content-length", params.length);
	  req.setRequestHeader("Connection", "close");
	  req.send(params);
    }
    this.doPostNow = function(params) {
      req.open("POST", url, false);
	  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	  req.setRequestHeader("Content-length", params.length);
	  req.setRequestHeader("Connection", "close");
	  req.send(params);
	  return(req.responseText);
    }
}
function ajax(url,resp,noload){
	var temp = new AJAXInteraction(url,resp);
	temp.doGet();
	hideloading();
	if(!noload) showloading();
}
function simpleajax(url,noload){
	if(typeof(noload) == 'undefined'){
		var noload = true;
	}
	var temp = new AJAXInteraction(url);
	hideloading();
	if(!noload) showloading();
	var res = temp.doGetNow();
	hideloading();
	return(res);
	
}
function postajax(url,param,resp){
	var temp = new AJAXInteraction(url,resp);
	return(temp.doPost(param));
}
function postsimpleajax(url,param){
	var temp = new AJAXInteraction(url);
	return(temp.doPostNow(param));
}
function selectwait(ws){
	ws.style.color = "#A60000";
	clear_select_box(ws);
	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{
		var temp = document.createElement('div');
		temp.setAttribute('id','loadingbar');
		temp.innerHTML += '<table cellspacing=0 cellpadding=0 border=0><tr><td class="list" style="padding:4px;" valign="middle"><img src="/images/loading/loading.gif" style="position:relative;top:3px;">&nbsp;Loading...</td></tr></table>';
		temp.style.position = 'absolute';
		temp.style.top = document.body.scrollTop + 5 + 'px';
		temp.style.left = document.body.scrollLeft + 5 + 'px';
		document.body.appendChild(temp);
	}
	fixloadingpos();
}
function fixloadingpos(){
	if(document.getElementById('loadingbar')){
		document.getElementById('loadingbar').style.top = document.body.scrollTop + 5 + 'px';
		document.getElementById('loadingbar').style.left = document.body.scrollLeft + 5 + 'px';
	}
	if(lbto) clearTimeout(lbto);
	lbto = setTimeout('fixloadingpos()',300);
}
function select_to_results(which) {
	var temp_options = new Array();
	if(typeof which == 'object' && which.tagName == 'SELECT'){
		for(var i=0;i<which.options.length;i++){
			temp_options.push(which.options[i].value+'|'+which.options[i].text)
		}
	}
	return temp_options.join('^');
}
function results_to_select(tr,tl,ag,val){
	tl.style.color = "";
	// 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);
				}
				// Check if value given, select it
				if (val && val != '' && val != 'undefined' && newopt.value == val) {
					newopt.selected = true;
				}
				// 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');
}
function cf_results(temp_array,temp_value_col,temp_name_col,filter_list){
	if (!temp_value_col) temp_value_col = 'id';
	if (!temp_name_col) temp_name_col = 'name';
	if (!filter_list) filter_list = '';
	var final_array = new Array();
	for(x=0;x<temp_array.length;x++){
		if(filter_list == '' || listfind(filter_list,temp_array[x][temp_value_col])){
			final_array.push(temp_array[x][temp_value_col]+'|'+temp_array[x][temp_name_col]);
		}
	}
	return final_array.join('^');
}
function agrian_function(temp_text,return_function){
	if(return_function){
		ajax('/global/ajax/agrian_function.cfm?what='+escape(temp_text),return_function,1);
	}else{
		return eval(simpleajax('/global/ajax/agrian_function.cfm?what='+escape(temp_text)));
	}
}
function slyrefresh(){
	ajax(document.location,slyrefresh_resp);
}
function slyrefresh_resp(what){
	document.body.innerHTML = what;
}
function clickad(adid,url){
	simpleajax('/global/ajax/adclick.cfm?adid='+adid);
	window.open(url);
}
function success(ajax_response){
	if(ajax_response.match(/\<\!\-\-\s+agrian_page_loaded/) || ajax_response.match(/\/\* agrian_page_loaded \*\//)){
		return true;
	}else{
		return false;
	}
}
function strip_success(ajax_response){
	return ajax_response.replace(/\/\* agrian_page_loaded \*\/|\<\!\-\-\s+agrian_page_loaded\s+\/\/\-\-\>/g,'').trim();
}
