/***********************************************
	stCookie CLASS
	an object representing a single cookie
************************************************/
stCookie = function(name,value,expires,domain,path,secure){
	this.name = name;
	this.value = value;
	this.expires = expires; //milliseconds
	this.domain = domain;
	this.path = path;
	this.secure = secure;
}

/**
	kill()
	causes this cookie to expire
**/
stCookie.prototype.kill = function(){
	this.expires = -1;
	this.set();
}
/**
	set()
	causes the current state of the cookie object
	to be written to the browser
**/
stCookie.prototype.set = function(){
	//alert("calling set");
	var CookieVal;
	if (this.name) {
		CookieVal=escape(this.name)+"=";
	    if (this.value) {
	    	CookieVal=CookieVal+escape(this.value);
			if (this.expires) {
				var d = new Date();
				var t = d.getTime();
				var expiresDate = new Date();
				expiresDate.setTime(t + this.expires); 
				CookieVal=CookieVal+"; expires="+expiresDate.toGMTString();
			}
			if (this.domain){
				CookieVal=CookieVal+"; domain="+this.domain;
			}
			if (this.path) {
				CookieVal=CookieVal+"; path="+this.path;
			}
			if (this.secure) {
				CookieVal=CookieVal+"; secure";
			}
		}
		//alert("SETCOOKIE: " + CookieVal);
		document.cookie=CookieVal;
	}
}

/***********************************************
	stCookies CLASS
	an object containing all visible cookies.
************************************************/
stCookies = function(){
	//trace("document.cookie = " + document.cookie);
	this.cookiesObj = new Object();
	
	if(document.cookie.length >0){
	 	var tmpCookies = document.cookie.split(";");
		for(var i=0;i<tmpCookies.length;i++){
			var tmpCookie = tmpCookies[i].split("=");
			this.cookiesObj[tmpCookie[0].split(" ").join("")] = new stCookie(tmpCookie[0].split(" ").join(""),tmpCookie[1]);
		}
	}
	
}

/**
	killAll()
	removes all visible cookies
**/
stCookies.prototype.killAll = function(){
	for(var i in this.cookiesObj){
		this.cookiesObj[i].kill();
		delete this.cookiesObj[i];
	}
}

/**
	killCookie(name)
	removes the cookie specified by name
**/
stCookies.prototype.killCookie = function(name){
	if(this.cookiesObj[name]){
		this.cookiesObj[name].kill();
		delete this.cookiesObj[name];
	}
}

/**
	addCookie(name,value[,expires[,domain[,path[,secure]]]])
	removes the cookie specified by name
**/
stCookies.prototype.setCookie = function(name,value,expires,domain,path,secure){
	this.cookiesObj[name] = new stCookie(name,value,expires,domain,path,secure);
	this.cookiesObj[name].set();
}

/**
	getValue(name)
	returns the values of the cookie specified by name
**/
stCookies.prototype.getValue = function(name){
	var retVal;
	if(this.cookiesObj[name]){
		//alert("found cookie " + name);
		retVal = this.cookiesObj[name].value;
		return retVal;
	}
	//alert("could not find cookie " + name);
	return -1;
}

/**
	showCookies(name)
	traces the value of all cookies
**/
stCookies.prototype.showCookies = function(){
	var tmpStr = "";
	for(var i in this.cookiesObj){
		tmpStr += i + " = " + this.cookiesObj[i].value + "\n";
	}
	if(tmpStr.length >0){
		//trace(tmpStr);
	}
}

