var curCellClicked;
var curEditingRow;
var curEditingRowCells = new Array();
var curEditingGrid;
var lastRowSel;
var selectNextRow=false;

function GetTopParentId (ctrl)
{
    var controlIdArr = ctrl.id.split('_');
    var controlId = controlIdArr [0];    
    
    for (var i = 1; i < controlIdArr.length - 1; i ++)
        controlId += "_" + controlIdArr [i];
        
    return controlId;
}

function columnClicked(col)
{
    RestoreOldSelectedRow ();
    var controlId = GetTopParentId (col);
    GLWebLibrary.GlWebGrid.ColumnClicked(document.getElementById("sessionState").value, col.id, controlId, columnClicked_callback);  // asynchronous call
}

// This method will be called after the method has been executed
// and the result has been sent to the client.

function columnClicked_callback (res)
{
    if (res.value != null)
    {
        if (res.value.GridId == "sessionEnded")
        {
            window.location = res.value.NewHtml;
            return;
        }
        
        document.getElementById(res.value.GridId).innerHTML = res.value.NewHtml;
    }
}

function glWebGid_selectFirstRow (grid)
{
    var stateId = GetAttribute (grid, "state");
    
    var row = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowId");
    
    if (document.getElementById(stateId) != null)
    {
        row = document.getElementById(row);
        if (row != null)
        {
            SetStateValue (document.getElementById(stateId), "lastSelectedRowId", row.id);
            SetStateValue (document.getElementById(stateId), "lastSelectedRowClass", row.className);
            row.className = "selectedRow";
        }
    }
}

function glWebGid_selectNextRow (grid)
{
    var stateId = GetAttribute (grid, "state");
    var lastSelectedRowId = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowId");
    var row = 0;
    var className;
    
    if (document.getElementById(stateId) != null)
    {
        if (document.getElementById(lastSelectedRowId) != null)
        {
            className = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowClass");
            className = className.replace ("selectedRow", "");
            
            document.getElementById(lastSelectedRowId).className = className;

            row = document.getElementById(GetAttribute (document.getElementById(lastSelectedRowId), "nextRowId"));

            if (row != null)
            {
                SetStateValue (document.getElementById(stateId), "lastSelectedRowId", row.id);
                SetStateValue (document.getElementById(stateId), "lastSelectedRowClass", row.className);
                row.className = "selectedRow";
            }
        }
    }
}

function glWebGid_selectPrevRow (grid)
{
    var stateId = GetAttribute (grid, "state");
    var lastSelectedRowId = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowId");
    var row = 0;
    
    if (document.getElementById(stateId) != null)
    {
        if (document.getElementById(lastSelectedRowId) != null)
        {
            document.getElementById(lastSelectedRowId).className = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowClass");
            row = document.getElementById(GetAttribute (document.getElementById(lastSelectedRowId), "prevRowId"));

            if (row != null)
            {
                SetStateValue (document.getElementById(stateId), "lastSelectedRowId", row.id);
                SetStateValue (document.getElementById(stateId), "lastSelectedRowClass", row.className);
                row.className = "selectedRow";
            }
        }
    }
}


function glWebGid_applyChanges (grid)
{
    var saveId = document.getElementById(grid.id + "_save");
//    var tdId = document.getElementById(grid.id + "_panel");
    
    // if (tdId != null && tdId.style.visibility == "visible")
    if (IsEditing (curEditingRow))
    {
        onGlButtonEx_click (saveId);
        docModified = true;
    } 
}

function glWebGid_confirm (grid)
{
    var stateId = GetAttribute (grid, "state");
    var lastSelectedRowId = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowId");
    
    if (document.getElementById(lastSelectedRowId) != null)
    {
        rowClicked (document.getElementById(lastSelectedRowId));
    }
}

