var RowMenuInterval;

var RowmenuTime = 4;

var CurrentRowIndex = null;
var CurrentRow = null;

var MenuClick=false;

var NextRowIndex = null;
var NextRow = null;
var NextArray=null;
var NextIndex=null;

var IndexValue=null;

var RowMenuID             = 'ctl00_RowMenuTable';
var RowMenuTextLinkName   = 'RowMenuTextLink';
var RowMenuImageLinkName  = 'RowMenuImageLink';
var RowMenuTextID         = 'ctl00_RowMenuTextTable';
var RowMenuDisableByIndexID = 'ctl00_RowMenuDisableByIndex';
var RowMenuTextHoverClass = "RowMenuTextHover";
var RowMenuTextClass      = "RowMenuText";
var SELECTED              = "SELECTED";
var KEYVALUEIDDELIMITER   = "#";

var RowMenu =null;
var MenuHeight = null;
var LeftOffset = 16;
var posLeft =  1+LeftOffset; //pRow.offsetLeft +



function MouseoverRowMenu(pObject) { window.clearTimeout(RowMenuInterval);}
function MouseoutRowMenu(pObject) { RowMenuInterval = window.setTimeout("HideRowMenu();", 0.5 * 1000 ); }

function GetRowKeyValueFromRow(pRow)
{
      return pRow.id;  
}

function UNSelectRowsAll()
{
   SelectRowsAll(GVLISTID,false);
}

/* SelectRowsAll
// <remarks> Fehler, josp, 02.06.2009, "Wenn alle Zeilen ausgewählt oder abgewählt werden wurde der Style der Fusszeile überschrieben. Diese wird nun anhand des Css-Klassennamens identifiziert." </remarks>
*/
function SelectRowsAll(pTableID,pSelect,pSelectionClear)
{
    var Table = document.getElementById(pTableID);
    if (!Table) Table = document.getElementById(IDPRAEFIX + pTableID);
    if (!Table) return;
    
    var Tbody = Table.childNodes[1]; 
    if ( isIE() ) Tbody = Table.childNodes[0];
    var c = 0;
   
    for ( var i = 0; i < Tbody.childNodes.length; i++)
    {
        var row = Tbody.childNodes[i];
        if ((row.tagName == "TR") && (row.cells[0].tagName == "TD") && (row.className != "GridViewFooterStyle"))
        {
            c++;
            
            var RowKeyValue = GetRowKeyValueFromRow(row);
            
            if(pSelectionClear)  SetSelectedRowKeyValue(row, RowKeyValue, pSelect); // if (RowKeyValue != null)
            
            if (pSelect)
            {
              SetRowStyle(row,ROWSELECTEDSTYLE);
            }
            else
            {
                if (row.className == ROWSELECTEDSTYLE|row.className ==ROWAKTIVSTYLE)
                {
                   if(GetSelectedRowKeyValue(row)!=SELECTED)
                  { 
                        if ( c % 2 == 0 ) 
                            SetRowStyle(row,ROWASTYLE);
                        else 
                            SetRowStyle(row,ROWSTYLE);
                   } 
                   else
                            SetRowStyle(row,ROWSELECTEDSTYLE);
                } 
            }
        }
    }
}

function GetSelectedRowIDs(pTableID)
{
    // Erstellt ein Array mit selectierten ID
   var IDs = new Array();
   var Table = document.getElementById(pTableID);
    if (!Table) Table = document.getElementById(IDPRAEFIX + pTableID);
   if (Table)
   {  
        for (var i=0; i < Table.rows.length; i++)
        {
            var row =  Table.rows[i];
            var KeyValueID = Table.id + KEYVALUEIDDELIMITER + row.id;
            if (row.getAttribute('selected') == 'true')
                IDs.push(row.id);
//            var hf = document.getElementById(KeyValueID); 
//            if (hf) 
//           {
//                 if (hf.value == SELECTED) IDs.push(row.id);
//           }  
        }  
   }
   return IDs; 
}

function GetSelectedRowCellValue(pTableID,pSpallte)
{
    // Erstellt ein Array mit selectierten ID
    
   var IDs = new Array();
   var Table = document.getElementById(pTableID);
    if (!Table) Table = document.getElementById(IDPRAEFIX + pTableID);
   if (Table)
   {  
        for (var i=0; i < Table.rows.length; i++)
        {
            var row =  Table.rows[i];
            var KeyValueID = Table.id + KEYVALUEIDDELIMITER + row.id;
            if (row.getAttribute('selected') == 'true')
                IDs.push(row.cells[pSpallte].innerText);
//            var hf = document.getElementById(KeyValueID); 
//            if (hf) 
//           {
//                 if (hf.value == SELECTED) 
//                 {
//                 
//                    IDs.push(Table.rows[i].cells[pSpallte].innerText);
//                 }
//           }  
        }  
   }
   return IDs; 
}

