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 resizePage()
{
// setTimeout('_resizePage()', 100);
}

function _resizePage()
{
// var headerDiv = el('headerDiv');
// var menuDiv = el('menuDiv');
// var contentDiv = el('contentDiv');
// var footerDiv = el('footerDiv');
// 
// var headerHeight = getHeight(headerDiv);
// var menuHeight = getHeight(menuDiv);
// var footerHeight = getHeight(footerDiv);
// var windowSize = getWindowSize();
// 
// var contentHeight = windowSize.height - headerHeight - menuHeight - footerHeight;
// contentDiv.style.height = contentHeight + 'px';
//
// setTimeout('scrollToAnchor()', 100);  // Hack to stop browser losing the anchor page position when the page is resized
}


//////////////////////////////////////////////////////////////////
//  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             //
//////////////////////////////////////////////////////////////////

function submitForm(formID) 
{
 try
 {
  var result = true;

  var form = el(formID);                      // Select the form using the provided Id string
    if (!isDef(form)) 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, (required="false" will override all validation)
     if (!isDef(required) || required != 'false' || trim(element.value) != '')
   {
    var validate = element.getAttribute('validate');                            // Get the form input element's custom "validate" attribute
      if (isDef(validate) && 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 = '#404040';  // If validated set the text colour of the input element and its parent cell to the default
        element.style.color = '#404040';
     } 
     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           //
////////////////////////////////////////////////////////////////

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; 
}