function rowClicked(row)
{
    if (IsEditing (row))
        return;

    var stateValue = "";
    var controlId = GetTopParentId (row);
    var stateId = GetAttribute (document.getElementById(controlId), "state");

    var isclicking = GetStateValueValue(document.getElementById(stateId), "buttonRowClicking");

    if (isclicking == 1) {
        SetStateValue(document.getElementById(stateId), "buttonRowClicking", 0);
        return;
    }
    
    if (curEditingGrid != null && curEditingGrid.id != controlId)
    {
        RestoreOldSelectedRow ();
        curEditingGrid = null;
    }
    
    var mdoc = GetAttribute (document.getElementById(controlId), "modifyDocumentOnClick");
    if (mdoc == "true")
    {
        docModified = true;
    }
    
    if (GetAttribute (document.getElementById(controlId), "multiselect") == "true")
    {
        if (row.className == "selectedRow")
        {
            lastRowSel = null;
            row.className = GetStateValueValue (document.getElementById(stateId), row.id + "oldClass");
            RemoveStateValue (document.getElementById(stateId), row.id + "oldClass");
        }
        else
        {
            SetStateValue (document.getElementById(stateId), row.id + "oldClass", row.className);
            row.className = "selectedRow";
            
            lastRowSel = row;
        }
    }
    else
    {
        if (row.className != "selectedRow" && document.getElementById(stateId) != null)
        {
            lastSelectedRowId = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowId");
            
            if (document.getElementById(lastSelectedRowId) != null)
            {
                document.getElementById(lastSelectedRowId).className = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowClass");
            }

            SetStateValue (document.getElementById(stateId), "lastSelectedRowId", row.id);
            SetStateValue (document.getElementById(stateId), "lastSelectedRowClass", row.className);
            row.className = "selectedRow";
        }

        if (GetAttribute (document.getElementById(controlId), "editable") == "true")
        {
             if (IsEditing (curEditingRow))
             {
                 var btnSave = document.getElementById(controlId + "_save");
                 
                 if (onGlButtonEx_click (btnSave))
                    EditRowWithoutPanel (row);
             }
             else
             {
                 while (curEditingRowCells.length > 0)
                    curEditingRowCells.pop ();

                EditRowWithoutPanel (row);
             }
        }
    }
    
    if (document.getElementById(stateId) != null)
        stateValue = document.getElementById(stateId).value;
    
    if (CallCustomFunction (document.getElementById(controlId), "customRowClicked"))
    {
        GLWebLibrary.GlWebGrid.RowClicked(document.getElementById("sessionState").value, stateValue, row.id, controlId, rowClicked_callback);  // asynchronous call
    }
}


function rowButtonClicked(row)
{
    if (IsEditing (row))
        return;


    var stateValue = "";
    var controlId = GetTopParentId (row);
    var stateId = GetAttribute(document.getElementById(controlId), "state");

    SetStateValue(document.getElementById(stateId), "buttonRowClicking", 1);
    
    if (curEditingGrid != null && curEditingGrid.id != controlId)
    {
        RestoreOldSelectedRow ();
        curEditingGrid = null;
    }
    
    var mdoc = GetAttribute (document.getElementById(controlId), "modifyDocumentOnClick");
    if (mdoc == "true")
    {
        docModified = true;
    }
    
    if (GetAttribute (document.getElementById(controlId), "multiselect") == "true")
    {
        if (row.className == "selectedRow")
        {
            lastRowSel = null;
            row.className = GetStateValueValue (document.getElementById(stateId), row.id + "oldClass");
            RemoveStateValue (document.getElementById(stateId), row.id + "oldClass");
        }
        else
        {
            SetStateValue (document.getElementById(stateId), row.id + "oldClass", row.className);
            row.className = "selectedRow";
            
            lastRowSel = row;
        }
    }
    else
    {
        if (row.className != "selectedRow" && document.getElementById(stateId) != null)
        {
            lastSelectedRowId = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowId");
            
            if (document.getElementById(lastSelectedRowId) != null)
            {
                document.getElementById(lastSelectedRowId).className = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowClass");
            }

            SetStateValue (document.getElementById(stateId), "lastSelectedRowId", row.id);
            SetStateValue (document.getElementById(stateId), "lastSelectedRowClass", row.className);
            row.className = "selectedRow";
        }

        
    }
    
    if (document.getElementById(stateId) != null)
        stateValue = document.getElementById(stateId).value;
            
    if (CallCustomFunction (document.getElementById(controlId), "customRowButtonClicked"))
    {
        GLWebLibrary.GlWebGrid.RowButtonClicked(document.getElementById("sessionState").value, stateValue, row.id, controlId, rowButtonClicked_callback);  // asynchronous call
    }
}

