/******************************************************************
 * Form Event Handlers
 ******************************************************************/

var frmWarned = false;
var frmUnldHandled = false;
var frmAlert = "Please ensure that you have completed all fields correctly.";


/******************************************************************
 * Function to check that the contents of a comment box does not
 * exceed the maximum.
 ******************************************************************/

function frmCount(frm, txtBox, charCount, max)
{
  var iLen = eval('document[frm].' + txtBox + '.value.length');
  if ("object" == typeof(document.all[charCount]))
  {
    document.all[charCount].innerHTML = iLen;
  }
  if (iLen > max && (!frmWarned))
  {
    alert ('Your response has exceed the ' + max + ' character limit. Please adjust your response.');
    frmWarned = true;
  }
}


/******************************************************************
 * Function to adjust the count on a comment box if required.
 ******************************************************************/

function frmLoad(frm, txtBox, charCount)
{
  var frmCheck = document[frm];
  if (frmCheck != null)
  {
    var iLen = eval('document[frm].' + txtBox + '.value.length');
    if ("object" == typeof(document.all[charCount]))
    {
      document.all[charCount].innerHTML = iLen;
    }
  }
}


/******************************************************************
 * Function to reset a form and adjust the count on a comment box
 * if required.
 ******************************************************************/

function frmClear(frm, txtBox, charCount)
{
  var frmCheck = document[frm];
  if (frmCheck != null)
  {
    frmCheck.reset();
    var iLen = eval('document[frm].' + txtBox + '.value.length');
    if ("object" == typeof(document.all[charCount]))
    {
      document.all[charCount].innerHTML = iLen;
    }
  }
}


/******************************************************************
 * Function to detect if a form field is defined or not.
 * True is returned if defined and false if not.
 ******************************************************************/

function frmDefined(elem)
{
  var sTag = new String(document.all[elem]);
  if (sTag != "undefined")
  {
    return true;
  }
  return false;
}


/******************************************************************
 * Function to iterate through form elements and detect if
 * any elements are dirty. Return true at the first dirty element.
 ******************************************************************/
 
function frmDirty(frm)
{
  if (frmDefined(frm))
  {
    var numElem = document[frm].elements.length;
    for (var i = 0; i < numElem; i++)
    {
      var frmElem = document[frm].elements[i];
      var elemType = frmElem.type.toLowerCase();
      var elemTag = frmElem.tagName.toLowerCase();
      if (elemType == "text" || elemType == "hidden" || elemTag == "textarea")
      {
        if (frmElem.value != frmElem.defaultValue) return true;
      }
      else if (elemType == "checkbox" || elemType == "radio")
      {
        if (frmElem.checked != frmElem.defaultChecked) return true;
      }
      else if (elemTag == "select")
      {
        for (var j = 0; j < frmElem.options.length; j++)
        {
          if (frmElem.options[j].selected != frmElem.options[j].defaultSelected) return true;
        }
      }
    }
  }
  return false;
}


/******************************************************************
 * Function to handle beforeunload event.
 ******************************************************************/

function frmUnload(frm)
{
  if (!frmUnldHandled && frmDirty(frm))
  {
      return "You have made changes to this form. If you would like to retain your\nchanges, click Cancel to remain on this page and then submit the form.";
  }
}


/******************************************************************
 * Function to handle onsubmit event.
 ******************************************************************/

function frmSubmit()
{
  if (frmValidate())
  {
    frmUnldHandled = true;
    return true;
  }
  alert(frmAlert);
  return false;
}


/******************************************************************
 * Function to perform pattern testing. Pass in a valid regular
 * express pattern (which can include flags) and test string.
 ******************************************************************/

function frmReTest(pattern, testString)
{
  re = new RegExp(pattern);
  return (re.test(testString));
}


/******************************************************************
 * Function to test whether at least one item in a radio element
 * has been checked.
 ******************************************************************/
function frmRadioCheck(frm, elem)
{
  if (frmDefined(frm))
  {
    var itemSelect = false;
    var e = eval('document[frm].' + elem);
    if (e.length > 1)
    {
      for (var i = 0; i < e.length; i++)
      {
        if (e[i].checked)
        {
          itemSelect = true;
          break;
        }
      }
    }
    else
    {
      if (e.checked)
        {
          itemSelect = true;
        }
    }
    if (itemSelect)
    {
      return true;
    }
    else
    {
      alert ('Please select an item from the list.');
      return false;
    }
  }
}