﻿var currentRowId = 0;
var keyCode = -1;

document.onclick = check;

// Hide box when clicking outside
function check(e) {
    var target = (e && e.target) || (event && event.srcElement);

    // From
    if (document.getElementById('addressSearchResultListucAddressPickerFrom') == null)
        return;
          
    var obj = document.getElementById('addressSearchResultListucAddressPickerFrom');
    checkParent(target, 'PickerFrom') ? obj.style.display = 'none' : null;

    // To
    if (document.getElementById('addressSearchResultListucAddressPickerTo') == null)
        return;

    var obj = document.getElementById('addressSearchResultListucAddressPickerTo');
    checkParent(target, 'PickerTo') ? obj.style.display = 'none' : null;
}
function checkParent(t, baseId) {
    while (t.parentNode) {
        if (t == document.getElementById('addressSearchResultListucAddress' + baseId) || t == document.getElementById('addressSearchPopularAddressesucAddress' + baseId)) {
            return false
        }
        t = t.parentNode
    }
    return true
}

function SelectRow(baseId) 
{
    // Arrow down
    if (keyCode == 40) 
    {
        if (currentRowId + 1 > 10)
            MarkRow(baseId, 1);
        else
            MarkRow(baseId, currentRowId + 1);
    }
    // Arrow up
    else if (keyCode == 38) 
    {
        if (currentRowId - 1 < 1)
            MarkRow(baseId, 10);
        else
            MarkRow(baseId, currentRowId - 1)
    }
}

function ClickRow(baseId, rowId, id) 
{
    MarkRow(baseId, rowId);
    document.getElementById(id).focus();
}

// Change the color of the selected row
function MarkRow(baseId, rowId) 
{
    if (document.getElementById(baseId + '__' + rowId) == null)
        return;

    if (document.getElementById(baseId + '__' + currentRowId) != null)
        if (currentRowId % 2)
            document.getElementById(baseId + '__' + currentRowId).setAttribute("class", "RowStyle");
        else
            document.getElementById(baseId + '__' + currentRowId).setAttribute("class", "AlternatingRowStyle");

    currentRowId = rowId;
    document.getElementById(baseId + '__' + currentRowId).setAttribute("class", "RowOverStyle");
}

// When the user types
function SearchKeyDown(baseId, gridId, textboxId)
{
    if (keyCode == 40) 
    {
        // Arrow down
        SelectRow(baseId);
    }
    else if (keyCode == 38) 
    {
        // Arrow up
        SelectRow(baseId);
    }
    else if (keyCode == 13 || keyCode == 9) 
    {
        // Enter key is pressed
        if (currentRowId == 0) {
            // Select the first as default
            if (document.getElementById(baseId + '__1') == null)
                return;

            document.getElementById(baseId + '__1').click();
        }

        // Select the row that is marked
        if (document.getElementById(baseId +'__'+ currentRowId) == null)
            return;

        document.getElementById(baseId + '__' + currentRowId).click();
    }
    else if (keyCode == 8 || keyCode == 22 || keyCode == 46 || keyCode > 64) 
    {
        // An alphabetic or numberic key, perform a search
        __doPostBack('ctl00$' + textboxId, '');
        currentRowId = 0;
    }

    return;
}

var memCtrl;
function selectTxt(txtBox) 
{
    if (memCtrl != txtBox) { txtBox.select(); memCtrl = this; }
}

