String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}


function AJAXInteraction(url, callback) {
	var req = init();
	req.onreadystatechange = processRequest;
	function init() {
	  if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}    
	function processRequest () {
	  // readyState of 4 signifies request is complete
	  if (req.readyState == 4) {
	  // status of 200 signifies sucessful HTTP call
		if (req.status == 200) {
		  if (callback) {
			  //alert(req.responseXML);
			  callback(req.responseXML)
		  };
		}
	  }
	}
	this.doGet = function() {
	  // make a HTTP GET request to the URL asynchronously
	  try{
	  	req.open("GET", url, true);
	  	req.send(null);
	  }catch(e){
		//alert("Exception "+e);
	  }
	}
}

function getParameter ( parameterName ) {
	queryString = window.top.location.search.substring(1);
	// Add "=" to the parameter name (i.e. parameterName=value)
	var parameterName = parameterName + "=";
	if ( queryString.length > 0 ) {
		// Find the beginning of the string
		begin = queryString.indexOf ( parameterName );
		// If the parameter name is not found, skip it, otherwise return the value
		if ( begin != -1 ) {
			// Add the length (integer) to the beginning
			begin += parameterName.length;
			// Multiple parameters are separated by the "&" sign
			end = queryString.indexOf ( "&" , begin );
			if ( end == -1 ) {
				end = queryString.length
			}
			// Return the string
			return unescape ( queryString.substring ( begin, end ) );
		}
		// Return "null" if no parameter has been found
		return "null";
	}
}

function validateCallback(responseXML){
	var xml = responseXML;
	try{
		if(xml != null && xml.getElementsByTagName("tidbit")!= null && xml.getElementsByTagName("tidbit")[0] != null){
		   var msg = xml.getElementsByTagName("tidbit")[0].firstChild.nodeValue;
		   if (msg != null && msg != "") {
		   		var mdiv = document.getElementById("message");
		       	mdiv.innerHTML = msg;
		   }
	    }
    }catch(e){}
}

function getRandomMessage(url){
	ajax = new AJAXInteraction(url, validateCallback); 
	ajax.doGet();
	wait(10000,url);
}

function getNextMessage(url){
	var code = 'true';
	var qryStr = '&getnext=' + encodeURIComponent(code);
	var nextURL = url + qryStr;
	var ajax = new AJAXInteraction(nextURL, validateCallback); 
	ajax.doGet();
}

function wait(delay ,url){
	string = "getRandomMessage('"+url+"')";
	setTimeout(string,delay);
}

function updateIntStats(statsRefURL){
	var cCode = getParameter('cc');
	var qryStr = '';
	if(cCode != null){
	qryStr = '&cc='+encodeURIComponent(cCode);
	}
	var url = statsRefURL + encodeURIComponent(document.referrer) + qryStr;
	var ajax = new AJAXInteraction(url, validateCallback); 
	ajax.doGet();
}