
var defaultDateVal;
var Current_;

//$(document).ready(function (event) {
//    if (Sys != undefined || typeof (Sys) != "undefined") {
//        RequestManager = Sys.WebForms.PageRequestManager.getInstance();
//        RequestManager.add_endRequest(EndRequestHandlerDT);
//    }


//    if (typeof FinIQ_DateTime_Picker != undefined) {
//        FinIQ_DateTime_Picker();
//    }
//});
//function EndRequestHandlerDT(sender, args) {
//    if (typeof FinIQ_DateTime_Picker != undefined) {
//        FinIQ_DateTime_Picker();
//    }
//}
 

function DateTime_Picker(ctl_DateTimePicker, _DateFormat, _disabled) {
    
    DisableDateTimePicker(ctl_DateTimePicker, _disabled);

}
//updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations  
function EndRequestHandlerDT(sender, args) {
    if (typeof FinIQ_DateTime_Picker != undefined) {
        FinIQ_DateTime_Picker();
    }
}

if (typeof FinIQ_DateTime_Picker != undefined) {
    $(document).ready(function () {
        FinIQ_DateTime_Picker(); 
    });
}

//updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
function FinIQ_DateTime_Picker() {
    try {
        
        var MONTH_NAMES1 = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

        $.support.cors = true;

        // $(document).ready(function () {
        $(document).find('.txtDateTime:visible').each(function (indexDT, UIDateTime) {
            //Added and updated by ShekharL @02-Jun-2022 : BBVAORINT - 574  Pre - Booking Done Queue > Amend Request > Trade Date and Settlement Date should be Read only
            var flag = $(UIDateTime).attr('disabled');
            if (flag == "disabled") {
                $(UIDateTime).datepicker('disable');
                $(UIDateTime).parent().find('.ui-datepicker-trigger').attr('disabled', 'disabled').prop('disabled', true);
            }
            else {
                $(UIDateTime).datepicker('enable');
                $(UIDateTime).parent().find('.ui-datepicker-trigger').prop('disabled', false).removeAttr('disabled');
            }
            //$('[id="' + ctl_DateTimePicker + '"]:visible')
            $(UIDateTime).datepicker({
                autoOpen: false,
                constrainInput: true,
                changeMonth: true,
                changeYear: true,
                showOn: "button",
                buttonImageOnly: false,
                buttonText: "",
                dateFormat: 'dd-M-yy',
                showOtherMonths: true,
                selectOtherMonths: true,
                showButtonPanel: false,
                // disable: _disabled,
                firstDay: 0,
                isRTL: false,
                stepMonths: 0,
                yearRange: "1900:+25",
                hideIfNoPrevNext: false,
                dayNamesMin: ["S", "M", "T", "W", "T", "F", "S"]

                , onSelect: function (datetext, inst) {
                    Current_ = inst;
                    DateFormat = $(this).parent().find('input[type = "hidden"]').val();
                    if (DateFormat == "DATETIME") {
                    //updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
                        h = $(".selHours").val();
                        m = $(".selMinuts").val();
                        s = $(".selSecond").val();
                        t = $(".selAMPM").val();
                        datetext = datetext + " " + h + ":" + m + ":" + s + " " + t;
                    }

                    else {

                        datetext = datetext;
                    }
                    $(this).val(datetext);
                    $("#" + Current_.id).focus();  //updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
                    //__doPostBack($(this).attr('id'), "TextChanged");
                }
                , beforeShow: function (input, inst) {
                    Current_ = inst;
                    DateFormat = $(this).parent().find('input[type="hidden"]').val();

                    if ($(this).attr('disabled') != 'undefined') {
                        var flag = $(this).attr('disabled');
                        if (flag == "disabled") {
                            $(this).datepicker('disable');
                            $(this).parent().find('.ui-datepicker-trigger').attr('disabled', 'disabled').prop('disabled', true);
                        }
                        else {
                            $(this).datepicker('enable');
                            $(this).parent().find('.ui-datepicker-trigger').prop('disabled', false).removeAttr('disabled');
                        }
                    }
                    else {
                        $(this).datepicker('enable');
                    }
                    if (DateFormat == "DATETIME") {
                        AddTimePicker(Current_); //updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 

                    }

                    defaultDateVal = $(this).val();

                }
                , onChangeMonthYear: function (year, month, inst) {
                    Current_ = inst;

                    DateFormat = $(this).parent().find('input[type="hidden"]').val();

                    if (DateFormat == "DATETIME") {
                        AddTimePicker(Current_);  //updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
                        $(".ui-datepicker-trigger").attr("alt", "");
                    }
                    else {
                        $(".ui-datepicker-trigger").attr("alt", "");
                    }

                }, onClose: function (input, inst) {

                    Current_ = inst;
                    $("#" + Current_.id).focus();  //updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
                    if (defaultDateVal != $(this).val()) {
                        __doPostBack($(this).attr('id'), "TextChanged");
                    }
                }
            });
//updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
        }).on('keypress', function (e) {
            try {
                DateFormat = $(this).parent().find('input[type="hidden"]').val();
                //if (DateFormat == "DATETIME" && $(".ui-datepicker").find(".ui-Timepicker").length == 0) {
                //    if (typeof AddTimePicker != undefined) {
                //        AddTimePicker($(this));
                //    }
                //    return false;
                //}
            } catch (er) {
                console.log(er);
            }
        }).find('.ui-Timepicker .ui-spinner ').css({ "box-shadow": "none" });
    } catch (er) {
        console.log(er);
    }
    $(document).ready(function () {
        DisableDateTimePicker($(document).find('.txtDateTime:visible'), '');
    })
   // $(".ui-datepicker-trigger").prop("alt", "");
    //$(".ui-datepicker-trigger").prop("title", "Ctrl+Shift+w");
//updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
    if (Sys != undefined || typeof (Sys) != "undefined") {
        RequestManager = Sys.WebForms.PageRequestManager.getInstance();
        RequestManager.remove_endRequest(EndRequestHandlerDT);
        RequestManager.add_endRequest(EndRequestHandlerDT);
    }
}

