function isjson(data) {
    try {
        var json = eval('(' + data + ')');
        return json;

    } catch (e)
    {
        return false;
    }
}

function notify(msg, title, typeD, icon) {
    try {
        var iconD = "fa-info-circle";
        type_icon = (typeof typeD == 'undefined') ? "info" : typeD;
        if (typeof (icon) == 'undefined') {
            switch (type_icon) {
                case "success":
                    iconD = "fa-thumbs-o-up";
                    break;
                case "info" :
                    iconD = "fa-info-circle";
                    break;
                case "error":
                case "warning":
                    iconD = "fa-info-circle";
                    break;
            }
        } else {
            iconD = icon;
        }

        if (title == null || title == false) {
            title = "Tekun";
        }
        new PNotify({
            title: title,
            text: msg,
            type: type_icon,
            icon: "fa " + iconD
        });

    } catch (err) {

    }
}


function popupform(content, header, footer, size_sm) {
    closepopupform();
    if (typeof size_sm == 'undefined') {
        $("#_popup_contenair").removeClass("modal-lg");
        $("#_popup_contenair").removeClass("modal-sm");
    } else {
        switch (size_sm) {
            case "sm":
                $("#_popup_contenair").removeClass("modal-lg");
                $("#_popup_contenair").addClass("modal-sm");
                break;
            case "me":
            case "medium":
                $("#_popup_contenair").removeClass("modal-lg");
                $("#_popup_contenair").removeClass("modal-sm");
                break;
            case "lg":
                $("#_popup_contenair").addClass("modal-lg");
                $("#_popup_contenair").removeClass("modal-sm");
        }
    }

    $("#_popup_body").empty();
    $("#_popup_body").append(content);
    popup_header_footer(header, footer);
    $("#_popup_modal").modal("show");
}

function closepopupform() {
    $("#_popup_modal").modal("hide");
}

function popup_header_footer(header, footer) {
    if (typeof header == 'undefined' || header == false || header == "") {
        $("#_popup_header").attr("style", "display:none");
    } else {
        $("#_popup_header").attr("style", "display:block");
        $("#_popup_header").empty();
        $("#_popup_header").append(header);
    }

    if (typeof footer == 'undefined' || footer == false || footer == "") {
        $("#_popup_footer").attr("style", "display:none")
    } else {
        $("#_popup_footer").attr("style", "display:block");
        $("#_popup_footer").empty();
        $("#_popup_footer").append(footer);
    }
}

$('#_popup_modal').on('hidden.bs.modal', function (e) {
    //need to verify if all variable are take before
    // $("#_popup_header").empty();
    //$("#_popup_body").empty();
    //$("#_popup_footer").empty();
});


function datatable_reoder_rowno(tname, startRow, columTo) {
    var startat = (typeof (startRow) == "undefined") ? 0 : startRow;
    var columTo = (typeof (columTo) == "undefined") ? 0 : columTo;
    var n = $("#" + tname + " > tbody > tr").length;
    var j = 0;
    for (i = startat; i < n; i++) {
        if (!$("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + columTo + ")").hasClass("dataTables_empty")) {
            $("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + columTo + ")").empty().append('<div class="text-center width-100">' + (++j) + '</div>');
        }
    }
}


