/* cookies.js */
/*
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
*/

// utility function to retrieve a future expiration date in proper format;
// pass three integer parameters for the number of days, hours,
// and minutes from now you want the cookie to expire; all three
// parameters required, so use zeros where appropriate
function getExpDate(days, hours, minutes) {
    var expDate = new Date();
    if (typeof days == "number" && typeof hours == "number" && typeof hours == "number") {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
        return expDate.toGMTString();
    }
}

// utility function called by getCookie()
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

// primary function to retrieve cookie by 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;
}

// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

// remove the cookie by setting ancient expiration date
function deleteCookie(name,domain) {//path,
    if (getCookie(name)) {
        document.cookie = name + "=" +
            //((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/*
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
*/
//from 5.9
function CheckForCookiesEnabled() {
	document.getElementById('userNameIn').focus(); //javascript:
	
	var cookieEnabled = false;
	var isIE4Min = getIEVersionNumber() >= 4;
	var isNN6Min = getNNVersionNumber() >= 6;
	if (isIE4Min || isNN6Min) {
		// perform statements for NN 6 or later or IE4 or later
		if (!navigator.cookieEnabled) {
			if (document.getElementById("cookiecolour")) {
				document.getElementById("cookiecolour").style.backgroundColor="#FF0000";
			}
			alert("Cookies need to be enabled past this point for these pages to work.");
		}
		else {
			if (document.getElementById("cookiecolour")) {
				document.getElementById("cookiecolour").style.backgroundColor="#00FF00";
			}
		}
		//alert("Level1 "+cookieEnabled);
	}
	else {
		if (document.getElementById("cookiecolour")) {
			document.getElementById("cookiecolour").style.backgroundColor="#00FF00";
		}
		if (typeof document.cookie == "string") {
			if (document.cookie.length == 0) {
				document.cookie = "test";
				cookieEnabled = (document.cookie == "test");
				document.cookie = "";
				//alert("Level2 "+cookieEnabled);
			} 
			else {
				cookieEnabled = true;
				//alert("Level3 "+cookieEnabled);
			}
		}
		/*else {
			alert("Cookies need to be enabled past this point for these pages to work.");
		}*/
	}
	
}

/*
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
*/
//example 5.3
function getIEVersionNumber() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

function getNNVersionNumber() {
    if (navigator.appName == "Netscape") {
        var appVer = parseFloat(navigator.appVersion);
        if (appVer < 5) {
            return appVer;
        } else {
            if (typeof navigator.vendorSub != "undefined") {
                return parseFloat(navigator.vendorSub);
            }
        }
    }
    return 0;
}

function GetQuery(Ident) {
   var args = new Object();
   var query = location.search.substring(1); //get query string. includes ? so have to start at '1' rather than '0' position
   var pairs = query.split("&"); //break at & for (var i=0; i",">");
   for (var i=0; i<pairs.length; i++) {
     var pos = pairs[i].indexOf('='); //looks for "name=value"
     if (pos == -1) {
        continue;   // if not found skip
     }
     var argname = pairs[i].substring(0,pos);
     var value = pairs[i].substring(pos+1);
     args[argname] = unescape(value);
     // in Javascript 1.5 use decodeURIComponent() instead of unescape()
   }
   if (args[Ident]) {
      var Result=args[Ident];
	  return Result;
   }
    
}


