﻿/////////////////////////////////////////////
// IST common java script functions
/////////////////////////////////////////////

var uniqueId = (function()
{
  var id = 0;
  return function()
  {
    return id++;
  };
})();

function zeroPad(n, digits)
{
  n = n.toString();
  while (n.length < digits)
    n = '0' + n;
  return n;
}

function GetParentByTagName(parentTagName, childElementObj)
{
  var parent = childElementObj.parentNode;
  while (parent.tagName.toLowerCase() != parentTagName.toLowerCase())
  {
    parent = parent.parentNode;
  }
  return parent;
}

function ToggleElement(element)
{
  if (element.style.display == 'none')
  {
    element.style.display = 'block';
  } 
  else
  {
    element.style.display = 'none';
  }
}

function ToggleCollapseExpandText(divElement, labelElement)
{
  if (divElement.style.display == 'none')
    labelElement.innerHTML = '[expand]';
  else
    labelElement.innerHTML = '[collapse]';
}

function ShowHideElement(element, show)
{
  if (show)
  {
    element.style.display = ''; //Should be block?
  }
  else
  {
    element.style.display = 'none';
  }
}

function SelectDropDownListOption(dropDownList, optionValue)
{
  for (i = 0; i < dropDownList.options.length; ++i)
  {
    if (dropDownList.options[i].value == optionValue)
    {
      dropDownList.selectedIndex = i;
      return;
    }
  }
}

function SelectListBoxOptions(listBox, optionValues)
{
  for (x = 0; x < listBox.options.length; ++x)
  {
    for (y = 0; y < optionValues.length; ++y)
    {
      if (listBox.options[x].value == optionValues[y])
        listBox.options[x].selected = true;
    }
  }
}

function UnSelectListBoxOptions(listBox)
{
  for (i = 0; i < listBox.options.length; ++i)
  {
    listBox.options[i].selected = false;
  }
}

/////////////////////////////////////////////
//The below is for checkbox trees
/////////////////////////////////////////////
function CheckUncheckChildTreeNodes(evt)
{
  var src = window.event != window.undefined ? window.event.srcElement : evt.target;
  var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");
  if (isChkBoxClick)
  {
    var parentTable = GetParentByTagName("table", src);
    var nxtSibling = parentTable.nextSibling;
    if (nxtSibling && nxtSibling.nodeType == 1)//check if nxt sibling is not null & is an element node 
    {
      if (nxtSibling.tagName.toLowerCase() == "div") //if node has children 
      {
        //check or uncheck children at all levels 
        CheckUncheckChildren(parentTable.nextSibling, src.checked);
      }
    }
  }
}

function CheckUncheckChildren(childContainer, check)
{
  var childChkBoxes = childContainer.getElementsByTagName("input");
  var childChkBoxCount = childChkBoxes.length;
  for (var i = 0; i < childChkBoxCount; i++)
  {
    childChkBoxes[i].checked = check;
  }
}

/////////////////////////////////////////////
//The below is for validation.
/////////////////////////////////////////////
function ValidationGroupValid(validationGroup)
{
  Page_ClientValidate();
  for (i = 0; i < Page_Validators.length; i++)
  {
    if (Page_Validators[i].validationGroup == validationGroup)
    {
      if (!Page_Validators[i].isvalid)
        return false;
    }
  }
  return true;
}

function ResetValidators(validationGroup)
{
  // Remove the validator control(s) from display.
  var pageValidators = Page_Validators;
  if ((typeof (pageValidators) != "undefined") && (pageValidators != null))
  {
    for (i = 0; i < pageValidators.length; ++i)
    {
      var pageValidator = pageValidators[i];
      if (validationGroup == null || IsValidationGroupMatch(pageValidator, validationGroup))
      {
        if (pageValidator.style.visibility.length > 0 && pageValidator.style.display.length == 0)
        {
          pageValidator.style.visibility = 'hidden';
        }
        else if (pageValidator.style.display.length > 0 && pageValidator.style.visibility.length == 0)
        {
          pageValidator.style.display = 'none';
        }
      }
    }
  }

  // Remove the validator summary(ies) from display. 
  if ((typeof (Page_ValidationSummaries) != "undefined") && (Page_ValidationSummaries != null))
  {
    var mySummaries = Page_ValidationSummaries;
    for (i = 0; i < mySummaries.length; i++)
    {
      var mySummary = mySummaries[i];
      if (validationGroup == null || IsValidationGroupMatch(mySummary, validationGroup))
      {
        mySummary.style.display = 'none';
      }
    }
  }
}

