isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

// Call the following with your function as the argument
//SafeAddOnload(yourfunctioname);

// Body onload utility (supports multiple onload functions)
var gSafeOnLoad = new Array();
function AddSafeOnLoad(f)
{
	if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
	{
		window.onload = SafeOnLoad;
		gSafeOnLoad[gSafeOnLoad.length] = f;
	}
	else if  (window.onload)
	{
		if (window.onload != SafeOnLoad)
		{
			gSafeOnLoad[0] = window.onload;
			window.onload = SafeOnLoad;
		}		
		gSafeOnLoad[gSafeOnLoad.length] = f;
	}
	else
		window.onload = f;
}

function SafeOnLoad()
{
	for (var i=0;i<gSafeOnLoad.length;i++)
		gSafeOnLoad[i]();
}

window.onload = SafeOnLoad();

function ExternalLinks() {
   if (!document.getElementsByTagName) return;
   var anchors = document.getElementsByTagName("a");
   for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") &&
         anchor.getAttribute("rel") == "ext")
      anchor.target = "_blank";
   }
}

AddSafeOnLoad(ExternalLinks);

// Function to create a cookie and set its value
function setCookie(name, value, expires) {
    document.cookie = escape(name) + "=" + escape(value) + "; path=/" + 
        ((expires == null) ? "" : "; expires=" + expires.toGMTString(  ));
}

// Function to retrieve a cookie's value
function getCookie(name) {
    var cookiename = name + "=";
    var dc = document.cookie;
    var begin, end;

    if (dc.length > 0) {
        begin = dc.indexOf(cookiename);
        if (begin != -1) {
            begin += cookiename.length;
            end = dc.indexOf(";", begin);
            if (end == -1) {
                end = dc.length;
            }
            return unescape(dc.substring(begin, end));
        } 
    }
    return null;
}

// Function to delete a cookie
function deleteCookie(name) {
    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" +  
        "; path=/";
}

// Function to set crumb value in the cookie
function setCrumb(cookie, name, value) {
    var cookievalue = getCookie(cookie);
    var crumbvalue = getCrumb(name); 
    var crumbname = name + '=';

    if (crumbvalue != null) {
        var start = cookievalue.indexOf(crumbname);
        if (start != -1) {
            var end = cookievalue.indexOf('|', start);
            setCookie(cookie, 
                      cookievalue.substring(0, start) + crumbname 
                      + value + '|' 
                      + cookievalue.substring(end + 1, cookievalue.length),
                      exp);
        }
    }
    else {
        if (cookievalue != null) {
            cookievalue += crumbname + value + '|';
        }
        else {
            cookievalue = crumbname + value + '|';
        }
        setCookie(cookie, cookievalue, exp);
    }
}

// Function to get crumb value from the cookie
function getCrumb(cookie, name) {
    var crumbname = name + '=';
    var cookievalue = getCookie(cookie);

    if (cookievalue != null) {
        var start = cookievalue.indexOf(crumbname);
        if (start != -1) { 
            start += crumbname.length;
            var end = cookievalue.indexOf('|', start);
            if (end != -1) {
                return unescape(cookievalue.substring(start, end));
            }
        }
    }
    return null;
}

// Function to delete crumb from the cookie
function deleteCrumb(cookie, name) {
    var cookievalue = getCookie(cookie);
    var crumbvalue = getCrumb(name); 
    var crumbname = name + '=';

    if (crumbvalue != null) {
        var start = cookievalue.indexOf(crumbname); 
        var end = cookievalue.indexOf('|', start);
        setCookie(cookie, 
                  cookievalue.substring(0, start) + 
                  cookievalue.substring(end + 1, cookievalue.length),
                  exp);
    }
}

var FirstVisitCookieName = 'FirstVisit';
var FirstPageCookieName  = 'FirstPage';
var LastVisitCookieName  = 'LastVisit';
var LastPageCookieName   = 'LastPage';
var VisitCountCookieName = 'VisitCount';
var PageCountCookieName  = 'PageCount';
var VisitState = 'VisitState';

function SaveVisitInfo() {

   var exp = new Date();

   exp.setTime(exp.getTime() + (1000 * 60 *60 * 24 * 31 * 12));

   var FirstVisit = getCookie(FirstVisitCookieName);
   var FirstPage  = getCookie(FirstPageCookieName);

   if (! FirstVisit) {
      setCookie(FirstVisitCookieName,document.referrer,exp);
   }

   if (! FirstPage) {
      setCookie(FirstPageCookieName,document.URL,exp);
   }

   setCookie(LastVisitCookieName,document.referrer,exp);
   setCookie(LastPageCookieName,document.URL,exp);

   // add VisitCount to increment once/session
   var count = getCookie(PageCountCookieName);
   if (! count) { count = 1; }
   else         { count = count + 1; }

   setCookie(PageCountCookieName,count,exp);

   var state = getCookie(VisitState);
   if (! state) {
      setCookie(VisitState,1,exp);
   }

}

function GetFirstVisit() {
   return getCookie(FirstVisitCookieName);
}

AddSafeOnLoad(SaveVisitInfo);
