function EnlargeImage ( iRecID )
{
   ProcessRequest ( "default.aspx", "action=DirectPOST&PostAction=paShowOriginalImage&RecID=" + encodeURIComponent ( iRecID ), "SHOW_ORIGINAL_IMAGE", "" );
}
//-----------------------------------------------------------------
function RemoveImageFunc(idParentForm, oButton, iRecID, iCell, iTabIndex)
{
    var oImageTD = GEBI ( "idCurrentImagePreview" );
    var oForm = GEBI ( idParentForm );
    
    if ( oImageTD && oForm )
    {
        var iIndex = (idParentForm == "fModalAddingForm") ? 10 : 5;
    
        if ( oButton.value == "Remove" )
        {
            oButton.value = "Restore";
            oImageTD.innerHTML = "No Image";
//            oImageTD.style.width = "50px";
            oImageTD.style.height = "40px";
            oForm.RemoveImage.value = "true";
            oForm[iIndex].disabled = "true";
        }
        else
        {
            oButton.value = "Remove";
            oImageTD.innerHTML = "<a TABINDEX=\"" + iTabIndex + "\" href=\"#\" onclick=\"ShowHintImage( " + iRecID + ", " + (iCell - 1) + ", event, false );\"><img src=\"Image.aspx?RecID=" + iRecID + "&CellID=" + iCell + "&Show=true\"   /></a>";
            oForm.RemoveImage.value = "false";
            oForm[iIndex].disabled = "";
        }
    }
}
//-----------------------------------------------------------------
function ShowModalPopUp (innerHTML, bReloadForm)
{
    strGlobalPopUpSource = innerHTML;
    
    setTimeout("ShowModalPopUpEx(" + bReloadForm + ")", 500);
}
//-----------------------------------------------------------------
function ShowStaticIdleInModalPopUp()
{
    try
    {
        var oDiv = GEBI("idForm");
        var oDivFg = GEBI("idForeground");

        var oWindowMeasures = new TWindowMeasures();

        var iScrollTop = oWindowMeasures.GetScrollTop();

        oDivFg.style.top = iScrollTop + "px";

        oDivFg.innerHTML = StaticIdleBox();;

        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";

        WindowResize();
    }
    catch (e) { }
}
//-----------------------------------------------------------------
function ShowModalPopUpEx(bReloadForm)
{
    try
    {
        var oDiv = GEBI("idForm");
        var oDivFg = GEBI("idForeground");

        if (bReloadForm)
        {
            oDivFg.innerHTML = strGlobalPopUpSource;
            // 1st form 1st text input
            FocusInput(1, 0, false);
            //            FocusInput(1, 0, false);

            return;
        }
        var oWindowMeasures = new TWindowMeasures();

        var iScrollTop = oWindowMeasures.GetScrollTop();

        //        oDivFg.style.top = iScrollTop + 145 + "px";
        oDivFg.style.top = iScrollTop + "px";

        oDivFg.innerHTML = strGlobalPopUpSource;

        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";

        WindowResize();

        if ((iActiveTabIndex == 4 || iActiveTabIndex == 5 || iActiveTabIndex == 6) && (strEditingForm == "fCurrentList"))
        {
            ShowHideUOMBlock("mShow");
        }

        FocusInput(1, iActiveTabIndex, false);

        if (iActiveTabIndex == 10 && (strEditingForm == "fCurrentList"))
        {
            SwitchPurchasedStatus("tdPurchasedStatus_POPUP", iPurchase_Status_InitialValue);
        }

        iActiveTabIndex = 0;

        CreateUploadComponent();

        CallEvalGeneratedJS(null, strGlobalPopUpSource);
    }
    catch (e) { }


}
//-----------------------------------------------------------------
function CloseModalPopUp()
{
    try
    {
        try
        {
            ShowHideUOMBlock("mHide");
        }
        catch (e)
        {   }

        var oDiv = GEBI("idForm");
        var oDivFg = GEBI("idForeground");

        oDivFg.style.display = "none";

        oDiv.style.display = "none";

        oDivFg.innerHTML = "";

        InEditMode = false;
        
    }
    catch ( e )
    { }
   
}
//-----------------------------------------------------------------
function StringHasProhibitedSymbols ( strValue )
{
    var bRet = "";
    try
    {
        if (strValue)
        {
            for (i = 0; i < strValue.length; i++)
            {
                var cChar = strValue.charAt(i);
                if (
                    cChar == "<" ||
                    cChar == ">" ||
                    cChar == "\`"
                   )
                {
                    bRet = cChar;
                    break;   
                }
            }
        }
    }
    catch ( e )
    {}
    
    return bRet;
}
//-----------------------------------------------------------------
function SwitchCheckBoxFlag(oCheckBox, idForm, idField)
{
    var oForm = GEBI(idForm);
    if ( oForm )
    {
        if (idField == "AutoSignInFlag")
        {
            if (oCheckBox.checked)
            {
                oForm["RememberMeFlag"].value = true;
                oForm["RememberMyPassFlag"].value = true;

                var cbRememberMe = GEBI("cbRememberMe");
                
                if (cbRememberMe)
                {
                    cbRememberMe.checked = true;
                    cbRememberMe.disabled = true;
                }

                var cbRememberMyPass = GEBI("cbRememberMyPass");

                if (cbRememberMyPass)
                {
                    cbRememberMyPass.checked = true;
                    cbRememberMyPass.disabled = true;
                }
            }
            else
            {
                oForm["RememberMeFlag"].value = false;
                oForm["RememberMyPassFlag"].value = false;

                var cbRememberMe = GEBI("cbRememberMe");

                if (cbRememberMe)
                {
                    cbRememberMe.checked = false;
                    cbRememberMe.disabled = false;
                }

                var cbRememberMyPass = GEBI("cbRememberMyPass");

                if (cbRememberMyPass)
                {
                    cbRememberMyPass.checked = false;
                    cbRememberMyPass.disabled = false;
                }

            }
        }
        else if (idField == "RememberMyPassFlag") 
        {
            if (oCheckBox.checked)
            {
                oForm["RememberMeFlag"].value = true;

                var cbRememberMe = GEBI("cbRememberMe");

                if (cbRememberMe)
                {
                    cbRememberMe.checked = true;
                    cbRememberMe.disabled = true;
                }

            }
            else
            {
                oForm["RememberMeFlag"].value = false;

                var cbRememberMe = GEBI("cbRememberMe");

                if (cbRememberMe)
                {
                    cbRememberMe.checked = false;
                    cbRememberMe.disabled = false;
                }
            }
        }
        
        oForm[idField].value = oCheckBox.checked;
    }
}
//-----------------------------------------------------------------
function ShowHintImage( iRecID, iCellID, MouseEvent, bHide)
{
    // получаем объект
    var oDivHint = GEBI("DivHint");
    var oDivFor = GEBI("idForeground");

    // если объект не создан - создаем его
    if (!oDivHint)
    {
        oDivHint = document.createElement("DIV");
        oDivHint.id = "DivHint";
        oDivHint.onclick = function() { oDivHint.style.display = "none"; }
        oDivHint.className = "DivHint";
        oDivFor.appendChild(oDivHint);

        oImg = document.createElement("IMG");
        oDivHint.appendChild(oImg);

    }

    // если все ок
    if (oDivHint)
    {
        if (bHide)
        {
            oDivHint.style.display = "none";
        }
        else
        {
            oImg.src = "Image.aspx?RecID=" + iRecID + "&CellID=" + iCellID;

            oDivHint.style.left = "0px";
            oDivHint.style.top = "0px";

            oDivHint.style.display = "block";

        }
    }
}
//---------------------------------------------------------------------------------------
function ShowMemo ( strText )
{
    ProcessRequest("default.aspx", "action=DirectPOST&PostAction=paShowMemo&Text=" + encodeURIComponent ( strText ), "SHOW_MEMO", null);
}
//---------------------------------------------------------------------------------------
function ProceedClick( oTH )
{
    var oTDCol = oTH.getElementsByTagName("TD");
    
    if ( oTDCol )
    {
        oTDCol[0].click();    
    }
}
//---------------------------------------------------------------------------------------
function CloseAllLists ( strIDKey, strExceptID, idOwnerControl )
{
    var oDivCollection = document.getElementsByTagName("DIV");
    var oDivOwnerControl = GEBI(idOwnerControl);

    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(strIDKey) != -1 && oDiv.id != strExceptID)
                    {
                        oDiv.style.display = "none";
                    }
                }
            }
        }
    }

    var oTDCollection = document.getElementsByTagName("IMG");

    var strExceptOpenCloseButton = "Open_Close_Button_IMG" + strExceptID.split("_")[1];

    if (oTDCollection)
    {
        for (i = 0; i < oTDCollection.length; i++)
        {
            var oTD = oTDCollection[i];

            if (oTD && (oTD.id))
            {
                if (oTD.id.length > 0)
                {
                    if (oTD.id.indexOf("Open_Close_Button_IMG") != -1 && oTD.id != strExceptOpenCloseButton)
                    {
                        oTD.src = "./Images/down.gif";
                    }
                }
            }
        }
    }
    
    
}
//---------------------------------------------------------------------------------------
function FocusInput( iFormIndex, iIndex, bBlur )
{
    try
    {
        var colFroms = document.forms;

        if (colFroms)
        {
            if (colFroms.length > 0)
            {
                var oForm = colFroms[iFormIndex];
                
                var iFieldIndex = 0, iCorrectInputIndex = 0;

                if (!oForm) return;
                
                while (iFieldIndex < oForm.length)
                {
                    var oField = oForm[iFieldIndex];

                    if (oField)
                    {
                        if (oField.tagName == "INPUT" || oField.tagName == "TEXTAREA")
                        {
                            if (oField.tagName == "INPUT")
                            {
                                if (oField.type != "text" && oField.type != "edit" && oField.type != "button" && oField.type != "checkbox")
                                {
                                    iFieldIndex++;
                                    continue;
                                }
                            }
                            
                            try
                            {
                                oField.focus();
                            }
                            catch ( e )
                            {
                            }

                            if (bBlur)
                            {
                                try
                                {
                                    oField.blur();
                                }
                                catch ( e )
                                {}
                            }

                            if (iCorrectInputIndex == iIndex) break;
                        }
                    }

                    iFieldIndex++;
                    iCorrectInputIndex++;
                }
                
            }
        }
    }
    catch ( e )
    { }
}
//-----------------------------------------------------------------
function FixForm ( oForm )
{
    if (oForm)
    {
        switch ( oForm.id )
        {
            case "fModalAddingForm":
                {
                    var oQuantity = GetFieldByName(oForm, "Quantity", false);
                    var oCategory = GetFieldByName(oForm, "Category", false);
                    var oPrice = GetFieldByName(oForm, "Price_CurrentList", false);
                    
                    if ( oQuantity && oCategory && oPrice )
                    {
                        var strTemp = oQuantity.value;
                        
                    }
                    
                }
            break;
        }
    }
}
//-----------------------------------------------------------------
function GetFieldByName ( oForm, strFieldName, bEqual )
{
    if ( oForm && strFieldName.length > 0 )
    {
        var colFormElements = oForm.getElementsByTagName("INPUT");
        for ( i = 0; i < colFormElements.length; i++ )
        {
            var oInput = colFormElements[i];
            
            if ( oInput )
            {
                if ( bEqual )
                {
                    if ( oInput.id == strFieldName || oInput.name == strFieldName )
                    {
                        return oInput;
                    }
                }
                else
                {
                    if (oInput.id.indexOf(strFieldName) != -1 || oInput.name.indexOf(strFieldName) != -1)
                    {
                        return oInput;
                    }
                }
            }
        }
    }
    
    return null;
    
}
//-----------------------------------------------------------------
function Sort ( idForm, iSortCol, iSortDir )
{
    var oTargetTD = GEBI("idMainBlockSource");

    var strSortAction = "";
    
    switch ( idForm )
    {
        case "fMasterList":
        {
            strSortAction = "paSortMasterList";
        }
        break;
        case "fCurrentList":
        {
            strSortAction = "paSortCurrentList";
        }
        break;
        case "fRecipe":
        {
            strSortAction = "paSortRecipe";
        }
        break;
}
   

    ProcessRequest("default.aspx", "action=DirectPOST&PostAction=" + encodeURIComponent ( strSortAction ) + "&SortCol=" + encodeURIComponent ( iSortCol ) + "&SortDir=" + encodeURIComponent ( iSortDir ), "SORT_GRID", oTargetTD);

}
//-----------------------------------------------------------------
function ShowPopupMessage ( strText, bIsLoadingMessage )
{
    try
    {
        var oSpan = GEBI("spanMessage");

        if (oSpan)
        {
            oSpan.innerHTML = "<b>" + strText + "</b>";
            oSpan.style.visibility = "visible";
        }

        if (timerMessageSpan)
        {
            clearTimeout(timerMessageSpan);
            timerMessageSpan = null;
        }

        if (!bIsLoadingMessage)
        {
            timerMessageSpan = setTimeout("CloseSpanMessage()", HIDE_MESSAGE_SPAN_TIMEOUT);
        }
    }
    catch (e) { }

//    ProcessRequest('default.aspx', 'action=DirectPOST&PostAction=paShowMessage&Text=' + strText, 'SHOW_MESSAGE', '');
}
//-----------------------------------------------------------------
function CloseSpanMessage()
{
    try
    {
        var oSpan = GEBI("spanMessage");

        if (oSpan)
        {
            clearTimeout(timerMessageSpan);
            timerMessageSpan = null;
            oSpan.innerHTML = "";
            oSpan.style.visibility = "hidden";
        }
    }
    catch ( e ){}
}
//-----------------------------------------------------------------
function stripHTML(oldString)
{
    var newString = "";
    var inTag = false;
    for (var i = 0; i < oldString.length; i++)
    {

        if (oldString.charAt(i) == '<') newString += "&lt;";
        else if (oldString.charAt(i) == '>') newString += "&gt;";
        else
            newString += oldString.charAt(i);

    }

    return newString;
}
//-----------------------------------------------------------------
function GetFormValueByPartName ( idForm, strPartFieldName )
{
    var strRet = "";
    try
    {
        var oForm = GEBI(idForm);

        if (oForm)
        {
            var colFormInputs = oForm.getElementsByTagName("INPUT");

            for (i = 0; i < colFormInputs.length; i++)
            {
                var oInput = colFormInputs[i];

                if (oInput.type == "text" || oInput.type == "hidden" )
                {
                    var strFieldName = (oInput.name != "undefined" && oInput.name != "") ? oInput.name : oInput.id;

                    if (strFieldName.indexOf(strPartFieldName) != -1)
                    {
                        strRet = oInput.value;
                        break;
                    }
                }
            }
        }
    }
    catch ( e )
    { }
    
    return strRet;

}
//-----------------------------------------------------------------
function GetFormFieldByPartName(idForm, strPartFieldName)
{
    var oRet = null;
    try
    {
        var oForm = GEBI(idForm);

        if (oForm)
        {
            var colFormInputs = oForm.getElementsByTagName("INPUT");

            for (i = 0; i < colFormInputs.length; i++)
            {
                var oInput = colFormInputs[i];

                if (oInput.type == "text" || oInput.type == "hidden")
                {
                    var strFieldName = (oInput.name != "undefined" && oInput.name != "") ? oInput.name : oInput.id;

                    if (strFieldName.indexOf(strPartFieldName) != -1)
                    {
                        oRet = oInput;
                        break;
                    }
                }
            }
        }
    }
    catch (e)
    { }

    return oRet;

}
//-----------------------------------------------------------------
function ShowIdleBox(bStatic)
{
    if (bStatic)
    {
        ShowModalPopUp(StaticIdleBox(), false);
    }
    else
    {
        ProcessRequest("default.aspx", "action=DirectPOST&PostAction=paShowIdle", "SHOW_IDLE", "");
    }
}
//-----------------------------------------------------------------
function StaticIdleBox()
{
    return  "<center><form id=\"fBlank\" ><div id=\"divStaticIdle\" style=\"width:200px; \"><table  cellpadding=\"0\" cellspacing=\"0\">" +
               "  </table>" +
               "  <table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">" +
               "     <tr>" +
               "        <td style=\"background-color:white;\">" +
               "           <div align=\"left\">" +
               "           <div style=\"padding:10px;\">" +
               "           <table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">" +
               "              <tr>" +

               "                 <TD ID=\"tdformFieldsCaption\" style=\"padding-top:0px; text-align:center;\"><img src=\"./Images/loading.gif\" />" +
               "                 </TD>" +

               "                 <TD ID=\"tdformFieldsCaption\" style=\"padding-top:10px; text-align:left;\"><h2>Please wait...</h2>" +
               "                 </TD>" +

               "              </tr>" +

               "              <tr>" +
               "              </tr>" +

               "           </table>" +
               "           </div>" +
               "        </td>" +
               "     </tr>" +
               "  </table>" +
               "</div></form></center>";

}
//-----------------------------------------------------------------
function ShowHideUOMBlock( strMode )
{
    try
    {
        var aUOM = GEBI("aUOM");

        if (aUOM)
        {
            var strDisplayMode = (bUOMBlockHidden) ? "trDisplayBlock" : "trDisplayNone";

            switch (strMode)
            {
                case "mAuto":
                {
                    aUOM.innerHTML = (bUOMBlockHidden) ? "Units of measures &lt;&lt;" : "Units of measures &gt;&gt;";
                    bUOMBlockHidden = (bUOMBlockHidden) ? false : true;
                }
                break;
                case "mShow":
                {
                    strDisplayMode = "trDisplayBlock";
                    aUOM.innerHTML = "Units of measures &lt;&lt;";
                    bUOMBlockHidden = false;
                }
                break;
                case "mHide":
                {
                    strDisplayMode = "trDisplayNone";
                    aUOM.innerHTML = "Units of measures &gt;&gt;";
                    bUOMBlockHidden = true;
                }
                break;
            }
          
            var oTRCol = document.getElementsByTagName("TR");

            if (oTRCol)
            {
                for (i = 0; i < oTRCol.length; i++)
                {
                    var oTR = oTRCol[i];

                    if (oTR)
                    {
                        if (oTR.id == "trUOM")
                        {
                            oTR.className = strDisplayMode;
                        }
                    }
                }
            }
        }
    }
    catch ( e ) {}
}
//-----------------------------------------------------------------
function SelectListRow(oDiv, idTextBox, strAction)
{
    var oTDCollection = oDiv.getElementsByTagName("TD");

    var oInput = GEBI(idTextBox);

    var strAjustedValue = oInput.value;

    var bIsListOpened = (oDiv.style.display == "block") ? true : false;

    var iSelectedIndex = -2;
    try
    {
        if (oTDCollection)
        {
            if (!bIsListOpened)
            {
                oDiv.style.display = "block"

                if (strAjustedValue.length > 0)
                {
                    for (i = 0; i < oTDCollection.length; i++)
                    {
                        var oTD = oTDCollection[i];

                        if (oTD)
                        {
                            if (oTD.innerHTML.toLowerCase() == strAjustedValue.toLowerCase() && oTD.className.indexOf("Custom_List_Item_option_unselecteble") == -1)
                            {
                                oTD.className = "Selected_List_Item";

                                oInput.value = oTD.innerHTML;

                                if (oTD.offsetTop + oTD.clientHeight > parseInt(oDiv.clientHeight))
                                {
                                    oDiv.scrollTop += parseInt(oTD.clientHeight) * i;
                                }
                                return;
                            }
                        }
                    }
                }
            }
            else
            {
                switch ( strAction )
                {
                    case "KEY_UP":
                    {
                        for (i = oTDCollection.length; i >= 0; i--)
                        {
                            var oTD = oTDCollection[i];

                            if (oTD)
                            {
                                if (oTD.className == "Selected_List_Item")
                                {
                                    oTD.className = "Custom_List_Item";

                                    iSelectedIndex = GetNextValidItemIndex(strAction, i, oTDCollection);
                                    
                                    break;
                                }
                            }
                        }
                    }
                    break;

                    case "KEY_DOWN":
                    {
                        for (i = 0; i < oTDCollection.length; i++)
                        {
                            var oTD = oTDCollection[i];

                            if (oTD)
                            {
                                if (oTD.className == "Selected_List_Item")
                                {
                                    oTD.className = "Custom_List_Item";

                                    iSelectedIndex = GetNextValidItemIndex(strAction, i, oTDCollection);

                                    break;
                                }
                            }
                        }
                    }
                    break;
                    
                    case "KEY_PAGEDOWN":
                    {
                        for (i = 0; i < oTDCollection.length; i++)
                        {
                            var oTD = oTDCollection[i];

                            if (oTD)
                            {
                                if (oTD.className == "Selected_List_Item")
                                {
                                    oTD.className = "Custom_List_Item";

                                    iSelectedIndex = GetNextValidItemIndex(strAction, i, oTDCollection);
                                    
                                    break;
                                }
                            }
                        }
                    }
                    break;
                    
                    case "KEY_PAGEUP":
                    {
                        for (i = 0; i < oTDCollection.length; i++)
                        {
                            var oTD = oTDCollection[i];

                            if (oTD)
                            {
                                if (oTD.className == "Selected_List_Item")
                                {
                                    oTD.className = "Custom_List_Item";

                                    iSelectedIndex = GetNextValidItemIndex(strAction, i, oTDCollection);
                                    
                                    break;
                                }
                            }
                        }
                    }
                    break;
                }
            }

            if (oTDCollection.length > 0)
            {
                if (iSelectedIndex == -2)
                {
                    if (HasAnyValidItem(oTDCollection))
                    {
                        oTDCollection[0].className = "Selected_List_Item";

                        oInput.value = oTDCollection[0].innerHTML;
                    }
                }
                else
                {
                    switch ( strAction )
                    {
                        case "KEY_UP":
                        {
                            if (iSelectedIndex < 0)
                            {
                                oInput.value = oTDCollection[oTDCollection.length - 1].innerHTML;
                                oTDCollection[oTDCollection.length - 1].className = "Selected_List_Item";

                                oDiv.scrollTop = parseInt(oDiv.scrollHeight) - parseInt(oDiv.clientHeight);
                            }
                            else
                            {
                                oTDCollection[iSelectedIndex].className = "Selected_List_Item";
                                oInput.value = oTDCollection[iSelectedIndex].innerHTML;

                                var oTD = oTDCollection[iSelectedIndex];

                                var iDelta = parseInt(oDiv.scrollHeight) - parseInt(oDiv.clientHeight);

                                if (oTD.offsetTop < iDelta)
                                {
                                    oDiv.scrollTop -= oTD.clientHeight;
                                }
                            }
                        }
                        break;

                        case "KEY_DOWN":
                        {
                            if (iSelectedIndex >= oTDCollection.length)
                            {
                                oTDCollection[0].className = "Selected_List_Item";
                                oInput.value = oTDCollection[0].innerHTML;

                                oDiv.scrollTop = 0;
                            }
                            else
                            {
                                oTDCollection[iSelectedIndex].className = "Selected_List_Item";
                                oInput.value = oTDCollection[iSelectedIndex].innerHTML;

                                var oTD = oTDCollection[iSelectedIndex];

                                if (oTD.offsetTop + oTD.clientHeight > parseInt(oDiv.clientHeight))
                                {
                                    oDiv.scrollTop += oTD.clientHeight;
                                }
                            }

                        }
                        break;
                        
                        case "KEY_PAGEDOWN":
                            {
                                if (iSelectedIndex >= oTDCollection.length)
                                {
                                    oTDCollection[0].className = "Selected_List_Item";
                                    oInput.value = oTDCollection[0].innerHTML;

                                    oDiv.scrollTop = 0;
                                }
                                else
                                {
                                    oTDCollection[iSelectedIndex].className = "Selected_List_Item";
                                    oInput.value = oTDCollection[iSelectedIndex].innerHTML;

                                    var oTD = oTDCollection[iSelectedIndex];

                                    if (oTD.offsetTop + oTD.clientHeight > parseInt(oDiv.clientHeight))
                                    {
                                        oDiv.scrollTop += (oTD.clientHeight*8);
                                    }
                                }
                        
                        }
                        break;

                        case "KEY_PAGEUP":
                            {
                                if (iSelectedIndex < 0)
                                {
                                    oInput.value = oTDCollection[oTDCollection.length - 1].innerHTML;
                                    oTDCollection[oTDCollection.length - 1].className = "Selected_List_Item";

                                    oDiv.scrollTop = parseInt(oDiv.scrollHeight) - parseInt(oDiv.clientHeight);
                                }
                                else
                                {
                                    oTDCollection[iSelectedIndex].className = "Selected_List_Item";
                                    oInput.value = oTDCollection[iSelectedIndex].innerHTML;

                                    var oTD = oTDCollection[iSelectedIndex];

                                    var iDelta = parseInt(oDiv.scrollHeight) - parseInt(oDiv.clientHeight);

                                    if (oTD.offsetTop < iDelta)
                                    {
                                        oDiv.scrollTop -= (oTD.clientHeight * 8);
                                    }
                                }
                        
                        }
                        break;
                        
                    }
                }
            }
        }
    }
    catch (e)
    {  }
}
//-----------------------------------------------------------------
function OpenCloseFrameBlock ( divId )
{
    try
    {
        var oDiv = GEBI ( divId );
        if ( oDiv )
        {
            oDiv.style.display = ( oDiv.style.display == "block" ) ? "none" : "block";
        }            

    }
    catch ( e ){}
}
//-----------------------------------------------------------------
function ReloadCurrentList ( iListID )
{
    try
    {
        var oTargetTD = GEBI("idMainBlockSource");

        ClearVariables();

        ShowLoadingMessageDelayed();
        
        ProcessRequest("default.aspx", "action=DirectPOST&PostAction=paReloadCurrentList&ListID=" + encodeURIComponent ( iListID ), "RELOAD_LIST", oTargetTD);
    }
    catch (e) { }
}
//-----------------------------------------------------------------
function ReloadRecipe(iRecipeID)
{
    try
    {
        var oTargetTD = GEBI("idMainBlockSource");

        ClearVariables();

        ShowLoadingMessageDelayed();

        ProcessRequest("default.aspx", "action=DirectPOST&PostAction=paReloadRecipe&RecipeID=" + encodeURIComponent ( iRecipeID ), "RELOAD_RECIPE", oTargetTD);
    }
    catch (e) { }
}
//-----------------------------------------------------------------
function SwitchTab(strTarget)
{
    try
    {
        InitAdsBlocks();

        ClearVariables();
               
        CloseSpanMessage();               
               
        strCurrentTab = strTarget;

        ShowLoadingMessageDelayed ();

        var oTargetTD = GEBI("divTabs");

        ProcessRequest("default.aspx", "action=DirectPOST&PostAction=paSwitchTabs&TargetTab=" + encodeURIComponent ( strCurrentTab ), "SWITCH_TABS", oTargetTD);
    }
    catch (e) { }
}
//-----------------------------------------------------------------
function InitAdsBlocks ()
{
    try
    {
        var oDivGoogleAds_468x15_Hidden = GEBI("idDivGoogleAds_468x15_Hidden");

        if (oDivGoogleAds_468x15_Hidden)
        {
            if (oDivGoogleAds_468x15_Hidden.parentNode)
            {
                if ( oDivGoogleAds_468x15_Hidden.parentNode.tagName.toLowerCase() != "body" )
                {
                    oDivGoogleAds_468x15_Hidden.style.display = "none";
                    document.body.appendChild(oDivGoogleAds_468x15_Hidden);
                }
            }
        }
    }
    catch (e) {}
}
//-----------------------------------------------------------------
function Logout ()
{
    try
    {
        ProcessRequest("default.aspx", "action=DirectPOST&PostAction=paLogout", "LOGOUT", "");
    }
    catch (e) {}
}
//-----------------------------------------------------------------
function SwitchMenu(strTarget)
{
    try
    {
        var oDivWideSkyscraper_160x600_Hidden = GEBI("idDivWideSkyscraper_160x600_Hidden");

        if (oDivWideSkyscraper_160x600_Hidden)
        {
            if (oDivWideSkyscraper_160x600_Hidden.parentNode)
            {
                if (oDivWideSkyscraper_160x600_Hidden.parentNode.tagName.toLowerCase() != "body")
                {
                    oDivWideSkyscraper_160x600_Hidden.style.display = "none";
                    document.body.appendChild(oDivWideSkyscraper_160x600_Hidden);
                }
            }
        }

        var oDivGoogleAds_160x600_Hidden = GEBI("idDivGoogleAds_160x600_Hidden");

        if (oDivGoogleAds_160x600_Hidden)
        {
            if (oDivGoogleAds_160x600_Hidden.parentNode)
            {
                if (oDivGoogleAds_160x600_Hidden.parentNode.tagName.toLowerCase() != "body")
                {
                    oDivGoogleAds_160x600_Hidden.style.display = "none";
                    document.body.appendChild(oDivGoogleAds_160x600_Hidden);
                }
            }
        }

        strMenuElement = strTarget;
        strCurrentTab = strMenuElement;
        
        var oTargetTD = GEBI("divMenu");

        ProcessRequest("default.aspx", "action=DirectPOST&PostAction=paSwitchMenu&TargetElement=" + encodeURIComponent ( strMenuElement ), "SWITCH_MENU", oTargetTD);
    }
    catch (e) { }
}
//-----------------------------------------------------------------
function ShowLoadingMessageDelayed()
{
    try
    {
        timerLoadingMessage = setTimeout("ShowPopupMessage('Loading...', true)", SHOW_LOADING_MESSAGE_TIMEOUT);
    }
    catch (e) { }
}
//-----------------------------------------------------------------
function HideLoadingMessage()
{
    try
    {
        if (timerLoadingMessage)
        {
            clearTimeout(timerLoadingMessage);
            timerLoadingMessage = null;
        }

        CloseSpanMessage();
    }
    catch (e) { }
}
//-----------------------------------------------------------------
function RotateCoupons()
{
    try
    {
        var otdPopupBanner = GEBI("tdPopupBanner");

        var oDivPopupBannerHidden = GEBI("idDivPopupBannerHidden");

        if (otdPopupBanner && oDivPopupBannerHidden)
        {
            otdPopupBanner.innerHTML = oDivPopupBannerHidden.innerHTML;

            var colTD = otdPopupBanner.getElementsByTagName("TD");

            var oRetElm = null;

            for (i = 0; i < colTD.length; i++)
            {
                var oTD = colTD[i];

                if (oTD.className.indexOf("ci_ccss_td") != -1)
                {
                    oRetElm = oTD;
                    break;
                }
            }

            if (oRetElm)
            {
                var attrOnClick = oRetElm.attributes.getNamedItem("onclick");

                if (attrOnClick)
                {
                    if (attrOnClick.value.length > 0)
                    {
                        if (attrOnClick.value.indexOf("ci_capf_Device_") != -1 && attrOnClick.value.indexOf("ScrollCoupons") != -1)
                        {
                            eval(attrOnClick.value);
                        }
                    }
                }
            }
        }
    }
    catch ( e ) {}
}
//-----------------------------------------------------------------
function UpdateShoppingListsControlTrigger()
{
    // update shopping lists comboboxes
    var aTD_IDs = Array("tdExistingListsUpdate", "tdExistingListsClone", "tdExistingListsMergeFrom", "tdExistingListsMergeInto", "tdImportList", "tdExistingListsDelete");
    var aCB_IDs = Array("faUpdateCL", "faCloneCL", "faMergeFromCL", "faMergeIntoCL", "faImportLists", "faListToDelete");

    for (i = 0; i < aCB_IDs.length; i++)
    {
        var oTD = GEBI(aTD_IDs[i]);

        if (oTD)
        {
            var oInputsCol = oTD.getElementsByTagName("INPUT");

            if (oInputsCol)
            {
                var oInput = oInputsCol[0];

                if (oInput)
                {
                    var strCallback = ""; 
                      
                    switch (aCB_IDs[i])
                    {
                        case "faUpdateCL":
                        {
                           strCallback = "ShoppingListOnSelect";
                        }
                        break;
                        case "faImportLists":
                        {
                           strCallback = "ImportOnSelect";
                        }
                        break;
                        
                    }

                    var strCustomListHTML = _Default.GetCurrentListsAsCustomListHTML(aCB_IDs[i], strCallback, oInput.value).value;

                    if (strCustomListHTML.length > 0)
                    {
                        oTD.innerHTML = strCustomListHTML;
                    }
                }
            }
        }
    }
}
//-----------------------------------------------------------------
function EnableDisableDeleteButton ( oObj )
{
   var oButton = null;
   
   if ( oObj )
   {
       switch ( oObj.id )
       {
           case "cbConfirmListDeletion":
               {
                   oButton = GEBI('bnListDelete');
               }
            break;
            case "cbConfirmStoreDeletion":
            {
                oButton = GEBI('bnStoreDelete');
            }
            break;
       }

       if (oButton)
       {
            oButton.disabled = !oObj.checked;
       }
   } 
}
//-----------------------------------------------------------------
function UpdateStoresControlTrigger()
{
    // update shopping lists comboboxes
    var aTD_IDs = Array("tdExistingStoreUpdate", "tdExistingStoreDelete");
    var aCB_IDs = Array("faUpdateStore", "faStoreToDelete");

    for (i = 0; i < aCB_IDs.length; i++)
    {
        var oTD = GEBI(aTD_IDs[i]);

        if (oTD)
        {
            var oInputsCol = oTD.getElementsByTagName("INPUT");

            if (oInputsCol)
            {
                var oInput = oInputsCol[0];

                if (oInput)
                {
                    var strCallback = "";

                    switch (aCB_IDs[i])
                    {
                        case "faUpdateStore":
                        {
                            strCallback = "StoresOnSelect";
                        }
                        break;
                    }

                    var strCustomListHTML = _Default.GetStoresAsCustomListHTML(aCB_IDs[i], strCallback, oInput.value).value;

                    if (strCustomListHTML.length > 0)
                    {
                        oTD.innerHTML = strCustomListHTML;
                    }
                }
            }
        }
    }
}
//-----------------------------------------------------------------
function StartProgressPooling()
{
    try
    {
        var oProgress = new TAsyncProgress;

        oProgress.OnStart = OnProgressStart;

        oProgress.OnProgress = OnProgress;

        oProgress.OnComplete = ProgressComplete;

        oProgress.StartProgress();

    }
    catch (ex) { }

}
//-----------------------------------------------------------------
function OnProgressStart()
{
    try
    {
    }
    catch (ex) { }
}
//-----------------------------------------------------------------
function OnProgress()
{
    try
    {
        var oTDStatus = GEBI("H3ProgressBarStatusString");
        var oTableIndicator = GEBI("tableProgressBarIndicator");
        var oTDContainer = GEBI("tdContainer");

        var strRet = _Default.GetProgress().value;

        oTDStatus.innerHTML = strRet.strStatusString + " " + ((strRet.iCurrentValue > 0) ? strRet.iCurrentValue + "%" : "<img src=\"./images/loader_small.gif\" />");

        if (strRet.iCurrentValue > 0)
        {
            oTDContainer.innerHTML = "&nbsp;";
            oTableIndicator.width = strRet.iCurrentValue + "%";
//            oTableIndicator.style.backgroundColor = "gray";
        }
        
        if (strRet.psState == "1")
        {
            AsyncProgress.ProgressComplete = true;
        }
    }
    catch ( ex ){}
}
//-----------------------------------------------------------------
function ProgressComplete()
{
    try
    {
        var oTargetTD = GEBI("idMainBlockSource");

        var strRet = _Default.GetBuffer().value;

        oTargetTD.innerHTML = strRet;
    }
    catch (ex) { }
}
//-----------------------------------------------------------------
function onCF_FrameLoad()
{
    var mainIframe = GEBI("cf_iframe");

    var StaticIdle = GEBI("divStaticIdle");
    if (mainIframe)
    {
        mainIframe.setAttribute('height', '1160px');
        mainIframe.setAttribute('align', 'center');

        if (StaticIdle)
        {
            StaticIdle.style.display = "none";
        }

        mainIframe.style.display = "block";
    }
}
//-----------------------------------------------------------------
function GetFormElementIndexByID ( idForm, idElement )
{
    var iRetIndex = -1;

    try
    {
        var oForm = GEBI(idForm);

        if (oForm)
        {
            var colFormInputs = oForm.getElementsByTagName("INPUT");

            for (i = 0; i < colFormInputs.length; i++)
            {
                var oInput = colFormInputs[i];

                if (oInput.type == "text")
                {
                    var strFieldName = (oInput.name != "undefined" && oInput.name != "") ? oInput.name : oInput.id;

                    if (strFieldName.indexOf(idElement) != -1)
                    {
                        iRetIndex = i;
                        break;
                    }
                }
            }
        }
    }
    catch (e) { }

    return iRetIndex;
}
//-----------------------------------------------------------------
function LoadMainContent (strTargetLink )
{
    strMenuElement = 'aDeviceInfo';
    strQueryParams = strTargetLink;
    SwitchMenu(strMenuElement);
}
//-----------------------------------------------------------------
var TCouponRequestStruct = function()
{
    this.strProdName = null;
    this.strCurrentStore = null;
}
//-----------------------------------------------------------------
function ShowCoupons(idForm, strCurrentStore)
{
    var oProgress = new TAsyncProgress;

    oProgress.OnStart = OnShowCouponsStart;
    oProgress.OnProgress = OnShowCouponsProgress;
    oProgress.OnComplete = OnShowCouponsComplete;
    

    var oInput = GetFormFieldByPartName(idForm, "faProductName");

    var CouponRequestStruct = new TCouponRequestStruct;
    
    CouponRequestStruct.strCurrentStore = strCurrentStore;
    CouponRequestStruct.strProdName = ( oInput.className == "Watermark" ) ? "" : oInput.value;
    
    AsyncProgress.Params = CouponRequestStruct;

    oProgress.StartProgress();
}
//-----------------------------------------------------------------
function OnShowCouponsStart()
{
    try
    {
        var odivActualCoupons = GEBI("divActualCoupons");


        if (odivActualCoupons)
        {
            var strRet = _Default.GetActualCoupons(AsyncProgress.Params.strProdName, AsyncProgress.Params.strCurrentStore).value;
            odivActualCoupons.innerHTML = strRet;

            GEBI('aShowCoupons').disabled = false;
        }
        
    }
    catch (ex) { }

}
//-----------------------------------------------------------------
function OnShowCouponsProgress()
{
    try
    {
        var oTDStatus = GEBI("H3ProgressBarStatusString");
        var oTableIndicator = GEBI("tableProgressBarIndicator");
        var oTDContainer = GEBI("tdContainer");

        var strRet = _Default.GetProgress().value;

        if (oTDStatus)
        {
            oTDStatus.innerHTML = strRet.strStatusString + " " + strRet.iCurrentValue + "%";
        }
        if (strRet.iCurrentValue > 0)
        {
            if (oTableIndicator && oTDContainer)
            {
                oTDContainer.innerHTML = "&nbsp;";
                oTableIndicator.width = strRet.iCurrentValue + "%";
            }
        }

        if (strRet.psState == "1")
        {
            AsyncProgress.ProgressComplete = true;
        }
    }
    catch (ex) { }

}
//-----------------------------------------------------------------
function OnShowCouponsComplete()
{
    try
    {
        var oTargetTD = GEBI("divActualCoupons");

        var strRet = _Default.GetBuffer().value;

        oTargetTD.innerHTML = strRet;
    }
    catch (ex) { }

}
//-----------------------------------------------------------------
function ResizeCF_Frame()
{
    if (windowCellFireID)
    {
        var CF_Frame = windowCellFireID.document.getElementById("cf_iframe");

        if (CF_Frame)
        {
            CF_Frame.setAttribute('height', '1160px');
        }
    }
}
//-----------------------------------------------------------------
function ShowCouponImage ( idCoupon )
{
    try
    {
        var strCouponImage = _Default.ShowCouponImageInner(idCoupon, null,
                            function()
                            {
                                return false;
                            }
                            ).value;

        var oDiv = GEBI("idForm");
        var oDivFg = GEBI("idForeground");

        var oWindowMeasures = new TWindowMeasures();

        var iScrollTop = oWindowMeasures.GetScrollTop();

        oDivFg.style.top = iScrollTop + "px";

        oDivFg.innerHTML = strCouponImage;

        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";

        WindowResize();
    }
    catch ( e )
    {}
}
//-----------------------------------------------------------------
function GetNextValidItemIndex( strAction, iStartIndex, oTDCollection )
{
    var iNextValidIndex = iStartIndex;

    try
    {
        switch (strAction)
        {
            case "KEY_UP":
                {
                    var bFound = false;

                    for (i = iStartIndex-1; i >= 0; i--)
                    {
                        var oTD = oTDCollection[i];

                        if (oTD)
                        {
                            if (oTD.className == "Custom_List_Item")
                            {
                                iNextValidIndex = i;
                                bFound = true;
                                break;
                            }
                        }
                    }

                    if (!bFound)
                    {
                        for (i = oTDCollection.length; i >= iStartIndex; i--)
                        {
                            var oTD = oTDCollection[i];

                            if (oTD)
                            {
                                if (oTD.className == "Custom_List_Item")
                                {
                                    iNextValidIndex = i;
                                    bFound = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                break;

            case "KEY_DOWN":
                {
                    var bFound = false;
                    
                    for (i = iStartIndex + 1; i < oTDCollection.length; i++)
                    {
                        var oTD = oTDCollection[i];

                        if (oTD)
                        {
                            if (oTD.className == "Custom_List_Item")
                            {
                                iNextValidIndex = i;
                                bFound = true;
                                break;
                            }
                        }
                    }

                    if (!bFound)
                    {
                        for (i = 0; i < iStartIndex; i++)
                        {
                            var oTD = oTDCollection[i];

                            if (oTD)
                            {
                                if (oTD.className == "Custom_List_Item")
                                {
                                    iNextValidIndex = i;
                                    break;
                                }
                            }
                        }
                    }
                }
                break;

            case "KEY_PAGEDOWN":
                {
                    var bFound = false;

                    for (i = iStartIndex - 8; i < oTDCollection.length; i++)
                    {
                        var oTD = oTDCollection[i];

                        if (oTD)
                        {
                            if (oTD.className == "Custom_List_Item")
                            {
                                iNextValidIndex = i;
                                bFound = true;
                                break;
                            }
                        }
                    }

                    if (!bFound)
                    {
                        for (i = 0; i < iStartIndex; i++)
                        {
                            var oTD = oTDCollection[i];

                            if (oTD)
                            {
                                if (oTD.className == "Custom_List_Item")
                                {
                                    iNextValidIndex = i;
                                    break;
                                }
                            }
                        }
                    }
                }
                break;

            case "KEY_PAGEUP":
                {
                    var bFound = false;

                    for (i = iStartIndex + 8; i >= 0; i--)
                    {
                        var oTD = oTDCollection[i];

                        if (oTD)
                        {
                            if (oTD.className == "Custom_List_Item")
                            {
                                iNextValidIndex = i;
                                bFound = true;
                                break;
                            }
                        }
                    }

                    if (!bFound)
                    {
                        for (i = oTDCollection.length; i >= iStartIndex; i--)
                        {
                            var oTD = oTDCollection[i];

                            if (oTD)
                            {
                                if (oTD.className == "Custom_List_Item")
                                {
                                    iNextValidIndex = i;
                                    bFound = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                break;
        }
    }
    catch (e) { }
    
    return iNextValidIndex;

}
//-----------------------------------------------------------------
function HasAnyValidItem(oTDCollection)
{
    var bFound = false;

    for (i = 0; i < oTDCollection.length; i++)
    {
        var oTD = oTDCollection[i];

        if (oTD)
        {
            if (oTD.className == "Custom_List_Item")
            {
                iNextValidIndex = i;
                bFound = true;
                break;
            }
        }
    }

    return bFound;
}