// Basic Cookie Functions

function getCookie(NameOfCookie){
// First we check to see if there is a cookie stored.
// Otherwise the length of document.cookie would be zero.
	if (document.cookie.length > 0){
	
		// Second we check to see if the cookie's name is stored in the
		// "document.cookie" object for the page.
		
		// Since more than one cookie can be set on a
		// single page it is possible that our cookie
		// is not present, even though the "document.cookie" object
		// is not just an empty text.
		// If our cookie name is not present the value -1 is stored
		// in the variable called "begin".
		var begin = document.cookie.indexOf(NameOfCookie+"=");       // set the begin var equal to the position of the "=" that comes after the cookie name we searched for
		if (begin != -1){ // Note: != means "is not equal to"
			// So long as begin doesnt = -1, our cookie was found.
			// Now we need to pull out the value stored in the cookie and return it.
			begin += NameOfCookie.length+1;                          // re-assign the value of begin to the char after the "=" since thats where the cookie value starts
			var end = document.cookie.indexOf(";", begin);           // figure out where the cookie value ends by locating the first ";" after the position of begin
			if (end == -1){                                          // if we dont find the ";" that would seperate multiple cookie values
				end = document.cookie.length;                        // then the cookie value ends at the end of the cookie
			}
			//alert('Cookie retrieved: '+unescape(document.cookie.substring(begin, end)));
			return unescape(document.cookie.substring(begin, end));  // finally, we return the cookie value to the function call
		}
	}else{
		return null;                                                 // or we tell the calling function that we didnt find a match
	}
}

function setCookie(NameOfCookie, value, expiredays){
	// Three arguments are used to set the new cookie.
	// The name of the cookie, the value to be stored,
	// and finally the number of days until the cookie expires.
	// The first lines in the function convert
	// the number of days to a valid date.
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	
	// The next line stores the cookie, simply by assigning
	// the values to the "document.cookie" object.
	// Note the date is converted to Greenwich Mean time using
	// the "toGMTstring()" function.
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
	//if there is no expiredays given, then the result here is the "" empty text return. if there is an expiredate, then everything after the : will be passed (the cookie will have this written after its name "; expires="expiredate"" in GMT.
}

function delCookie (NameOfCookie){
	// The function simply checks to see if the cookie is set.
	// If so, the expiration date is set to Jan. 1st 1970.
	if (getCookie(NameOfCookie)) {
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}