// JavaScript Document

function adClick(advertID, orglink){

	// we use a javascript feature here called "inner functions"
	// using these means the local variables retain their values after the outer function
	// has returned. this is useful for thread safety, so
	// reassigning the onreadystatechange function doesn't stomp over earlier requests.
		
	function ajaxBindCallback(){
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				// Goto the url returned from the function
				window.location = trim(ajaxRequest.responseText);
				//alert(trim(ajaxRequest.responseText));
			}
		}
	}

	// use a local variable to hold our request and callback until the inner function is called...
	var ajaxRequest = null;
	var url = '/ajax/ads.cfc?method=advertClick&adID=' + advertID + '&orglink=' + orglink;
	
	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} else if (window.ActiveXObject) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}
}
// Function to trim down all the white space that comes back from the cfc
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
} 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
} 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}