function getQueryStrVal(nameStr) {
	// returns the value of a given name from a query string name-value pair.

	var urlStr						= window.location.toString();
	var queryStr					= urlStr.split("?")[1];
	var paramList					= new Array();

	if (queryStr) {
		var params						= queryStr.split("&");

		for (var i = 0; i < params.length; i++) {
			var paramItem				= params[i].split("=");
			paramList[paramItem[0]]		= unescape(paramItem[1]);
		}
	}

	return paramList[nameStr];
}


function setCookie(nameStr, cookieVal, lifespan, path, domain, secure) {
	var today				= new Date();								// new Date object
	today.setTime( today.getTime() );									// set to todays date
	if ( lifespan != "" ) {												// if lifespan is specified
		lifespan			= 1000 * 60 * 60 * 24 * lifespan;			// calculate lifespan in milliseconds
	} else {
		lifeSpan			= 0;
	}
	var expiryDate			= new Date( today.getTime() + lifeSpan );	// add lifespan to current date/time

	var cookieStr			= nameStr + "=" +escape(cookieVal) + ( (lifeSpan) ? ";expires=" + expiryDate.toGMTString() : "" ) + ( (path) ? ";path=" + path : "" ) + ( (domain) ? ";domain=" + domain : "" ) + ( (secure) ? ";secure" : "" );
	document.cookie			= cookieStr;								// set cookie
}


function getCookieVal(nameStr) {
	// returns the value of a cookie with a given name
	var cookieStr			= document.cookie;
	var cookies				= cookieStr.split(";");
	var cookieList			= new Array();

	for (var i = 0; i < cookies.length; i++) {
		if (cookies[i].indexOf("=") > -1) {
			var cookie			= cookies[i].split("=");
			var key				= cookie[0].replace(/^\s+|\s+$/g, '');
			var value			= cookie[1].replace(/^\s+|\s+$/g, '');
			cookieList[key]		= unescape(value);
		}
	}

	return cookieList[nameStr];
}


function gwUtilSim() {
	// replaces functionality of gwUtil with client-side only script:
	// tries to retrieve value for "ref_id" from query string and cookie.
	// Querystring takes precedence over cookie, overwriting it if necessary.

	var port						= "";
	var refId						= "";
	var referrer					= document.referrer;			// get referrer

	if ((!referrer) || (referrer == null) || (referrer == "undefined") || (referrer == "")) {
		var currURL					= window.location;				// if no referrer, get current location
		if ((!currURL.port) || (currURL.port == "80")) {			// if port is 80 or none, ignore,
			port					= "";
		} else {
			port					= ":" + currURL.port;			// otherwise get the port number
		}
		referrer					= currURL.hostname + port;		// and attach it to the location
	}
	var qsVal						= getQueryStrVal("ref_id");		// try to get ref_id from query string
	var cVal						= getCookieVal("xb_track");		// try to get ref_id from cookie
	var trackValStr					=  "";


	if (!cVal) {
		if (!qsVal) {
			trackValStr				= referrer + "#" + document.location.href;		// no cookie, no querystring - use URL
		} else {
			trackValStr				= referrer + "#" + qsVal;						// no cookie, but querystring - use querystring
			setCookie("xb_track", trackValStr, "", "/", "", "");
		}
	} else {
		if (!qsVal) {
			trackValStr				= cVal;											// cookie, no querystring - use cookie
			setCookie("xb_track", trackValStr, "", "/", "", "");
		} else {
			trackValStr				= referrer + "#" + qsVal;						// cookie and querystring - use querystring
			setCookie("xb_track", trackValStr, "", "/", "", "");
		}
	}

	// if this is part of the IF (SalesForce) app, set the hidden field with the value of the cookie
	if ( document.getElementById("00N20000001Sg5w") ) {
		document.getElementById("00N20000001Sg5w").value	= trackValStr;
	}
}

gwUtilSim();