function DisableDateTimePicker(txtDateTime, _disabled) {
    try {
        //Added by ShekharL @02-Jun-2022 : BBVAORINT - 574  Pre - Booking Done Queue > Amend Request > Trade Date and Settlement Date should be Read only
        $(document).find('.txtDateTime:visible').each(function () {
            if ($(this).attr('disabled') != 'undefined') {
                var flag = $(this).attr('disabled');
                if (flag == 'disabled') {
                    $(this).datepicker('disable');
                    $(this).parent().attr('disabled', 'disabled');
                    $(this).parent().find('.ui-datepicker-trigger').attr('disabled', 'disabled').prop('disabled', true); 
                }
                else {
                    $(this).datepicker('enable');
                    $(this).parent().find('.ui-datepicker-trigger').prop('disabled', false).removeAttr('disabled');
                }
            }
            else {
                $(this).datepicker('enable');
                $(this).parent().find('.ui-datepicker-trigger').prop('disabled', false).removeAttr('disabled');
            }
        });

    } catch (er) {
        console.log(er);
    }

}
//updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
function AddTimePicker(ctl) {
    try {
        setTimeout(function () {
            var d = new Date();
            if (typeof (ctl) == "object") {
                d1 = ctl.input;
            } else {
                d1 = $(ctl);
            }
            if ($(d1).val() != "") {
                ValidateDT(d1);
            }

            if ($(d1).val() != "") {
                $(d1).val(Date.parse(new Date($(d1).val().replace(/-/g, " "))));
                d = new Date($(d1).val());
            }
            else {
                d = new Date();
            }

            // var d = new Date($(d1).val()); // for now  //Added by TejaswiniB on 25-Oct-2019
            var h = d.getHours();
            t = h < 12 ? "AM" : "PM";
            h = (h > 12) ? (h - 12) : h;
            h = (h < 10) ? ("0" + h) : h;
            var m = d.getMinutes();
            m = (m < 10) ? ("0" + m) : m;
            var s = d.getSeconds();
            s = (s < 10) ? ("0" + s) : s;
            var str = '<div class="ui-Timepicker">';
            str = str + '<input class="selHours" value="' + h + '"  />';
            str = str + '<span>:</span>';
            str = str + '<input class="selMinuts" value="' + m + '" />';
            str = str + '<span>:</span>';
            str = str + '<input class="selSecond" value="' + s + '" />';
            str = str + '<span>:</span>';
            str = str + '<input class="selAMPM" value="' + t + '" />';
            str = str + '</div>';
            if ($(".ui-datepicker").find(".ui-Timepicker").length > 0) {
                $(".ui-datepicker").find(".ui-Timepicker").remove();
            }
            $(".ui-datepicker .ui-datepicker-calendar").after(str);

            $(".selHours").spinner({
                step: 1,
                max: 12,
                min: 1,
                mouseWheel: true,
                increment: 'fast',
                numberFormat: "d2",  
                disabled: false,
                
                create: function (event, ui) {
                    // validDate(datetext, ctrl);
                    },
                change: function () {
                    h = $(this).spinner('value');
                    h = (parseInt(h) < 10) ? ("0" + h) : h;
                    $(this).val((isNaN(h) ? "00" : ((parseInt(h) > 0 && parseInt(h) <= 12) ? h : "00")));
                    SetSelected_DateTime(this);
                }
                , stop: function (event, ui) {
                    h = isNaN($(this).spinner('value')) ? "00" : $(this).spinner('value');  
                    h = (parseInt(h) < 10) ? ("0" + h) : h; 
                    $(this).val(h);
                    SetSelected_DateTime(this);
                }
            });
            $(".selMinuts").spinner({
                step: 1,
                max: 59,
                min: 0,
                mouseWheel: true,
                increment: 'fast',
               numberFormat: "d2", 
                allowNull: false,
                disabled: false,
                change: function () {
                    h = $(this).spinner('value');
                    h = (parseInt(h) < 10) ? ("0" + h) : h;
                    $(this).val((isNaN(h) ? "00" : ((parseInt(h) > 0 && parseInt(h) <= 59) ? h : "00")));
                    SetSelected_DateTime(this);
                },
                stop: function () {
                    h = isNaN($(this).spinner('value')) ? "00" : $(this).spinner('value');
                   h = (h < 10) ? ("0" + h) : h;
                    $(this).val((isNaN(h) ? "00" : h));
                    SetSelected_DateTime(this);
                }
            });
            $(".selSecond").spinner({
                step: 1,
                max: 59,
                min: 0,
                mouseWheel: true,
                increment: 'fast',
               numberFormat: "d2",  
                allowNull: false,
                disabled: false,
                change: function () {
                    h = $(this).spinner('value');
                    h = (parseInt(h) < 10) ? ("0" + h) : h;
                    $(this).val((isNaN(h) ? "00" : ((parseInt(h) > 0 && parseInt(h) <= 59) ? h : "00")));
                    SetSelected_DateTime(this);
                },
                stop: function () {
                    h = $(this).spinner('value');
                    h = (h < 10) ? ("0" + h) : h;
                    $(this).val((isNaN(h) ? "00" : h));
                    SetSelected_DateTime(this);
                }
            });

            var arrText = ["AM", "PM"];
            $(".selAMPM").textSpinner({
                values: arrText,
                stop: function (event, ui) {
                    //$( "#spinner-value" ).text(ui.value); 
                    SetSelected_DateTime(this);
                }, change: function () {
                    if ($(this).val().length > 2) {
                        $(this).value('');
                    }
                    SetSelected_DateTime(this);
                }
            });
            //updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
            $(document).ready(function () {
                $(document).find('.ui-Timepicker .ui-spinner').css({ "box-shadow": "none" });
                $(document).find('.ui-Timepicker:focus, .hasDatepicker:focus').css({ "outline": "none", "border-width": "1px" });
            });

            if ($(d1).val() != "") {          //Added by TejaswiniB on 25-Oct-2019
                d = Date.parse(new Date($(d1).val().replace(/-/g, " ")));

                if (isNaN(d)) {
                    d = new Date();
                }
                else {
                    d = new Date($(d1).val().replace(/-/g, " "));
                }
            }
            else {
                d = new Date();
            }
            var h = d.getHours();
            t = h < 12 ? "AM" : "PM";
            h = (h > 12) ? (h - 12) : h;
            h = (h < 10) ? ("0" + h) : h;
            var m = d.getMinutes();
            m = (m < 10) ? ("0" + m) : m;
            var s = d.getSeconds();
            s = (s < 10) ? ("0" + s) : s;
            $(".selHours").val(h);
            $(".selMinuts").val(m);
            $(".selSecond").val(s);
        }, 100);
    } catch (er) {
        console.log(er);
    }


    $.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;
        }
    });
}




