﻿var highlightbox = "<div class=\"ui-state-highlight ui-corner-all\" style=\"margin-top: 20px; padding: 0pt 0.7em;\"><p><span class=\"ui-icon ui-icon-info\" style=\"float: left; margin-right: 0.3em;\"></span>##TEXT##</p></div>";
var errorbox = "<div class=\"ui-state-error ui-corner-all\" style=\"padding: 0pt 0.7em;\"><p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: 0.3em;\"></span>##TEXT##</p></div>";
var highlightokbox = "<div class=\"ui-state-highlight ui-corner-all greenicon\" style=\"margin-top: 20px; padding: 0pt 0.7em;\"><p><span class=\"ui-icon ui-icon-circle-check\" style=\"float: left; margin-right: 0.3em;\"></span>##TEXT##</p></div>";

function getHighlightBox(text) {
    return highlightbox.replace("##TEXT##", text);
}

function getHighlightOKBox(text) {
    return highlightokbox.replace("##TEXT##", text);
}

function getErrorBox(text) {
    return errorbox.replace("##TEXT##", text);
}

function setHighlight(text) {
    $('#messagediv').html(getHighlightBox(text));
}
function setHighlightOK(text) {
    $('#messagediv').html(getHighlightOKBox(text));
}
function setError(text) {
    $('#messagediv').html(getErrorBox(text));
}

function removeHighlight() {
    $('#messagediv').html("");
}

//function deleteConfirmation(msg, handler) {
//    var succes = function () {handler.parent().parent().fadeOut("slow");};
//    alert(succes);
//    deleteConfirmation(msg, handler, succes);
//} javascript kent geen method overloading. enkel de laatste method wordt gecalled. indien er items ontbreken zijn die undefined.

function todaysDate() {
    var datum = new Date();
    return datum.getDate() + "/" + (datum.getMonth()+1 > 9 ? "" : "0") + (datum.getMonth() + 1) + "/" + datum.getFullYear()
}

function fadeHandler (handler) {
    handler.parent().parent().parent().fadeOut("slow");
}

function inactiveHandler(handler) {
    handler.parent().parent().parent().addClass("rowinactive");
}

function inactiveHandlerHideButtons(handler) {
    inactiveHandler(handler);
    handler.parent(".controllingbuttons").fadeOut("fast");
}

function deleteConfirmation(msg, handler, onSucces) {
    //default behaviour.
    if (onSucces == undefined) {
        onSucces = fadeHandler;
    }

    var showIt = function (hash) {
        hash.w.find("#modaltekst").text(msg);
        $("#btnJa").data('handler', $(handler));
        $("#btnJa").data('onSucces', onSucces);
        $("#delmodal").show();
        return false;
    };

    $("#delmodal").jqm({ onShow: showIt }).jqmShow();
}
function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }

    alert(out);

    // or, if you wanted to avoid alerts...

    var pre = document.createElement('pre');
    pre.innerHTML = out;
    document.body.appendChild(pre)
}
$(document).ready(function () {
    $("#btnJa").click(function () {
        $("#delmodal").jqmHide();
        setHighlight("Verwijderen...");
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: $(this).data('handler').attr('href'),
            data: "{'id': " + $(this).data('handler').attr("data") + "}",
            dataType: "json",
            success: function (data) {
                if (data.d.succes) {
                    setHighlightOK("Verwijderd");
                    var fnc = $("#btnJa").data('onSucces');
                    fnc($("#btnJa").data('handler'));
                } else {
                    if (data.d.error != "") {
                        setError(data.d.error);
                    } else {
                        setError("Verwijderen mislukt.");
                    }
                }
            }
        });
    });

    $("#btnNee").click(function () {
        $("#delmodal").jqmHide();
    });
});

