function Wishlist(document, name, database, hours, path, domain, secure)
{
	path = "/";
    this.$document = document;
    this.$name = name;
    this.$database = database;
    if (!hours) hours = 30*24;  // efault to 30 days.
    this.$expiration = new Date((new Date()).getTime() + hours*3600000);
    if (path) this.$path = path; else this.$path = null;
    if (domain) this.$domain = domain; else this.$domain = null;
    if (secure) this.$secure = true; else this.$secure = false;
    this.load();
}


Wishlist.prototype.load = function() { 
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;   // Cookie not defined for this page.
    start += this.$name.length + 1;  // Skip name and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    var a = cookieval.split('&');    // Break it into array of name/value pairs.
    for(var i=0; i < a.length; i++)  // Break each pair into an array.
        a[i] = a[i].split(':');

    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }

    return true;
}


Wishlist.prototype.store = function () {
    var cookieval = "";
    for(var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
            continue;
        if (cookieval != "") cookieval += '&';
        cookieval += prop + ':' + escape(this[prop]);
    }
    var cookie = this.$name + '=' + cookieval;
    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';

    this.$document.cookie = cookie;
}


Wishlist.prototype.store_item = function (item, value) {
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;   // Cookie not defined for this page.
    start += this.$name.length + 1;  // Skip name and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);
    cookieval += '&' + item + ':' + escape(value);
    
    var cookie = this.$name + '=' + cookieval;
    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';

    this.$document.cookie = cookie;
}


Wishlist.prototype.add_to = function (name, value) {
    if (!value) value = 1;
    if (name>=0) this.store_item(name, value);
}


Wishlist.prototype.add_to_old = function (name, value) {
    if (!value) value = 1;
    if (name>=0) this[name] = value;
    this.store();
}


Wishlist.prototype.remove_from = function (name) {
    if (name>=0) delete this[name];
    this.store();
    location.reload();
}


Wishlist.prototype.remove = function() {
    var cookie;
    cookie = this.$name + '=';
    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;
    this.load();
}


Wishlist.prototype.showdatabase = function() {
        this.$database.printAll();
}


Wishlist.prototype.view = function() {
    document.write('<table class="database">');
    j = 0;
    for(var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
            continue;
        j++;
        if (j % 2 == 0) class = 'even';
        else class = 'odd';
        this.$database.printRow(prop, class, 'remove_from', "Remove");
    }
    document.write('</table>');
}

Wishlist.prototype.viewRows = function() {
    j = 0;
    for(var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
            continue;
        j++;
        if (j % 2 == 0) class = 'even';
        else class = 'odd';
        this.$database.printRow(prop, class, 'remove_from', "Remove");
    }
}

