// Basic Cookie Scripts Created 14 Sept 2008

// Function 1: setCookie(name,value,days)
// Function 2: getCookie(name)				// returns the cookie value
// Function 3: clearCookie(name)

// Note: To make things clear, cookie names begin with "c_"

//The following functions contains parts that derives from the Cookies tutorial on quirksmode.org
function setCookie(name,value,days){
	//if "days" is defined
	if (days){
		// create new date to be defined as number of days from now that the cookie will expire
		var date=new Date();
		// the math behind setting the date
		date.setTime(date.getTime()+(days*24*60*60*1000));
		// get a new var to hold the date in GMT format
		var expires=date.toGMTString();
		} else {
		//if "days" isn't defined, just make "expires" nothing, so the cookie will expire right after the browser closes
		var expires="";
		}
	
	//put the cookie together: name, value, expiration date
	document.cookie=name+"="+escape(value)+"; expires="+expires;
	}

//The following functions contains parts that derives from the Cookies tutorial on quirksmode.org
function getCookie(name){
	// get new var for convenience
	var nameEq=name+"=";
	// define an array for the cookies, which are split up with the ";"
	var cookieArray=document.cookie.split(";");
	// run through all the cookies
	for (var i=0;i<cookieArray.length;i++){
		// temporarily label each cookie unit
		var cookieUnit=cookieArray[i];
		// before doing anything to the cookieUnit, check to see if there are spaces, if there are, only use the part without the space
		while (cookieUnit.charAt(0)==" ") cookieUnit=cookieUnit.substring(1,cookieUnit.length);
		// if the name of the cookie exists at character "0," that means the cookie exists and is found. so, return the characters between the "=" after the cookie name and the end of the cookieUnit, which means: GET THE VALUE
		if (cookieUnit.indexOf(nameEq)==0) {
			return unescape(cookieUnit.substring(nameEq.length,cookieUnit.length));
			} 
		}
	}

// Simplified way to clear a cookie
function clearCookie(name){
	//set the cookie to a blank value with the expiration date as yesterday, resulting in immediate removal
	setCookie(name,"",-1)
	}