//Shekhar changes on20-Sep-2017 for DateTime Validation. txtDate.Attributes.Add("onblur", "validDate('" & txtDate.Text & "', this);")
var AllowBlank = false;
//updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
function validDate(datetext, ctrl) {
    that = document.getElementById(ctrl.id);
    var DateFormat = $(that).parent().find(".txtDateFormat").val();
    if (document.getElementById(ctrl.id.replace("txtDate", "hdnAllowBlank"))) {
        AllowBlank = document.getElementById(ctrl.id.replace("txtDate", "hdnAllowBlank")).value; //akshayD 05-Sep-2019
    } else {
        AllowBlank = false;
    }

    datetext = $(that).val();

    var dateTime = new Date(); // for now
    var day = dateTime.getDate()
    var month = dateTime.getMonth();
    var year = dateTime.getFullYear();
    var h = dateTime.getHours();
    t = h < 12 ? "AM" : "PM";
    h = (h > 12) ? (h - 12) : h;
    h = (h < 10) ? ("0" + h) : h;
    var m = dateTime.getMinutes();
    m = (m < 10) ? ("0" + m) : m;
    var s = dateTime.getSeconds();
    s = (s < 10) ? ("0" + s) : s;


    if (datetext != "" && datetext != null) { //akshaydD 05-sep-2019
        var d;
        var matches;
        if (DateFormat == "DATETIME") {
            matches = datetext.match(/^((([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}) ([0]?[1-9]|1[0-2]):([0-5]\d):([0-5]\d)\s?(AM|PM)$/i);
            if (matches === null) {
                // invalid       
                alert("Date Time Format Must be e.g. 01-Jan-2017 11:30:00 AM");
                datetext = day + "-" + MONTH_NAMES1[month] + "-" + year + " " + h + ":" + m + ":" + s + " " + t;
                dateTime = new Date(datetext);
            }
            else {
                d = Date.parse(new Date(datetext.replace(/-/g, " ")));
                if (isNaN(d)) {
                    alert("Date Time Format Must be e.g. 01-Jan-2017 11:30:00 AM");
                    datetext = day + "-" + MONTH_NAMES1[month] + "-" + year + " " + h + ":" + m + ":" + s + " " + t;
                }
                dateTime = new Date(datetext);
            }
        }
        else {
            matches = datetext.match(/^((([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4})$/i);
            if (matches === null) {
                // invalid       
                alert("Date Time Format Must be e.g. 01-Jan-2017");
                datetext = day + "-" + MONTH_NAMES1[month] + "-" + year; //akshayd 05-sep-2019
                // datetext = day + "-" + MONTH_NAMES1[month] + "-" + year + " " + h + ":" + m + ":" + s + " " + t;
            }
            else {
                // d = Date.parse(datetext, "dd-MMM-yyyy hh:mm:ss tt");
                d = Date.parse(new Date(datetext.replace(/-/g, " ")));
                if (isNaN(d)) {
                    alert("Date Time Format Must be e.g. 01-Jan-2017 ");
                    datetext = day + "-" + MONTH_NAMES1[month] + "-" + year;//akshayd 05-sep-2019
                }
                dateTime = new Date(datetext);
            }
        }
    }
    else {
        //AkshayD 05-09-2019 for allow blank date feature

        if (AllowBlank.toUpperCase() != "TRUE") {
            if (DateFormat == "DATETIME") {
                datetext = day + "-" + MONTH_NAMES1[month] + "-" + year + " " + h + ":" + m + ":" + s + " " + t;
            }
            else {
                datetext = day + "-" + MONTH_NAMES1[month] + "-" + year;
            }
        }
        else {
            datetext = "";
        }
    }

    $(that).val(datetext);
}

//updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 
function SetSelected_DateTime(sp) {

    h = $(sp).closest('.ui-Timepicker').find(".selHours").val();
    m = $(sp).closest('.ui-Timepicker').find(".selMinuts").val();
    s = $(sp).closest('.ui-Timepicker').find(".selSecond").val();
    t = $(sp).closest('.ui-Timepicker').find(".selAMPM").val();
    datetext = '';
    datetext = $.datepicker.formatDate('dd-M-yy', new Date($("#" + Current_.id).datepicker('getDate')));
    datetext = datetext + " " + h + ":" + m + ":" + s + " " + t;
    $("#" + Current_.id).val(datetext)
    //validDate($("#" + Current_.id).val(), Current_);
    //__doPostBack(Current_.id, "TextChanged");
}


    // $(document).ready(function () {
/*$('.txtDateTime').blur(function () {
    validDate($(this).val(), this);
});
$('.txtDateTime').on('keydown', function (e) {
    if (DateFormat == "DATETIME" && $(".ui-datepicker").find(".ui-Timepicker").length==0) {
            AddTimePicker();
            return false;
    }
});*/


//updated by Shekhar L/Akshay D. @07-Apr-2022  UWI-308, BBVAORINT-114  date time control picker visibility and  validations 

function checkDateTime(ctl, event) {
    var ctlVal = $(ctl).val();
    var event = event || window.event;
    key = (event.which ? event.which : event.keyCode);
    $(ctl).off('blur').on('blur', function (event) {
        ValidateDT(this);
    });
    if (key == 9) {
        ValidateDT(ctl);
    }
    if (((key == 8 || key == 46) || (key >= 37 && key <= 40) || key == 9) || ((key > 47 && key < 58) || (key >= 96 && key <= 106)) || ((key == 65 || key == 80 || key == 77) || (key.shiftKey && (key == 65 || key == 80 || key == 77)))) {

        if (ctlVal.length == 2) {
            if (parseInt(ctlVal) > 31) {
                alert("Please enter valid date(less than 32).")
                $(ctl).val('');
            } else {
                $(ctl).val(ctlVal + "-");
            }
        } else if (ctlVal.length == 5) {
            m = ctlVal.split('-')[1];
            if (m <= 12) {
                ctlVal = ctlVal.split('-')[0] + '-' + MONTH_NAMES1[m - 1].substring(0, 3);
                $(ctl).val(ctlVal + "-");
            } else {
                if (alert("Please enter valid month(less than 13).")) {
                    $(ctl).val(ctlVal.split('-')[0]);
                    return false;
                }
            }
        } else if (ctlVal.length == 7) {
            $(ctl).val(ctlVal + "-");
        }
        else if (ctlVal.length == 11) {
            $(ctl).val(ctlVal + " ");
        }
        else if (ctlVal.length == 14) {
            $(ctl).val(ctlVal + ":");
        }
        else if (ctlVal.length == 17) {
            $(ctl).val(ctlVal + ":");
        }
        else if (ctlVal.length == 20) {
            $(ctl).val(ctlVal + " ");
        }
        else if (ctlVal.length == 22 || ctlVal.length == 23) {
            ampm = ctlVal.substring(ctlVal.length - 2, ctlVal.length).toUpperCase();
            ctlVal = ctlVal.replace(ctlVal.substring(ctlVal.length - 2, ctlVal.length), ampm);
            $(ctl).val(ctlVal);
        }

        return true;
    }

}

function ValidateDT(ctl) {
    try {
        ctlVal = $(ctl).val();
        d = Date.parse(new Date(ctlVal.replace(/-/g, " ")));

        if (isNaN(d)) {
            alert("Date Time Format Must be e.g. 31-Jan-2021 07:35:00 AM");
            var dateTime = new Date(); // for now
            var day = dateTime.getDate()
            var month = dateTime.getMonth();
            var year = dateTime.getFullYear();
            var h = dateTime.getHours();
            t = h < 12 ? "AM" : "PM";
            h = (h > 12) ? (h - 12) : h;
            h = (h < 10) ? ("0" + h) : h;
            var m = dateTime.getMinutes();
            m = (m < 10) ? ("0" + m) : m;
            var s = dateTime.getSeconds();
            s = (s < 10) ? ("0" + s) : s;
            datetext = '';
            datetext = $.datepicker.formatDate('dd-M-yy', new Date((window.GlobalServerDate == new Date() ? "" : window.GlobalServerDate)));
            datetext = datetext + " " + h + ":" + m + ":" + s + " " + t;
            $(ctl).val(datetext)
        }
    } catch (er) { }
}