﻿function readCookie(CookieName) {
	var nameEQ = CookieName + "=";
	//divide cookies into array
	var cookielist = document.cookie.split(';');
	for(var i=0;i < cookielist.length;i++) {
	    //get next cookie from list
		var CurrentCookie = cookielist[i];
		//remove leading spaces from cookie
		while (CurrentCookie.charAt(0)==' ') CurrentCookie = CurrentCookie.substring(1,CurrentCookie.length);
		//return cookie contents
		if (CurrentCookie.indexOf(nameEQ) == 0) return CurrentCookie.substring(nameEQ.length,CurrentCookie.length);
	}
	return null;
}

function readCookieParam(ParamName,sCookie) {
	var nameEQ = ParamName + "=";
	//divide cookies into array
	var paramlist = sCookie.split('&');
	for(var i=0;i < paramlist.length;i++) {
	    //get next cookie from list
		var CurrentParam = paramlist[i];
		//remove leading spaces from cookie
		while (CurrentParam.charAt(0)==' ') CurrentParam = CurrentParam.substring(1,CurrentParam.length);
		//return cookie contents
		if (CurrentParam.indexOf(nameEQ) == 0) return CurrentParam.substring(nameEQ.length,CurrentParam.length);
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
