
// "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));
}


// supports GetMyCookie()
// myCookie = string that you want to parse
// offset = location in string
// delimiter (optional) = if you need to override delimiter
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;
}

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 RememberIt(name, value) {
	var exp = new Date(); 
	FixCookieDate(exp);
	exp.setTime(exp.getTime() + (5*365*24*60*60*1000));

	document.cookie = name + "=" + value +
		"; expires=" + exp.toGMTString() +
		"; path=/" +
		"; domain=" + parseHost(self.location.host);
}	
	
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
  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);
}