/////////////////////////////////////////////
//The below is to show/hide blocking screen.
/////////////////////////////////////////////
function ShowBlockingScreen()
{
  document.getElementById('blockingScreen').className = 'blocking-visible';
}

function HideBlockingScreen()
{
  document.getElementById('blockingScreen').className = 'blocking-invisible';
}

/////////////////////////////////////////////
//The below is to show/hide loading screen.
/////////////////////////////////////////////
function ShowLoadingScreen()
{
  document.getElementById('loadingScreen').className = 'blocking-visible';
  ShowHideElement(document.getElementById('mainScreen'), false);
  setTimeout("document.getElementById('imgLoadingScreen').src = '/Images/spinnerLarge.gif';", 10);
}

function HideLoadingScreen()
{
  document.getElementById('loadingScreen').className = 'blocking-invisible';
  ShowHideElement(document.getElementById('mainScreen'), true);
}

function ShowLoadingScreenIfValid()
{
  Page_ClientValidate();
  if (Page_IsValid)
  {
    ShowLoadingScreen();
  }
}

function ShowLoadingScreenIfValid(validationGroup)
{
  if (ValidationGroupValid(validationGroup))
    ShowLoadingScreen();
}

/////////////////////////////////////////////
//The below is for helping save client state 
//of checkbox selection
/////////////////////////////////////////////
var _checkBoxIdentifiers = new Array();
var _checkBoxStates = new Array();

function selectCheckBoxStates(checkBoxPrefix)
{
  //setup all changed checkbox states
  for (var i = 0; i < _checkBoxIdentifiers.length; i++)
  {
    var checkBox = document.getElementById(checkBoxPrefix + _checkBoxIdentifiers[i]);
    if (checkBox != undefined)
      checkBox.checked = _checkBoxStates[i];
  }
}

function saveCheckBoxState(checkBoxIdentifier, state)
{
  //Check if it has already been added.
  for (var i = 0; i < _checkBoxIdentifiers.length; i++)
  {
    if (_checkBoxIdentifiers[i] == checkBoxIdentifier)
    {
      _checkBoxStates[i] = state;
      return;
    }
  }
  //Not found so add it.
  _checkBoxIdentifiers[_checkBoxIdentifiers.length] = checkBoxIdentifier;
  _checkBoxStates[_checkBoxStates.length] = state;
}
/////////////////////////////////////////////

/////////////////////////////////////////////
//The below is for helping save client state 
//of textbox selection
/////////////////////////////////////////////
var _textBoxIdentifiers = new Array();
var _textBoxStates = new Array();

function selectTextBoxValues(textBoxPrefix)
{
  //setup all changed textbox states
  for (var i = 0; i < _textBoxIdentifiers.length; i++)
  {
    var textBox = document.getElementById(textBoxPrefix + _textBoxIdentifiers[i]);
    if (textBox != undefined)
      textBox.value = _textBoxStates[i];
  }
}

function saveTextBoxValue(textBoxIdentifier, value)
{
  //Check if it has already been added.
  for (var i = 0; i < _textBoxIdentifiers.length; i++)
  {
    if (_textBoxIdentifiers[i] == textBoxIdentifier)
    {
      _textBoxStates[i] = value;
      return;
    }
  }
  //Not found so add it.
  _textBoxIdentifiers[_textBoxIdentifiers.length] = textBoxIdentifier;
  _textBoxStates[_textBoxStates.length] = value;
}
/////////////////////////////////////////////

/////////////////////////////////////////////
//The below is for helping save client state 
//of image state selection
/////////////////////////////////////////////
var _imageIdentifiers = new Array();
var _imageStates = new Array();

function selectImageStates(imagePrefix, visible)
{
  //setup all changed image states
  for (var i = 0; i < _imageIdentifiers.length; i++)
  {
    var image = document.getElementById(imagePrefix + _imageIdentifiers[i]);
    if (image != undefined)
    {
      if (_imageStates[i] == visible)
        image.style.display = "block";
      else
        image.style.display = "none";
    }
  }
}

function saveImageState(imageIdentifier, state)
{
  //Check if it has already been added.
  for (var i = 0; i < _imageIdentifiers.length; i++)
  {
    if (_imageIdentifiers[i] == imageIdentifier)
    {
      _imageStates[i] = state;
      return;
    }
  }
  //Not found so add it.
  _imageIdentifiers[_imageIdentifiers.length] = imageIdentifier;
  _imageStates[_imageStates.length] = state;
}