﻿var Page_GridViewVer = "111";
var SelectedRow =null;
function GridViewCheckAll(oCheckbox,gsdGridView)
{
   // var gsdGridView = document.getElementById(GridViewID)
    for(i = 1;i < gsdGridView.rows.length; i++)
    {
        var inputArray = gsdGridView.rows[i].getElementsByTagName('INPUT');
        for(var j=0;j<inputArray.length;j++)
        {
            if(inputArray[j].type=='checkbox')
            {
                if(inputArray[j].id.indexOf('ItemCheckBox',0)>-1)
                {
                    inputArray[j].checked =oCheckbox.checked; 
                }
            }  
        }
    }
 }

function SetCheckBox(chkName, value)
{
    if (value == "1" || value == "true") 
    {
      chkName.checked = true; 
    }
    else
    {
        chkName.checked = false;
    }
}

function SetCheckBoxList(chkName, valueList)
{
    var chks = document.getElementById(chkName).getElementsByTagName("input");  
    for(var i = 0;i<chks.length;i++)
    {  
        if(chks[i].type == "checkbox")
        {
            chks[i].checked = false;        
        }
     }
    var list = valueList.split(',');  
    for(var j in list)
    {
        if (list[j]=="") 
        {
            continue;
        }
        for(var i = 0;i<chks.length;i++)
        {  
            if(chks[i].type == "checkbox")
            {          
                var lbl=chks[i].nextSibling;
                if (lbl.innerText == list[j]) 
                {
                    chks[i].checked = true;
                }                   
            }
         }
    } 
     
}

function SetRadioList(rdlName, value)
{
    var rdl = rdlName.getElementsByTagName("input");  
    for(var i = 0;i<rdl.length;i++)
    {  
        if(rdl[i].type == "radio")
        {
            rdl[i].checked = false;        
        }
     }  
    for(var i = 0;i<rdl.length;i++)
    {  
        if(rdl[i].type == "radio")
        {          
            var lbl=rdl[i].nextSibling;
            if (lbl.innerText == value) 
            {
                rdl[i].checked = true;
            }                   
        }
     }       
}

function SetDropdownList(objSelect, objItemText) 
{  
     
    var Isselected = false;             
    for (var i = 0; i < objSelect.options.length; i++) 
    {        
        if (objSelect.options[i].text == objItemText) 
        {        
            objSelect.options[i].selected = true;
            objSelect.fireEvent('onchange'); 
            Isselected = true;             
            break;        
        }  
    }
    if (!Isselected) 
    {
        if(objSelect.options[0] != null)
        objSelect.options[0].selected = true;   
    }      
}     


function GetValueFromList(KeyValueList, key)
{
    var  kvList = KeyValueList.split(';');
    for(var i in  kvList)
    {
        var Name = kvList[i].split(':')[0];
        var Value = kvList[i].split(':')[1];  
        if ( Name.toLowerCase() == key.toLowerCase()) 
        {
             return Value;
        }
    }
 }
