﻿// File JScript
var offsetTop;
var offsetWidth;
var offsetLeft;
var searchBox;
var activeSearchGrid;

function searchBox_keyUp (ctrl, event)
{
    var grid = document.getElementById(GetAttribute (ctrl, "searchGrid"));
    if (GetAttribute (ctrl, "numeric") == "true" && Number (ctrl.value).toString () == Number.NaN.toString ())
    {
        ctrl.value = GetAttribute (ctrl, "oldValue");
    }
    else
        SetAttribute (ctrl, "oldValue", ctrl.value);
    
    if (event.keyCode.toString () == "27" && grid.style.visibility == "visible")
    {
        grid.style.left = "-1000px";
        grid.style.visibility = "hidden";
    }
    else if (event.keyCode.toString () == "40" && grid.style.visibility == "visible")
    {
        glWebGid_selectNextRow (grid);
    }
    else if (event.keyCode.toString () == "38" && grid.style.visibility == "visible")
    {
        glWebGid_selectPrevRow (grid);
    }
    else if (event.keyCode.toString () == "13" && grid.style.visibility == "visible")
    {
        glWebGid_confirm (grid);
    }
    else if (ctrl.value.length >= GetAttribute (ctrl, "nMinChars") || event.keyCode.toString () == "40")
    {
        offsetTop = ctrl.parentNode.offsetHeight;
        offsetWidth = ctrl.offsetWidth;
        offsetLeft = ctrl.offsetLeft;
        GLWebLibrary.SearchBox.KeyUp (document.getElementById("sessionState").value, ctrl.id, ctrl.value, searchBox_keyUpCallback);
        searchBox = ctrl;
        
        CallCustomFunction (searchBox.parentNode, "searchBoxKeyUp");
    }
    else
    {
        grid.style.left = "-1000px";
        grid.style.visibility = "hidden";
    }
}

function searchBox_keyUpCallback (res)
{
    if (res.value != null && res.value.GridId == "sessionEnded")
    {
        window.location = res.value.NewHtml;
        return;
    }
    
    if (activeSearchGrid != null)
    {
        activeSearchGrid.style.left = "-1000px";
        activeSearchGrid.style.visibility = "hidden";
    }
    
    activeSearchGrid = document.getElementById(res.value.GridId);
    activeSearchGrid.innerHTML = res.value.NewHtml;
   
    if (navigator.appName == "Microsoft Internet Explorer")
        SetGridPositionIE ();
    else
        SetGridPositionFirefox ();
    
    glWebGid_selectFirstRow (activeSearchGrid);
   
    activeSearchGrid.style.display = "block";
    activeSearchGrid.style.visibility = "visible";
   
    activeSearchGrid.style.width = offsetWidth + "px";
}

function SetGridPositionIE ()
{
    var parent;
    
    if (activeSearchGrid.parentNode != document.forms [0])
    {
        parent = activeSearchGrid.parentNode;
        activeSearchGrid.oldParent = parent.id;
        activeSearchGrid.parentNode.removeChild (activeSearchGrid);
        document.forms [0].appendChild (activeSearchGrid);
    }
    else
        parent = document.getElementById (activeSearchGrid.oldParent);
    
    var coords = GetElementPos (parent);
    activeSearchGrid.style.left = coords [0] + "px";
    activeSearchGrid.style.top = coords [1] + parent.offsetHeight + "px";
    activeSearchGrid.style.textAlign = "left";
   
    glWebGid_selectFirstRow (activeSearchGrid);

    activeSearchGrid.style.position = "absolute";
}

function SetGridPositionFirefox ()
{
    activeSearchGrid.style.position = "relative";
    activeSearchGrid.style.left = "0px";
    activeSearchGrid.style.left = GetAttribute (activeSearchGrid, "gridLeftPosition");
}

function customRowClicked (gridId, row)
{
    var res = CallCustomFunction (searchBox.parentNode, "searchBoxSelectingValue");
    if (res == false)
        return res;
    
    if (gridId.id.indexOf ("resultGrid_") != -1)
    {
        gridId.style.left = "-1000px";
        gridId.style.visibility = "hidden";
        searchBox.value = "";
        SetAttribute (searchBox, "oldValue", "");
        
        var md = GetAttribute (searchBox.parentNode, "modifyDocument");
        if (md == null || md == "true")
            docModified = true;
            
        return true;
    }
    else
        return false;
}

function sbCustomRowClickedCallback (grid)
{
    selectedValue = GetAttribute (grid, "resSelectedValue");
    SetAttribute (searchBox.parentNode, "value", selectedValue);
    CallCustomFunction (searchBox.parentNode, "searchBoxValueSelected");
}

function searchBox_onfocus (ctrl)
{
    ctrl.select ();
}

function sblens_onclick (ctrl)
{
    var parent = ctrl.parentNode;
    var sb = document.getElementById(parent.id + "_input");
    
    offsetTop = sb.parentNode.offsetHeight;
    offsetWidth = sb.offsetWidth;
    offsetLeft = sb.offsetLeft;
    GLWebLibrary.SearchBox.KeyUp (document.getElementById("sessionState").value, sb.id, sb.value, searchBox_keyUpCallback);
    
    searchBox = sb;
    
    searchBox.focus ();
    
    CallCustomFunction (searchBox.parentNode, "searchBoxKeyUp");
}