function datatable_total(tname, columns) {

    var oTable = $("#" + tname).DataTable(),
            ln = $("select[name=" + tname + "_length]").val();
    oTable.page.len(oTable.page.info().recordsTotal).draw();

    var n = $("#" + tname + " > tbody > tr").length;
    var col = columns + "";
    var a_columns = col.split(",");

    var j = 0;
    var total = [];
    var nC = a_columns.length;

    for (k = 0; k < nC; k++) {
        total[a_columns[k]] = 0;
    }
    if (typeof accounting.unformat == 'function') {
        for (i = 0; i < n; i++) {
            for (j = 0; j < nC; j++) {
                if (!$("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + a_columns[j] + ")").hasClass("dataTables_empty")) {
                    total[a_columns[j]] += accounting.unformat($("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + a_columns[j] + ")").text());
                    total[a_columns[j]] += ($("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + a_columns[j] + ") input[type=text]").length) ? accounting.unformat($("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + a_columns[j] + ") input[type=text]").val()) : 0;
                }
            }
        }
    } else {
        for (i = 0; i < n; i++) {
            for (j = 0; j < nC; j++) {
                if (!$("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + a_columns[j] + ")").hasClass("dataTables_empty")) {
                    total[a_columns[j]] += parseFloat($("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + a_columns[j] + ")").text());
                    total[a_columns[j]] += ($("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + a_columns[j] + ") input[type=text]").length) ? parseFloat($("#" + tname + " > tbody > tr:eq(" + i + ") > td:eq(" + a_columns[j] + ") input[type=text]").val()) : 0;
                }
            }

        }
    }
    oTable.page.len(ln).draw();
    if (nC > 1) {
        return total;
    } else {
        return total[columns * 1];
    }

}

function confirmBox(callbackfct, msg, yesBtn, noBtn) {
    var yesBtn = (typeof (yesBtn) == "undefined") ? custombox_confirm : yesBtn;
    var noBtn = (typeof (noBtn) == "undefined") ? custombox_cancel : noBtn;
    header = '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h4 class="modal-title" id="myModalLabel">Tekun</h4></div>';
    var msg = (typeof (msg) == "undefined") ? custombox_defaultmsg : msg;
    var footer = '<button class="btn btn-default" data-dismiss="modal" aria-hidden="true"> <i class="fa fa-close"> '+ noBtn +' </i> </button> <button class="btn btn-primary" type="button" title="" onclick="closepopupform();'+ callbackfct +'"> <i class="fa fa-check"></i> '+ yesBtn +'</button>',
            content = "<div class='popup_message_contenair'><div class='popup_message_icon'><i class='fa fa-question-circle'></i></div><div class='popup_message_content'>" + msg + "</div></div>";
    popupform(content, header, footer,'me');
}

function date_format(date_or, from, to) {


    if (date_or == "" || date_or == null) {
        return "";
    }



    if (date_or == "lastday" || date_or == "lastdayofmonth") {
        var d = new Date(),
                lastday = new Date(d.getFullYear(), d.getMonth() + 1, 0),
                mm = ((lastday.getMonth() + 1) > 9) ? lastday.getMonth() + 1 : "0" + (lastday.getMonth() + 1),
                dd = (lastday.getDate() > 9) ? lastday.getDate() : "0" + lastday.getDate(),
                yyyy = lastday.getFullYear();
        if (from == "en" || from == "mm/dd/yyyy") {
            return mm + "/" + dd + "/" + yyyy;
        } else {
            return dd + "/" + mm + "/" + yyyy;
        }
    }

    if (date_or == "firstday" || date_or == "firstdayofmonth") {
        var d = new Date(),
                day = new Date(d.getFullYear(), d.getMonth(), 1);
        var mm = ((day.getMonth() + 1) > 9) ? day.getMonth() + 1 : "0" + (day.getMonth() + 1),
                dd = (day.getDate() > 9) ? day.getDate() : "0" + day.getDate(),
                yyyy = day.getFullYear();
        if (from == "en" || from == "mm/dd/yyyy") {
            return mm + "/" + dd + "/" + yyyy;
        } else {
            return dd + "/" + mm + "/" + yyyy;
        }
    }

    if (date_or == "today") {
        var day = new Date();
        var mm = ((day.getMonth() + 1) > 9) ? day.getMonth() + 1 : "0" + (day.getMonth() + 1),
                dd = (day.getDate() > 9) ? day.getDate() : "0" + day.getDate(),
                yyyy = day.getFullYear();
        if(from == "db"){
            return yyyy+'-'+mm+'-'+dd;
        }else
        if (from == "en" || from == "mm/dd/yyyy") {
            return mm + "/" + dd + "/" + yyyy;
        } else {
            return dd + "/" + mm + "/" + yyyy;
        }
    }

    if(date_or =="now"){
        var day = new Date();
        var mm = ((day.getMonth() + 1) > 9) ? day.getMonth() + 1 : "0" + (day.getMonth() + 1),
                dd = (day.getDate() > 9) ? day.getDate() : "0" + day.getDate(),
                yyyy = day.getFullYear(),
                hh = (day.getHours()<10) ? '0'+day.getHours(): day.getHours() ,
                mn = (day.getMinutes()<10) ? '0'+day.getMinutes(): day.getMinutes();
        if(from == "db"){
            return yyyy+'-'+mm+'-'+dd + " " + hh + ':'+mn;
        }else
        if (from == "en" || from == "mm/dd/yyyy") {
            return mm + "-" + dd + "-" + yyyy + " " + hh + ':'+mn;
        } else {
            return dd + "-" + mm + "-" + yyyy+ " " + hh + ':'+mn;
        }
    }

    from = (typeof (from) === "undefined") ? "db" : from;
    to = (typeof (to) === "undefined") ? "fr" : to;


    if (from == "db") {
        var d1 = date_or;
        var a1 = d1.split("-");
        if (a1.length < 3) {
            return "";
        }

        if (to == "fr" || to == "dd/mm/yyyy") {
            var res = a1[2] + "/" + a1[1] + "/" + a1[0];
            return res;
        }
    }
}

function abbreviateNumber(value) {
    var newValue = value;
    if (value >= 1000) {
        var suffixes = ['', 'K', 'M', 'B', 'T'];
        var suffixNum = Math.floor((""+value).length/3 );
        var shortValue = '';
        for (var precision = 3; precision >= 1; precision--) {
            shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
            var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g, '');
            if (dotLessShortValue.length <= 3) { break; }
        }
        if (shortValue % 1 != 0) shortNum = shortValue.toFixed(1);
        newValue = shortValue + suffixes[suffixNum];
    }
    return newValue;
}
