if (typeof(decodeURIComponent) == "undefined") {
	decodeURIComponent = new Function("s", "return unescape(s);");
	encodeURIComponent = new Function("s", "return escape(s);");
}

var gAcceptsCookies = -1;
var gEquate = '=';
var gDelimiter = '|';				//delimits key/value pairs in the metacookies
var gCookieLife = (5*365*24*60*60*1000);

// "Internal" function to return the decoded value of a cookie
function getCookieVal(offset) 
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}


function ReadCookieVal(offset) 
{
	var endstr = document.cookie.indexOf (";", offset);

	if (endstr == -1)
		endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));
}


// supports GetMyCookie()
function getmyCookieVal(myCookie, offset, delimiter) 
{
	if (getmyCookieVal.arguments[2] == " ")
		delimiter = ";";

	var endstr = myCookie.indexOf (delimiter, offset);
	if (endstr == -1)
		endstr = myCookie.length;
	return unescape(myCookie.substring(offset, endstr));
}	

function FixCookieDate(date) 
{
	var base = new Date(0);
	var skew = base.getTime();	// dawn of (Unix) time - should be 0
	if (skew > 0)				// Except on the Mac - ahead of its time
		date.setTime(date.getTime() - skew);
	return true;
}

// Some files are calling this function that is in navbar.js, I'm trying to remove this dependency
function ReadCookie(name) 
{
	return GetCookie(name);
}

function GetCookie(name) 
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) 
			break; 
	}
	return null;
}

// given a string with more than one name=value pair, it will extract the value.
// myCookie = string that you want to parse
// name = name of value you want to get
// delimiter (optional) = if you need to override delimiter
function GetMyCookie(myCookie, name, delimiter) 
{
  if (GetMyCookie.arguments.length < 3)
	delimiter = " ";

  var arg = name + "=";
  var alen = arg.length;
  var clen = myCookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (myCookie.substring(i, j) == arg)
      return getmyCookieVal (myCookie,j,delimiter);
    i = myCookie.indexOf(delimiter, i) + 1;
    if (i == 0) break; 
  }
  return null;
}	

function parseHost(host)
{
	var tmpStr;
	var lastPeriod;
	var secondPeriod;
	
   	lastPeriod = host.lastIndexOf("."); 
    tmpstr = host.slice(0,lastPeriod);               // This string contains the url minus the last slash
	secondPeriod = tmpstr.lastIndexOf(".");         // Find the location of the second to last slash
    domainName = host.slice(secondPeriod+1,lastPeriod); // Now extract the user name that is between the last 2 slashes
	domainName = "." + domainName + ".com";
	return (domainName);
}

function SetCookie(name,value,expires,path,domain,secure) 
{
	var vHost=self.location.host;
	domain = parseHost(vHost);
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		"; path=" +((path) ?  path : "/") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	return true;
}

function AcceptsCookies() 
{
	var exp = new Date();
	var name="acceptCookies";
	var value=exp.getSeconds()*exp.getMinutes();	//a pseudo rand
	var readback_value;

	//Bail out for now, to avoid cookie overload (IE has a max of 20!)
	
	gAcceptsCookies = 1;
	return 1;				
	
	//can't use rememberIt here as we get recursion
		
	if (gAcceptsCookies == -1) {				//not set yet
	 	FixCookieDate(exp);	
		exp.setTime(exp.getTime() + gCookieLife);
	
		document.cookie = name + "=" + value +
		"; expires=" + exp.toGMTString() +
		"; path=/" +
		"; domain=" + parseHost(self.location.host);
		
		readback_value=GetCookie (name);
		
		if (readback_value == value)
			gAcceptsCookies = 1;		
		else
			gAcceptsCookies = 0;
	}

	return gAcceptsCookies;
}


function RememberIt(name, value) 
{
	var exp = new Date(); 
	var cookie;

	if (AcceptsCookies()==0) {
		alert("Your browser needs to be enabled to accept cookies");
		return false;
	}

	FixCookieDate(exp);
	exp.setTime(exp.getTime() + gCookieLife);
			
	cookie = name + "=" + value +
		"; expires=" + exp.toGMTString() +
		"; path=/" +
		"; domain=" + parseHost(self.location.host);

	document.cookie = cookie;
	return true;
}	
	