function rowClicked_callback (res)
{
    if (res.value != null && res.value.GridId == "sessionEnded")
    {
        window.location = res.value.NewHtml;
        return;
    }
        
    if (res.value != null && res.value.GridId != null)
    {
        if (document.getElementById(res.value.GridId).tagName.toLowerCase() == "input")
            document.getElementById(res.value.GridId).value = res.value.NewHtml;
        else
            document.getElementById(res.value.GridId).innerHTML = res.value.NewHtml;
    }

    if (res.value != null && res.value.ControlToUpdate != null && res.value.ControlToUpdate != "")
    {
        if (document.getElementById(res.value.ControlToUpdate).tagName.toLowerCase() == "input")
            document.getElementById(res.value.ControlToUpdate).value = res.value.ControlToUpdateNewHtml;
        else
            document.getElementById(res.value.ControlToUpdate).innerHTML = res.value.ControlToUpdateNewHtml;
    }
    
    if (res.value != null && res.value.ControlToUpdate2 != null && res.value.ControlToUpdate2 != "")
    {
        if (document.getElementById(res.value.ControlToUpdate2).tagName.toLowerCase() == "input")
            document.getElementById(res.value.ControlToUpdate2).value = res.value.ControlToUpdate2NewHtml;
        else
            document.getElementById(res.value.ControlToUpdate2).innerHTML = res.value.ControlToUpdate2NewHtml;
    }
    
    if (res.value != null && res.value.ControlToUpdate3 != null && res.value.ControlToUpdate3 != "")
    {
        if (document.getElementById(res.value.ControlToUpdate3).tagName.toLowerCase() == "input")
            document.getElementById(res.value.ControlToUpdate3).value = res.value.ControlToUpdate3NewHtml;
        else
            document.getElementById(res.value.ControlToUpdate3).innerHTML = res.value.ControlToUpdate3NewHtml;
    }
    
    if (res.value != null && res.value.ControlToUpdate4 != null && res.value.ControlToUpdate4 != "")
    {
        if (document.getElementById(res.value.ControlToUpdate4).tagName.toLowerCase() == "input")
            document.getElementById(res.value.ControlToUpdate4).value = res.value.ControlToUpdate4NewHtml;
        else
            document.getElementById(res.value.ControlToUpdate4).innerHTML = res.value.ControlToUpdate4NewHtml;
    }
    if (res.value != null && res.value.SourceGridId != null)
    {
        SetAttribute (document.getElementById(res.value.SourceGridId), "resSelectedValue", res.value.SelectedValue);
        CallCustomFunction (document.getElementById(res.value.SourceGridId), "customRowClickedCallback");
    }
}

