﻿// JScript File

/*********************************************************
* Listbox functions
********************************************************** */

// Clears all the elements in a list box.
function ClearList( listId )
{
    var dropdown = document.getElementById(listId);
    var length = dropdown.length;

    if( length == 0 )
        return;

    for( ndx = length -1; ndx >= 0; ndx--)
    {
        dropdown.remove(ndx);
    }
}


///Adds an option to a dropdown or combolist.
function AddOptionToList(listControlId, optionText, optionValue )
{
    var dropdown = document.getElementById(listControlId);
    var option = document.createElement("option");
    option.text = optionText;
    option.value = optionValue;
    dropdown.options.add(option);
}


// Finds and sets an option as selected.
// Searches for the option using either its text or val based on the
// third parameter.
function SetListOption( listControlId, val, findByText )
{
    var listControl = document.getElementById(listControlId);
    var indexOfOption = FindIndexOfListOption(listControlId, val, findByText);

    if( indexOfOption >= 0 )
    {
        listControl.options[indexOfOption].selected = true;
    }
}


// Set list options as selected by values supplied.
function SetListOptionsUsingArrayOfValues(listControlId, arrValues)
{
    var val;

    for ( ndxValue = 0; ndxValue < arrValues.length; ndxValue++ )
    {
        val = arrValues[ndxValue];
        SetListOption(listControlId, val, false);
    }
}


// Returns the index of the option to find in the list control.
// Returns -1 if not found.
function FindIndexOfListOption(listControlId, val, compareUsingText)
{
    var listControl = document.getElementById(listControlId);
    var optionValue;
    var indexOfOption = -1;

    for ( i=0; i < listControl.length; i++)
    {
      if( compareUsingText )
      {
         optionValue = listControl.options[i].text;
      }
      else
      {
         optionValue = listControl.options[i].value;
      }

      if( optionValue == val)
      {
            indexOfOption = i;
            break;
      }
    }
    return indexOfOption;
}


function GetSelectedListValuesAsDelimitedString(controlId)
{
    return GetSelectedListIndexesOrValuesAsDelimitedString(controlId, true);
}


function GetSelectedListIndexesAsDelimitedString(controlId)
{
   return GetSelectedListIndexesOrValuesAsDelimitedString(controlId, false);
}


// Gets the selected values or indexes of the items in a list box.
function GetSelectedListIndexesOrValuesAsDelimitedString(controlId, getByValue )
{
    var listControl = document.getElementById(controlId);

    if( !listControl.options )
        return "";

    var delimitedValues = "";
    var isAnySelected = false;

    for (var ndx = 0; ndx < listControl.options.length; ndx++)
    {
        if (listControl.options[ ndx ].selected)
        {
            if( isAnySelected )
            {
                if ( getByValue )
                {
                    delimitedValues = delimitedValues + "," + listControl.options[ ndx ].value;
                }
                else
                {
                    delimitedValues = delimitedValues + "," + ndx;
                }
            }
            else
            {
                if( getByValue )
                {
                    delimitedValues = listControl.options[ ndx ].value;
                }
                else
                {
                    delimitedValues = ndx;
                }
                isAnySelected = true;
            }
        }
    }
    return delimitedValues;
}


