// *************** CB's Remote Window Controller *************** //
function remoteWindow(theLink, bringForward) {
	// bringForward = flag to focus opener window
	// theLink = new href for opener window
	if (window.opener) { // if we have an opener window...
		window.opener.location.href = theLink;
		if (bringForward) {
			window.opener.focus();
		}
	} else {
		window.location.href = theLink;
	}
}
function popRemoteWinNoFeatures(theLink, theName) {
	window.open(theLink,theName,"alwaysRaised=1,dependent=1,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1 resizable=1");
}
// *************** /CB's Remote Window Controller *************** //

function gotoURL() {
	var Current = document.toswitch.isplist.selectedIndex;
	window.location.href = document.toswitch.isplist.options[Current].value;
	return false;
}

// ************************************** //
// ********** Tracking Cookies ********** //
// ------ Global Tracking ------ //

var stripHttpRegExp = /https?:\/\/(.*)/;
var srcIdPattern = /\d{3}-\d{10}/;

function setCookie(cookieName, cookieValue, numOfWeeks) {
	var expDate = new Date();
	expDate.setTime(expDate.getTime() + (numOfWeeks * 604800000));	//# milliseconds in a week
	var cookieData = cookieName+"="+escape(cookieValue)+";expires="+expDate.toGMTString()+";domain=covad.com;path=/;";
	document.cookie = cookieData;
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	
	if (begin == -1) {
		begin = dc.indexOf(prefix);
	
		if (begin != 0) return "";
			//changed to "" from null
	  	} else {
	    	begin += 2;
		}
		
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	  
	var info = unescape(dc.substring(begin + prefix.length, end));
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name) {
	if (getCookie(name)) {
		document.cookie = name + "=;path=/;domain=covad.com;expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
// ------ /Global Tracking ------ //

// ********** SOURCE ID COOKIE SNIFFER ********** //
function getSrcId() { // helper function to retrieve srcid value from querystring
	var strTheQs, intSrcIdPos, intSrcIdEndPos, strThisSrcId;
	strTheQs = location.search.toLowerCase();		// get the querystring from the URL
	if (strTheQs) { // if we have a querystring to deal with then...
		intSrcIdPos = strTheQs.indexOf('srcid=');		// find the last char position of srcid=		
		if (intSrcIdPos != -1) {
			// ladies and gents we have a srcid key, see if there's a worthwhile value...
			// but first, add 6 to the starting position (to exclude the srcid= string)
			intSrcIdPos += 6;
			intSrcIdEndPos = intSrcIdPos + 14;									// compute the last position of the srcid
			strThisSrcId = strTheQs.substring(intSrcIdPos, intSrcIdEndPos);		// extract the srcid string
			// if the strSrcId var is a viable srcid, it will be returned
			if (strThisSrcId.match(srcIdPattern)){
				return strThisSrcId;
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}
// now if we have a srcid cookie already set, and a new srcid in the querystring, overwrite the
// first srcid cookie.  if we do not have a cookie set already, just write the cookie.
var strSrcId = getSrcId();

// if the srcid in the QS is valid, set the cookie regardless if this is the first or Nth srcid
// (i.e. any previous srcid cookies will be overwritten)
if (strSrcId) { // ok we have a valid srcid!  Set the cookie and let the user be on their way
	setCookie("srcid", strSrcId, "8");
	setCookie("srcidStore", strSrcId, "8");
}

// ********** /SOURCE ID COOKIE SNIFFER ********** //

// ********** SESSION ID (MPUID) COOKIE SNIFFER ********** //
var bc=document.cookie;
var beenThere = bc.indexOf("mpuid");
if (beenThere == -1) {
	var bc=document.cookie;
	var prefix = "session_id";
	var begin = bc.indexOf(prefix);
	
	if (begin == -1) {
		begin = bc.indexOf(prefix);
	
		if (begin != 0) {};
	} else {
		begin += 2;
	}
	
	var end = bc.indexOf(";", begin);
	if (end == -1)
		end = bc.length;
	
	var ssidval = unescape(bc.substring(begin + prefix.length, end));
	setCookie("mpuid",ssidval ,".003");
}
// ********** /SESSION ID (MPUID) COOKIE SNIFFER ********** //

// ********** FIRST URL COOKIE ********** //
// *** EDITED 030106 AB MAX 100 CHAR!!!
var bc=document.cookie;
var beenThere = bc.indexOf("firstUrl");
if (beenThere == -1) {
	var url = window.location.toString();
	var urlLocation=url.split("?")[0];
	var qString=url.indexOf("SrcID");
	var scrIDString=url.slice(qString,(qString+20));
	url = urlLocation + "?" + scrIDString;
	url = url.replace(stripHttpRegExp, "$1");
	setCookie("firstUrl",url ,"8");
}
// ********** /FIRST URL COOKIE ********** //

// ********** Referrer COOKIE ********** //
var bc=document.cookie;
var referringUrl = bc.indexOf("referrer");
if (referringUrl == -1) {
	var referredBy = document.referrer.toString();;
	referredBy = referredBy.replace(stripHttpRegExp, "$1");
	setCookie("referrer",referredBy,"8");
}
// ********** /Referrer COOKIE ********** //

// ********** KeyWord COOKIE ********** //
if (location.href.indexOf("kw")!=-1) {
var keyWordVal=location.href.split("kw=")[1].split("&")[0].replace(/%20/g," ");
setCookie("keyword",keyWordVal,"8");
}
// ********** /KeyWord COOKIE ********** //

// ********** COOKIE VALUES For GLOBAL USE ********** //
var MPUID = getCookie("mpuid");
var FirstURL = getCookie("firstUrl");
var SrcID = getCookie("srcid");
var SrcIDStore = getCookie("srcidStore");
var SessionID = getCookie("session_id");
var ReferringURL = getCookie("referrer");
var KeyWord = getCookie("keyword");

// if the srcid cookie is invalid, expired, null, blank, fubar'd, we need
// to make sure the value isn't "null" and that it is "" instead.
if(SrcID == "null" || !SrcID || SrcID == null){
	// now check to see if we have the SrcIDStore available, if so use that
	if(SrcIDStore == "null" || !SrcIDStore || SrcIDStore == null){
		// nevermind, just go with an empty string
		SrcID = "";
	} else {
		// cool we'll just use the var we stored which somehow got overwritten earlier!
		if (SrcIDStore == "null" || !SrcIDStore || SrcID == null) {
			SrcID = "";	// give up, srcid lost or not present
		} else {
			SrcID = SrcIDStore;
			setCookie("srcid", SrcIDStore, "8");
		}
	}
}
// ********** /COOKIE VALUES For GLOBAL USE ********** //

// ********** /Tracking Cookies ********** //
// *************************************** //