function rowButtonClicked_callback (res)
{
    if (res.value != null && res.value.GridId == "sessionEnded")
    {
        window.location = res.value.NewHtml;
        return;
    }
        
    if (res.value != null && res.value.GridId != null)
    {
        if (document.getElementById(res.value.GridId).tagName.toLowerCase() == "input")
            document.getElementById(res.value.GridId).value = res.value.NewHtml;
        else
            document.getElementById(res.value.GridId).innerHTML = res.value.NewHtml;
    }

    if (res.value != null && res.value.ControlToUpdate != null && res.value.ControlToUpdate != "")
    {
        if (document.getElementById(res.value.ControlToUpdate).tagName.toLowerCase() == "input")
            document.getElementById(res.value.ControlToUpdate).value = res.value.ControlToUpdateNewHtml;
        else
            document.getElementById(res.value.ControlToUpdate).innerHTML = res.value.ControlToUpdateNewHtml;
    }
    
    if (res.value != null && res.value.ControlToUpdate2 != null && res.value.ControlToUpdate2 != "")
    {
        if (document.getElementById(res.value.ControlToUpdate2).tagName.toLowerCase() == "input")
            document.getElementById(res.value.ControlToUpdate2).value = res.value.ControlToUpdate2NewHtml;
        else
            document.getElementById(res.value.ControlToUpdate2).innerHTML = res.value.ControlToUpdate2NewHtml;
    }
    
    if (res.value != null && res.value.ControlToUpdate3 != null && res.value.ControlToUpdate3 != "")
    {
        if (document.getElementById(res.value.ControlToUpdate3).tagName.toLowerCase() == "input")
            document.getElementById(res.value.ControlToUpdate3).value = res.value.ControlToUpdate3NewHtml;
        else
            document.getElementById(res.value.ControlToUpdate3).innerHTML = res.value.ControlToUpdate3NewHtml;
    }
    
    if (res.value != null && res.value.ControlToUpdate4 != null && res.value.ControlToUpdate4 != "")
    {
        if (document.getElementById(res.value.ControlToUpdate4).tagName.toLowerCase() == "input")
            document.getElementById(res.value.ControlToUpdate4).value = res.value.ControlToUpdate4NewHtml;
        else
            document.getElementById(res.value.ControlToUpdate4).innerHTML = res.value.ControlToUpdate4NewHtml;
    }
    if (res.value != null && res.value.SourceGridId != null)
    {
        SetAttribute (document.getElementById(res.value.SourceGridId), "resSelectedValue", res.value.SelectedValue);
        CallCustomFunction (document.getElementById(res.value.SourceGridId), "customRowButtonClickedCallback");
    }
}

function IsEditing (row)
{
    if (row == null)
    {
        while (curEditingRowCells.length > 0)
            curEditingRowCells.pop ();
            
        return false;
    }
            
    var controlId = GetTopParentId (row);
    var tdId = document.getElementById(controlId + "_panel");

    // Se il parentId della riga è null è perchè probabilmente stavamo editando in un'altra pagina
    // quindi ripuliamo il tutto ed usciamo
    if (tdId == null)
    {
        while (curEditingRowCells.length > 0)
            curEditingRowCells.pop ();

        return false;
    }   
    
    var editElement = document.getElementById(tdId.id + "_c0");
        
    if (curEditingRow == row && editElement != null && editElement.style.visibility == "visible")
        return true;
    
    return false;
}

function EditRow (row)
{
    var controlId = GetTopParentId (row);
    var tdId = document.getElementById(controlId + "_panel");
    var editElement;
    var cell;
    var tdValueID;
    var tdWidths = new Array ();
    var index = 0;
    var grid = document.getElementById(controlId);
    
    if (IsEditing (row))
        return;

    var oldScrollPos = GetAttribute (grid.parent, "scrollPos");
    
    RestoreOldSelectedRow ();
    
    // Salviamo la riga per ripristinarla in seguito
    curEditingRow = row;
        
    for (var n = 0; n < row.childNodes.length; n ++)
    {
        tdWidths [n] = row.childNodes [n].offsetWidth;
    }
    
    var offset = 0;
    var elementToFocus = null;
    
    for (var n = 0; n < row.childNodes.length;)
    {
        // Salviamo le celle per ripristinarle in seguito
        curEditingRowCells.push (row.childNodes [0]);

        editElement = document.getElementById(tdId.id + "_c" + (curEditingRowCells.length - 1));
        editElement.style.width = (tdWidths [index] - 10) + "px";
        
        if (index > 0)
            offset += tdWidths [index - 1];
                                
        editElement.style.left = offset + "px";
        
        if (editElement.style.left == "0px")
            editElement.style.left = "3px";
        
        index ++;
     
        cell = row.childNodes [0].id;
        tdValueID = GetAttribute (document.getElementById(cell), "tdValueID");

        if (tdValueID == "")
        {
            SetValue (editElement, row.childNodes [0].innerHTML);

            if (elementToFocus == null && editElement.value != null)
            {
                elementToFocus = editElement;
            }
        }
        else
        {
            editElement.value = tdValueID;
            
            if (elementToFocus == null)
            {
                elementToFocus = editElement;
            }
        }
        
        row.removeChild (row.childNodes [0]);
    }
    
    // Ci cloniamo l'ultima cella
    var clonedCell = curEditingRowCells [0].cloneNode (true);
    
    // rimuoviamo il pannello di edit dalla griglia
    // document.getElementById(controlId).removeChild (tdId);
    tdId.parentNode.removeChild (tdId);

    // Settiamo la col span della cella a tutta riga
    SetAttribute (clonedCell, "colspan", curEditingRowCells.length, null);
    clonedCell.style.padding = "0";
    tdId.style.padding = "0";
    // Aggiungiamo la cella alla riga selezionata
    row.appendChild (clonedCell);
    // Puliamo il contenuto della cella
    clonedCell.innerHTML = "";
    // Aggiungiamo il pannello alla cella
    row.childNodes [0].appendChild (tdId);
    // Rendiamo visibile il pannello di edit e lo posizioniamo in maniera relativa alla cella
    tdId.style.visibility = "visible";
    tdId.style.position = "";
    
    if (elementToFocus != null)
    {
        elementToFocus.focus ();
    }
 }

