/**************************************
* Validates the input of all controls.
**************************************/
function validateInput(oValidator)
{
  //Control references
  var aControlsToValidate = oValidator.controlsToValidate.split("|");

  //Loop through all of the controls marked for validation
  for (var iControls = 0; iControls < aControlsToValidate.length; iControls++)
  {
    var oControlToValidate = document.getElementById(eval(aControlsToValidate[iControls]));

    if (oControlToValidate == null)
      continue;

    if (!validateControl(oControlToValidate, oValidator))
    {
      //Display the appropriate error message
      setElementText(oValidator, oControlToValidate.ErrorMessage);
      
      //Change the CSS class if specified
      if (oControlToValidate.ErrorCssClass != null && oControlToValidate.ErrorCssClass != "")
      {
        //Add script to change the CSS class back
        var sOldCssClass = oControlToValidate.className;
        oControlToValidate.className = oControlToValidate.ErrorCssClass;
        
        oControlToValidate.onfocus = function()
        {
          oControlToValidate.className = sOldCssClass;
          setElementText(oValidator, "");
        }
      }
      
      return false;
    }
  }
  
  return true;
}

/********************************************
* Performs validation on a specific control.
********************************************/
function validateControl(cntrlTarget)
{
  //Validation is only done against INPUT, SELECT, and TEXTAREA elements
  if (cntrlTarget.tagName.toLowerCase() == "input")
  {
    switch (cntrlTarget.type.toLowerCase())
    {
      case "checkbox":
        if (!validateCheckedItem(cntrlTarget))
          return false;

        break;
      case "text":
      case "hidden":
      case "password":
      case "":
        if (!validateText(cntrlTarget))
          return false;

        break;
    }
  }
  else if (cntrlTarget.tagName.toLowerCase() == "select")
  {
    if (!validateSelect(cntrlTarget))
      return false;
  }
  else if (cntrlTarget.tagName.toLowerCase() == "textarea")
  {
    if (!validateText(cntrlTarget))
      return false;
  }
    
  return true;
}

/***************************************************
* Performs validation on INPUT (checkbox) elements.
***************************************************/
function validateCheckedItem(chkTarget)
{
  if (chkTarget.type.toLowerCase() == "checkbox")
    return chkTarget.checked;
  else
  {
    var frmParent = chkTarget.form;
    var sTarget = chkTarget.name;

    for (var iRadios = 0; iRadios < frmParent.sTarget.length; iRadios++)
    {
      if (document.frmParent.sTarget[iRadios].checked)
        return true;
    }
    
    return false;
  }
}

/*****************************************
* Performs validation on SELECT elements.
*****************************************/
function validateSelect(selectTarget)
{
  var aSelected = new Array();

  for (var iOptions = 0; iOptions < selectTarget.options.length; iOptions++)
  {
    if (selectTarget.options[iOptions].selected)
    {
      if (selectTarget.options[iOptions].text != "")
        aSelected[aSelected.length] = selectTarget.options[iOptions];
    }
  }

  //Make sure the user selected the minimum number of items, if appropriate
  if (selectTarget.MinSelectableItems > 0 && aSelected.length < selectTarget.MinSelectableItems)
    return false;

  //Make sure the user selected the maximum number of items, if appropriate
  if (selectTarget.MaxSelectableItems > 0 && aSelected.length > selectTarget.MaxSelectableItems)
    return false;
  
  return true;
}

/*****************************************************
* Performs validation on TEXTAREA and INPUT elements.
*****************************************************/
function validateText(txtTarget)
{
  if (txtTarget.MinLength != null && txtTarget.value.length < txtTarget.MinLength)
    return false;
    
  if (txtTarget.MaxLength != null && txtTarget.value.length > txtTarget.MaxLength)
    return false;
  
  if (txtTarget.MinValue != null && parseFloat(txtTarget.value) < parseFloat(txtTarget.MinValue))
    return false;
  
  if (txtTarget.MaxValue != null && parseFloat(txtTarget.value) > parseFloat(txtTarget.MaxValue))
    return false;
  
  //Iterate the regular expressions to match against the value of the control
  if (txtTarget.ValidationExpressions != null)
  {
    var aPatterns = txtTarget.ValidationExpressions.split("|");
    for (var iPatterns = 0; iPatterns < aPatterns.length; iPatterns++)
    {
      var regexPattern = new RegExp(eval(aPatterns[iPatterns]));
      if (!regexPattern.test(txtTarget.value))
        return false;
    }
  }
  
  return true;
}

/*******************************
* Gets the text of the element.
*******************************/
function setElementText(oElement, sValue)
{
  if (oElement.innerHTML)
    oElement.innerHTML = sValue;
  else if (oElement.childNodes[0] == null)
    oElement.appendChild(document.createTextNode(sValue));
  else
    oElement.childNodes[0].nodeValue = sValue;
}
