/*
Miscellaneous javascript functions organized into classes
*/

//============== class definition using the dynamic prototype method: BEGIN ==============
function Form(sFormID) { // constructor function
  this.formID = sFormID;

  // define methods only once
  if (typeof Form._initialized == "undefined") {

    // METHOD DEFINITIONS: BEGIN

    //------------ method -----------------------
    Form.prototype.getCheckedRadioButtonValue = function(radioGroupName) {
      // return the value of the radio button that is checked
      // return an empty string if none are checked, or
      // there are no radio buttons
      var radioObj = eval("document.getElementById(this.formID)." + radioGroupName);

      if (!radioObj) return "";
      var radioLength = radioObj.length;
      if (radioLength == undefined) {
		if(radioObj.checked) {
			return radioObj.value;
		} else {
			return "";
        }
      } else {
        for (var i = 0; i < radioLength; i++) {
          if(radioObj[i].checked) {
            return radioObj[i].value;
          }
        }
      }
      return "";
    };

    //------------ method -----------------------
    Form.prototype.validate = function(fieldID, vtype, customRegExp, required, defaultValue) {
      /*
       all regular expressions, including customRegExp should match from 
       the beginning of the line to the end of the line, i.e., /^expression$/
       if text field or text area, then check if it is non-blank/default value and is required and then validate with regular expression.
       if select, then just check to see if is non-blank/default value and is required
      */

      var field = document.getElementById(fieldID);
      var tagname = field.tagName.toLowerCase();
      var fieldValue = document.getElementById(fieldID).value;

      // if field is nonblank or nondefault value, check to see if required or not
      if ( (tagname == 'input' && field.getAttribute("type").toLowerCase() == 'text') ||
           field.tagName.toLowerCase() == 'textarea' ) {
         if ( MiscUtils.prototype.trim(fieldValue) == '' || fieldValue == defaultValue ) { // blank/default value
           if ( required ) {
             return false;
           } else {
             return true;
           }
         }
      } else if (tagname == 'select') {
         if ( isNaN(defaultValue) ) {
           alert('Form.prototype.validate - ERROR: supplied defaultValue is NaN - contact webmaster');
           return false;
         }
         if ( field.selectedIndex == parseInt(defaultValue) ) { // default value
           if ( required ) {
             return false;
           } else {
             return true;
           }
         } else {
           return true; // if select, then do not continue and validate with regular expression - not needed
         }
      } else {
         alert('Form.prototype.validate - ERROR: unsupported form field specified - contact webmaster');
         return false;
      }

      switch (vtype) {
         case 'name':
            var regExp = /^[a-zA-Z][a-zA-Z0-9 \.\-\'\,]+$/;
            break;
         case 'email':
            var regExp = /^[\w][\w\,\-\.]*\@[\w]+[\w\-\.]*$/;
            break;
         case 'textfield':
            var regExp = /^[a-zA-Z0-9 \.\-:\/\(\)\'\,\;\"_]+$/;
            break;
         case 'textarea':
            //var regExp = new RegExp("/^.*$/s");  // this didn't work, so added hack below
            return true;  // don't bother validating textarea if non-custom regular expression - not trying to untaint input
            break;
         case 'custom':
            eval('var regExp = ' + customRegExp);
            break;
         default:
           alert('Form.prototype.validate - ERROR: unsupported vtype specified - contact webmaster');
           return false;
      }

      // validate text box field value
      if ( fieldValue.search(regExp) == 0 ) { 
        return true; 
      } else { 
        return false; 
      } 
    };

    // METHOD DEFINITIONS: END

    Form._initialized = true;
  }
} 
//============== class definition using the dynamic prototype method: END ==============

//============== class definition using the dynamic prototype method: BEGIN ==============
function MiscUtils() { // constructor function

//  this.formID = sMiscUtilsID;

  // define methods only once
  if (typeof MiscUtils._initialized == "undefined") {

    // METHOD DEFINITIONS: BEGIN

    //------------ method -----------------------
    MiscUtils.prototype.createTextElement = function(tagName, className, idName, text) {
      var container = document.createElement(tagName);
      if (className != '') container.setAttribute("class", className);
      if (idName != '') container.setAttribute("id", idName);
      var textNode = document.createTextNode(text);
      container.appendChild(textNode);
      return container;
    };

    //------------ method -----------------------
    MiscUtils.prototype.trim = function(str) {
      str = str.replace(/^\s+/, '');
      str = str.replace(/\s+$/, '');
      return str;
    };

    //------------ method -----------------------
    MiscUtils.prototype.getElementsByName = function(name) {
      var allElements = document.getElementsByTagName("*");
      var requestedElements = new Array();
      for (var i=0; i < allElements.length; i++) {
        if (allElements[i].getAttribute("name") == name) {
          requestedElements.push(allElements[i]);
        }
      }

      return requestedElements;
    };

    //------------ method -----------------------
    MiscUtils.prototype.getElementsByClassName = function(className) {
      var requestedElements = new Array();
      if (document.all) { // IE
        var allElements = document.all;
        for (var i=0; i < allElements.length; i++) {
          if (allElements[i].className == className) {
            requestedElements.push(allElements[i]);
          }
        }
      } else { // good browsers
        var allElements = document.getElementsByTagName("*");
        for (var i=0; i < allElements.length; i++) {
          if (allElements[i].getAttribute("class") == className) {
            requestedElements.push(allElements[i]);
          }
        }
      }
      return requestedElements;    
    }

    //------------ method -----------------------
    MiscUtils.prototype.getParam = function(param) {
      // get the value for the requested GET parameter from the query string
      var rExp = /\?.*$/;
      var queryString = window.top.location.href.substr(window.top.location.href.search(rExp) + 1);
      rExp = /#/;
      var ind = queryString.search(rExp);
      if (ind != -1) {
        queryString = queryString.substr(0, ind);
      }
      var paramStrings = queryString.split('&');
      var keyVal = new Array();
      var paramValue = '';
      for (var i=0; i < paramStrings.length; i++) {
        keyVal = paramStrings[i].split('=');
        if (keyVal[0] == param) {
          paramValue = keyVal[1];
          break;
        }
      }

      return paramValue;
    };

    // METHOD DEFINITIONS: END

    MiscUtils._initialized = true;
  }
}
//============== class definition using the dynamic prototype method: END ==============