function EditRowWithoutPanel (row)
{
    var controlId = GetTopParentId (row);
    var tdId = document.getElementById(controlId + "_panel");
    var editElement;
    var cell;
    var tdValueID;
    var tdWidths = new Array ();
    var index = 0;
    var grid = curEditingGrid = document.getElementById(controlId);

            
    if (IsEditing (row))
        return;
   
    RestoreOldSelectedRow ();
    
    // Salviamo la riga per ripristinarla in seguito
    curEditingRow = row;

    var offset = 0;
    var elementToFocus = null;
    
    for (var n = 0; n < row.childNodes.length; n ++)
    {
        if (tdId != null)
            editElement = document.getElementById(tdId.id + "_c" + n);
        else
            editElement = document.getElementById(controlId + "_panel_c" + n);

        if (editElement != null) {
            editElement.style.width = row.childNodes[n].offsetWidth - 10 + "px";

            editElement.style.visibility = "visible";
            editElement.style.position = "relative";
            editElement.style.fontSize = "1.0em";

            //        for (var cn = 0; cn < editElement.childNodes.length; cn ++)
            //        {
            //            if (editElement.childNodes [cn].id != null && editElement.childNodes [cn].id != "")
            //            {
            //                editElement.style.border = "1px solid black";
            //                editElement.childNodes [cn].style.top = "-1px";
            //            }
            //        }

            cell = row.childNodes[n].id;
            tdValueID = GetAttribute(document.getElementById(cell), "tdValueID");

            if (tdValueID == "") {
                SetValue(editElement, row.childNodes[n].innerHTML.replace(/<br>/g, "\n"));

                if (elementToFocus == null && editElement.value != null) {
                    elementToFocus = editElement;
                }
            }
            else {
                editElement.value = tdValueID.replace(/<br>/g, "\n");

                if (elementToFocus == null) {
                    elementToFocus = editElement;
                }
            }

            curEditingRowCells.push(row.childNodes[n].innerHTML);
            row.childNodes[n].innerHTML = "";
            row.childNodes[n].appendChild(editElement);
        }
    }

    if (tdId != null) {

        var etf = document.getElementById(tdId.id + "_c" + curCellClicked);

        if (etf != null && etf.value != null)
            etf.focus()
        else if (elementToFocus != null && (grid.parentNode == null || grid.parentNode.scrollLeft == 0)) {
            elementToFocus.focus();
        }
    }
}


