﻿$(document).ready(function(){
    ajaxReloadFunctions();


    $(".numeric").keydown(function (event) {
        return isNumberKey(event);

    });

    $(".toolTip").tooltip();


    $("select.other").change(function () {

        if ($(this).children("option:selected").attr("hasOther") == "True") {
            $("#" + $(this).data("other")).show();
        }
        else {
            $("#" + $(this).data("other")).hide();
        }

    });

    $("select.other").each(function () {

        $(this).trigger('change');
    });


    $(".confirm").click(function () {
        var confirmQuestion = "Are you sure you want to do this?";

        if ($(this).attr("confirm") != undefined) {
            confirmQuestion = $(this).attr("confirm");
        }

        return confirm(confirmQuestion);
    });



});

function ajaxReloadFunctions() {





    $(".datepicker").each(function () {

        if ($(this).data("datepicker_setup") != "1") {
            $(this).datepicker({ dateFormat: "dd/mm/yy", changeYear: true });
            $(this).data("datepicker_setup", "1");
        }
    });


    $(".timepicker").each(function () {

        if ($(this).data("timepicker_setup") != "1") {
            var stepv = 15;
            if ($(this).data("time_step") != undefined) {
                stepv = $(this).data("time_step");
            }

            $(this).timepicker({ step: stepv, 'timeFormat': 'H:i' });
            $(this).keydown(function () { return false });
            $(this).data("timepicker_setup", "1");
        }
    });
}



function checkBoxAll(inputRootClass) {


    $('.' + inputRootClass).prop('checked', true);

    return false;
}

function checkBoxNone(inputRootClass) {
    $('.' + inputRootClass).prop('checked', false);
    return false;
}

function checkBox_WithClass(inputRootClass, className) {

    $('.' + inputRootClass).prop('checked', false);

    $('.' + inputRootClass + '.' + className).prop('checked', true);

    return false;
}

function htmlEnc(s) {
  return s.replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/'/g, '&#39;')
    .replace(/"/g, '&#34;');
}

function getFormData(selector) {
    var fd = {};

    var f = $(selector);
    f.find("input,select,textarea").each(function () {

        var fieldName = $(this).attr("name");

        if ($(this).data("field")) {
            fieldName = $(this).data("field");
        }


        switch ($(this)[0].tagName) {
            case "INPUT":

                switch ($(this).attr("type").toLowerCase()) {
                    case "checkbox":
                        fd[fieldName] = $(this).prop("checked");
                        break;
                    default:
                        fd[fieldName] = $(this).val();
                        break;

                }

                break;
            default:
                fd[fieldName] = $(this).val();
                break;
        }


    });


    return fd;
}