// ***************************************************************************
// Author: Nicolas Livaditis
// Super Cookie management Code Started @ 8-6-99
// Javascript 1.0 compliant
// Last updated 5-5-2000 : Added Compact Get/Set cookie functions
// to get the usage from from 1 cookie... Overcome the 20 cookie
// limit imposed by browsers
// Version 3.0  - Added GetCCookie + SetCCookie
// ***************************************************************************
function GetCCookie(ParentName, Name) {
	var CCookie = GetCookie(ParentName);
	var start = CCookie.indexOf(Name + "=");
	if ( start >= 0 )  {
		start += Name.length + 1;
		var end = CCookie.indexOf(";", start);
		if ( !(end >= 0) ) end = CCookie.length;
		return unescape(CCookie.substring(start,end));
	}
	return "";
}
// Create or update a cookie given it's name and value.
// The name and value are required
function SetCCookie(ParentName, Name, Value, Expires, Path, Domain) {
	var ParentValue = GetCookie(ParentName);
	var docCookie = Name + "=" + Value + ";";
	if ( ParentValue ) {
		var end, pos = ParentValue.indexOf(Name + "=");
		if ( pos >= 0 ) {
			end = ParentValue.indexOf(";", pos + 1) + 1;
			docCookie = ParentValue.substring(0,pos) + docCookie + ParentValue.substring(end);
		}
		else
			docCookie += ParentValue;
	}
// Finally, attempt to save the cookie
	docCookie = ParentName + "=" + escape(docCookie) + MakeCookieAttrStr(Expires, Path, Domain);
	if ( docCookie.length > 1024 )
		alert("Cookie " + ParentName + " is too large to save with " + Name + " sub cookie!");
	else
		self.document.cookie = docCookie;
}

function GetCookie(Name)  { // Return the value of a cookie by name
	var allcookies = self.document.cookie;
	var pos = allcookies.indexOf(Name + "=");
	if ( !(pos >= 0) ) return "";
	var start = pos + Name.length + 1;
	var end = allcookies.indexOf(";", start);
	if ( !(end >= 0) ) end = allcookies.length;
	return unescape(allcookies.substring(start,end));
}
// Create or update a cookie given it's name and value.
// The name and value are required
function SetCookie(CookieName, Value, Expires, Path, Domain) {
	self.document.cookie = CookieName + "=" + escape(Value) + MakeCookieAttrStr(Expires, Path, Domain);
}

function DeleteCookie(CookieName, Path, Domain) {
	Path? (Path = "path=" + Path + ";") : Path = "";
	Domain? (Domain = "domain=" + Domain + ";") : Domain = "";
	self.document.cookie = CookieName + "=; expires=Mon, 26 Jul 1997 05:00:00 GMT;" + Domain + Path;
}

function MakeCookieAttrStr(Expires, Path, Domain) {
	var curDate;
	if ( !Expires ) { // Make stored cookie
		curDate = new Date();
		curDate.setMonth(curDate.getMonth()+84); // Set for 7 years off current year
		Expires = ";expires=" + curDate.toGMTString();
	}
	else if ( Expires == true ) // Make session cookie
		Expires = "";
	else // Set reasonble expire date for stored cookie
		Expires = ";expires=" + Expires;
	Path? (Path = ";path=" + Path) : Path = "";
	Domain? (Domain = ";domain=" + Domain) : Domain = "";	
	return Expires + Domain + Path;
}

function DeleteCCookie(ParentName, Name, Path, Domain) {
	var ParentValue = GetCookie(ParentName);
	if ( ParentValue ) {
		var end, pos = ParentValue.indexOf(Name + "=");
		if ( pos >= 0 ) {
			end = ParentValue.indexOf(";", pos + 1) + 1;
			docCookie = ParentValue.substring(0,pos) + ParentValue.substring(end);
			self.document.cookie = ParentName + "=" + escape(docCookie);
		}
	}
}
// Returns a GMT Date string based on your
// specified Y,M,D plus the current system date.
function GetExpDate(Years, Months, Days) {
	var expDate = new Date();
	if ( Years ) expDate.setMonth(expDate.getMonth() + 12 * Years);
	if ( Months ) expDate.setMonth(expDate.getMonth() + Months);
	if ( Days ) expDate.setDate(expDate.getDate() + Days);
	return expDate.toGMTString();
}
// JScript 1.0 string replace function
// Performs a basic Find and Replace operation on a string
function Replace(Expression, Find, Replace) {
	var pos = 0;
	while ( (pos = Expression.indexOf(Find,pos)) >= 0 )
		Expression = Expression.substring(0, pos) + Replace + Expression.substring(pos+Find.length);
	return Expression;
}
