/**
 * @Copyright (C) 1999-2007, CCSTM Computing LLC.
 * http://www.ccstm.com
 * All Rights Reserved
 * See the product License for more information
 * 
 * @author Seth Morecraft (morecraf@ccstm.com)
 * @date June, 2006
 */

/**
 * Verifies that a given string is a valid email address
 */
function CheckEmailAddress(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if(str.indexOf(at)==-1){
    return false
  }

  if(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
    return false
  }

  if(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    return false
  }

  if(str.indexOf(at,(lat+1))!=-1){
    return false
  }

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    return false
  }

  if(str.indexOf(dot,(lat+2))==-1){
    return false
  }
    
  if(str.indexOf(" ")!=-1){
    return false
  }
  return true          
}

/**
 * Adds an onLoad event to the document
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


/**
 * determine if a character string is a number or not
 * @param string for qualification
 * @param bool allow negative numbers?
 * @param bool allow decimal places?
 * @reutrn bool true if all tests pass
 */
function IsNumeric(sText, allow_negative, allow_decimal){
 var ValidChars = "0123456789";
 if(allow_negative)
   ValidChars = '-' + ValidChars;
 if(allow_decimal)
   ValidChars = ValidChars + '.';
 var Char;

 for(i = 0; i < sText.length; i++){
    Char = sText.charAt(i);
    if(ValidChars.indexOf(Char) == -1){
      return false;
    }
 }
 return true;
}

/**
 * dertemine if a string has all zeros in it (allows for decimals)
 */
function IsZero(sText, allow_decimal){
 var ValidChars = "123456789";
 if(allow_decimal)
   ValidChars = ValidChars + '.';
 var Char;

 for(i = 0; i < sText.length; i++){
    Char = sText.charAt(i);
    if(Char == "0")
      continue;
    else if(allow_decimal && Char == ".")
      continue;
    else
      return false;
 }
 return true;
}


/**
 * Disables or enables an entire form
 * @param FormObject (ex: doucment.FormName)
 * @param bool Enable(true) Disable(false) a form
 */
function ToggleFormActive(FormObject, Enable){
  var state = true;  // Disable by default
  if(Enable)
    state = false;

  for(i = 0; i < FormObject.elements.length; i++){
    FormObject.elements[i].disabled = state;
  }
}

/**
 * Toggle the readOnly state of an Input Param.
 * (Also changes the style)
 * @param FormObject (ex: doucment.FormName.inputName)
 * @param bool Enable(true) Disable(false) an input
 */
function ToggleReadOnly(FormObject, Enable){
  FormObject.readOnly = Enable;
  if(Enable){
    FormObject.style.borderColor = '#FFFFFF';
  }
  else{
    FormObject.style.borderColor = '#000000';
  }
}


var MAX_DUMP_DEPTH = 10;
function dumpObj(obj, name, indent, depth) {
      if (depth > MAX_DUMP_DEPTH) {
             return indent + name + ": <Maximum Depth Reached>\n";
      }
      if (typeof obj == "object") {
             var child = null;
             var output = indent + name + "\n";
             indent += "\t";
             for (var item in obj)
             {
                   try {
                          child = obj[item];
                   } catch (e) {
                          child = "<Unable to Evaluate><br />";
                   }
                   if (typeof child == "object") {
                          output += dumpObj(child, item, indent, depth + 1);
                   } else {
                          output += indent + item + ": " + child + "\n<br />";
                   }
             }
             return output;
      } else {
             return obj;
      }
}

/** 
 * Pulls all data from a form
 */
function getFormValues(fobj){
  var str = "";
  var valueArr = null;
  var val = ""; 
  var cmd = ""; 

  for(var i = 0;i < fobj.elements.length;i++){ 
    // Handle the different input types...
    switch(fobj.elements[i].type){ 
      case "text":     
      case "textarea":     
        str += fobj.elements[i].name + "=" + encodeURI(fobj.elements[i].value) + "&";
        break;
      case "hidden":     
        str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
        break;

      case "checkbox":     
        str += fobj.elements[i].name + "=";
        if(fobj.elements[i].checked)
          str += "on";
        else
          str += "off";
        str += "&";
        break;

      case "select-one":
        str += fobj.elements[i].name + "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
        break;
    }
 }
 str = str.substr(0,(str.length - 1));
 return str;
}

/**
 * Check or uncheck all checkboxes in a form
 * @param FormObject (ex: doucment.FormName.inputName)
 * @param bool Check or Uncheck all
 */
function checkAllCheckboxes(FormObject, checked){
  for (var i=0;i < FormObject.elements.length; i++){
    var e = FormObject.elements[i];
    if((e.type=='checkbox') && (!e.disabled)){
      e.checked = checked;
    }
  }
}

/**
 * Trim
 * @param string the string to trim
 * @return string the trimmed string
 */
// thanks to http://www.aspdev.org/articles/javascript-trim/
// for the trim function
function trim(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
        }
        while (sString.substring(sString.length-1, sString.length) == ' ') {
                sString = sString.substring(0,sString.length-1);
        }
        return sString;
}
