﻿var Comments = new function () {
    this.save = function (CoId) {
        var msgInvalid = $("#commentInvalid" + CoId).val();
        var msgAdded = $("#commentAdded" + CoId).val();

        var name = $("#commentName" + CoId).val();
        var msg = $("#commentMsg" + CoId).val();
        if (name.length < 3 || msg.length < 3) {
            alert(msgInvalid);
        } else {
            $.ajax({
                type: 'POST',
                url: "comments.aspx",
                contentType: "application/x-www-form-urlencoded;charset=utf-8",
                data: {
                    action: "ADD",
                    name: name,
                    msg: msg,
                    CoId: CoId,
                    LaId: curLanguage
                },
                success: function (a, b, c) {
                    // add the comment clientside
                    $("#commentsContainer" + CoId).prepend("<div class='comment'>" +
                        "<b>" + name + "</b> <small><i>(" + Comments.formatDate(new Date()) + ")</i></small><br>" +
                        msg + "<hr>" +
                        "</div>");
                    // increase comment counter
                    var el = $("#commentCounter" + CoId);
                    el.text(parseInt(el.text()) + 1);

                    alert(msgAdded);
                    document.getElementById("commentName" + CoId).value = "";
                    document.getElementById("commentMsg" + CoId).value = "";
                },
                DataType: "json"
            });
        }
    }
    this.Toggle = function (CoId) {
        var el = document.getElementById("commentEmbed" + CoId);
        var jel = $("#commentEmbed" + CoId);
        if (el.style.display == "none") {
            jel.show('slow');
        } else {
            jel.hide('slow');
        }
    }
    this.formatDate=function(da) {
        var d=Comments.fillDigits(da.getDate(),2);
        var m = Comments.fillDigits(da.getMonth() + 1, 2);
        var y=da.getFullYear();
        var h = Comments.fillDigits(da.getHours(), 2);
        var min = Comments.fillDigits(da.getMinutes(), 2);
        return d+ "-" + m + "-" + y + " " + h + ":" + min;
    }
    this.fillDigits=function (v) {
        if (new String(v).length==1) {
            return "0" + v;
        }
        return v;
    }
}
