function appendOption(apel, str, id) {
	var opts = str.split("||");
	clearOptions(apel);
	var oval = "";
	for (var i=1, len = opts.length; i < len; ++i) {
		oval = opts[i].split("|");
		if (oval[1] != "") {
			opt = document.createElement('option');
			opt.value = oval[0];
			if (typeof(oval[1])=="undefined") {oval[1]=" ";}
			opt.appendChild(document.createTextNode(oval[1]));
			Dom.add(opt, el);
		}
	}
}

function clearOptions(apel) {
	el = Dom.get(apel);
	while(el.hasChildNodes()){
		el.removeChild(el.lastChild);
	}
}

// **** CORE AJAX FUNCTION **** 
	
function loadXmlHttp(url, id, type) {
	var f = this;
	var xmlTimeout;
	f.xmlHttp = null;
	/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
	/*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
	try {f.ie = window.ActiveXObject}catch(e){f.ie = false;}
	@end @*/
	if (window.XMLHttpRequest && !f.ie || /^http/.test(window.location.href)) {
		f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
	}
	else if (/(object)|(function)/.test(typeof createRequest)) {
		f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
	} else {
		f.xmlHttp = null;
	// Internet Explorer 5 to 6, includes IE 7+ when local //
	/*@cc_on @*/
	/*@if(@_jscript_version >= 5)
	 try{f.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
	 catch (e){try{f.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){f.xmlHttp=null;}}
	 @end @*/
	}
	if (f.xmlHttp != null) {
		f.type = type;
		if (f.type != "showData") {
			f.el = document.getElementById(id);
		} else {
			f.el = id;
		}
		
		// Function to append a random number parameter in the url to prevent caching
		var ses=Math.floor(Math.random()*100000000001)
		url = (url.indexOf('?')) ? url + "&sesid=" + ses : url+"?sesid=" + ses;
		if (f.xmlHttp.overrideMimeType) {
            f.xmlHttp.overrideMimeType('text/xml; charset=iso-8859-1');
		} 
		f.xmlHttp.open("GET", url, true);
		f.xmlHttp.onreadystatechange = function(){
			f.stateChanged();
		};
		f.xmlHttp.send(null);
		// Call timeout function to display message after 10 seconds if response not received
		f.xmlTimeout=setTimeout(function() {f.timeOut();},10000);

	}
	else {
		alert('This browser does not support AJAX!'); // substitute your desired request object unsupported code here
		this.el.innerHTML = "<p style='color: red; font-weight: bold;'>This browser does not support AJAX.  This page will not function properly.</p>";
	}
}

// *** STATE CHANGE HANDLER ***
loadXmlHttp.prototype.stateChanged=function () {
	if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href))) {
		// this.el.innerHTML = this.xmlHttp.responseText;
		clearTimeout(this.xmlTimeout);
		switch (this.type) {
			case "opt":
			case 1:
				appendOption(this.el, this.xmlHttp.responseText);
				break;
			case "showData":
				showData(this.xmlHttp.responseText);
				break;
			default:
				this.el.innerHTML = this.xmlHttp.responseText;
		}
		clearWait(this.el.id);
	}
}

// *** TIMEOUT HANDLER ***
loadXmlHttp.prototype.timeOut=function(){
   this.xmlHttp.abort();
   var strMsg = "Oops! The server could not be reached.  Try again, or check your network connection."; 
   alert(strMsg);
   clearWait(this.el.id, strMsg);
 
}

