var x;

//Added By Bhavya for selection using Arrow keys 02-11-19 
$(document).keydown(function (event) {
    event = event || window.event;
    var tbody = $(".CcyPairTableSearchResult").find("tbody");
    selected = tbody.find(".selected");
    if (event.keyCode == 38) {
        $(".CcyPairTableSearchResult").find(".selected").removeClass("selected");
        if (selected.prev().length == 0) {
            tbody.find("tr:last").addClass("selected");
        } else {
            selected.prev().addClass("selected");
            var scrollTP = $(".Grid").scrollTop();
            $(".Grid").scrollTop(scrollTP - 36);
        }
    }
    else if (event.keyCode == 40) {
        tbody.find(".selected").removeClass("selected");
        if (selected.next().length == 0) {
            tbody.find("tr:nth-child(2)").addClass("selected");
        } else {
            $(".CcyPairTableSearchResult").find(".selected").removeClass("selected");
            selected.next().addClass("selected");
            var scrollTP = $(".Grid").scrollTop();
            $(".Grid").scrollTop(scrollTP + 36);
        }
    }
    else if (event.keyCode == 13) {
        $("#tbl_PairControl").hide();
    }
    else if (event.keyCode == 27) {
        $("#tbl_PairControl").hide();
    }
    else if (event.keyCode == 16 && event.keyCode == 9) {
        $("#tbl_PairControl").hide();
    }
    else if (event.keyCode == 9) {
        $("#tbl_PairControl").hide();
    }
})
function EnterEvent_PairControl(control, e) {

    //<Start || Changed $("#" + x) by Arshin S || on 8th Jan 2018 ||PP-31 Multi deal entry issues ::  When click on search icon should search resp search items || asked by Ganesh D>
    var x = control.id.replace("Image", "");   
    //<End || Changed by Arshin S || on 8th Jan 2018 ||PP-31 Multi deal entry issues ::  When click on search icon should search resp search items || asked by Ganesh D>
    e = e || window.event;

    var keyCode = e.which;
    var txt = $("#" + x).val().trim();
    var term = "";
    term = txt;
    if (e.keyCode == 13) {
        $('form input, body').on("keypress", function (e) {
            if (e.keyCode == 13) {
                
                e.stopPropagation ? e.stopPropagation() : (e.cancelBubble = true);
                e.returnvalue = null;
                return false;
            }
        });
        return false;
    }

 if (e.keyCode==8 && term == "") {
            $("#" + x).parent().find("#tbl_PairControl").html("");
            $("#" + x).parent().find("#tbl_PairControl").hide();
            autocomplete_clicked_CcyPair(x, '', '0', '', '', '', '', '', '');
            return;
        }
    if (e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 27 || e.keyCode == 9 || e.keyCode == 16 && e.keyCode == 9) {
        return;
    }
    else if (term.length >= 3) {
        var urlToRest = "CCYPAIR";
        var keyCode = event.which;
        var txt = $("#" + x).val().trim();
        var term = "";
        term = txt;

        // 
        var path_Name = window.location.pathname;
        var myPageName = path_Name.substring(path_Name.lastIndexOf('/') + 1);
        //alert(myPageName);
        $.support.cors = true;
        $.ajax({
            type: "POST",
            url: "" + myPageName + "/GetCcyPairs",
            contentType: "application/json; charset=utf-8",
            dataType: "text",
            cache: false,
            data: "{'term':'" + term + "', 'Mode':'" + urlToRest + "'}",
            crossDomain: true,
            success: function (data) {
                var obj = jQuery.parseJSON(data);
                var tbl = "<div style='overflow-y:auto;height:auto;max-height:300px;background-color:#FFFFFF;' class='Grid'>"
                tbl += '<table class="CcyPairTableSearchResult" cellpadding=1 cellspacing=1 style="border-collapse:collapse;">'
                tbl += "<th>Currency Pair</th>"
                tbl += '<th><div style="width:20px; float:right; padding:3px;cursor:pointer;" onClick=\"' + "autocomplete_clicked_CcyPair('" + x + "','" + '' + "','" + '' + "','" + '' + "','" + '' + "','" + '' + "','" + '' + "')" + '\">X</div></th>';
                tbl += "</tr>";
                var objData;
                $.each(obj, function (key, value) {

                    tojson: var jsonData = JSON.stringify(value);
                    objData = $.parseJSON(jsonData);
                })
                //  alert(objData);
                if (objData == null || objData.length === 0) {
                    $("#" + x).parent().find("#tbl_PairControl").html("");
                    $("#" + x).parent().find("#tbl_PairControl").hide();

                    //<Start || Changed by Arshin S || on 11th Jan 2019 || Asked by Milind Sir || PP-46 Tab control movement, validation message position issue>
                    MsgDialog(this, 'Warning', 0, 'Currency Pair not found.');
                    //alert("Currency Pair not found.");
                    //<End || Changed by Arshin S || on 11th Jan 2019 || Asked by Milind Sir || PP-46 Tab control movement, validation message position issue>

                    autocomplete_clicked_CcyPair(x, '', '0', '', '', '', '', '', '')
                    return;


                }
                else {
                    for (var i = 0; i < objData.length; i++) {
                        //tbl += "<tr>";
                        tbl += "<tr class='SearchResultRow' onClick=\"autocomplete_clicked_CcyPair('" + x + "','" + objData[i].Asset_Pair + "','" + objData[i].Pair_Id + "')\">";
                        //tbl += "<td>hhhhhh</td>";
                        // tbl += "<td>" + objData[i].Pair_Id + "</td>";
                        tbl += "<td style='height:0px !important;'>" + objData[i].Asset_Pair + "</td>";


                        tbl += "</tr>";
                    }
                    tbl += "</table></div>";
                    //alert(x);
                    $("#" + x).parent().find("#tbl_PairControl").html(tbl);
                    $("#" + x).parent().find("#tbl_PairControl").show();
                    var tbody = $(".CcyPairTableSearchResult").find("tbody");
                    var selected = tbody.find("tr:nth-child(2)").addClass("selected");
                }
                $("#" + x).parent().find("#dialog_PairControl").hide();
            },

            error: function (xhr, textStatus, error) {

            }
        });

    }


}

