/**
 * Add javascript file to body to simulate a more error-free
 * approach to AJAX functionality. 
 * Preventing server overloading if action is called too often
 * with ajap_redo.
 */
var REFRESH_INTERVAL = 1000;
sUrl = document.location.href;
iUrlEnd = sUrl.lastIndexOf("/") + 1;
var BASE_URL = sUrl.substring(0, iUrlEnd);
var m_iOldTime = 1;
var m_sLastUrl = '';

/**
 * Calls URL to be parsed which contains javascript
 * @param string url to be called
 * @param boolean force url to be called (no interval)
 */
function ajap(p_sUrl, p_bForce) {
	bForce = (p_bForce == null) ? false : p_bForce;
	iCurTime = (new Date()).getTime();
	if (bForce || m_iOldTime < iCurTime-REFRESH_INTERVAL) {
		if (p_sUrl.substring(0, 4) != 'http') {
			p_sUrl = BASE_URL+p_sUrl;
		}
		var oJS = document.createElement('SCRIPT');
		oJS.type = 'text/javascript';
		oJS.src = p_sUrl;
		document.body.appendChild(oJS);
		m_iOldTime = iCurTime;
	} else {
		m_sLastUrl = p_sUrl;
		ajap_redo(false);
	}
}

/**
 * If ajap is called too fast set timeout to do it later
 * @param boolean perform actions
 */
function ajap_redo(p_bPerform) {
	bPerform = (p_bPerform == null) ? false : p_bPerform;
	if (bPerform) {
		ajap(m_sLastUrl, true);
	} else {
		iCurTime = (new Date()).getTime();
		setTimeout("ajap_redo(true);", REFRESH_INTERVAL-(iCurTime-m_iOldTime));
	}
}

/**
 * When passing user input through the url piece you should
 * encode it safely with ajap_encode.
 * @param string value to be parsed safely in an url
 * @return string value formatted to a safe url string
 */
function ajap_encode(p_sVarToUrl) { 
	if (p_sVarToUrl) { 
		sReturn = escape(p_sVarToUrl);
		sReturn = sReturn.replace(/@/g,'_at_');
		// The '.', is replaced by %FF and not %2E because the last is often
		// automatically replaced by browsers into a normal dot
		sReturn = sReturn.replace(/\./,'_dot_');
		return sReturn; 
	} else { 
		return ''; 
	}
}