function SelectRowByRowIndex(pTableID,pRowIndex)
{
   var Table = document.getElementById(pTableID);
   if (!Table) Table = document.getElementById(IDPRAEFIX + pTableID);
   if(Table)
  {  
    var Row = Table.rows[pRowIndex];
    if (typeof (Row) != 'undefined')
        if (Row != null) {
        SetRowSelected(Row, pRowIndex);
        SetSelectedRowKeyValue(Row, Row.id, true);
        return Row;
    }
   }
   return null;
}

function SelectRowByKeyValue(pTableID, pValue) {
    SelectRowsAll(pTableID, false, true);
    var Table = document.getElementById(pTableID);
    if (!Table) Table = document.getElementById(IDPRAEFIX + pTableID);
    if (Table) {
        for (var i = 0; i < Table.rows.length; i++) {
            var Row = Table.rows[i];
            if (typeof (Row) != 'undefined')
                if (Row != null) if(Row.id==pValue.toString()) {
                //Row.click();
                SetRowSelected(Row, i);
                SetSelectedRowKeyValue(Row, Row.id, true);
                break;
            }
        }
    }

}
function getHF(pRow) {
    var TableID = pRow.parentNode.parentNode.id;
    var KeyValueID = TableID + KEYVALUEIDDELIMITER + pRow.id;//KeyValue;
    for (var i = 0; i < pRow.cells[0].childNodes.length; i++) {
        if (pRow.cells[0].childNodes[i].id == KeyValueID)
            return pRow.cells[0].childNodes[i];
    }
}

function SetSelectedRowKeyValue(pRow, pRowKeyValue, pSelect)
{
   var TableID = pRow.parentNode.parentNode.id;
   var KeyValueID = TableID + KEYVALUEIDDELIMITER + pRow.id;//KeyValue;

   //   var hf = document.getElementById(KeyValueID);
   var hf = getHF(pRow);
   var cl = pRow.cells[0];
   pRow.setAttribute('selected', pSelect.toString());
   if ( (!hf) && (pSelect) ) { 
       var hf = document.createElement("input");
       hf.type = "hidden";
       hf.name = KeyValueID;
       hf.id   = KeyValueID;
       cl.appendChild(hf);
   }
   if ( (hf) &&  (pSelect) ) hf.value = SELECTED;
   if ( (hf) && (!pSelect) ) hf.value = "";  
}

function GetSelectedRowKeyValue(pRow)
{
   if(!pRow)return "";
   if(!pRow.parentNode)return "";
   if(!pRow.parentNode)return "";
   if(!pRow.parentNode.parentNode)return "";
   var TableID = pRow.parentNode.parentNode.id;
   var KeyValueID = TableID + KEYVALUEIDDELIMITER + GetRowKeyValueFromRow(pRow);

   if (pRow.getAttribute('selected') == 'true')
       return SELECTED;
   return "";
//   var hf = document.getElementById(KeyValueID);
//   var cl = pRow.cells[0];
//   
//   if (hf) return hf.value;
//   else   return "";
}

var DisabledRowAktion = false;

function onClickRow(pRow, pRowIndex, pRowKeyValue, pMultiSelect) {
    if (DisabledRowAktion) {DisabledRowAktion = false;return; }
    if(MenuClick)
        MenuClick=false;
    else
   { 
        var currentselection=GetSelectedRowKeyValue(pRow);
        if (!pMultiSelect) 
               SelectRowsAll(pRow.parentNode.parentNode.id, false,true);
        var RowIndex = parseInt(pRowIndex,10);
        
       if(currentselection!=SELECTED) 
      { 
            SetRowSelected(pRow, pRowIndex);
            SetSelectedRowKeyValue(pRow,GetRowKeyValueFromRow(pRow),true)
       }
       else if(pMultiSelect)
      { 
           SetSelectedRowKeyValue(pRow,GetRowKeyValueFromRow(pRow),false)
           if ( RowIndex % 2 != 0 ) 
             SetRowStyle(pRow,ROWASTYLE); 
           else
             SetRowStyle(pRow,ROWSTYLE);
      } 
   } 
   if(typeof(onClickRow_Custom)!="undefined")onClickRow_Custom(pRow);
}

function onClickRowSel(pRow, pRowIndex, pRowKeyValue,pArray)
{
    DoShowRowMenu(pRow,pRowKeyValue,pRowIndex,pArray);
    onClickRow(pRow, pRowIndex, pRowKeyValue);
   return false; 
}


function SetRowStyle(pRow, pClassName)
{
   if(pRow)pRow.className = pClassName;
}