//<End || Changed by Arshin S || on 23th July 2018 || Asked by Prashant M>


function ShowPopup_PairControl() {
    $(function () {
        $("#" + x).parent().find("#dialog_PairControl").show();

    });
};

function ClearPairControl(event) {
    var txt = $("#" + x).val().trim();

    if (term == "") {
        autocomplete_clicked_CcyPair("'" + x + "'", '', '0', '', '', '', '', '', '')

        return;
    }
}

function autocomplete_clicked_CcyPair(tr, Asset_Pair, Pair_Id, Doc_Id, Type, CIF_No, Either_To_Sign, CIN) {

    x = tr;
    //   
    // alert("#" + x);
    //  alert($(tr).parent().parent().parent().parent().html());


    $("#" + x).val(Asset_Pair);
    $("#" + x).parent().find('#hdnCcyPair').val(Asset_Pair);

    // $("#" + x).parent().find('#hdnPairID').val(Pair_Id);
    // $('#sel_cust_tlt_id').val(CDM_TLT_ID);


    $("#" + x).parent().find("#tbl_PairControl").hide();
    //__doPostBack('cust_name_ValueChanged', 'ValueChanged');
    $("#" + x).parent().find("#btnSelect_CcyPair").click();

}


function SearchByChanged_CcyPair() {
    var y = '<%= ddlFilterOn_CcyPair.ClientID()%>';
    var dropdown1 = document.getElementById(y);
    var selected = dropdown1.selectedIndex;
    if (selected == 0) {
        urlToRest = "PAIRID";
    }
    else if (selected == 1) {
        urlToRest = "CCYPAIR";
    }

    $("#" + x).val("");
    $("#" + x).parent().find("#tbl_PairControl").hide();
}

function DisallowSpecialCharacters_PairControl(e) {
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (event)
        keycode = event.keyCode;
    else if (e)
        keycode = e.which;
    else return true;

    if ((keycode > 32 && keycode < 38) || keycode == 39 || (keycode > 41 && keycode < 45) || keycode == 47
        || (keycode > 58 && keycode < 65) || (keycode > 90 && keycode < 95) || (keycode > 122 && keycode < 126)) {
        return false;
    }
    else {
        return true;
    }
    return true;
}