﻿
var BaseName = "lList";
var Retain = 2;
var MaxSuffix = 9;
var MaxLength = 4000;
var US = String.fromCharCode(126);
var RS = String.fromCharCode(127);

var BRAND = 0;
var TYPE = BRAND + 1;
var QUERY = TYPE + 1;
var ID = QUERY + 1;
var NAME = ID + 1;
var COUNT = NAME + 1;
var COMMENT = COUNT + 1;

function parse(link) {
    var Fields = link.split(US);
    this.brand = Fields[BRAND];
    this.type = Fields[TYPE];
    this.query = Fields[QUERY];
    this.id = Fields[ID];
    this.name = Fields[NAME];
    this.count = Fields[COUNT];
    this.comment = Fields[COMMENT];
}
function getComment(text) {
    // var ReturnValue = prompt(text, "");
    // return ReturnValue;
    jPrompt(text, ' ', 'Comment', function(r) {
        if (r) {
            return r;
        }
        else {
            return null;
        }
    });
}
function saveLink(brand, type, query, id, name, count) {
    // var Comment = prompt("Enter optional comment", "");
    // var Comment = getComment("Enter optional comment");
    jPrompt("Enter optional comment", " ", "Comment", function(Comment) {
        if (Comment) {
            var Link = brand.toString() + US + type.toString() + US + query + US + id + US + name + US + count.toString() + US + $.trim(Comment);
            var CookieName = scanForCookie(BaseName, MaxSuffix);
            if (CookieName == null)
                createCookie(BaseName + "0", Link, Retain)
            else {
                var Value = readCookie(CookieName);
                if (Value.length + Link.length < MaxLength)
                    createCookie(CookieName, Value + RS + Link, Retain)
                else {
                    CookieName = BaseName + (parseInt(CookieName.substr(BaseName.length, 1)) + 1).toString();
                    createCookie(CookieName, Link, Retain);
                }
            }
        }
        else
            jAlert("Link not saved", "Warning");
    });
}

function createCookie(name, value, days) {
    if (days) {
        var Now = new Date();
        Now.setTime(Now.getTime() + (days * 24 * 60 * 60 * 1000));
        var Expires = "; expires=" + Now.toUTCString();
    }
    else
        var Expires = "";
    document.cookie = name + "=" + value + Expires + "; path=/";
}
function readCookie(name) {
    var NameEq = name + "=";
    var Cookies = document.cookie.split(";");
    for (i in Cookies) {
        var Cookie = Cookies[i];
        while (Cookie.charAt(0) == " ")
            Cookie = Cookie.substring(1, Cookie.length);
        if (Cookie.indexOf(NameEq) == 0)
            return Cookie.substring(NameEq.length, Cookie.length);
    }
    return null;
}
function deleteCookie(name) {
    createCookie(name, "", -1);
}
function scanForCookie(name, suffix) {
    var Cookies = document.cookie.split(";");
    for (var j = suffix; j >= 0; j--) {
        var NameEq = name + j.toString() + "=";
        for (i in Cookies) {
            var Cookie = Cookies[i];
            while (Cookie.charAt(0) == " ")
                Cookie = Cookie.substring(1, Cookie.length);
            if (Cookie.indexOf(NameEq) == 0)
                return NameEq.substr(0, NameEq.length - 1);
        }
    }
    return null;
}
function clearCookies() {
    for (var i = 0; i <= MaxSuffix; i++) {
        var Cookie = readCookie(BaseName + i.toString());
        if (Cookie == null)
            return;
        deleteCookie(BaseName + i.toString());
    }
}
