function CookieManager(iExp,sDomain,sPath) {
    this.expireDays = iExp;
    this.domain     = sDomain;
    this.path       = sPath;
    this.loadCookies();
}

CookieManager.prototype = {
    lCookies:   null,
    expireDays: null,
    domain:     null,
    path:       null,

    loadCookies: function() {
        this.lCookies = new Array();
        var sc = document.cookie;

        if ( sc.length ) {
            var cookies = sc.split(';');
            for(var i=0; i<cookies.length; i++) {
                var cs = cookies[i].split("=");
                this.lCookies[i] = new Array();
                this.lCookies[i][0] = cs[0];
                this.lCookies[i][1] = cs[1];
                this.lCookies[i][2] = cs[1].split(":");
                if (this.lCookies[i][2].length == 1) {
                    this.lCookies[i][2] = null;
                }
            }
        }
    },

    getCookie: function(cname) {
        for (var i=0; i<this.lCookies.length; i++){
            if ( this.lCookies[i][0] == cname ) {
                if ( this.lCookies[i][2] != null ) {
                    return ""; // user has to use getCookiePart()
                }
                else {
                    return this.lCookies[i][1];
                }
            }
        }

        return null;
    },

    getCookiePart: function(cname,pname) {
        for (var i=0; i<this.lCookies.length; i++){
            if ( this.lCookies[i][0] == cname ) {
                if ( this.lCookies[i][2] != null ) {
                    for (var j=0; j<this.lCookies[i][2].length; j=j+2) {
                        if ( this.lCookies[i][2][j] == pname ) {
                            //alert( i + ", " + j);
                            var rv = this.lCookies[i][2][j+1];
                            //alert( "'"+ rv + "'" );
                            return rv;
                        }
                    }
                }
                else {
                    //alert( 'bad thing' );
                    return ""; // user has to use getCookie()
                }
            }
        }
        //alert( 'not found' );
        return null;
    },

    setCookie: function(cname, cvalue) {
        var bSkip = 1;
        
        for (var i=0; i<this.lCookies.length; i++ ) {
            if ( this.lCookies[i][0] == cname ) {
                if ( this.lCookies[i][2] == null ) {
                    this.lCookies[i][1] = cvalue;
                }
                bSkip = 0;
                i = this.lCookies.length;
            }            
        }
        
        if ( bSkip == 1 ) {
            this.lCookies.push([cname, cvalue, null]);
        }
    },

    setCookiePart: function(cname, pname, pvalue) {
        var bSkip = 1;
        for (var i=0; i<this.lCookies.length; i++ ) {
            if ( this.lCookies[i][0] == cname ) {
                if ( this.lCookies[i][2] != null ) {
                    for (var j=0; j<this.lCookies[i][2].length; j=j+2) {
                        if ( this.lCookies[i][2][j] == pname ) {
                            //alert( "'"+ pvalue + "'" );
                            this.lCookies[i][2][j+1] = pvalue;
                            return;
                        }                        
                    }
                    
                    if (bSkip == 1) {
                        this.lCookies[i][2].push(pname);
                        this.lCookies[i][2].push(pvalue);
                        return;
                    }
                }
                return;
            }            
        } 
        if ( bSkip == 1 ) {
            // alert("create new cookie");
            this.lCookies.push([cname, null, [pname, pvalue]]);
        }
       
    },

    storeCookies: function(bSingle) {
        // if bSingle is set to non zero value (e.g. 1), the manager
        // stores singletons as well
        var tar;
        var eD = null;
        var ext = "";
        if ( this.expireDays != null) {
            var d = new Date();
            d.setTime(d.getTime() + (this.expireDays*24*3600000));
            ext += "; expires="+d.toGMTString();
        }

        if ( this.path  != null ) {
            ext += "; path="+ this.path;
        }
        
        if ( this.domain  != null ) {
            ext += "; domain="+this.domain;
        }

        for(var i=0; i<this.lCookies.length; i++) {
            if ( this.lCookies[i][2] != null ) {
                var tstr=this.lCookies[i][0]
                    + "="
                    + this.lCookies[i][2].join(":")
                    + ext;
                //alert(tstr);
                document.cookie = tstr;
            }
            else if (bSingle != null && bSingle != 0) {
                document.cookie = this.lCookies[i][0]+"="+this.lCookies[i][1]+ext;
            }
        }
    }
};