function encodeURIPlus(thetext){
	returnvalue = encodeURI(thetext).replace(/&/g, "%26");
	return returnvalue;
}

function switchTinyMCE(){
	if(!DoTinyMCE) return false;
	
	if(arguments.length > 0) state = arguments[0];
	else state = 'off';
	
	if(state == 'off'){
		zz = document.getElementsByTagName('TEXTAREA')
		for ( var i=0;i<zz.length;i++ ){
			if (tinyMCE.getInstanceById(zz[i].name) != null) tinyMCE.execCommand('mceRemoveControl', false, zz[i].name);
		}
	}else{
		tinyMCE_init();
	}
}


function setWait(state){
	if(state == 0){
		cursorstate = 'auto';
		document.body.style.cursor='auto';
	}else{
		//set to 'waiting...' for presentation divs, append waiting for others
		//do nothing for functions
		if(state != '' && !eval("typeof " + state + " == 'function'")){
			if(findObj(state).className == 'presentation'){
				findObj(state).innerHTML = 'waiting...';
			}else{
				findObj(state).innerHTML = findObj(state).innerHTML + '<br />waiting...';
			}
		}
		cursorstate = 'wait';
		document.body.style.cursor=cursorstate;
	}
	
	zz = document.getElementsByTagName('A')
	for (var i=0;i < zz.length; i++) zz[i].style.cursor=cursorstate;
	
	zz = document.getElementsByTagName('INPUT')
	for (var i=0;i < zz.length; i++) zz[i].style.cursor=cursorstate;

	zz = document.getElementsByTagName('SELECT')
	for (var i=0;i < zz.length; i++) zz[i].style.cursor=cursorstate;
}

function AjaxRequest(Action, Target) {
	// Action : action=theaction&var=val
    var xmlHttpReq = false;
    var self = this;
	
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	var tinyobj = '';
	if(arguments.length > 2 && arguments[2].length > 0) tinyobj = arguments[2];
	
	setWait(Target[0]);
	self.xmlHttpReq.open('POST', AjaxHandler, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
			setWait(0);
			if(Target[0] != '' && eval("typeof " + Target[0] + " == 'function'")){
				theFunction = Target.shift();
			}else{
				theFunction = 'AjaxOutput';
			}
			theresponse = self.xmlHttpReq.responseText;
			debug(html_encode(theresponse));
			
			if(trim(theresponse) == 'DOLOGIN'){
				window.location = "home.cfm";
			}else{
				Target.unshift(self.xmlHttpReq.responseText);
				eval(theFunction + "(Target, tinyobj)");
			}
        }
    }
    self.xmlHttpReq.send(Action);
}

function AjaxOutput(target){
	// target = array(newvalue, targetdiv)
	
	// find javascript: for example:
	//
	// start ajax
	// ajax run: command1;
	// ajax run: command2;
	// end ajax
	//
	var parts = target[0].split("//");
	var newstring = '';
	var jscommands = Array();
	for(var i=0; i<parts.length; i++){
		if(parts[i].indexOf('ajax run:') >= 0){
			jscommands.push(parts[i].substr(9));
		}
		if(newstring != '') newstring = newstring + '//';
		newstring = newstring + parts[i];	
	}
	if(target[1] != '') findObj(target[1]).innerHTML = newstring;
	for (var i=0; i<jscommands.length; i++){
		eval(jscommands[i]);
	}
	if (use_disclaimer) add_disclaimers();
	
}

function AjaxOutput2(Param){
	var str = Param[0];
	var ErrorDiv = Param[1];
	var ServiceDiv = Param[2];
	var entries = AjaxCreateResults(str);
	if(AjaxGetResult(entries,'ERROR')){
		findObj(ErrorDiv).innerHTML = AjaxGetResult(entries,'ERROR');
		findObj(ErrorDiv).className = 'showError';
		if(arguments.length > 1 && arguments[1].length > 0)	tinyMCE_init();
	}else{
		target = new Array();
		target[0] = str;
		target[1] = ServiceDiv;
		AjaxOutput(target);
	}
}

function AjaxSend(action, target){
	// action: 	1. name of form
	//			2. urlstring
	// target: 	1. functionname, followed by arguments
	//			2. id of DOM element where response is outputed
	var tinyobj = '';
	if(findObj(action) && findObj(action).tagName == 'FORM'){
		formdetails = AjaxFormAction(action);
		action = formdetails[0];
		tinyobj = formdetails[1];
	}
		
	var thetarget = Array();
	if(arguments.length > 2){
		// multiple arguments
		thetarget = Array.prototype.slice.call(arguments);
		thetarget.shift();
	}else if(typeof target == 'object'){
		// target is array
		thetarget = target;		
	}else{
		thetarget[0] = target;	
	}
	
	AjaxRequest(action, thetarget, tinyobj);	
}

function AjaxFormAction(form){

	var theForm = findObj(form);
	var returnval = new Array();
	var action = 'ajax=1';
	var tinyobj = '';
	
	for ( var i=0;i<theForm.elements.length;i++ ){
		obj = theForm.elements[i];
				
		if(obj.name == '') obj.name = obj.id; // fix name problem with select
 		if (DoTinyMCE && tinyMCE.getInstanceById(obj.name) != null){
			action = action + '&' + obj.name + '=' + encodeURIPlus(tinyMCE.getInstanceById(obj.name).getContent());
			tinyMCE.execCommand('mceRemoveControl', false, obj.name);
			if(tinyobj != '') tinyobj = tinyobj + ',';
			tinyobj = tinyobj + ',' + obj.name;
		}else if(obj.tagName == 'SELECT' && obj.type == 'select-one'){
			action = action + '&' + obj.name + '=' + encodeURIPlus(obj.value);
		}else if(obj.tagName == 'SELECT' && obj.type == 'select-multiple'){
			for (var j=0; j < obj.options.length; j++){
				if(obj.options[j].selected) action = action + '&' + obj.name + '=' + encodeURIPlus(obj.options[j].value);
			}
		}else if((obj.type == 'checkbox' && obj.checked)
			|| (obj.type == 'radio' && obj.checked)				
			|| (obj.type != 'checkbox' && obj.type != 'radio')){
			action = action + '&' + obj.name + '=' + encodeURIPlus(obj.value);
		}
				
	}
	// tinyMCE_init();
	returnval[0] = action;
	returnval[1] = tinyobj;
	return returnval;	
	
}

function AjaxCreateResults(str){
	var rawentries = str.split("$$");
	var entries = Array();
	for(i=0; i<rawentries.length; i++){
		rawentry = rawentries[i].split(":");
		if(rawentry.length >= 2){
			key = trim(rawentry.shift());
			value = trim(rawentry.join(":"));
			entries.push(key,value);
		}
	}
	return entries;
}

function AjaxGetResult(entries, key){
	for(i=0; i<entries.length; i=i+2)
		if(entries[i] == key) return entries[i+1];
	return false;
}