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 lastPeriod = host.lastIndexOf("."); 
    var tmpstr = host.slice(0,lastPeriod);               // This string contains the url minus the last slash
	var 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);
}

// ParseCookieEx: returns a hash of  key/value pairs in the form: ^key1=value1^key2=value2^ where ^ can be any delimiter
function ParseCookieEx(cookie, delim)
{	
	var results = new Array();
	
	if (cookie) {
		var nvPairs = cookie.split(delim);
		
		if (nvPairs.length > 0) {
			for (var i = 0; i < nvPairs.length; i++) {
				var pair = nvPairs[i].split('=');
				if (pair[0]) {
					var v = pair[1];
					for (var j = 2; j < pair.length; j++)
						v += '=' + pair[j];
					results[pair[0]] = v;
				}
			}
		}
	}
	
	return results;
}

// addUpdateValue: either adds a new key/value pair to the string or updates a value.
function addUpdateValue(cookie, key, value, delim)
{
	var ckHash = ParseCookieEx(cookie, delim);
	ckHash[key] = value;

	//create a new string in the form ":key1=value1:key2=value2:"		
	var newcookie = delim;
		
	for (var name in ckHash) {
		if (name && typeof(ckHash[name]) != 'function') {
			var value = ckHash[name];
			
			if (value !== undefined)
				newcookie += name+gEquate+value+delim;
		}
	}

	return newcookie;
}

//takes a key/value cookie string, and returns a value, given a key

function getParsedCookieValue(cookie, key, delim)
{
	var ckHash = ParseCookieEx(cookie, delim);
	
	return ckHash[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()
{
	if (GetCookieEx('box_mc', 'bitrate'))
		RememberItEx('box_mc', 'bitrate', 256);
}

MigrateCookies();