function panelGridValuesApplied (ctrl)
{
    var editElement;
    var controlId = GetAttribute (ctrl, "gridID");
    var grid = document.getElementById (controlId);
   
    if (GetAttribute (grid, "doApplyViaAjax") != "true")
    {
        // var controlId = GetTopParentId (ctrl);
        var tdId = document.getElementById(controlId + "_panel");
        var cell;
        var tdValueID;
        
        RestoreOldSelectedRow ();

        if (curEditingRow == null)
            return;

        for (var n = 0; n < curEditingRow.childNodes.length; n ++)
        {
            cell = document.getElementById(curEditingRow.childNodes [n].id);
            
            editElement = document.getElementById(tdId.id + "_c" + n);
            tdValueID = GetAttribute (cell, "tdValueID");

            if (tdValueID != "")
            {
                cell.innerHTML = GetDDLText (editElement);
                SetAttribute (cell, "tdValueID", editElement.value);
            }
            else
            {
                if (editElement.value != null)
                {
                    cell.innerHTML = editElement.value.replace (/\n/g, "<br>");
                }
                else
                {
                  
                    cell.innerHTML = GetAttribute (editElement, "value");
                    if ((cell.innerHTML == "") || (cell.innerHTML == "null"))
                    {
                        if (GetAttribute (editElement, "ctrlType") == "GlCalendar")
                            cell.innerHTML = "//";
                        else    
                            cell.innerHTML = editElement.innerHTML;
                    }
                }
            }
        }
    }
    docModified = true;    
    CallCustomFunction (document.getElementById(controlId), "customPanelValueApplied");
}

function panelGridValuesAppliedFailed (ctrl)
{
    RestoreOldSelectedRow ();
    var controlId = GetAttribute (ctrl, "gridID");
    
    alert (GetAttribute (document.getElementById(controlId), "applyErrorMessage"));
//    var errorLabel = document.getElementById(controlId + "_panel_errorLabel");
//    var errorMessage = document.getElementById(controlId + "_panel_errorLabel_errorMessage");
//    
//    errorLabel.style.visibility = "visible";
//    errorLabel.style.position = "relative";
//    errorMessage.innerHTML = GetAttribute (document.getElementById(controlId), "applyErrorMessage");
}

function editCancelled (ctrl)
{
    RestoreOldSelectedRow ();
}

function RestoreOldSelectedRow ()
{
    if (curEditingRow != null && document.getElementById(curEditingRow.id) != null && curEditingRowCells.length > 0)
    {
        var editElement;
        var row = curEditingRow;
        var controlId = GetTopParentId (row);
        
        var tdId = document.getElementById(controlId + "_panel");

        for (var n = 0; n < row.childNodes.length; n ++)
        {
            editElement = document.getElementById(tdId.id + "_c" + n);
            editElement.style.visibility = "hidden";
            tdId.appendChild (editElement);

            row.childNodes [n].innerHTML = curEditingRowCells [n];
        }

        
//        if (row.childNodes.length > 0 && tdId != null)
//        {
//            tdId.parentNode.removeChild (tdId);
//            // row.childNodes [0].removeChild (tdId);
//            row.removeChild (row.childNodes [0]);

//            for (var n = 0; n < curEditingRowCells.length; n++)
//            {
//                row.appendChild (curEditingRowCells [n]);
//            }
//        }

//        if (tdId != null)
//        {
//            // Nascondiamo il pannello di edit e lo rendiamo figlio della griglia
//            tdId.style.visibility = "hidden";
//            tdId.style.position = "absolute";
//            document.getElementById(controlId).appendChild (tdId);
//            
//            // Rendiamo invisibile l'eventuale messaggio d'errore settato in precedenza
//            var errorLabel = document.getElementById(tdId.id + "_errorLabel");
//            
//            errorLabel.style.visibility = "hidden";
//            errorLabel.style.position = "absolute";
//        }
    }

    while (curEditingRowCells.length > 0)
        curEditingRowCells.pop ();
}


function glGridNewItem (btn)
{
    while (curEditingRowCells.length > 0)
        curEditingRowCells.pop ();
        
    rowClicked (document.getElementById(GetAttribute (btn, "newRowID")));
    
    var controlId = GetAttribute (btn, "gridID");
    var errorLabel = document.getElementById(controlId + "_errorLabelNew");
    
    errorLabel.style.visibility = "hidden";
    errorLabel.style.position = "absolute";
    
    docModified = true;
}