function DeleteCookie (name,path,domain) 
{
  if (AcceptsCookies() == 0) {
     alert("Your browser needs to be enabled to accept cookies");
	 return false;
  }
	
  if (GetCookie(name)) {
	var cookieStr = document.cookie = name + "=" +
        "; path=" +
      ((path) ? path : "/") +
        "; domain=" +
      ((domain) ? domain : parseHost(self.location.host) ) +
        "; expires=Thu, 02-Jan-1970 00:00:01 GMT";

        document.cookie = cookieStr;
  }
  return true;
}

// determin the browser.. but this seems to be duplicated in the determine client function.. this should be phased out.
function DetermineCurrentBrowser() 
{
	var current_browser =  "Other"; 
	var bwr = navigator.appName; 
	var ver = parseInt(navigator.appVersion, 10); 
	
	if (bwr == "Netscape") {
	    if (ver <= 2)
			current_browser = "Old";
	    else
			current_browser = "Netscape";
	}
	if (bwr == "Microsoft Internet Explorer") {
	    if (ver <= 2)
			current_browser = "Old";
	    else
			current_browser = "ie";
	}
	
	if (current_browser == "old"){
		alert("You are currently using " + current_browser + ". You need version 3.0 or greater.");
		return false;
	}

	return current_browser;
}  

function AddHours(fromdate, hours)
{
	var frominMS = fromdate.getTime();
	var toinMS = frominMS +(60 * 60 * 1000 * hours);
	return new Date(toinMS);
}

//tokenize: splits apart a token separated string and returns each one at a time.
// delim=delimiter
// which=the number of the token in the string to return,
// starts with 1.
// inwhat=string to crack.
function tokenize(delim,which,inwhat)
{
	var n=0;
	var wstr=0;
	var i=0;
	var s=0;		//start of token
	var f=0;		//end of token
	var firstdel=0;	//first delimiter
	
	if (inwhat == null)
		return 0;
	
	//find the start and end seperators
	
	if (inwhat.indexOf(delim,0) > 0)
		inwhat = delim + inwhat;
		
	for (i = 0; i < which; i++) {	
		n = inwhat.indexOf(delim,n);//find the next del
			
		if (n < 0)
			return '';			//nothing found
		
		n++;				//skip over the delimiter
	}
	
	f = inwhat.indexOf(delim,n);
	if (f < 0)
		f = inwhat.length;		//if there's no ending delimetre
	
	wstr = inwhat.substring(n,f);

	return wstr;
}

//parsecookie: returns a hash of  key/value pairs in the form: ^key1=value1^key2=value2^ where ^ can be any delimiter
function parsecookie(cookie, delim)
{	
	var tmp=new Array();
	var pair;
	var i = 1;
	var j = 0;

	if (cookie == null)
		return tmp;
	
	do {
		pair=tokenize(delim,i,cookie);
	
		if (pair == '')
			break;
	
	    tmp[j] = splitKeyValues(gEquate,1,pair);		//left
		tmp[tmp[j]] = splitKeyValues(gEquate,2,pair);	//right

		i++;
		j++;
	} while (pair != '');

	return tmp;
}

// split: splits apart a key=value pair. The usual delimiter is '=' 
function splitKeyValues(delim, which, pair)
{
	var n;
	
	n=pair.indexOf(delim);
	
	//no delimiter exists.
	
	if (n < 0)
		return '';
	
	if (which == 1)			//first item in pair
		return pair.substring(0, n);
	else if (which == 2)
		return pair.substring(n+1, pair.length);
	else
		return '';			//should never get here.
}

