	//Load openX ad zone by passing in the zoneid and the type 'interstitial' or 'banner'
	function loadOpenXAd(zoneid, adType, cats, debug) {
		var m3_r = Math.floor(Math.random()*99999999999);
		var url;
		var qs;
	   	
	   	switch(adType) {
			case 'interstitial':
			  	url = (location.protocol=='https:'?'https://www.kciinvesting.com/pan/www/delivery/al.php':'http://www.kciinvesting.com/pan/www/delivery/al.php');
			  	qs = '?zoneid=' + zoneid;
			  	qs += '&cb=' + m3_r;
			  	qs += '&layerstyle=simple';
			  	
			  	break;
			case 'banner':
				url = (location.protocol=='https:'?'https://www.kciinvesting.com/pan/www/delivery/ajs.php':'http://www.kciinvesting.com/pan/www/delivery/ajs.php');
				qs = '?zoneid=' + zoneid;
				qs += '&target=_blank';
				qs += '&cb=' + m3_r;
				qs += (document.MAX_used && document.MAX_used != ',') ? '&exclude=' + document.MAX_used : '';
	   			qs += (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : ''));
	   			qs += '&loc=' + escape(window.location);
	   			qs += (document.referrer) ? '&referer=' + escape(document.referrer) : '';
	   			qs += (document.context)? '&context=' + escape(document.context) : '';
	   			qs += (document.mmm_fo) ? '&mmm_fo=1' : '';
				
				break;
			default: 
		  	//do nothing
		}
	  
	  if(cats==""){
	   		cats = 0;
	   	}
	   	
	  var cigx = '&cigx=' + getQuerystring('cigx',0);
	  var randomnumber=Math.floor(Math.random()*10001);
			   
			   //window.alert(cigx);
			   	
		if(cigx == "&cigx="){
			setCookie('cigx',cigx,10);
		}
		else{
			//if CIGX not passed, remove it. it may be passed via a cookie
			cigx = "";
		}
			   	
		var adZoneJs = '<script type="text/javascript" src="' + url + arrayToQuerystring(getPageVars(), qs) + '&cats=' + cats + cigx + '&cachebuster=' + randomnumber + '"></script>';
				
		//Alert javascript code to be executed for debugging
		if (debug) alert(adZoneJs);
		
		document.write(adZoneJs);
	}
	
	//Returns array of all cookie and querystring vars. A variable in the url overrides a cookie value of the same name
	function getPageVars() {
		var vars = {};
		
	    if (document.cookie && document.cookie != '') {
	        var split = document.cookie.split(';');
	        for (var i = 0; i < split.length; i++) {
	            var name_value = split[i].split("=");
	            name_value[0] = name_value[0].replace(/^ /, '');
	            vars[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
	        }
	    }
	    
	    
	    var qKeys = {};
		var re = /[?&]([^=]+)(?:=([^&]*))?/g;
		var matchInfo;
		
		while(matchInfo = re.exec(location.search)){
			vars[matchInfo[1]] = matchInfo[2];
		}
		
		//set authkey to -1 if not set
		if (vars['authkey'] == undefined) vars['authkey'] = -1;
	
		return vars;
	}
	
	//Converts an associative array of keys and vals to a querystring formatted string and creates a new qs or appends it to a passed qs
	function arrayToQuerystring(arr, qs) {
		qs = (qs == undefined) ? '' : qs;
		marker = (qs == '') ? '?' : '&';
		
		for (var key in arr) {
	    	qs += marker + key + '=' + arr[key];
	    	marker = '&';	
	    }
	    
	    return qs;
	}
	
	//Replaces all site links with merge tags %%something%% with a varibale of the same name in the page vars. 
	function mergeSiteLinks() {
		var vars = getPageVars();
		var anchors = document.getElementsByTagName('a');
		var re = /%%(([A-Za-z0-9_]+)=?([A-Za-z0-9_]+)?)%%/g;
		var tmpLink
		var matchInfo;
		var tagToReplace;
		var varName;
		var defaultVal;
		var newVal;
		
		for (i = 0; i < anchors.length; i++) {
			tmpLink = anchors[i].href
			if (tmpLink.match(/%%.*%%/) == null) tmpLink = decodeURIComponent(tmpLink);
			
			while(matchInfo = re.exec(tmpLink)){
				tagToReplace = matchInfo[0];
				varName = matchInfo[2];
				defaultVal = matchInfo[3] || undefined;
				newVal = (vars[varName] != undefined) ? vars[varName] : defaultVal;
				
				tmpLink = tmpLink.replace(tagToReplace, newVal);
				//document.write("<!-- replacing: " + tagToReplace + " with " + newVal + "-->");
				
			} 
			tmpLink = tmpLink.replace(/&?([A-Za-z0-9]+)=undefined/g, '');
			document.getElementsByTagName('a')[i].href = tmpLink;       
	  }
	  
	  
	}


//Replaces all site links with merge tags %%something%% with a varibale of the same name in the page vars. 
	function mergeSiteLinks2() {
		var vars = getPageVars();
		var anchors = document.getElementsByTagName('a');
		var re = /%%(([A-Za-z0-9_]+)=?([A-Za-z0-9_]+)?)%%/g;
		var tmpLink
		var matchInfo;
		var tagToReplace;
		var varName;
		var defaultVal;
		var newVal;
		
		for (i = 0; i < anchors.length; i++) {
			alert(anchors[i].href);
			tmpLink = anchors[i].href;
			alert(tmpLink);
			while(matchInfo = re.exec(tmpLink)){
				tagToReplace = matchInfo[0];
				varName = matchInfo[2];
				defaultVal = matchInfo[3] || undefined;
				newVal = (vars[varName] != undefined) ? vars[varName] : defaultVal;
				
				document.getElementsByTagName('a')[i].href = tmpLink.replace(tagToReplace, newVal);
				
			} 
			document.getElementsByTagName('a')[i].href = tmpLink.replace(/&?([A-Za-z0-9]+)=undefined/g, '');       
	  }
	  
	  
	}
	
	
//Parse queryString to find cigx value, if exists
function getQuerystring(key, default_){
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

function setCookie(pLabel, pVal, pMinutes)
{
 var tExpDate=new Date();
 tExpDate.setTime( tExpDate.getTime()+(pMinutes*60*1000) );
 document.cookie= pLabel + "=" +escape(pVal)+((pMinutes==null) ? "" : ";expires="+ tExpDate.toGMTString());
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}