function SetRowUnSelected(pRow, pRowIndex)
{
   if(GetSelectedRowKeyValue(pRow)!=SELECTED)
  { 
       var RowIndex = parseInt(pRowIndex,10);
       if ( RowIndex % 2 != 0 ) 
         SetRowStyle(pRow,ROWASTYLE); 
       else
         SetRowStyle(pRow,ROWSTYLE);
  } 
   else
            SetRowStyle(pRow,ROWSELECTEDSTYLE);
}


function SetRowSelected(pRow, pRowIndex)
{
   if(pRow)
    if(pRow.className!=ROWSELECTEDSTYLE);
       SetRowStyle(pRow,ROWSELECTEDSTYLE); 
}

function SetRowAktiv(pRow, pRowIndex)
{
   if(pRow)
    if(pRow.className!=ROWAKTIVSTYLE);
       SetRowStyle(pRow,ROWAKTIVSTYLE); 
}

  
function HideRowMenu()
{
   if(RowMenu)
  { 
       if(NextRow==null) 
       { 
            RowMenu.style.visibility = "hidden";
            RowMenu.style.top = '1000px';
            RowMenu.style.left = '1000px';
            seteckposition(-1000,-1000,1);
       } 
       window.clearTimeout(RowMenuInterval);
       SetRowUnSelected(CurrentRow,CurrentRowIndex);
       var checkrow=CurrentRow; 
       CurrentRow=null;
       CurrentRowIndex=null; 
       if(NextRow)if(NextRow!=checkrow)DoShowRowMenu(NextRow, NextIndex, NextRowIndex, NextArray);
  } 
}

function RowMenuCellUnHover(pCell, pRowIndex) 
{
    NextRow=null;
    NextRowIndex=null; 
    NextArray=null;
    NextIndex=null;
    var RowIndex = parseInt(pRowIndex,10);
    var Row = pCell.parentNode;
    
    pCell.className = ROWMENUCELLSTYLE;
//    SetRowUnSelected(Row,RowIndex);
}

function ShowRowMenu(pCell, pIndex, pRowIndex, pColumns)
{   
    var RowIndex = parseInt(pRowIndex,10);
    var Row = pCell.parentNode;
//    pCell.className = ROWMENUCELLHOVERSTYLE;
    if(CurrentRow!=null&false)           
   {                                                                   // erst nah dem schliessen naechste aufmachen
        NextRowIndex = pRowIndex;
        NextRow = Row;
        NextArray=pColumns;
        NextIndex=pIndex;
    }
   else 
        DoShowRowMenu(Row, pIndex, pRowIndex, pColumns);
}

function ShowRowMenuClick(pCell, pIndex, pRowIndex, pColumns)
{   
    MenuClick=true;
    var RowIndex = parseInt(pRowIndex,10);
    var Row = pCell.parentNode;
    
//    pCell.className = ROWMENUCELLHOVERSTYLE;

    NextRowIndex = null;
    NextRow = null;
    NextArray=null;
    NextIndex=null;
    DoShowRowMenu(Row, pIndex, pRowIndex, pColumns);
}

function DoShowRowMenu(pRow, pIndex, pRowIndex, pColumns)
{   
    window.clearTimeout(RowMenuInterval);
    RowMenuInterval = window.setTimeout("HideRowMenu();", RowmenuTime * 1000); 

    var RowIndex = parseInt(pRowIndex,10);
    
    SelectRowsAll(pRow.parentNode.parentNode.id,false,false);

    SetRowAktiv(pRow,RowIndex);//SetRowSelected(pRow,RowIndex);
    
    CurrentRowIndex = RowIndex;
    CurrentRow = pRow;

    if(RowMenu==null)
   { 
        RowMenu = document.getElementById(RowMenuID);
        MenuHeight = (RowMenu.rows.length * ROWMENUROWHEIGHT);
   } 
   
   IndexValue=pIndex;
   
   // Hiddenfield fuer Refresh setzen, zum markieren der selektierten Zeile     
   var civ = document.getElementById(IDPRAEFIX + 'hdfCurrentIndexValue');
   if (civ) civ.value = IndexValue;
   //
   
   var posTop  = 0;
   var windowHeight = GetWindowHeight();

    ROWposition=findPos(pRow);
    if(ROWposition[1]+GRIDROWHEIGHT+MenuHeight>windowHeight)
    { 
        posTop= ROWposition[1]-MenuHeight+1
        seteckposition(ROWposition[1]+3,posLeft,-1)
    }
    else
   { 
        posTop=ROWposition[1]+GRIDROWHEIGHT-2;
        seteckposition(posTop,posLeft,1)
    }
        
    RowMenu.style.top = posTop + 'px';
    RowMenu.style.left = posLeft + 'px';
    RowMenu.style.visibility = "visible";
   
   DisableRowMenuByIndex(IndexValue); 

}

