/**
 * C'tor set up the default params used for cookie handling.
 * @param myDoc		required the Document object that the cookie is stored for.
 * @param myHours	optional number that specifies the number of hours from now
 *     				that the cookie should expire.
 * @param myPath	optional string that specifies the cookie path attribute.
 * @param myDomain	optional string that specifies the cookie domain attribute.
 * @param isSecure	optional boolean value that, if true, requests a secure cookie.
 */
function Cookie(myDoc, myHours, myPath, myDomain, isSecure) 
{
//	alert("myDoc:" + myDoc);
    this.$document = testParam( myDoc, document);
	this.$hours = testParam( myHours, null);
	this.$path = testParam( myPath, null);
	this.$domain = testParam( myDomain, null);
	this.$secure = (testParam( isSecure, false) == true);
}

/**
 * Sets cookie using the c'tor defaults unless overridden here.
 * @param myName	required a string that specifies a name for the cookie.
 * @param myValue	required a string that specifies a value for the cookie.
 * @param myHours	optional number that specifies the number of hours from now
 *     				that the cookie should expire.
 * @param myPath	optional string that specifies the cookie path attribute.
 * @param myDomain	optional string that specifies the cookie domain attribute.
 * @param isSecure	optional boolean value that, if true, requests a secure cookie.
 * @param myDoc		optional the Document object that the cookie is stored for.
 */
function testParam(param, fallback) {
    if (typeof(param) == 'undefined') param = fallback;	
	return param;
}


function _Cookie_setCookie (myName, myValue, myHours, myPath, myDomain, isSecure, myDoc) {
    var cText = myName + '=' + myValue;
	
	myHours = testParam( myHours, this.$hours);
	myPath = testParam( myPath, this.$path);
	myDomain = testParam( myDomain, this.$domain);
	isSecure = testParam( isSecure, this.$secure);
	myDoc = testParam( myDoc, this.$document);
    
	if (myHours) {
		myExp = new Date( (new Date()).getTime() + myHours * 60 * 60 * 1000 );
        cText += '; expires=' + myExp.toUTCString();
	}
    if (myPath) cText += '; path=' + myPath;
    if (myDomain) cText += '; domain=' + myDomain;
    if (isSecure) cText += '; secure';

	myDoc.cookie = cText;
//	alert(cText);
}

/**
 * Gets cookie using the c'tor defaults unless overridden here.
 * @param myName	required a string that specifies a name for the cookie.
 * @param myDoc		optional the Document object that the cookie is stored for.
 */
function _Cookie_getCookie(myName, myDoc)
{
	myDoc = testParam( myDoc, this.$document);

    var allcookies = myDoc.cookie;
    if (allcookies == "") return false;

    // Now extract just the named cookie from that list.
    var start = allcookies.indexOf(myName + '=');
    if (start == -1) return false;   // cookie not defined for this page.
    start += myName.length + 1;  // skip name and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    return allcookies.substring(start, end);
}

/**
 * Deletes cookie using the c'tor defaults.
 * @param myName	required a string that specifies a name for the cookie.
 */
function _Cookie_deleteCookie(myName)
{
    var cookie;
    cookie = myName + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
}



// Create a dummy Cookie object, so we can use the prototype object to make
// the functions above into methods.
new Cookie();
Cookie.prototype.get = _Cookie_getCookie;
Cookie.prototype.set = _Cookie_setCookie;
Cookie.prototype.zap = _Cookie_deleteCookie;

