document.onkeyup = DocumentKeyUp;
document.onkeydown = DocumentKeyDown;
document.onkeypress = DocumentKeyPress;
window.onresize = WindowResize;
window.onscroll = WindowScroll;
document.onclick = WindowClick;
// document.ondblclick = WindowDblClick;

//*****************************************************************
// class TWindowMeasures
//*****************************************************************

var TWindowMeasures = function()
{
    this.Height = 0;
    this.Width = 0;
    this.ScrollTop = 0;
}

TWindowMeasures.prototype.GetHeight = function()
{
    if (typeof (window.innerWidth) == 'number')
    {
        //Non-IE
        this.Height = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        this.Height = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        this.Height = document.body.clientHeight;
    }

    return this.Height;
}

TWindowMeasures.prototype.GetWidth = function()
{
    if (typeof (window.innerWidth) == 'number')
    {
        //Non-IE
        this.Width = window.innerWidth;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        this.Width = document.documentElement.clientWidth;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        this.Width = document.body.clientWidth;
    }

    return this.Width;
}

TWindowMeasures.prototype.GetScrollTop = function()
{
    if (document.documentElement && document.documentElement.scrollTop)
    {
        //IE 6+ in 'standards compliant mode'
        this.ScrollTop = document.documentElement.scrollTop;
    }
    else if (document.body && document.body.scrollTop)
    {
        //IE 4 compatible
        this.ScrollTop = document.body.scrollTop;
    }

    return this.ScrollTop;
}
//*****************************************************************

