
function restrictInput(parent) {
    debugger



    var timeContainer = $(parent).parent()
    var el = $(timeContainer).find(".ui-time-picker-textbox")
    var time = el.val()

    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;




    if (keycode === 8 || keycode === 46) {
        return true
    }
    var length = time.length
    switch (true) {
        case (length === 0 || length === 1 || length === 3 || length === 4 || length === 6 || length === 7):
            if ((keycode >= 48 && keycode <= 57)) {
                return true;
            }
            break;
        case (length === 2 || length === 5 || length === 8):
            formatText(parent);
            if (keycode === 97 || keycode === 65 || keycode === 77 || keycode === 80 || keycode === 109 || keycode === 112) {
                return true;
            }
            break;
        case (length === 9 || length === 10):
            if (keycode === 97 || keycode === 65 || keycode === 77 || keycode === 80 || keycode === 109 || keycode === 112) {
                return true;
            }
            break;

        default:
            return false
            break;

    }
    return false;
}

function formatText(parent) {
    debugger
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;


    if (keycode === 8 || keycode === 46) {
        return true;
    }

    var timeContainer = $(parent).parent()
    var el = $(timeContainer).find(".ui-time-picker-textbox")
    var time = el.val()

    switch (time.length) {
        case 2:
            el.val(time + ":");
            break;
        case 5:
            el.val(time + ":");
            break;
        case 8:
            el.val(time + " ");
            break;
        default:
            el.val(time.toUpperCase());
            break;

    }
}

function validateTime(parent) {
    var timeContainer = $(parent).parent()
    debugger
    setSpinnerValues(timeContainer);
    //if ($(timeContainer).find(".ui-time-picker-textbox").val()==="") {
    __doPostBack($(timeContainer).find('.ui-time-picker-textbox').attr('id'), "TextChanged");
    //}
}



function toggleTimePicker(parent) {
    var timeContainer = $(parent).parent()

    $.widget("ui.textSpinner", $.ui.spinner, {
        options: {
            wrap: false
        },
        _parse: function (value) {
            if ((value === '') || isNaN(value)) {
                value = this.options.values.indexOf(value);
                if (value === -1) {
                    value = 0;
                }
            }
            if (value < 0) {
                value = this.options.wrap ? (this.options.values.length - 1) : 0;
            } else if (value >= this.options.values.length) {
                value = this.options.wrap ? 0 : (this.options.values.length - 1);
            }
            return value;
        },
        _format: function (value) {
            return this.options.values[value];
        },
        _adjustValue: function (value) {
            if (value < 0) {
                value = this.options.wrap ? (this.options.values.length - 1) : 0;
            } else if (value >= this.options.values.length) {
                value = this.options.wrap ? 0 : (this.options.values.length - 1);
            }
            return value;
        }
    });

    if ($(timeContainer).find('.ui-time-picker-div')[0].style.display === "block") {
        $(timeContainer).find('.ui-time-picker-div')[0].style.display = "none";
        __doPostBack($(timeContainer).find('.ui-time-picker-textbox').attr('id'), "TextChanged");
    } else {
        createSpinner(timeContainer);
        $(timeContainer).find('.ui-time-picker-div')[0].style.display = "block";
    }
    setSpinnerValues(timeContainer);
    return false;

}


function createSpinner(timeContainer) {

    $(timeContainer).find(".selHours").spinner({
        step: 1,
        max: 12,
        min: 1,
        mouseWheel: true,
        increment: 'fast',
        numberFormat: "n",
        allowNull: false,
        disabled: false,
        create: function (event, ui) {

        },
        change: function () {
            h = $(this).spinner('value');
            h = (h < 10) ? ("0" + h) : h;
            $(this).val(h);
            setTime(timeContainer);
        }
        , stop: function (event, ui) {
            h = $(this).spinner('value');
            h = (h < 10) ? ("0" + h) : h;
            $(this).val(h);
            setTime(timeContainer);
        }
    });
    $(timeContainer).find(".selMinutes").spinner({
        step: 1,
        max: 59,
        min: 0,
        mouseWheel: true,
        increment: 'fast',
        numberFormat: "n",
        allowNull: false,
        disabled: false,
        change: function () {
            h = $(this).spinner('value');
            h = (h < 10) ? ("0" + h) : h;
            $(this).val(h);
            setTime(timeContainer);
        },
        stop: function () {
            h = $(this).spinner('value');
            h = (h < 10) ? ("0" + h) : h;
            $(this).val(h);
            setTime(timeContainer);
        }
    });
    $(timeContainer).find(".selSeconds").spinner({
        step: 1,
        max: 59,
        min: 0,
        mouseWheel: true,
        increment: 'fast',
        numberFormat: "n",
        allowNull: false,
        disabled: false,
        change: function () {
            h = $(this).spinner('value');
            h = (h < 10) ? ("0" + h) : h;
            $(this).val(h);
            setTime(timeContainer);
        },
        stop: function () {
            h = $(this).spinner('value');
            h = (h < 10) ? ("0" + h) : h;
            $(this).val(h);
            setTime(timeContainer);
        }
    });

    var arrText = ["AM", "PM"];
    $(timeContainer).find(".selAMPM").textSpinner({
        values: arrText,
        stop: function (event, ui) {

            setTime(timeContainer);
        }, change: function () {
            setTime(timeContainer);
        }
    });
}

