$(function() {
    // $.calculator.setDefaults({showOn: 'both', buttonImageOnly: true, buttonImage: 'img/calculator.png'});
    // $('.calculator').attr('readonly','readonly').calculator({
    //     calculatorClass: 'box box-success box-solid calculator',
    //     onClose: function(value, inst) {
    //         $(this).blur();
    //     }
    // });
});

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 = "SPS Lite Accounting";
        }
        new PNotify({
            title: title,
            text: msg,
            type: type_icon,
            icon: "fa " + iconD
        });
    } catch (err) {

    }
}

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;
    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"></i> '+noBtn+'</button>&nbsp;<button class="btn btn-primary" onClick="closepopupform();'+callbackfct+'"><i class="fa fa-check"></i> '+ yesBtn +'</button>',
    content = '<div class="popup_message_container"><div class="popup_message_icon"><i class="fa fa-question-circle fa-sm"></i></div><div class="popup_message_content">'+msg+'</div></div>';
    popupform(content, footer, 'medium');
}

function popupform(content, footer, size_sm) {
    closepopupform();
    if (typeof size_sm == 'undefined') {
        $('#_popup_container').removeClass().addClass('modal-dialog');
    } else {
        switch (size_sm) {
            case 'small':
			case 'sm':
                $('#_popup_container').removeClass().addClass('modal-dialog modal-sm');
                break;
			case 'medium':
            case 'md':
                $('#_popup_container').removeClass().addClass('modal-dialog modal-md');
                break;
            case 'large':
			case 'lg':
                $('#_popup_container').removeClass().addClass('modal-dialog modal-lg');
        }
    }

    $('#_popup_body').empty().html(content);
	$('#_popup_footer').empty().html(footer);
    $('#_popup_modal').modal('show');
}

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

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;
        }
    }
}
