// dartwarecookies.js file
// These functions support the Dartware-specific cookie implementation
// They are executed at the bottom of this file

function addCookieOnce( key, value)
{ // Check to see if the 'key' cookie is present. If not, add it with the corresponding value
	//alert("addCookieOnce: key,value = "+key+","+value);
	var now = new Date();
	var theCookie = getCookie(key);
	if (!theCookie) {
		now.setTime(now.getTime() + 1 * 24 * 60 * 60 * 1000); // 30 days
		keyval = escape(value);
		path=escape("/");
		domain = escape("."+document.domain);
		expires = now.toGMTString();
		keystr = key + "=" + keyval + "; expires=" + expires+ "; path="+path+";	domain="+domain;
		document.cookie = keystr;
	}
}
/**
 * Gets the value of the specified cookie.
 * name  Name of the desired cookie.
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 * From http://www.netspade.com/2005/11/16/javascript-cookies/
 */
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 null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
	var returnstr= unescape(dc.substring(begin + prefix.length, end));
    return returnstr;
}

function geturlparam(paramname) {
	var returnstr = "";
	//searches the current URL parameter list for the specified string
	//if found, returns the value of the string
	var thisurl = window.location.search; //get the current parameters
	var urlparts = thisurl.split("?");
	if(urlparts.length>1) {
		paramstr = urlparts[1];
	} else {
		return returnstr;
	}
	if(paramstr.length != undefined && paramstr.indexOf(paramname) > -1) { //check for the specified parameter
		paramparts = paramstr.split("&"); //split the parameter list into parts
		for(i in paramparts) { //process each parameter
			thispart = paramparts[i].split("="); //get the name/value pair
			if(thispart[0] == paramname) {
				return returnstr = thispart[1]; //return the value of the specified parameter
			}
		}
	}
	return returnstr;
}

// always add the path of the first Dartware page encountered
addCookieOnce("sfxentry", location.pathname);

// always add the first non-Dartware referer cookie
var lcReferrer = geturlparam("refer"); 

//if refer or referrer parameter is set in URL, use it
lcReferrer = lcReferrer + geturlparam("referrer"); 
if (lcReferrer!="") { // tack on "-" if not empty
   lcReferrer = lcReferrer + "-";
}
lcReferrer = lcReferrer + document.referrer;

addCookieOnce("sfxrefer", lcReferrer);