//---------------------------------------------------------------------------------------
function WindowResize(event)
{
    try
    {
        if (!event) event = window.event;

        var oDiv = GEBI("idForm");
        var oDivFg = GEBI("idForeground");

        if (oDiv && oDivFg)
        {
            var bPopupIsShown = (oDivFg.style.display == "block" && oDiv.style.display == "block") ? true : false;

            if (bPopupIsShown)
            {

//                oDivFg.innerHTML = oXMLHttpCommonRequest.responseText;

                var oWindowMeasures = new TWindowMeasures();

                var iScrollTop = oWindowMeasures.GetScrollTop();

                var iHeight = oWindowMeasures.GetHeight();

                var iDivHeight = 0;

                var iInnerDivCol = oDivFg.getElementsByTagName ("DIV");
                
                var bIsImage = false;
                
                if (iInnerDivCol)
                {
                    if ( iInnerDivCol.length > 0 )
                    {
                        var oTargetDiv = iInnerDivCol[0];
                    
                        if ( oTargetDiv )
                        {
                            bIsImage = (oTargetDiv.firstChild.tagName == "IMG") ? true : false;   
                        
                            iDivHeight = parseInt(oTargetDiv.clientHeight);

                            if (!bIsImage)
                            {                          
                                oTargetDiv.className = "PopUpDiv";  
                            }
                        }
                    }
                }

                var iResTop = (iHeight / 2) - (iDivHeight  / 2);

                if (iResTop < 0 || bIsImage) iResTop = 0;
                
                oDivFg.style.top = iScrollTop + iResTop + "px";
                
                oDivFg.style.display = "block";
                oDivFg.style.width = document.body.clientWidth + document.body.scrollLeft + 20 + "px";
                oDivFg.style.height = document.body.clientHeight - oDivFg.style.pixelTop + 20 + "px";

                oDiv.style.display = "block";
                oDiv.style.width = document.body.clientWidth + document.body.scrollLeft + 20 + "px";
                oDiv.style.height = document.body.clientHeight + 20 + "px";
            }
        }
    }
    catch (e)
    { }
}
//---------------------------------------------------------------------------------------
function WindowScroll ( event )
{
/*
    var oWindowMeasures = new TWindowMeasures();

    var iScrollTop = oWindowMeasures.GetScrollTop();
    
    var oDivFg = GEBI("idForeground");

    if ( oDivFg)
    {
        var bPopupIsShown = (oDivFg.style.display == "block" ) ? true : false;

        if (bPopupIsShown)
        {
//            oDivFg.style.top = iScrollTop + 145 + "px";
            oDivFg.style.top = iScrollTop + "px";
            oDivFg.style.height = document.body.clientHeight - oDivFg.style.pixelTop + "px";
        }
    }
*/
    if (!event) event = window.event;

    var oDiv = GEBI("divMessageContainer");

    if (oDiv)
    {
        var oWindowMeasures = new TWindowMeasures();

        var iScrollTop = oWindowMeasures.GetScrollTop();

        oDiv.style.top = iScrollTop + "px";
    }
}
//---------------------------------------------------------------------------------------
function WindowClick(event)
{
    if (!event) event = window.event;

    var oObj = null;

    if (bIsFireFox)
    {
        oObj = event.target;
    }
    else
    {
        oObj = event.srcElement;
    }
    
    if ( oObj )
    {
        if ( oObj.tagName.toLowerCase() == "a" )
        {
            if (oObj.href)
            {
                if ( oObj.href.indexOf("#") != -1 )
                {
                    if (event.stopPropagation)
                    {
                        event.stopPropagation();
                    }

                    if (event.preventDefault)
                    {
                        event.preventDefault();
                    }

                    event.returnValue = false;
                    event.cancelBubble = true;
                }
            }
        }
    }

/*
    try
    {
        if (!event) event = window.event;

        var oDivCollection = document.getElementsByTagName("DIV");

        if (oDivCollection)
        {
            for (i = 0; i < oDivCollection.length; i++)
            {
                var oDiv = oDivCollection[i];

                if (oDiv && (oDiv.id))
                {
                    if (oDiv.id.length > 0)
                    {
                        if (oDiv.id.indexOf("divList_") != -1 && event.srcElement != oDiv.parentElement)
                        {
                            if (oDiv.style.display == "block")
                            {
                                oDiv.style.display = "none";
                            }
                        }
                    }
                }
            }
        }

        var oImgCollection = document.getElementsByTagName("IMG");

        if (oImgCollection)
        {
            for (i = 0; i < oImgCollection.length; i++)
            {
                var oImg = oImgCollection[i];

                if (oImg && (oImg.id))
                {
                    if (oImg.id.length > 0)
                    {
                        if (oImg.id.indexOf("Open_Close_Button_IMG_") != -1)
                        {
                            oImg.src = "./Images/down.gif";
                        }
                    }
                }
            }
        }
    }
    catch ( e )
    {
        alert (e.Message);
    }
*/    
}
//---------------------------------------------------------------------------------------
function WindowDblClick(event)
{
    if (!event) event = window.event;

    if (event.stopPropagation)
    {
        event.stopPropagation();
    }

    if (event.preventDefault)
    {
        event.preventDefault();
    }

    event.returnValue = false;
    event.cancelBubble = true;

    alert(event.returnValue + " " + event.cancelBubble);
    
}
//---------------------------------------------------------------------------------------
function DocumentKeyPress(event)
{
    if (!event) event = window.event;
    var bRetVal = true;

    var oDivBack = GEBI("idForm");
    var oDivFor = GEBI("idForeground");

    if (!oDivBack || !oDivFor) return;

    var bPopupIsShown = (oDivFor.style.display == "block" && oDivBack.style.display == "block") ? true : false;

    switch (event.keyCode)
    {
        case 13:
            {
                if (bPopupIsShown)
                {
                    bRetVal = false;
                }
                else
                {
                    switch (strSrcAction)
                    {
                        case "ROW_KEYDOWN":
                            {
                                bRetVal = false;
                                strSrcAction = "ROW_KEYPRESS";
                            }
                            break;
                    }

                }
            }
            break;
    }
    if (bRetVal == false)
    {
        if (event.stopPropagation)
        {
            event.stopPropagation();
        }

        if (event.preventDefault)
        {
            event.preventDefault();
        }
    }

    event.returnValue = bRetVal;
    event.cancelBubble = !bRetVal;

}
//---------------------------------------------------------------------------------------
function DocumentKeyUp(event)
{
    if (!event) event = window.event;

    var bRetVal = true;
    var oDivBack = GEBI("idForm");
    var oDivFor = GEBI("idForeground");

    if (!oDivBack || !oDivFor) return;

    var bPopupIsShown = (oDivFor.style.display == "block" && oDivBack.style.display == "block") ? true : false;

    switch (event.keyCode)
    {
        // Escape key 
        case 27:
            {
               
                if (event.returnValue != false)
                {
                    if (bPopupIsShown)
                    {
                        CloseModalPopUp();
                        
//                        FocusInput(1, 0, false);
                    }
                }
                else
                {
                    bRetVal = false;
                }
            }
            break;

        // Delete key 
        case 46:
            {
                if (!bPopupIsShown)
                {
                    if (PrevSelectedRowID)
                    {
                        var oEventElement = null;

                        if (bIsFireFox)
                        {
                            oEventElement = event.target;
                        }
                        else
                        {
                            oEventElement = event.srcElement;
                        }

                        if (oEventElement.tagName == "INPUT")
                        {
                            break;
                        }
                        
                        var arrSplittedID = PrevSelectedRowID.split("_");

                        var strForm = "";
                        var strButtonID = "";

                        if (arrSplittedID[2] == "undefined" || arrSplittedID[2] == null)
                        {
                            strForm = "fCurrentLists";
                            strButtonID = "bnConfirmListDeleteDialog_ListID_" + arrSplittedID[1];
                        }
                        else if (arrSplittedID[2] == "ML")
                        {
                            strForm = "fMasterList";
                            strButtonID = "bnConfirmDeleteDialog_ListItemID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                        }
                        else if (arrSplittedID[2] == "RC")
                        {
                            strForm = "fRecipes";
                            strButtonID = "bnConfirmRecipeDeleteDialog_RecipeID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                        }
                        else if (arrSplittedID[3] == "RCITEM")
                        {
                            strForm = "fRecipe";
                            strButtonID = "bnConfirmRecipeItemDeleteDialog_RecipeID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                        }
                        else
                        {
                            strForm = "fCurrentList";
                            strButtonID = "bnConfirmDeleteDialog_ListItemID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                        }

                        if (GetSelectedRowsCount(strForm) == 0)
                        {
                            ShowPopupMessage("No items selected");
                            bRetVal = false;
                            break;
                        }


                        SubmitForm(strForm, strButtonID);
                        strSrcAction = "DELETING_ITEM";
                        bRetVal = false;

                    }
                }
            }
            break;

        // backspace key 
        case 8:
            {

//                var bInCurrentList = (strCurrentTab.indexOf("aManageCurrentList") != -1) ? true : false;

                var oEventElement = null;

                if (bIsFireFox)
                {
                    oEventElement = event.target;
                }
                else
                {
                    oEventElement = event.srcElement;
                }

                if (!bPopupIsShown && PrevSelectedRowID)
                {
//                    if (!bInCurrentList)
//                    {
                        if (oEventElement.tagName == "INPUT")
                        {
                            break;
                        }
//                    }

                    var arrSplittedID = PrevSelectedRowID.split("_");

                    var strForm = "";
                    var strButtonID = "";

                    if (arrSplittedID[2] == "undefined" || arrSplittedID[2] == null)
                    {
                        strForm = "fCurrentLists";
                        strButtonID = "bnConfirmListDeleteDialog_ListID_" + arrSplittedID[1];
                    }
                    else if (arrSplittedID[2] == "ML")
                    {
                        strForm = "fMasterList";
                        strButtonID = "bnConfirmDeleteDialog_ListItemID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                    }
                    else if (arrSplittedID[2] == "RC")
                    {
                        strForm = "fStores";
                        strButtonID = "bnConfirmRecipeDeleteDialog_RecipeID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                    }
                    else if (arrSplittedID[3] == "RCITEM")
                    {
                        strForm = "fRecipe";
                        strButtonID = "bnConfirmRecipeItemDeleteDialog_RecipeID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                    }
                    else
                    {
                        strForm = "fCurrentList";
                        strButtonID = "bnConfirmDeleteDialog_ListItemID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                    }

                    if (GetSelectedRowsCount(strForm) == 0)
                    {
                        ShowPopupMessage("No items selected");
                        bRetVal = false;
                        break;
                    }

                    SubmitForm(strForm, strButtonID);
                    strSrcAction = "DELETING_ITEM";
                    bRetVal = false;

                }
            }
            break;

        // Enter key      
        case 13:
            {
                if (bPopupIsShown)
                {
                    if (strSrcAction == "DELETING_ITEM")
                    {
                        var oButton = null;

                        if (bIsFireFox)
                        {
                            oButton = event.target;
                        }
                        else
                        {
                            oButton = event.srcElement;
                        }

                        if (oButton)
                        {
                            if (oButton.tagName == "INPUT")
                            {
                                if (oButton.type == "button")
                                {
                                    switch (oButton.id)
                                    {
                                        case "bnYes":
                                            {
                                                if (strEditingForm == "fCurrentLists")
                                                {
                                                    var arrSplittedID = strEditingButton.split("_");

                                                    SubmitForm(strEditingForm, "bnListDelete_ListID_" + arrSplittedID[2]);
                                                }
                                                else if (strEditingForm == "fRecipes")
                                                {
                                                    var arrSplittedID = strEditingButton.split("_");

                                                    SubmitForm(strEditingForm, "bnRecipeDelete_RecipeID_" + arrSplittedID[2]);
                                                }
                                                else if (strEditingForm == "fStores")
                                                {
                                                    var arrSplittedID = strEditingButton.split("_");

                                                    SubmitForm(strEditingForm, "bnStoreDelete_StoreID_" + arrSplittedID[2]);
                                                }
                                                else if (strEditingForm == "fRecipe")
                                                {
                                                    var arrSplittedID = strEditingButton.split("_");

                                                    SubmitForm(strEditingForm, "bnRecipeItemDelete_ItemID_" + arrSplittedID[2]);
                                                }
                                                else
                                                {
                                                    SubmitForm(strEditingForm, "bnDeleteRange");
                                                }
                                            }
                                            break;

                                        case "bnNo":
                                            {
                                                CloseModalPopUp();
                                            }
                                            break;
                                    }
                                }
                            }
                        }

                        strSrcAction = "";

                        break;
                    }

                    if (event.returnValue == false)
                    {
                        bRetVal = false;
                    }
                    else
                    {
                        if (InEditMode)
                        {
                            var strActualForm = "";

                            switch (strEditingForm)
                            {
                                case "fCurrentLists":
                                    {
                                        strActualForm = "fRenameCurrentListForm";
                                    }
                                    break;
                                case "fStores":
                                    {
                                        strActualForm = "fAddEditStoreForm";
                                    }
                                    break;
                                case "fRecipes":
                                    {
                                        strActualForm = "fAddEditRecipeForm";
                                    }
                                    break;
                                case "fRecipe":
                                    {
                                        strActualForm = "fAddEditIngridientForm";
                                    }
                                    break;
                                case "fCurrentList":
                                    {
                                        strActualForm = (strEditingButton == "bnResetList") ? "fResetListForm" : "fModalAddingForm";
                                    }
                                    break;
                                case "fMasterList":
                                    {
                                        strActualForm = (strEditingButton == "bnRenameCategory") ? "fRenameCategoryForm" : "fUpdateMasterListForm";
                                    }
                                    break;

                            }
                            SubmitForm(strActualForm, strEditingButton);
                        }
                    }
                }
                else
                {
                    var oEventElement = null;

                    if (bIsFireFox)
                    {
                        oEventElement = event.target;
                    }
                    else
                    {
                        oEventElement = event.srcElement;
                    }

                    if (PrevSelectedRowID)
                    {
                        if (oEventElement.tagName == "INPUT")
                        {
                            bRetVal = false;
                            break;
                        }
                    }
                
                    if (event.returnValue == false)
                    {
                        bRetVal = false;
                    }
                    else
                    {
                        if (PrevSelectedRowID)
                        {
                            var arrSplittedID = PrevSelectedRowID.split("_");

                            var strForm = "";
                            var strButtonID = "";

                            if (arrSplittedID[2] == "undefined" || arrSplittedID[2] == null)
                            {
                                strForm = "fCurrentLists";
                                strButtonID = "bnListEdit_ListID_" + arrSplittedID[1];
                            }
                            else if (arrSplittedID[2] == "ML")
                            {
                                strForm = "fMasterList";
                                strButtonID = "bnEdit_ListItemID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                            }
                            else if (arrSplittedID[2] == "RC")
                            {
                                strForm = "fRecipes";
                                strButtonID = "bnEditRecipe_RecipeID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                            }
                            else if (arrSplittedID[3] == "RCITEM")
                            {
                                strForm = "fRecipe";
                                strButtonID = "bnEditRecipeItem_ItemID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                            }
                            else
                            {
                                strForm = "fCurrentList";
                                strButtonID = "bnEdit_ListItemID_" + arrSplittedID[1] + "_" + arrSplittedID[2];
                            }

                            SubmitForm(strForm, strButtonID);

                            bRetVal = false;
                        }
                    }
                }
            }
            break;

    }
    
    if (bRetVal == false)
    {
        if (event.stopPropagation)
        {
            event.stopPropagation();
        }

        if (event.preventDefault)
        {
            event.preventDefault();
        }
    }

    event.returnValue = bRetVal;
    event.cancelBubble = !bRetVal;

}
//---------------------------------------------------------------------------------------
function DocumentKeyDown(event)
{
    if (!event) event = window.event;

    var bRetVal = true;
    var oDivBack = GEBI("idForm");
    var oDivFor = GEBI("idForeground");

    if (!oDivBack || !oDivFor) return;

    var bPopupIsShown = (oDivFor.style.display == "block" && oDivBack.style.display == "block") ? true : false;

    switch (event.keyCode)
    {
        // Up Arrow    
        case 38:
            {
                try
                {
                    if (!bPopupIsShown)
                    {
                        if (PrevSelectedRowID)
                        {
                            var arrSplittedID = PrevSelectedRowID.split("_");

                            var strTableName = "";
                            var strFormName = "";

                            if (arrSplittedID[2] == "undefined" || arrSplittedID[2] == null)
                            {
                                strTableName = "tableCurrentLists";
                                strFormName = "fCurrentLists";
                            }
                            else if (arrSplittedID[2] == "ML")
                            {
                                strTableName = "tableMasterList";
                                strFormName = "fMasterList";
                            }
                            else if (arrSplittedID[2] == "RC")
                            {
                                strTableName = "tableRecipes";
                                strFormName = "fRecipes";
                            }
                            else if (arrSplittedID[2] == "CO")
                            {
                                strTableName = "tableCoupons";
                                strFormName = "fCoupons";
                            }
                            else if (arrSplittedID[3] == "RCITEM")
                            {
                                strTableName = "tableRecipe";
                                strFormName = "fRecipe";
                            }
                            else
                            {
                                strTableName = "tableCurrentList";
                                strFormName = "fCurrentList";
                            }

                            var oParentTable = GEBI(strTableName);

                            if (oParentTable)
                            {
                                var oTRCol = oParentTable.getElementsByTagName("TR");

                                var oSelectedRow = GEBI(PrevSelectedRowID);

                                var arrFilteredRows = new Array();

                                var iArrayIndex = 0;

                                var iSelectedIndex = -1;

                                if (oTRCol)
                                {
                                    for (i = 0; i < oTRCol.length; i++)
                                    {
                                        var oTR = oTRCol[i];

                                        if (oTR)
                                        {
                                            if (oTR.id.indexOf("tr_") != -1)
                                            {
                                                arrFilteredRows[iArrayIndex++] = oTR;
                                            }
                                        }
                                    }
                                    for (i = 0; i < arrFilteredRows.length; i++)
                                    {
                                        var oTR = arrFilteredRows[i];

                                        if (oTR)
                                        {
                                            if (oTR == oSelectedRow)
                                            {
                                                iSelectedIndex = i;
                                                break;
                                            }
                                        }
                                    }

                                    if (iSelectedIndex > 0)
                                    {
                                        if ((iSelectedIndex - 1) >= 0)
                                        {
                                            var oTargetTR = arrFilteredRows[iSelectedIndex - 1];

                                            if (oTargetTR)
                                            {
                                                var iScrollBy = 1;
                                            
                                                SelectRow(oTargetTR.id, strFormName, bListInPrepareMode, event);
                                                ScrollWindow(oTargetTR.id, event, iScrollBy, false);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        var oTR = arrFilteredRows[iSelectedIndex];
                                        
                                        var oWindowMeasures= new TWindowMeasures();
                                        
                                        if (oWindowMeasures.GetScrollTop() > 0)
                                        {
                                            window.scrollBy(0, parseInt((oTR.clientHeight) * (-1)));
                                        }
                                    }
                                }
                            }

                            // don't scroll
                            bRetVal = false;
                        }
                    }
                }
                catch (e)
                {
                    ShowPopupMessage(e);
                }
            }
            break;

        // Down Arrow   
        case 40:
            {
                try
                {
                    if (!bPopupIsShown)
                    {
                        if (PrevSelectedRowID)
                        {
                            var arrSplittedID = PrevSelectedRowID.split("_");

                            var strTableName = "";
                            var strFormName = "";

                            if (arrSplittedID[2] == "undefined" || arrSplittedID[2] == null)
                            {
                                strTableName = "tableCurrentLists";
                                strFormName = "fCurrentLists";
                            }
                            else if (arrSplittedID[2] == "ML")
                            {
                                strTableName = "tableMasterList";
                                strFormName = "fMasterList";
                            }
                            else if (arrSplittedID[2] == "RC")
                            {
                                strTableName = "tableRecipes";
                                strFormName = "fRecipes";
                            }
                            else if (arrSplittedID[2] == "CO")
                            {
                                strTableName = "tableCoupons";
                                strFormName = "fCoupons";
                            }
                            else if (arrSplittedID[3] == "RCITEM")
                            {
                                strTableName = "tableRecipe";
                                strFormName = "fRecipe";
                            }
                            else
                            {
                                strTableName = "tableCurrentList";
                                strFormName = "fCurrentList";
                            }

                            var oParentTable = GEBI(strTableName);

                            if (oParentTable)
                            {
                                var oTRCol = oParentTable.getElementsByTagName("TR");

                                var arrFilteredRows = new Array();

                                var iArrayIndex = 0;

                                var oSelectedRow = GEBI(PrevSelectedRowID);

                                var iSelectedIndex = -1;

                                if (oTRCol)
                                {

                                    for (i = 0; i < oTRCol.length; i++)
                                    {
                                        var oTR = oTRCol[i];

                                        if (oTR)
                                        {
                                            if (oTR.id.indexOf("tr_") != -1)
                                            {
                                                arrFilteredRows[iArrayIndex++] = oTR;
                                            }
                                        }
                                    }

                                    for (i = 0; i < arrFilteredRows.length; i++)
                                    {
                                        var oTR = arrFilteredRows[i];

                                        if (oTR)
                                        {
                                            if (oTR == oSelectedRow)
                                            {
                                                iSelectedIndex = i;
                                                break;
                                            }
                                        }
                                    }

                                    if (iSelectedIndex >= 0)
                                    {
                                        if ((iSelectedIndex + 1) < arrFilteredRows.length)
                                        {
                                            var oTargetTR = arrFilteredRows[iSelectedIndex + 1];

                                            if (oTargetTR)
                                            {
                                                var iScrollBy = 1;

                                                SelectRow(oTargetTR.id, strFormName, bListInPrepareMode, event);
                                                ScrollWindow(oTargetTR.id, event, iScrollBy, true);
                                            }
                                        }
                                    }
                                }
                            }

                            // dont'scroll 
                            bRetVal = false;

                        }
                    }
                }
                catch (e)
                {
                    ShowPopupMessage(e);
                }
                if (PrevSelectedRowID)
                {
                    bRetVal = false;
                }
            }
            break;

        // page down key    
        case 34:
            {
                try
                {
                    if (!bPopupIsShown)
                    {
                        if (PrevSelectedRowID)
                        {
                            var arrSplittedID = PrevSelectedRowID.split("_");

                            var strTableName = "";
                            var strFormName = "";

                            if (arrSplittedID[2] == "undefined" || arrSplittedID[2] == null)
                            {
                                strTableName = "tableCurrentLists";
                                strFormName = "fCurrentLists";
                            }
                            else if (arrSplittedID[2] == "ML")
                            {
                                strTableName = "tableMasterList";
                                strFormName = "fMasterList";
                            }
                            else if (arrSplittedID[2] == "RC")
                            {
                                strTableName = "tableRecipes";
                                strFormName = "fRecipes";
                            }
                            else if (arrSplittedID[2] == "CO")
                            {
                                strTableName = "tableCoupons";
                                strFormName = "fCoupons";
                            }
                            else if (arrSplittedID[3] == "RCITEM")
                            {
                                strTableName = "tableRecipe";
                                strFormName = "fRecipe";
                            }
                            else
                            {
                                strTableName = "tableCurrentList";
                                strFormName = "fCurrentList";
                            }

                            var oParentTable = GEBI(strTableName);

                            if (oParentTable)
                            {
                                var oTRCol = oParentTable.getElementsByTagName("TR");

                                var arrFilteredRows = new Array();

                                var iArrayIndex = 0;

                                var oSelectedRow = GEBI(PrevSelectedRowID);

                                var iSelectedIndex = -1;

                                if (oTRCol)
                                {

                                    for (i = 0; i < oTRCol.length; i++)
                                    {
                                        var oTR = oTRCol[i];

                                        if (oTR)
                                        {
                                            if (oTR.id.indexOf("tr_") != -1)
                                            {
                                                arrFilteredRows[iArrayIndex++] = oTR;
                                            }
                                        }
                                    }

                                    for (i = 0; i < arrFilteredRows.length; i++)
                                    {
                                        var oTR = arrFilteredRows[i];

                                        if (oTR)
                                        {
                                            if (oTR == oSelectedRow)
                                            {
                                                iSelectedIndex = i;
                                                break;
                                            }
                                        }
                                    }

                                    if (iSelectedIndex >= 0)
                                    {
                                        var iTmpIndex = GetLastVisibleRow(arrFilteredRows) + 1;

                                        var iNextSelectedRowIndex = (iTmpIndex <= arrFilteredRows.length) ? iTmpIndex : arrFilteredRows.length - 1 ;

                                        var oTargetTR = arrFilteredRows[iNextSelectedRowIndex];

                                        if (oTargetTR)
                                        {
                                            var iScrollBy = (iNextSelectedRowIndex - iSelectedIndex);
                                        
                                            SelectRow(oTargetTR.id, strFormName, bListInPrepareMode, event);
                                            ScrollWindow(oTargetTR.id, event, iScrollBy, true);
                                        }
                                    }
                                }
                            }

                            // dont'scroll 
                            bRetVal = false;

                        }
                    }
                }
                catch (e)
                {
                    ShowPopupMessage(e);
                }
                if (PrevSelectedRowID)
                {
                    bRetVal = false;
                }
            }
            break;

        // page up key     
        case 33:
            {
                try
                {
                    if (!bPopupIsShown)
                    {
                        if (PrevSelectedRowID)
                        {
                            var arrSplittedID = PrevSelectedRowID.split("_");

                            var strTableName = "";
                            var strFormName = "";

                            if (arrSplittedID[2] == "undefined" || arrSplittedID[2] == null)
                            {
                                strTableName = "tableCurrentLists";
                                strFormName = "fCurrentLists";
                            }
                            else if (arrSplittedID[2] == "ML")
                            {
                                strTableName = "tableMasterList";
                                strFormName = "fMasterList";
                            }
                            else if (arrSplittedID[2] == "RC")
                            {
                                strTableName = "tableRecipes";
                                strFormName = "fRecipes";
                            }
                            else if (arrSplittedID[2] == "CO")
                            {
                                strTableName = "tableCoupons";
                                strFormName = "fCoupons";
                            }
                            else if (arrSplittedID[3] == "RCITEM")
                            {
                                strTableName = "tableRecipe";
                                strFormName = "fRecipe";
                            }
                            else
                            {
                                strTableName = "tableCurrentList";
                                strFormName = "fCurrentList";
                            }

                            var oParentTable = GEBI(strTableName);

                            if (oParentTable)
                            {
                                var oTRCol = oParentTable.getElementsByTagName("TR");

                                var arrFilteredRows = new Array();

                                var iArrayIndex = 0;

                                var oSelectedRow = GEBI(PrevSelectedRowID);

                                var iSelectedIndex = -1;

                                if (oTRCol)
                                {

                                    for (i = 0; i < oTRCol.length; i++)
                                    {
                                        var oTR = oTRCol[i];

                                        if (oTR)
                                        {
                                            if (oTR.id.indexOf("tr_") != -1)
                                            {
                                                arrFilteredRows[iArrayIndex++] = oTR;
                                            }
                                        }
                                    }

                                    for (i = 0; i < arrFilteredRows.length; i++)
                                    {
                                        var oTR = arrFilteredRows[i];

                                        if (oTR)
                                        {
                                            if (oTR == oSelectedRow)
                                            {
                                                iSelectedIndex = i;
                                                break;
                                            }
                                        }
                                    }

                                    if (iSelectedIndex >= 0)
                                    {
                                        var iTmpIndex = GetFirstVisibleRow(arrFilteredRows) - 2;

                                        var iNextSelectedRowIndex = (iTmpIndex <= 0) ? 0 : iTmpIndex;

                                        var oTargetTR = arrFilteredRows[iNextSelectedRowIndex];

                                        if (oTargetTR)
                                        {
                                            var iScrollBy = (iSelectedIndex - iNextSelectedRowIndex);

                                            SelectRow(oTargetTR.id, strFormName, bListInPrepareMode, event);
                                            ScrollWindow(oTargetTR.id, event, iScrollBy, false);
                                        }
                                    }
                                }
                            }

                            // dont'scroll 
                            bRetVal = false;

                        }
                    }
                }
                catch (e)
                {
                    ShowPopupMessage(e);
                }
                if (PrevSelectedRowID)
                {
                    bRetVal = false;
                }
            }
            break;

        // Enter key         
        case 13:
            {
                var oEventElement = null;
                
                if (bIsFireFox)
                {
                    oEventElement = event.target;
                }
                else
                {
                    oEventElement = event.srcElement;
                }


                bRetVal = (oEventElement.tagName == "TEXTAREA") ? true : false;
            }
            break;

        case 8:
            {
//                var bInCurrentList = (strCurrentTab.indexOf("aManageCurrentList") != -1) ? true : false;

                var oEventElement = null;

                if (bIsFireFox)
                {
                    oEventElement = event.target;
                }
                else
                {
                    oEventElement = event.srcElement;
                }

                if (!bPopupIsShown && PrevSelectedRowID)
                {
//                    if (!bInCurrentList)
//                    {
                        if (oEventElement.tagName != "INPUT")
                        {
                            bRetVal = false;
                        }
//                    }
                }

                if (!bPopupIsShown && PrevSelectedRowID && oEventElement.tagName != "INPUT")
                {
                    bRetVal = false;
                }

            }
            break;

    }


    if (bRetVal == false)
    {
        if (event.stopPropagation)
        {
            event.stopPropagation();
        }

        if (event.preventDefault)
        {
            event.preventDefault();
        }
    }

    event.returnValue = bRetVal;
    event.cancelBubble = !bRetVal;
}

//---------------------------------------------------------------------------------------
function BlockWheel(oObj)
{
    oObj.onmousewheel = function() { event.returnValue = false; }
}
//-----------------------------------------------------------------
function ScrollWindow ( strTargetID, event, iScrollBy, bForward )
{
    try
    {
        var oTR = GEBI(strTargetID);

        var oParentDiv = GEBI("idMainBlockSource");
        var oTabsDiv = GEBI("divTabs");
        var oMenuDiv = GEBI("divMenu");
        var iDivOffset = 0;

        var bPageDownOrPageUp = (iScrollBy > 1) ? true : false;
         
        
        if (oParentDiv)
        {
//            iDivOffset = parseInt(oParentDiv.offsetTop) + parseInt(oMenuDiv.offsetTop) + parseInt(oLogoDiv.offsetTop);
            iDivOffset = parseInt(oMenuDiv.offsetTop) + parseInt(oMenuDiv.clientHeight) + parseInt(oTabsDiv.offsetHeight) + (oTR.clientHeight*2);      
            
            if (oTR)
            {
                if (oTR.children.length > 0)
                {
                
                    var oWindowMeasures = new TWindowMeasures();
                    var iWorkingAreaHeight = 0;

                    if (oWindowMeasures)
                    {
                        iWorkingAreaHeight = oWindowMeasures.GetHeight();
                    }
                
                    var oTRRealOffset = 0;

                    var iDeltaScroll = (bPageDownOrPageUp) ? iDivOffset : 0;

                    if (bForward)
                    {
                        oTRRealOffset = (oTR.offsetTop + oTR.clientHeight + iDivOffset);

                        if (oTRRealOffset > iWorkingAreaHeight)
                        {
                            window.scrollBy(0, parseInt((iScrollBy * oTR.clientHeight) + iDeltaScroll));
                        }
                    }
                    else
                    {
                        oTRRealOffset = parseInt(oWindowMeasures.GetScrollTop()) - iDivOffset - oTR.clientHeight;

                        if ( oTR.offsetTop < oTRRealOffset)
                        {
                            var iScroll = ((iScrollBy * oTR.clientHeight) + iDeltaScroll) * (-1);
                            
                            window.scrollBy(0, parseInt( iScroll ));
                        }
                    }
                }
            }
        }
    }
    catch ( e ) { }
}
//-----------------------------------------------------------------
function GetLastVisibleRow ( oRowsCol )
{
    var iRet = 1;
    try
    {
        if ( oRowsCol )
        {
            if ( oRowsCol.length > 0 )
            {
                var oParentDiv = GEBI("idMainBlockSource");
                var oLogoDiv = GEBI("divLogo");
                var oMenuDiv = GEBI("divMenu");
                var iDivOffset = 0;

                if (oParentDiv)
                {
                    iDivOffset = parseInt(oParentDiv.offsetTop) + parseInt(oMenuDiv.offsetTop) + parseInt(oLogoDiv.offsetTop);
                }

                var oWindowMeasures = new TWindowMeasures();
                var iWorkingAreaHeight = 0;

                if (oWindowMeasures)
                {
                    iWorkingAreaHeight = oWindowMeasures.GetHeight() + oWindowMeasures.GetScrollTop();
                }
                
                var bOutOfBounds = true;
                
                for ( i = 0; i < oRowsCol.length; i++)
                {
                    var oTR = oRowsCol[i];
                    
                    if ( oTR )
                    {
                        var oTROffset = (oTR.offsetTop + oTR.clientHeight + iDivOffset);

                        if (oTROffset > iWorkingAreaHeight)
                        {
                            iRet = i;
                            bOutOfBounds = false;
                            break;    
                        }
                    }
                }

                if (bOutOfBounds)
                {
                    iRet = oRowsCol.length  - 2;
                }
            }
        }
    }
    catch (e) { }
    
    return iRet;
}
//-----------------------------------------------------------------
function GetFirstVisibleRow(oRowsCol)
{
    var iRet = 1;
    try
    {
        if (oRowsCol)
        {
            if (oRowsCol.length > 0)
            {
                var oParentDiv = GEBI("idMainBlockSource");
                var oLogoDiv = GEBI("divLogo");
                var oMenuDiv = GEBI("divMenu");
                var iDivOffset = 0;

                if (oParentDiv)
                {
                    iDivOffset = parseInt(oParentDiv.offsetTop) + parseInt(oMenuDiv.offsetTop) + parseInt(oLogoDiv.offsetTop);
                }

                var oWindowMeasures = new TWindowMeasures();
                var iWorkingAreaHeight = 0;

                if (oWindowMeasures)
                {
                    iWorkingAreaHeight = oWindowMeasures.GetHeight() + oWindowMeasures.GetScrollTop();
                }

                var bOutOfBounds = true;

                for (i = 0; i < oRowsCol.length; i++)
                {
                    var oTR = oRowsCol[i];

                    if (oTR)
                    {
                        var oTRRealOffset = parseInt(oWindowMeasures.GetScrollTop()) - iDivOffset - oTR.clientHeight;

                        if (oTR.offsetTop > oTRRealOffset)
                        {
                            iRet = i;
                            bOutOfBounds = false;
                            break;
                        }
                    }
                }

                if (bOutOfBounds)
                {
                    iRet = 1;
                }
            }
        }
    }
    catch (e) { }

    return iRet;
}
//-----------------------------------------------------------------
