 function trim(str)
 {
  return (str ? '' + str : '').replace(/^\s*|\s*$/g, '');
 }

 function isDef(arg, argtype)
 {
  try
  {
   if (argtype === null || typeof(argtype) == 'undefined') return (arg !== null && typeof(arg) != 'undefined');
   return (arg !== null && typeof(arg) == argtype);
  }
  catch (e) {}
  return false;
 }

 function el(id)
 {
  try
  {
   if (!isDef(id, 'string') || id == '') return null;
   return document.getElementById(id);
  }
  catch(e) {}
  return null;
 }

 function getWidth(elem)
 {
  try
  {
   if (!isDef(elem)) return 0;
   if (elem.style.display == 'none') return 0;
   return elem.innerWidth ? elem.innerWidth :elem.clientWidth ? elem.clientWidth : elem.offsetWidth;
  }
  catch(e) {}

  return 0;
 }

 function getHeight(elem)
 {
  try
  {
   if (!isDef(elem)) return 0;
   if (elem.style.display == 'none') return 0;
   return elem.innerHeight ? elem.innerHeight :elem.clientHeight ? elem.clientHeight : elem.offsetHeight;
  }
  catch(e) {}

  return 0;
 }

 function getWindowSize()
 {
  try
  {
   var windowWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
   var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
   return {width: windowWidth , height: windowHeight};
  }
  catch (e) {}

  return {width: 0 , height: 0};
 }

 function resizePage()
 {
  setTimeout('_resizePage()', 100);
 }

 function _resizePage()
 {
  var windowSize = getWindowSize();

  var deltaHeight = getHeight(el('pageTable')) - getHeight(el('contentDiv'));
  el('contentDiv').style.height = (windowSize.height - 12 - (deltaHeight + getHeight(el('headerTable')) + getHeight(el('menuTable')) + getHeight(el('footerTable')))) + 'px';  // I have no idea why I have to subtract an additional 12 pixels?
 }
 
 //////////////////////////////////////////////////////////////////
 //  Function:     submitForm                                    //
 //  Description:  Iterates through each of the selected form's  //
 //                elements and applies the regular expression   //
 //                validation test to their respective values.   //
 //  Arguments:    formID - Id of selected form to submit        //
 //  Returns:      true on Success, false on Failure             //
 //                                                              //
 //  Author:       Richard Willis                                //
 //  Date:         4 Sep 2009                                    //
 //////////////////////////////////////////////////////////////////
 
 function submitForm(formID) 
 {
  try
  {
   var result = true;
 
   var form = el(formID);                      // Select the form using the provided Id string
   if (form == null) return false;
 
   for (var i=0; i<form.elements.length; i++)  // Iterate through the form's input element array
   {
    var element = form.elements[i];
 
    var required = element.getAttribute('required');                            // Get the form input element's custom "required" attribute
    if (required != 'false' || trim(element.value) != '')
    {
     var validate = element.getAttribute('validate');                            // Get the form input element's custom "validate" attribute
     if (validate != null && typeof(validate) != 'undefined' && validate != '')  // Only apply validation test to form input elements that have a custom "validate" attribute defined
     {
      var validated = false;
      var tagType = element.type.toUpperCase();  // Determine if form input element is of type "CHECKBOX" or "RADIO"
      var isCheckbox = (tagType == 'CHECKBOX');
      var isRadioButton = (tagType == 'RADIO');
      
      if (isCheckbox) validated = element.checked; // Checkbox must be checked
      else if (isRadioButton)                      // Test that at least one of a set of radio buttons is checked
      {
       var j, radioButtons = eval('form.' + element.name);                          // Get the array holding all the radio button and all of its siblings
 
       for (j=0; j<radioButtons.length; j++) {if (radioButtons[j].checked) break;}  // Iterate through the array of radio buttons until one is found to be checked
       validated = (j < radioButtons.length);                                       // Test if no radio buttons checked
       i += (radioButtons.length - 1);
      }
      else
      {
       if (element.value != 'value required')               // Value not equal to default error string
       {
        var regExParam = validate.split('!');               // Extract the regular expression and any associated pattern flags by splitting the "validate" value on the '!' delimiter
        if (regExParam[0] == '' && regExParam.length == 3)  // Validation string must be in the form "!expression!flags" (flags are optional)
        {
         var regEx = RegExp(regExParam[1], regExParam[2]);  // Create Regular Expression
         validated = regEx.test(element.value);             // Passes Regular Expression Test
        } 
       }
      }
 
      if (validated)
      {
       if (element.parentNode) element.parentNode.style.color = '#606060';  // If validated set the text colour of the input element and its parent cell to the default 
       element.style.color = '#606060';
      } 
      else
      {                                                                                                                                  
       result = false;                                                                                                                   
                                                                                                                                         
       if (element.parentNode) element.parentNode.style.color = 'red';      // Visually indicate error by setting text colour of the input element and its parent cell to red
       element.style.color = 'red';
       
       if (element.value == '') element.value = 'value required';           // If input element's value is an empty string, set it to the error string, "value required"
      }
     }
    }
   }
   
   if (result)
   {
    form.submit();          // Submit validated form, ignored for demo
    return true;
   } 
   else alert('Form values shown in red are invalid!');  // Show alert dialog to indicate that form has failed to validate
  }
  catch (e) {}

  return false; 
 }
 
 
 ////////////////////////////////////////////////////////////////
 //  Function:     resetForm                                   //
 //  Description:  Clear all the values in the selected form.  //
 //  Arguments:    formID - Id of selected form to submit      //
 //  Returns:      true on Success, false on Failure           //
 //                                                            //
 //  Author:       Richard Willis                              //
 //  Date:         4 Sep 2009                                  //
 ////////////////////////////////////////////////////////////////
 
 function resetForm(formID) 
 {
  try
  {
   var form = el(formID);               // Select the form using the provided Id string
   if (form == null) return false;
 
   form.reset();                        // Clear the form

   return true;
  }
  catch (e) {}

  return false; 
 }
 
 
 
 