/*
*	 AJAX Loading routine
*/

var req; 
function loadXMLDoc(opt,key,file,sid,loc,tmp) {
	if(opt) opt=opt+"=";
	var url=file+"?"+sid+"&"+opt+key;
	if (sid && key) {
	  url=file+"?"+sid+"&"+opt+key;
	} else {
	  if (sid) {
	    url=file+"?"+sid;
	  } else {
	    if (key) {
	      url=file+"?"+opt+key;
	    } else {
	      url=file;
	    }
	  }
	}
	if (tmp) {getObject(loc).innerHTML = tmp;}
	try { req = new ActiveXObject("Msxml2.XMLHTTP"); } 
	catch(e) { 
	try { req = new ActiveXObject("Microsoft.XMLHTTP"); } 
	catch(oc) { req = null; } 
	} 
	if (!req && typeof XMLHttpRequest != "undefined") { req = new XMLHttpRequest(); } 
	  if (req != null) {
	    req.onreadystatechange=function(){if(req.readyState!=4)return;if(req.status==200){getObject(loc).innerHTML = req.responseText}};
	    req.open("GET", url, true); 
	    req.send(null); 
	  }
	return true;
} 

function getObject(name) { 
   var ns4 = (document.layers) ? true : false; 
   var w3c = (document.getElementById) ? true : false; 
   var ie4 = (document.all) ? true : false; 

   if (ns4) return eval('document.' + name); 
   if (w3c) return document.getElementById(name); 
   if (ie4) return eval('document.all.' + name); 
   return false; 
}


function create_http_handle(TYPE){
	var http_handle = false;
	if (window.XMLHttpRequest){
		http_handle = new XMLHttpRequest();
		if (http_handle.overrideMimeType){
			if (TYPE == "XML"){
				http_handle.overrideMimeType('text/xml');
			} else {
				http_handle.overrideMimeType('text/html');
			}
		}
	} else if (window.ActiveXObject){
		try {
			http_handle = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_handle = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_handle){
		alert("We are sorry but you are using an outdated browser.  To view this site you must update your browser.");
		return false;
	} else {
		return http_handle;
	}
}

function sendHTTPrequest(URL, PARAMETERS, ONCHANGE, METHOD, TYPE){
	if (TYPE == "") TYPE = "HTML";
	http = create_http_handle(TYPE);
	if (!http) return false;
	http.onreadystatechange = function() {if(http.readyState == 4 && http.status == 200) {ONCHANGE(http);}}

	//Kill the Cache problem in IE.
	var now = "upid=" + new Date().getTime();
//	PARAMETERS += (PARAMETERS.indexOf("?")+1) ? "&" : "?";
	PARAMETERS += "&"+now;
//alert('url calling:'+URL);

	if (METHOD == "POST"){
//alert('POST url calling:'+URL+'?'+PARAMETERS);
		http.open('POST', URL , true);
		http.setRequestHeader("Content-type", "application/x-www-form-URLencoded");
//		http.setRequestHeader("Content-length", PARAMETERS.length);
		http.setRequestHeader("Content-length", binaryLength_UTF8(PARAMETERS));
		http.setRequestHeader("Connection", "close");
		http.send(PARAMETERS);
	} else {
//alert('GET url calling:'+URL+'?'+PARAMETERS);
		http.open('GET', URL + "?"+PARAMETERS, true);
		http.send(null);
	}
}

function binaryLength_UTF8(str){	
	var bytes, charCode, a, len;
	bytes = 0;
	for (a = 0, len = str.length; a < len; a++)	{
		charCode = str.charCodeAt(a);
		if (charCode < 128) //2^7
			bytes += 1;
		else if (charCode < 2048) // 2^11
			bytes += 2;
		else if (charCode < 65536) // 2^16
			bytes += 3;
		else
			bytes += 4;
	}
	return bytes;
}