function setSpinnerValues(timeContainer) {
    var isValidatedTime = true;
    var time = $(timeContainer).find(".ui-time-picker-textbox").val()
    if (time !== "") {
        try {

            var arrTime = time.split(":");


            if (!(arrTime[0].length === 2 && arrTime[1].length === 2)) {
                isValidatedTime = false;
            }

            if (!isNaN(arrTime[0]) && isValidatedTime) {
                if (parseInt(arrTime[0]) > 0 && parseInt(arrTime[0]) < 13) {
                    $(timeContainer).find(".selHours").val(arrTime[0]);
                } else {
                    isValidatedTime = false;
                }
            } else {
                isValidatedTime = false;
            }



            if (!isNaN(arrTime[1]) && isValidatedTime) {
                if (parseInt(arrTime[1]) >= 0 && parseInt(arrTime[1]) < 60) {
                    $(timeContainer).find(".selMinutes").val(arrTime[1]);
                } else {
                    isValidatedTime = false;
                }
            } else {
                isValidatedTime = false;
            }




            var t = arrTime[2].split(" ");
            if (t.length === 2 && isValidatedTime) {

                if ((!isNaN(t[0])) && isValidatedTime && t[0].length === 2) {
                    if (parseInt(t[0]) >= 0 && parseInt(t[0]) < 60) {
                        $(timeContainer).find(".selSeconds").val(t[0]);
                    } else {
                        isValidatedTime = false;
                    }
                } else {
                    isValidatedTime = false;
                }


                if (t[1].toUpperCase() === "AM" || t[1].toUpperCase() === "PM") {
                    $(timeContainer).find(".selAMPM").val(t[1]);
                } else {
                    isValidatedTime = false;
                }


            } else {
                isValidatedTime = false;
            }

        } catch (e) {
            isValidatedTime = false;

        }
        if (isValidatedTime === false) {
            setDefaultTimetoSpinner(timeContainer)
            setTime(timeContainer);
            __doPostBack($(timeContainer).find('.ui-time-picker-textbox').attr('id'), "TextChanged");
            alert("Please enter  time in hh:mm:ss AM/PM format.");
        }
    }
}

function setDefaultTimetoSpinner(timeContainer) {
    $(timeContainer).find(".selHours").val("01");
    $(timeContainer).find(".selMinutes").val("01");
    $(timeContainer).find(".selSeconds").val("01");
    $(timeContainer).find(".selAMPM").val("AM");
}




function setTime(timeContainer) {
    var time;
    var hour = $(timeContainer).find(".selHours").val();
    var minute = $(timeContainer).find(".selMinutes").val();
    var second = $(timeContainer).find(".selSeconds").val();
    var ampm = $(timeContainer).find(".selAMPM").val();
    time = hour + ":" + minute + ":" + second + " " + ampm
    $(timeContainer).find(".ui-time-picker-textbox").val(time)


}



//Temporary function: this not ideal solution consult shekhar sir(pending work) auto hide functionality
$(document).on('click', function (e) {
    var visibility = false;

    var btnTrigger = $(".btn-toggletimer");
    var container = $(".ui-time-picker-div");

    if (container.is(e.target) || container.has(e.target).length != 0) {
        return;
    }

    if (btnTrigger.is(e.target) || btnTrigger.has(e.target).length != 0) {
        return;
    }

    $(".ui-time-picker-div").each(function () {
        if ($(this)[0].style.display === "block") {
            visibility = true;
        }
    })


    $(".ui-time-picker-div").each(function () {
        if (visibility) {
            $(this)[0].style.display = "none"
            var timeContainer = $($(this)[0]).parent()
            __doPostBack($(timeContainer).find('.ui-time-picker-textbox').attr('id'), "TextChanged");
        }
    })
});