/* END of list box functions
********************************************************* */

        // Set the disabled property of a control.
        function SetControlDisabledProperty(controlId, disabled)
	    {
		    var control = document.getElementById(controlId);
		    control.disabled = disabled;
		}


		// Set the readonly property of a control
		function SetControlReadOnlyProperty(controlId, isReadOnly)
	    {
		    var control = document.getElementById(controlId);
		    control.readOnly = isReadOnly;
		}


		// Set the readonly property of a control
		function ChangeControlEditState(controlId, isReadOnly)
	    {
		    var control = document.getElementById(controlId);

            if(isReadOnly)
            {
                control.style.border = "0px none";
                control.style.backgroundColor = "#eff2e9";
                control.style.borderColor = "#eff2e9";
            }
            else
            {
                control.style.border = "1px solid";
                control.style.backgroundColor = "#ffffff";
                control.style.borderColor = "#A4B97F";
            }
            control.readOnly = isReadOnly;
		}

    function MakeControlReadOnlyById( controlId, isReadOnly )
    {
      MakeControlReadOnly( document.getElementById( controlId ), isReadOnly );
    }

    function MakeControlReadOnly( control, isReadOnly )
    {
      if( isReadOnly )
      {
          control.style.border = "1px solid";
          control.style.backgroundColor = "#dfe2d9";
          control.style.borderColor = "#7f9db9";
      }
      else
      {
          control.style.border = "1px solid";
          control.style.backgroundColor = "#ffffff";
          control.style.borderColor = "#7f9db9";
      }
      control.readOnly = isReadOnly;
    }


		function ShowHideItem(controlId, blnShow)
		{
		    var style = 'none';

            if ( blnShow )
            {
                style = 'block';
            }

		    var controlToShowHide = document.getElementById(controlId);
            controlToShowHide.style.display = style;
		}


		
		///<summary>Gets the checked value from a radio group.</summary>
        ///<param name="radioObj">The radio group control name.</param>
        function GetCheckedValue(radioObj) 
        { 
                if(!radioObj) 
                        return ""; 
                        
                var radioLength = radioObj.length; 
                
                if(radioLength == undefined) 
                        if(radioObj.checked) 
                                return radioObj.value; 
                        else 
                                return ""; 
                                
                for(var i = 0; i < radioLength; i++) 
                { 
                        if(radioObj[i].checked) 
                        { 
                                return radioObj[i].value; 
                        } 
                } 
                return ""; 
        }
        
        
        ///<summary>Transfers text data from one control to another.</summary>
        ///<param name="sourceControlId">The id of the control transfer data from.</param>
        ///<param name="destinationControlId">The id of the control to transfer data to.</param>
        function TransferTextFromSourceToIndirectDestination(sourceControlId, destinationControlRefId )
        {
            var sourceValue = document.getElementById(sourceControlId).value;
            var destinationControlId = document.getElementById(destinationControlRefId).value;
            var destinationControl = document.getElementById(destinationControlId);
            destinationControl.value = sourceValue; 
        }
        
        
        ///<summary>Transfers text data from one control to another.</summary>
        ///<param name="sourceControlId">The id of the control transfer data from.</param>
        ///<param name="destinationControlId">The id of the control to transfer data to.</param>
        function TransferTextFromSourceToDestination(sourceControlId, destinationControlId )
        {
            var sourceValue = document.getElementById(sourceControlId).value;
            var destinationControl = document.getElementById(destinationControlId);
            destinationControl.value = sourceValue; 
        }
        
        
        //Expands or collapses an area.
        function ExpandCollapseItem(controlId, imgControlId, pathCollapeGIF, pathExpandGIF) 
        {
            element = document.getElementById(controlId);
            image = document.getElementById(imgControlId);
            if (element.style.display == "none") 
            {
                element.style.display = "";
                image.src = pathCollapeGIF;
                image.alt = "Collapse";
            } 
            else 
            {
                element.style.display = "none";
                image.src = pathExpandGIF;
                image.alt = "Expand";
            }
        }


  ///<summary>Shows or hides a column in a table.</summary>
  ///<param name="idTable">The id of the table to show/hide column</param>
  ///<param name="columnsDelimited">Comma delimeted string of column numbers to hide/show.
  /// e.g "1" or "2,4"</param>
  ///<param name="blnShow">Indicate whether to show the column, false means hide.</param>
  function ShowHideTableColumn( idTable, columnsDelimited, blnShow) 
  {
        if( !columnsDelimited)
            return;
            
        var strColumnsDelimited = columnsDelimited.toString();
        
        var columnsToShowHide;
        
        if( strColumnsDelimited.length == 1)
        {
            columnsToShowHide = new Array();
            columnsToShowHide[0] = strColumnsDelimited;
        }
        else
        { 
            columnsToShowHide = strColumnsDelimited.split(",");        
        }
        
        var style = 'none';
        
        if ( blnShow )
        {
            style = 'block';
        }

        var tbl  = document.getElementById(idTable);
        var rows = tbl.getElementsByTagName('tr');

        for (var row = 0; row < rows.length; row++)
        {
          var cells = rows[row].getElementsByTagName('td')
          
          for( var columnCount = 0; columnCount < columnsToShowHide.length; columnCount++)
          {
            var ndxColumn = columnsToShowHide[columnCount];
            cells[ndxColumn].style.display = style;
          }
        }
  }

