//----------------------------------------------------
//  Cookie Manager Object
//----------------------------------------------------
function CookieManager() {
    this.document = document;
    this.reload();
}

CookieManager.prototype.reload = function() {
    this.cookies = this.document.cookie;
}
CookieManager.prototype.toString = function() {
    var strProps = "";
    
    for ( prop in this ) {
        strProps =+ prop + ": " + this[prop];
    }
    
    return strProps;
}
CookieManager.prototype.getCookieString = function(format) {
    if ( format ) {
        var strFormat = format.toLowerCase();
        
        if ( strFormat == "html" ) {
            return this.cookies.replace(/;/g, ";<br/>");
        }
        if ( strFormat == "javascript" ) {
            return this.cookies.replace(/;/g, ";\n");
        }
        else {
            return this.cookies;
        }
    }
    else {
        return this.cookies;
    }
}
CookieManager.prototype.getCookieObjects = function(name) {
    var arrCookieStrings = this.cookies.split(";");
    var arrNameValPair;
    var arrCookieObjects = new Array();
    
    for ( var c in arrCookies ) {
        arrNameValPair = arrCookies[c].split("=");
        var oCookie = new Cookie();
        
        oCookie.name = arrNameValuePair[0];
        oCookie.value = arrNameValuePair[1];
        
        arrCookieObjects.push(oCookie);
    }
    return arrCookieObjects;
}
CookieManager.prototype.getCookie = function(name) {
    //debugger;
    var regExCookie;
    eval("regExCookie = /(" + name + ")=([^;]+);/");
    var arrMatches = this.cookies.match(regExCookie);

    //alert(regExCookie);
    var oCookie = new Cookie();
    
    if ( arrMatches != null ) {
        oCookie.name = arrMatches[1];
        oCookie.value = arrMatches[2];
    }
    else {
        oCookie.name = "not found";
    }
    
    return oCookie;
}
CookieManager.prototype.commit = function(obj_cookie) {
    //oPageUtil.updateContent(oPageUtil.outputPaneID, "calling commit...");
    var strData = obj_cookie.getFormattedData();
    
    if ( strData.length > 0 ) {
        this.document.cookie = strData;
        this.reload();
    }
    
    //oPageUtil.updateContent(oPageUtil.outputPaneID, obj_cookie.getFormattedData() + "...done.");
}

function Cookie() {
    this.max_age = "";
    this.expires = "";
    this.path = "";
    this.domain = "";
    this.secure = "";
    this.name = "";
    this.value = "";
}
Cookie.prototype.getFormattedData = function() {                
    var strProps = "";
    
    for ( var prop in this ) {
        eval("strType = typeof(this." + prop + ")");
        
        if ( strType == "string" ) {
            eval("strVal = this." + prop);
            if ( strVal.length > 0 ) {
                if ( prop != "value" && prop != "name" ) {
                    strProps += prop + "=" + strVal + "; ";
                }
            }
        }
        //strProps += prop + "|" + strType + "<br/>";
    }
    
    this.value = "" + this.value + "";
    
    //if ( this.value.length > 0 ) {
        if ( strProps.length > 0 ) {
            strProps = "; " + strProps;
        }
        return this.name + "=" + escape(this.value) + strProps;
    /*    
    }
    else {
        return "";
    }*/
    
    //oPageUtil.updateContent(oPageUtil.outputPaneID, strProps);
}

Cookie.prototype.getEscapedProp = function(name) {
    var strProp;
    
    eval("strProp = unescape(this." + name + ");");
    
    return strProp;
}