// addUpdateValue: either adds a new key/value pair to the string or updates a value.
function addUpdateValue(cookie, key, value, delim)
{
	var aArray = parsecookie(cookie, delim);
	var newcookie = delim;
	var left, right;
	
	if (aArray[key] != null){		//update a value (case sensitive)
		aArray[key] = value;
	}
	else{						//add a new pair
		aArray[aArray.length] = key;
		aArray[key] = value;
	}
	
	//create a new string in the form ":key1=value1:key2=value2:"		
		
	for (i = 0; i < aArray.length; i++) {
		left = aArray[i];
		right = aArray[left];
		
		if (left && right)  // only add to cookie if there's a key & a value!
			newcookie += left+gEquate+right+delim;
	}
	
	return newcookie;
}

//takes a key/value cookie string, and returns a value, given a key

function getParsedCookieValue(cookie, key, delim)
{
	var aArray = parsecookie(cookie, delim);
	
	return aArray[key];
}

// Same as GetCookie, but uses the key/value strings.
// master is the full string, name is the sub-cookie embedded inside the string.
function GetCookieEx(master, name) 
{
	var arg = master + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;

		if (document.cookie.substring(i, j) == arg) {
			longcookie = getCookieVal(j);
			return getParsedCookieValue(longcookie, name, gDelimiter);
		}

		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) 
			break; 
	}

	return null;
}

//same as RememberIt() but, puts the cookie into a key/value sequence, a meta-cookie
function RememberItEx(name, key, value, bSessionOnly) 
{
	var exp = new Date(); 
	var outcookie;	    //the modified meta-cookie	
	var incookie;		//the meta-cookie

	if (AcceptsCookies() == 0) {
		alert("Your browser needs to be enabled to accept cookies");
		return false;
	}

	var expires = '';

	if (!bSessionOnly) {
		FixCookieDate(exp);
		exp.setTime(exp.getTime() + gCookieLife);
		expires = "; expires=" + exp.toGMTString();
	}

	incookie = GetCookie(name);

	incookie = addUpdateValue(incookie,key,value,gDelimiter);
		
	outcookie = name + "=" + escape(incookie) + expires + 
		"; path=/" +
		"; domain=" + parseHost(self.location.host);

	document.cookie = outcookie;
	
	return true;
}	


//same as DeleteCookie() but, puts the cookie into a key/value sequence, a meta-cookie
function DeleteCookieEx(metaName, key) 
{
	if (AcceptsCookies() == 0) {
		alert("Your browser needs to be enabled to accept cookies");
		return false;
	}

	var exp = new Date(); 
	var outcookie;	    //the modified meta-cookie	
	var incookie;		//the meta-cookie

	exp.setTime(exp.getTime() + gCookieLife);
	FixCookieDate(exp);
	
	incookie = GetCookie(metaName);

	//note NULL input value below...addUpdateValue will only rebuild subfields if a key AND a value exists.
	incookie = addUpdateValue(incookie,key,'',gDelimiter);
		
	outcookie = metaName + "=" + escape(incookie)+
		"; expires=" + exp.toGMTString() +
		"; path=/" +
		"; domain=" + parseHost(self.location.host);

	document.cookie = outcookie;
	
	return true;
}	

function MigrateCookies()
{
	// Migrated to temporary session_mc; force to sniff once per session
	DeleteCookieEx('player_mc', 'verReal');
	DeleteCookieEx('player_mc', 'verFlash');
	DeleteCookieEx('player_mc', 'verWMP');
	DeleteCookieEx('player_mc', 'host');

	// Migrate to new POC scheme 4/08
	var poc			= GetCookieEx('box_mc', 'POC');
	var beta		= GetCookieEx('box_mc', 'beta');
	var ph			= GetCookieEx('player_mc', 'PH');
	var platform	= GetCookie('platform');

	DeleteCookieEx('player_mc', 'PH');
	DeleteCookieEx('box_mc', 'beta');

	if (poc == 1 || poc == 2 || poc == 4 || poc == 6) {
		if (ph && (poc != 2 || platform == 'wmp10'))
			RememberItEx('box_mc', 'POC', 10 + poc); // 1->11, 2->12, 4->14, 6->16
	}
	else if (poc == 3) {
		if (beta == 'mp3PRO')
			RememberItEx('box_mc', 'POC', 9);
	}
}

MigrateCookies();
