/***********************************************
 *
 ***********************************************/

function ajaxpage(url, containerid) {
	//alert('url: '+url+'\nid: '+containerid);
	if (document.getElementById('loader'+containerid)!=null) {
		document.getElementById('loader'+containerid).style.display="inline";
	}
	clearHTML(containerid);
	var page_request = false;
	if (window.XMLHttpRequest) { // if Mozilla, Safari etc
		page_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!page_request) {
		alert('Unable to create XMLHTTP object');
		return false;
	}
	
	page_request.onreadystatechange=function() {
		if (page_request.readyState == 4 && (page_request.status==200 )) {
			document.getElementById(containerid).innerHTML=page_request.responseText;
			if (typeof ajaxOnLoad == 'function') { ajaxOnLoad(); }
			if (document.getElementById('loader'+containerid)!=null) {
				document.getElementById('loader'+containerid).style.display="none";
			}
			//alert(page_request.getAllResponseHeaders());
		}
	}
	page_request.open('GET', url, true);
	page_request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	page_request.send(null);
}
function getHTML(containerID) {
	var str = '';
	var container = document.getElementById(''+containerID);
	if (container != null) {
		if (window.XMLHttpRequest) { // if Mozilla, Safari etc
			str += container.textContent;
		} else if (window.ActiveXObject) { // if IE
			str += container.innerHTML;
		}
	}
	return str;
}
function setHTML(containerID,text) {
	if (window.XMLHttpRequest) { // if Mozilla, Safari etc
		document.getElementById(''+containerID).textContent = text;
	} else if (window.ActiveXObject) { // if IE
		document.getElementById(containerID).innerHTML=text;
	}
}

function clearHTML(containerID) {
	if (window.XMLHttpRequest) { // if Mozilla, Safari etc
		removeAllChildren(document.getElementById(''+containerID));
	} else if (window.ActiveXObject) { // if IE
		document.getElementById(containerID).innerHTML="";
	}
}

function removeAllChildren(cid) {
	var children = cid.childNodes;
	for (var i = children.length-1; i >= 0; i--) {
		removeAllChildren(children[i]);
		cid.removeChild(children[i]);
	}
}

function showTooltip( table, column ) {
	showTooltipAlt( table, column, 'ttdata' );
}

function showTooltipAlt( table, column, ttname ) {
	tturi = getPF_ROOT()+'tooltip.php?tname='+table+'-'+column+'-'+ttname+'&';
	ajaxpage( tturi  , ttname );
	ttr = document.getElementById(ttname);
	ttr.setAttribute('class', 'tooltip');
	ttr.setAttribute('className', 'tooltip'); // for IE
}

// This function should operate the same as the PHP function urlencode()
// http://www.phpbuilder.com/board/showthread.php?t=10318476
function urlencode(thestr) {
	thestr = trim(thestr);
	thestr = escape(thestr);
	thestr = thestr.replace(/\+/g,'%2B').replace(/%20/g, '+');
	thestr = thestr.replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
	return thestr;
}

function trim( str ) {
	return str.replace(/^\s+|\s+$/g, '');
}
