// This is code to set and unset a cookie specified by ICOOKIE for the purpose
// of blocking popup ads when directed to our site from google.
// This happens when we recognize a parameter the the url called imode
// the value is the number of hops, after this one, that will suppress popup ads.
// and a value of -1 if you want to suppress popups for the remainder of the session.
// Any non number value will be ignored and disable popup blocking. So to turn it off
// now, just go ?imode= or ?imode=& or ?imode=non_numeric_garbage
//
// Modified to support persistence - cookie will now stay for entire session - and now
// also supports number:source format to track source 
//
// This all works by including this file at the top of your page, then the code you want to 
// suppress should be enclosed by two calls to i_mask like this
//
//	<script language="JavaScript">
//	<!--
//		i_mask(1);
//	//-->
//	</script>
//	<script>
//	Offending popup code you want blocked right here
//	</script>
//	Inbetween
//	<script language="JavaScript">
//	<!--
//	i_mask(0);
//	//-->
//	</script>
//

var is_imode;
is_imode = false;



function adjustAdCookie(){
// The following two lines should be CONST NOT VAR but IE had a little fit.
var IMODE = "imode";
var ICOOKIE = "icookie";
var path = "/";
var mydom = getTopDomain();
var expires = "";
var lvalue;

	if ((lvalue = getParameter(IMODE)) !== null){
		    is_imode = true;
			// parse out number vs. source
			var avalue = lvalue.split(":");
		    // make sure we are a number and only a number
		    if(avalue[0] == "" || avalue[0].search(/[^-0-9]+/) != -1 || isNaN(parseInt(avalue[0]))){
				is_imode=false;
		    }
		    setCookie(ICOOKIE, lvalue, expires, path, mydom);
	}else {
		var value;
		if((value = getCookie(ICOOKIE)) !== null){
		    is_imode = true;
			// parse out number vs. source
			var avalue = value.split(":");
		    if(avalue[0] == 0 || avalue[0] == "" || avalue[0].search(/[^-0-9]+/) != -1 || isNaN(parseInt(avalue[0]))){
				is_imode = false;
		    }else{
			    if(avalue[0] > 0){
				avalue[0]--;
			    setCookie(ICOOKIE, avalue.join(":"), expires, path, mydom);
			    }
		    }
		}
	}
}

// Function to find a names parameter within the location property
// name - name of parameter to look for
function getParameter(name){
	var loc = window.location.search.toLowerCase();
	loc = loc.replace(/^\?/,"");
	var allparams = loc.split("&");
	return getNameValue(name, allparams);
}

// Function to search through array and find elements that match
// name=value where name is the passes parameter
//
// name - the name of the name/value pair to look for
// list - an array of items to search
function getNameValue(name, list){
	if(list === null){
		return null;
	}
	var pattern = new RegExp("^\\s*"+name+"\\s*=\\s*([^=\\s]*)\\s*$");
	var result;
	var i;
	var item;
	for (i in list){
		item = list[i];
		if((result = pattern.exec(item)) != null){
			return unescape(result[1]);
		}
	}
	return null;
}

// Function to find a named cookie and return it.
// name - name of the cookie
// return null if not found elsewise the value (even if it's an empty string "")
// So check for null like this getCookie() === null
function getCookie(name){
	var allcookies = document.cookie;
	var cookies = allcookies.split(";");
	return getNameValue(name, cookies);
}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (if null: end of current session)
// [path] - path for which the cookie is valid (if null: path of calling document)
// [domain] - domain for which the cookie is valid (if null: domain of calling document)
// [secure] - boolean value for secure transmission
function setCookie(name, value, expires, path, domain, secure) {
	var newCookie = name + "=" + 
		escape(value) + 
		((expires) ? "; expires=" + expires : "") + 
		((path) ? "; path=" + path : "") + 
		((domain) ? "; domain=" + domain : "") + 
		((secure) ? "; secure" : "");
		document.cookie = newCookie;
}

// onoff - turn on/off suppressing popups (or any code you choose)
// top of file
function i_mask(onoff) {
            if (onoff && is_imode) {
                        document.write("<!--");
            } else if (!onoff && is_imode) {
                        document.write("-->");
            }
}

// Get the top level domain. so that x.y.z.z.y.com will turn into y.com
// This is sometimes called the 2 dot domain name.
function getTopDomain(){
	var host = location.host.toLowerCase();
	var start;
	start = host.search(/[^.]+\.[^.]+$/);
	if(start < 0){
		alert("Funny Host Name "+host);
		return host;
	}
	return host.substring(start);
}

// new function to enable roadblocks from new platform
// pass url and cookie name for roadblock - honors the imode variable
function doRoadblock(rb_cookie, rb_url){
	var rbvalue;
	var DETOURED = "detoured";
	var rb_return_page = (location.search == "") ? escape(location.href) + escape('?detoured=1') : escape(location.href) + escape('&detoured=1')
	if (! is_imode && (rbvalue = getParameter(DETOURED)) == null && testDetourCookie(rb_cookie)){
		location.replace(rb_url + "?page=" + rb_return_page);
	} else {
		bumpDetourCookie(rb_cookie);
	}
}