///<summary>Shows or hides a column in a table.</summary>
  ///<param name="idTable">The id of the table to show/hide column</param>
  ///<param name="columnsDelimited">Comma delimeted string of column numbers to hide/show.
  /// e.g "1" or "2,4"</param>
  ///<param name="blnShow">Indicate whether to show the column, false means hide.</param>
  function EnableDisableTableColumn( idTable, columnsDelimited, disabled) 
  {
        
        if( columnsDelimited == null || columnsDelimited == "" )
            return;
            
        var strColumnsDelimited = columnsDelimited.toString();
        
        var columnsToShowHide;
        
        if( strColumnsDelimited.length == 1)
        {
            columnsToShowHide = new Array();
            columnsToShowHide[0] = strColumnsDelimited;
        }
        else
        { 
            columnsToShowHide = strColumnsDelimited.split(",");        
        }
        
        
        var tbl  = document.getElementById(idTable);
        var rows = tbl.getElementsByTagName('tr');

        for (var row = 0; row < rows.length; row++)
        {
          var cells = rows[row].getElementsByTagName('td')
          
          for( var columnCount = 0; columnCount < columnsToShowHide.length; columnCount++)
          {
            var ndxColumn = columnsToShowHide[columnCount];
            cells[ndxColumn].disabled = disabled;
          }
        }
  }
  
  function ShowHideTableRows( idTable, rowsDelimited, blnShow) 
  {
    ShowHideTableRowsWithHeaders(idTable, rowsDelimeited, blnShow, false);
  }
  

  ///<summary>Shows or hides rows in a table.</summary>
  ///<param name="idTable">The id of the table to show/hide column</param>
  ///<param name="rowsDelimited">Comma delimeted string of row numbers to hide/show.
  /// e.g "1" or "2,4"</param>
  ///<param name="blnShow">Indicate whether to show the column, false means hide.</param>
  function ShowHideTableRowsWithHeaders( idTable, rowsDelimited, blnShow, areRowsOffSetByHeader) 
  {     
        if( !rowsDelimited)
            return;
            
        var strRowsDelimited = rowsDelimited.toString();        
        
        var rowsToShowHide;
        
        if( strRowsDelimited.length == 1)
        {
            rowsToShowHide = new Array();
            rowsToShowHide[0] = strRowsDelimited;
        }
        else
        { 
            rowsToShowHide = strRowsDelimited.split(",");        
        }
        
        var style = 'none';
        
        if ( blnShow )
        {
            style = 'block';
        }

        var tbl  = document.getElementById(idTable);
        var rows = tbl.getElementsByTagName('tr');

        for (var count = 0; count < rowsToShowHide.length; count++)
        {
          var ndxRow = rowsToShowHide[count];
          ndxRow = parseInt(ndxRow);
          
          if(areRowsOffSetByHeader)
            ndxRow = ndxRow + 1;
            
          rows[ndxRow].style.display = style;
        }
  }  
  
  
  ///<summary>Displays the red border around the control</summary>
    function DisplayErrorUI(errorUIControlId, hasError)
    {
       var visibility = 'hidden';
       if(hasError) { visibility = 'visible'; }
       
       var errorUIControl = document.getElementById(errorUIControlId);  
       errorUIControl.style.visibility = visibility;
       errorUIControl.style.display = 'block';
    } 
    
    
    // Checkbox validation functions used for the custom server control. CheckBoxValidator.
function CheckBoxValidatorDisableButton(chkId, mustBeChecked, btnId)
{
    var button = document.getElementById(btnId);
    var chkbox = document.getElementById(chkId);
    
    if (button && chkbox)
    {
        button.disabled = (chkbox.checked != mustBeChecked);
    }
}


function CheckBoxValidatorEvaluateIsValid(val)
{
    var control = document.getElementById(val.controltovalidate);
    var mustBeChecked = Boolean(val.mustBeChecked == 'true');

    return control.checked == mustBeChecked;
}


function CheckBoxListValidatorEvaluateIsValid(val)
{
    var control = document.getElementById(val.controltovalidate);
    var minimumNumberOfSelectedCheckBoxes = parseInt(val.minimumNumberOfSelectedCheckBoxes);

    var selectedItemCount = 0;
    var liIndex = 0;
    var currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    while (currentListItem != null)
    {
        if (currentListItem.checked) selectedItemCount++;
        liIndex++;
        currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    }
    
    return selectedItemCount >= minimumNumberOfSelectedCheckBoxes;
}