function Checklist(name, entityId, uriContext) {
    this.name = name;
    this.entityId = entityId;
    this.uriContext = uriContext;

    this.changeLabel = changeLabel;
    this.setWikiInput = setWikiInput;
    this.editWiki = editWiki;
    this.changeSelect = changeSelect;
    this.changeInput = changeInput;
    this.changeCheckbox = changeCheckbox;

    function isIe() {
        return navigator.userAgent.indexOf("Safari") < 0 && navigator.product != "Gecko";
    }

    function setSortValue(cellId, value) {
        tdElement = document.getElementById('sort_' + cellId);
        tdElement.abbr = value;
    }

    function updateSortValue(pageId, columnKey, cellId, columnType, optionIds, storeInRows) {
        var uri = makeUri(pageId,columnKey,cellId,"getSortValue");
        uri = uri + "&columnType=" + columnType;
        uri = uri + "&storeInRows=" + storeInRows;
        uri = uri + "&optionIds=" + encodeURIComponent(optionIds);
        var opts = {
                onComplete:function (data) {
                    setSortValue(cellId,data.responseText);
                }
            }
        var req = new Ajax.Request(uri, opts);
    }

    function changeLabel(pageId, labelName, cellId, value, checkValue) {
        var uri = makeUri(pageId,labelName,cellId,"changeLabel");
        uri = uri + "&value=" + value;
        var opts = {
                onComplete:function (data) {
                    var newvalue;
                    var icon;
                    if (value) {
                        newValue = false;
                        setSortValue(cellId,"0");
                        icon = 'error.gif';
                    } else {
                        newValue = true;
                        setSortValue(cellId,"1");
                        icon = 'check.gif';
                    }
                    var anchorElem = document.getElementById(cellId);
                    var callback = "return " + name + ".changeLabel('"
                            + pageId + "','"
                            + labelName + "','"
                            + cellId + "',"
                            + newValue + ",'')";
                    if (isIe()) {
                        anchorElem.onclick = new Function("evt",callback);
                    } else {
                        anchorElem.setAttribute("onclick",callback);
                    }
                    var imgElem = document.getElementById("img_" + cellId);
                    imgElem.setAttribute("src",uriContext + "/images/icons/emoticons/" + icon);
                }
        }
        var req = new Ajax.Request(uri, opts);
        return false;
    }

    function changeCheckbox(pageId, columnKey, cellId, value, checkValue) {
        var uri = makeUri(pageId,columnKey,cellId,"changeCheckbox");
        uri = uri + "&storeInRows=true";
        if (! value) {
            uri = uri + "&value=" + checkValue;
        }
        var opts = {
                onComplete:function (data) {
                    var icon;
                    if (value) {
                        newValue = false;
                        setSortValue(cellId,"FALSE");
                        icon = 'error.gif';
                    } else {
                        newValue = true;
                        setSortValue(cellId,"TRUE");
                        icon = 'check.gif';
                    }
                    var anchorElem = document.getElementById(cellId);
                    var callback = "return " + name + ".changeCheckbox('"
                            + pageId + "','"
                            + columnKey + "','"
                            + cellId + "',"
                            + newValue + ",'"
                            + checkValue + "')";
                    if (isIe()) {
                        anchorElem.onclick = new Function("evt",callback);
                    } else {
                        anchorElem.setAttribute("onclick",callback);
                    }
                    var imgElem = document.getElementById("img_" + cellId);
                    imgElem.setAttribute("src",uriContext + "/images/icons/emoticons/" + icon);
                }
        }
        var req = new Ajax.Request(uri, opts);
        return false;
    }

    function changeSelect(pageId, columnKey, cellId, optionIds, storeInRows) {
        var uri = makeUri(pageId, columnKey, cellId, "changeSelect");
        uri = uri + "&storeInRows=" + storeInRows;
        var selectElem = document.getElementById(cellId);
        var value = selectElem.options[selectElem.selectedIndex].value;
        uri = uri + "&value=" + value;
        uri = uri + "&optionIds=" + encodeURIComponent(optionIds);
        var opts = {
                onComplete:function (data) {
                    updateSortValue(pageId,columnKey, cellId, 'select',optionIds, storeInRows);
                }
        }
        var req = new Ajax.Request(uri, opts);
        return false;
    }

    function changeInput(pageId, columnKey, cellId, storeInRows) {
        var uri = makeUri(pageId, columnKey, cellId, "changeInput");
        var value = document.getElementById(cellId).value;
        uri = uri + "&value=" + value;
        uri = uri + "&storeInRows=" + storeInRows;
        var opts = {
                onComplete:function (data) {
                    updateSortValue(pageId,columnKey, cellId, 'select',"",storeInRows);
                }
        }
        var req = new Ajax.Request(uri, opts);
    }


    function editWiki(pageId, columnKey, cellId, rows, cols, storeInRows) {
        var uri = makeUri(pageId, columnKey, cellId, "editWiki");
        uri = uri + "&checklistId=" + name;
        uri = uri + "&rows=" + rows;
        uri = uri + "&cols=" + cols;
        uri = uri + "&storeInRows=" + storeInRows;
        var opts = {
                onComplete:function (data) {
                    document.getElementById("wiki_" + cellId).innerHTML = "";
                    document.getElementById("form_" + cellId).innerHTML = data.responseText;
                }
        }
        var req = new Ajax.Request(uri, opts);
        return false;
    }

    function setWikiInput(pageId, columnKey, cellId, rows, cols, storeInRows) {
        var uri = makeUri(pageId, columnKey, cellId, "setWikiInput");
        uri = uri + "&checklistId=" + name;
        uri = uri + "&rows=" + rows;
        uri = uri + "&cols=" + cols;
        uri = uri + "&storeInRows=" + storeInRows;
        value = document.getElementById(cellId).value;
        uri = uri + "&value=" + encodeURIComponent(value);
        var opts = {
                onComplete:function (data) {
                    document.getElementById("form_" + cellId).innerHTML = "";
                    var textElem = document.getElementById("wiki_" + cellId);
                    textElem.innerHTML = data.responseText;
                    updateSortValue(pageId,columnKey, cellId, 'wikiinput',"",storeInRows);
                }
        }
        var req = new Ajax.Request(uri, opts);
        return false;
    }

    function makeUri(pageId, columnKey, cellId, action) {
        var uri = uriContext + "/plugins/checklists/" + action + ".action" +
                  "?entityId=" + entityId +
                  "&columnKey=" + columnKey +
                  "&cellId=" + cellId +
                 "&pageId=" + pageId;
        return uri;
    }

}