function DisableRowMenuByIndex(pIndex)
{
     var IndexTable = document.getElementById(RowMenuDisableByIndexID);
     
     if (IndexTable) 
     {  
         for (var i = 0; i < RowMenu.rows.length; i++)
         {
             for (var c = 0; c < RowMenu.rows[i].cells.length; c++)
             {
                    var cell = RowMenu.rows[i].cells[c];
                    var a = cell.firstChild;
                    
                    if (a != null && a.name == RowMenuTextLinkName) 
                    {
                        var imga = cell.previousSibling.firstChild;
                        
                        if (imga == null) imga = cell.previousSibling.previousSibling.firstChild;
                        
                        var Disabled = IsDisabledByIndex(pIndex,a.innerText);
                       
                        if (Disabled)
                        {
                            a.style.color = 'gray'; 
                            a.href = "javascript:noop();";
                            imga.href = a.href;  
                        }    
                        else
                        {
                           a.style.color = 'black';  
                           a.href = GetHrefByText(a.innerText);  
                           imga.href = a.href;
                        }   
                    }  
             }
         }  
     } 
}

function GetHrefByText(pText)
{
    var TextTable = document.getElementById(RowMenuTextID);
    if (TextTable) 
    {
          for (var i = 0; i < TextTable.rows.length; i++)
         {
              var Text = TextTable.rows[i].cells[0].innerText;
              var Href = TextTable.rows[i].cells[1].innerText;
              if (Text == pText) 
              { 
                 if (Href.indexOf('javascript:') < 0) return 'javascript:' + Href; else return Href;
              } 
         }
    } 
    return 'javascript:noop();'; 
}

function IsDisabledByIndex(pIndex,pText)
{
     var IndexTable = document.getElementById(RowMenuDisableByIndexID);
     
     if (IndexTable) 
     {
              for (var i = 0; i < IndexTable.rows.length ; i++)
             {
                    var Index = IndexTable.rows[i].cells[0].innerText;
                    var Text = IndexTable.rows[i].cells[1].innerText;
                    var Disable = IndexTable.rows[i].cells[2].innerText;
                    
                    if (Index == pIndex && Text == pText)
                    {
                            if ( Disable == '0') return false; else return true;
                    }
             }  
     } 
    
    return false;  
}

function seteckposition(pMenutop,pMenuleft,pDir)
{
    if(MenuHeight>35)
   { 
        var fullheight=0;
        for(var eckind=1;eckind<14;eckind++)
       {
            var eckelement=document.getElementById("Eck"+eckind);
            var eckheigth=1;
            if(eckind>5)eckheigth=2;
            if(eckind>7)eckheigth=3;
            if(eckind>8)eckheigth=4;
            if(eckind>10)eckheigth=5;
//            if(eckind>11)eckheigth=5;
            fullheight+=eckheigth;
            if (eckelement) eckelement.style.borderLeft="#c0c0c0 1px solid"; else return;
            if(pDir>0)
            {
                if(eckind==1)
                {
                    eckelement.style.top=pMenutop+fullheight-1+"px";
                    eckelement.style.height=eckheigth+"px";
                    eckelement.style.borderTop="#c0c0c0 1px solid";
                    eckelement.style.borderBottom="";
                }
                else
                {
                    eckelement.style.top=pMenutop+fullheight-1+"px";
                    eckelement.style.height=eckheigth+1+"px";
                }
                eckelement.style.zIndex=300+eckind+"";
                eckelement.style.left=pMenuleft-14+eckind+"px";
                eckelement.style.width=14-eckind+"px";
           } 
           else
           {
                if(eckind==1)
                {
                    eckelement.style.top=pMenutop-fullheight-eckheigth+"px";
                    eckelement.style.height=eckheigth+"px";
                    eckelement.style.borderBottom="#c0c0c0 1px solid";
                    eckelement.style.borderTop="";
                }
                else
                {
                    eckelement.style.top=pMenutop-fullheight-eckheigth+"px";
                    eckelement.style.height=eckheigth+1+"px";
                }
                eckelement.style.zIndex=300+eckind+"";
                eckelement.style.left=pMenuleft-14+eckind+"px";
                eckelement.style.width=14-eckind+"px";
           }
       }
   } 
}

function HoverRowMenu(pObject,pHover,pNext)
{   
    var o = pObject;
    if ( pNext ) { if ( isIE() ) { o = pObject.nextSibling } else { o = pObject.nextSibling.nextSibling; } }
    if ( pHover ) { o.className=RowMenuTextHoverClass; } else { o.className=RowMenuTextClass; }

//   if ( o.id.indexOf('MAXROWCOUNTMENU') < 0  ) { SetRowSelected(CurrentRow,CurrentRowIndex); }
}