function  Trim(str)  
{
   if( typeof(str)!= "undefined")
    return   str.replace(/(^\s*)|(\s*$)/g,"");
    else
     return "";
}   

      
function SelectRowToText(rowEl,gsdGridView)
{
    //var gsdGridView = document.getElementById(GridViewID);
    if (SelectedRow != null) 
    {
        SelectedRow.runtimeStyle.backgroundColor=SelectedRow.style.backgroundColor;
        SelectedRow.runtimeStyle.fontWeight =SelectedRow.style.fontWeight;
    }
    var id =   gsdGridView.id;
    var strKey = "hfPrimaryKey";
    var last = id.substr(id.length-1,1);
    if (parseInt(last)>1) 
    {
        strKey += last;
    }
    var ColumnInfo = document.getElementById(id+'ColumnInfo');
    var  hfPrimaryKey =  document.getElementById(strKey);
    if(hfPrimaryKey==null) return;
    if (hfPrimaryKey!=null) {
             hfPrimaryKey.value = rowEl.PrimaryKey;
        }
    var ColumArray = ColumnInfo.value.split(';');
  
    for(var j=0;j<ColumArray.length;j++)
    {
        var ColumIndex =ColumArray[j].split(':')[0];
        var ColumName =ColumArray[j].split(':')[1];
        var txt = null;
        if (ColumIndex=="") 
        {
            continue;         
        }
        if ( typeof(gsdGridView.FieldToContorl) != "undefined" ) 
        {
            var controlName = GetValueFromList(gsdGridView.FieldToContorl, ColumName);
            txt = document.getElementById(controlName);
        }
        if (txt == null) 
        {
            txt = document.getElementById('txt'+ColumName);
        }
       
        var value = "";
        if (ColumIndex=="-1") 
        {
            value = GetValueFromList(hfPrimaryKey.value,ColumName);
        }
        else
          value = gsdGridView.rows[rowEl.rowIndex].cells[ColumIndex].innerText;
         value = Trim(value);
        if(txt!=null) 
        {
            txt.value =  value;
         }
         else
         {
            var cbl =  document.getElementById('cbl'+ColumName);
            if (cbl != null) 
            {
                SetCheckBoxList('cbl'+ColumName, value);
            }
            else
            {
                var ddl =  document.getElementById('ddl'+ColumName);
                if (ddl != null) 
                {
                    SetDropdownList(ddl, value);
                }
                else
                {
                    var rbl =  document.getElementById('rbl'+ColumName);
                    if (rbl != null) 
                    {
                        SetRadioList(rbl, value);
                    }
                    else
                    {
                        var lbl = document.getElementById('lbl'+ColumName);
                        if (lbl != null) 
                        {
                           lbl.innerText =value;
                        }
                         else
                        {
                            var cbx =  document.getElementById('cbx'+ColumName);
                            if (cbx != null) 
                            {
                                SetCheckBox(cbx, value);
                            }
                            
                        }
                        
                    }
                }
            }
            
         }
    }
    if (typeof(gsdGridView.RemarkField) != "undefined" ) 
    {
            txt = document.getElementById("txt" + gsdGridView.RemarkField);
            if(txt != null)
            txt.value = rowEl.Remark;
     }
    for(i = 1;i < gsdGridView.rows.length; i++)
    {
        GridViewSetRowRadio(gsdGridView.rows[i],false);   
    }
    GridViewSetRowRadio(rowEl,true); 
    SelectedRow = rowEl;
    rowEl.runtimeStyle.backgroundColor='#FFFFAA';
    rowEl.runtimeStyle.fontWeight ='bold';
    
 }
 
function GridViewSetRowRadio(rowEl,value)
{
    var inputArray = rowEl.getElementsByTagName("INPUT")
    for(var j=0;j<inputArray.length;j++)
    { 
        if(inputArray[j].type=='radio')
        {
            if(inputArray[j].id.indexOf('ItemRadio',0)>-1)
            {
                inputArray[j].checked = value; 
            }
        }  
    }
}
 
function GridViewRadioAll(oRadiobox,gsdGridView)
{
   // var gsdGridView = document.getElementById(GridViewID);
    var OriValue = oRadiobox.checked;
    for(i = 1;i < gsdGridView.rows.length; i++)
    {
        GridViewSetRowRadio(gsdGridView.rows[i],!OriValue);   
    }
     oRadiobox.checked = OriValue;
}
                             
var sri ;
function funcSR(rowEl)
{
    if(event.srcElement.tagName!='A')
    {
        if(sri==rowEl.rowIndex)
        {
            rowEl.runtimeStyle.backgroundColor=rowEl.style.backgroundColor;
            rowEl.runtimeStyle.color=rowEl.style.color;
            sri=-1;
            setLinkClass(rowEl,false);
         }
        else 
        {
            if(sri>-1)
            {
                var oldRow=rowEl.parentElement.rows[sri];
                oldRow.runtimeStyle.backgroundColor=oldRow.style.backgroundColor;
                oldRow.runtimeStyle.color=oldRow.style.color;
                setLinkClass(oldRow,false);
             }
             rowEl.runtimeStyle.backgroundColor='highlight';
             rowEl.runtimeStyle.color='highlighttext';
             sri=rowEl.rowIndex;
             setLinkClass(rowEl,true);
         }
     }
}

function GridViewMout(rowEl)
{
    if(SelectedRow != rowEl)
    {
        rowEl.runtimeStyle.backgroundColor=rowEl.style.backgroundColor;
    }
}

function GridViewMover(rowEl)
{ 
    if(SelectedRow != rowEl)
    {
        rowEl.runtimeStyle.backgroundColor='#d3fcd7';
        //rowEl.runtimeStyle.cursor="hand";
    }
}

function setLinkClass(rowEl,isSelected)
{
    for(i=0;i<rowEl.cells.length;i++)
    {
        for(j=0;j<rowEl.cells[i].children.length;j++)
        {
            if(rowEl.cells[i].children[j].tagName=="A")
            {
                if(isSelected)
                {
                    rowEl.cells[i].children[j].className="whitelink";
                }
                else
                {
                    rowEl.cells[i].children[j].className="";
                }
            }
        }
    }
}



