function ChangeCheckBoxState(id, checkState)
{
  var cb = document.getElementById(id);
  if (cb !== null)
     cb.checked = checkState;
}

function ChangeAllCheckBoxStates(headerCheckBox, checkBoxList)
{
  // Toggles through all of the checkboxes defined in the checkBoxList array
  // and updates their value to the checkState input parameter
  if (headerCheckBox === null)
    return;
    
  if (checkBoxList !== null)
  {
     for (var i = 0; i < checkBoxList.length; i++)
        ChangeCheckBoxState(checkBoxList[i], headerCheckBox.checked);
  }
}

function ChangeHeaderAsNeeded(headerCheckBoxId, checkBoxList)
{
    // Whenever a checkbox in the GridView is toggled, we need to
    // check the Header checkbox if ALL of the GridView checkboxes are
    // checked, and uncheck it otherwise
    if (checkBoxList !== null)
    {
        // check to see if all other checkboxes are checked
        for (var i = 1; i < checkBoxList.length; i++)
        {
            var cb = document.getElementById(checkBoxList[i]);
            if (!cb.checked)
            {
                // Whoops, there is an unchecked checkbox, make sure
                // that the header checkbox is unchecked
                ChangeCheckBoxState(headerCheckBoxId, false);
                return;
            }
        }
        
        // If we reach here, ALL GridView checkboxes are checked
        ChangeCheckBoxState(headerCheckBoxId, true);
    }
}

function TglRow(ctl)
{
	var row = ctl.parentNode.parentNode;
	var tbl = row.parentNode;
	var crow = tbl.rows[row.rowIndex + 1];
	var ihExp = ctl.parentNode.getElementsByTagName('input').item(0);

	tbl = tbl.parentNode;

	//var expandClass = tbl.attributes.getNamedItem('expandClass').value;
	//var collapseClass = tbl.attributes.getNamedItem('collapseClass').value;
	var expandText = tbl.attributes.getNamedItem('expandText').value;
	var collapseText = tbl.attributes.getNamedItem('collapseText').value;

	if (crow.style.display == 'none')
	{
		var i = 1;
		do
		{			
			crow.style.display = '';
			crow = tbl.rows[row.rowIndex + i++];
			if(crow.getAttribute('lastrow')) 
			{
				break;
			}
		}
		while(true)
		
		ctl.innerHTML = collapseText;
		//ctl.className = collapseClass;
		ihExp.value = '1';
	}
	else
	{
		var i = 1;
		do
		{
			crow.style.display = 'none';
			crow = tbl.rows[row.rowIndex + i++];
			if(crow.getAttribute('lastrow')) 
			{
				break;
			}
		}
		while(true)

		ctl.innerHTML = expandText;
		//ctl.className = expandClass;
		ihExp.value = '';
	}
}

function onLoadExtGridView(gridId, checkBoxListName)
{
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function() { SetupCheckBoxList(gridId); });
}

function SetupCheckBoxList(gridId)
{
    var divArea = $get(gridId + '_CheckBoxArrayList');
    if (divArea === null)
        return;
    var js = divArea.innerHTML;
    if ((js === null) || (js.length === 0))
        return;
    eval(js);   
}

function CountChecked(checkBoxList)
{
    // Check to make sure they selected at least 1 vehicle from the list
    if ((checkBoxList != null) && (checkBoxList != undefined))
    {
        var numSelected = 0;
        for (var i = 0; i < checkBoxList.length; i++)
        {
            var cb = $get(checkBoxList[i]);
            if ((cb != null) && (cb.checked))
                numSelected++;
        }                                
        return numSelected;
    }
    return -1;
}

