/**
 * Cookie getter and setter
 */

String.prototype.trim = function()
{
    var value = this;
    while (value.substring(0, 1).match(/\s/)) {
        value = value.substring(1, value.length);
    }
    while (value.substring(value.length-1,value.length).match(/\s/)) {
        value = value.substring(0, value.length-1);
    }
    return value;
}

CKW.Cookie = function(cookieName)
{
    this.name = cookieName;
    this.data = "";
    this.expiry = "";
    this.values = new Object;
    this.load();
}

CKW.Cookie.prototype.load = function(section_separator, value_separator)
{
    if (document.cookie) {
        var allcookies = document.cookie;
        var start = allcookies.indexOf(this.name);
        if (start != -1) {
            start += this.name.length + 1;
            var end = allcookies.indexOf(";", start);
            if (end == -1) {
                end = allcookies.length;
            }
            var data = allcookies.substring(start, end);
            this.data = unescape(data);
            this.getValuePairs(section_separator, value_separator);
        }
    }
    return false;
}

CKW.Cookie.prototype.packValues = function()
{
    var data = "";
    var str;
    for (value in this.values) {
        str = value + ":" + this.values[value] + "|";
        data += str;
    }
    data = data.substring(0, data.length-1);
    this.setData(data);
}

CKW.Cookie.prototype.getCookieData = function(cookieName)
{
    for (var i=0; i<this.cookies.length; i++) {
        if (this.cookies[i].name == cookieName) {
            return this.cookies[i].value;
        }
    }
    return false;
}

CKW.Cookie.prototype.getValuePairs = function(section_separator, value_separator)
{
    if (!section_separator) {
        section_separator = "|";
    }
    if (!value_separator) {
        value_separator = ":";
    }
    var parts = this.data.split(section_separator);
    if (!parts[1] && parts[0].length > 0) {
        parts[0] = parts[0].trim();
        var bits = parts[0].split(value_separator);
        bits[0] = bits[0].trim();
        bits[1] = bits[1].trim();
        this.values[bits[0]] = bits[1];
        return true;
    }
    if (parts[1]) {
        for (var i=0; i<parts.length; i++) {
            parts[i] = parts[i].trim();
            var bits = parts[i].split(value_separator);
            bits[0] = bits[0].trim();
            bits[1] = bits[1].trim();
            this.values[bits[0]] = bits[1];
        }
        return true;
    }
    return false;
}

CKW.Cookie.prototype.set = function()
{
    var cookie = this.name + "=" + this.data;
    if (this.expiry) {
        cookie += "; expires=" + this.expiry;
    }
    if (this.path) {
        cookie += "; path=" + this.path;
    }
    document.cookie = cookie;
}

CKW.Cookie.prototype.destroy = function()
{
    this.setExpiry(0, 0, 0, -60);
    var cookie = this.name + "=\"\";expires=" + this.expiry + ";path=" + this.path;
    document.cookie = cookie;
}

CKW.Cookie.prototype.setData = function(data)
{
    this.data = data;
}

CKW.Cookie.prototype.setExpiry = function(years, days, hours, minutes)
{
    if (!years) years = 0;
    if (!days) days = 0;
    if (!hours) hours = 0;
    if (!minutes) minutes = 0;
    var date = new Date();
    var exp = date.getTime() + (years * 31536000000) + (days *86400000) + (hours * 3600000) + (minutes * 60000);
    date= new Date(exp);
    this.expiry = date.toGMTString();
}

CKW.Cookie.prototype.setPath = function(path)
{
    this.path = path;
}
