//
//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
//
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//  The following functions are released to the public domain.
//
//  This version takes a more aggressive approach to deleting
//  cookies.  Previous versions set the expiration date to one
//  millisecond prior to the current time; however, this method
//  did not work in Netscape 2.02 (though it does in earlier and
//  later versions), resulting in "zombie" cookies that would not
//  die.  DeleteCookie now sets the expiration date to the earliest
//  usable date (one second into 1970), and sets the cookie's value
//  to null for good measure.
//
//  Also, this version adds optional path and domain parameters to
//  the DeleteCookie function.  If you specify a path and/or domain
//  when creating (setting) a cookie**, you must specify the same
//  path/domain when deleting it, or deletion will not occur.
//
//  The FixCookieDate function must now be called explicitly to
//  correct for the 2.x Mac date bug.  This function should be
//  called *once* after a Date object is created and before it
//  is passed (as an expiration date) to SetCookie.  Because the
//  Mac date bug affects all dates, not just those passed to
//  SetCookie, you might want to make it a habit to call
//  FixCookieDate any time you create a new Date object:
//
//    var theDate = new Date();
//    FixCookieDate (theDate);
//
//  Calling FixCookieDate has no effect on platforms other than
//  the Mac, so there is no need to determine the user's platform
//  prior to calling it.
//
//  This version also incorporates several minor coding improvements.
//
//  **Note that it is possible to set multiple cookies with the same
//  name but different (nested) paths.  For example:
//
//    SetCookie ("color","red",null,"/outer");
//    SetCookie ("color","blue",null,"/outer/inner");
//
//  However, GetCookie cannot distinguish between these and will return
//  the first cookie that matches a given name.  It is therefore
//  recommended that you *not* use the same name for cookies with
//  different paths.  (Bear in mind that there is *always* a path
//  associated with a cookie; if you don't explicitly specify one,
//  the path of the setting document is used.)
//  
//  Revision History:
//
//    "Toss Your Cookies" Version (22-Mar-96)
//      - Added FixCookieDate() function to correct for Mac date bug
//
//    "Second Helping" Version (21-Jan-96)
//      - Added path, domain and secure parameters to SetCookie
//      - Replaced home-rolled encode/decode functions with Netscape's
//        new (then) escape and unescape functions
//
//    "Free Cookies" Version (December 95)
//
//
//  For information on the significance of cookie parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html    
//
//******************************************************************
//
// "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 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;
}


/* ======================================================================
FUNCTION:	DetermineCurrentBrowser 
 
INPUT: 		none.

RETURN:		a string indicating the current browser

DESC:			This function is used with client-side JavaScript to detect
				the brand and version of the user's browser.  Detects only
				Netscape and Microsoft browsers.  Returns "Other" if a different
				brand is detected.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
====================================================================== */
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;
}  