function glGridNewItemFailed (btn)
{
    RestoreOldSelectedRow ();

    var controlId = GetAttribute (btn, "gridID");
    var errorLabel = document.getElementById(controlId + "_errorLabelNew");
    var errorMessage = document.getElementById(controlId + "_errorLabelNew_errorMessage");
    
    errorLabel.style.visibility = "visible";
    errorLabel.style.position = "relative";
    errorMessage.innerHTML = GetAttribute (document.getElementById(controlId), "newErrorMessage");
}

function preDeleteItemGrid (btn)
{
    var message = GetAttribute (btn, "deleteWarningMessage");
    return confirm (message);
}

function panelGridItemDeleted (btn)
{
    while (curEditingRowCells.length > 0)
        curEditingRowCells.pop ();

    var controlId = GetAttribute (btn, "gridID");
    var stateId = GetAttribute (document.getElementById(controlId), "state");
    curEditingRow = null;
    SetStateValue (document.getElementById(stateId), "lastSelectedRowId", "");
    
    var res = GLWebLibrary.GlWebGrid.RowDeleted(document.getElementById("sessionState").value, controlId);
    if (res != null && res.value != null) 
    {
        var target = document.getElementById(res.value.TargetID);
        
        if (target != null)
            target.innerHTML = res.value.EndContent;
        
        target = document.getElementById(res.value.TargetID2);
        
        if (target != null)
            target.innerHTML = res.value.EndContent2;
    }
    
    docModified = true;
    
    CallCustomFunction (document.getElementById(controlId), "customItemDeleted");    
}

function panelGridItemDeletedFailed (btn)
{
    var controlId = GetAttribute (btn, "gridID");
    var errorLabel = document.getElementById(controlId + "_errorLabelNew");
    var errorMessage = document.getElementById(controlId + "_errorLabelNew_errorMessage");
    
    errorLabel.style.visibility = "visible";
    errorLabel.style.position = "relative";
    errorMessage.innerHTML = GetAttribute (document.getElementById(controlId), "deleteErrorMessage");
}

function GetRowsCount (grid)
{
    return parseInt (GetAttribute (grid, "rowsCount"), 10);
}

function gridctrl_onKeyDown(gridId, event, ctrl)
{
    if (GetAttribute (ctrl, "textType") == "textarea")
    {
        return; 
    }
    
    if (GetAttribute (ctrl, "textType") == "glnud")
        glnud_onKeyDown (ctrl, event);
        
    if (event.keyCode.toString () == "13" && !event.ctrlKey) // invio
    {
        glWebGid_applyChanges (document.getElementById(gridId));
      
        var stateId = GetAttribute (document.getElementById(gridId), "state");
        var lastSelectedRowId = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowId");
        var row = 0;
    
        if (document.getElementById(stateId) != null)
        {
            if (document.getElementById(lastSelectedRowId) != null)
            {
                document.getElementById(lastSelectedRowId).className = GetStateValueValue (document.getElementById(stateId), "lastSelectedRowClass");
               
                if (selectNextRow)
                {
                    row = GetAttribute (document.getElementById(lastSelectedRowId), "nextRowId");

                    if (row == lastSelectedRowId || row == "" || row == null)
                        RestoreOldSelectedRow ();
                    else    
                        rowClicked (document.getElementById(row));
                }
                else
                {
                    RestoreOldSelectedRow ();
                }
            }
        }
    }
    else if (event.keyCode.toString () == "27") // escape
    {
        RestoreOldSelectedRow ();
    }
}

function btncellclicked (cell, progr_cell)
{

    try {
        var prow = document.getElementById(cell.parentElement.parentElement.id);
    }
    catch (e) {
        var prow = document.getElementById(cell.parentNode.parentNode.id);
    }
    
    rowButtonClicked (prow);
}

function cellClicked (cell, progr_cell)
{
    curCellClicked = progr_cell;
}

function webGridCaption_onclick (gridId)
{
    var grid = document.getElementById (gridId);
    grid.style.position = "absolute";
    grid.style.visibility = "hidden";
}
