/* ---------------------------------------------------------
 * Copyright (c) 2004 Participate Systems, Inc.  All rights reserved.
 * ---------------------------------------------------------
 */

/**
 * Validates text area length
 * @param field field object whose length you need to check
 * @param fieldDescription description of the field
 * @param maxLength maxmium length of the field.

 Commented out for I18N - current I18N mechanism does not process .js files.

 */

//function validateLength (field,
//                         fieldDescription,
//                         maxLength)
//{
//   var actualLength = field.value.length;
//   var extraCharLength = actualLength - maxLength;
//   var valid=true;
//   if (actualLength > maxLength)
//   {
//      alert ("You have exceeded the maximum "
//             + fieldDescription
//             + " size of "
//             + maxLength
//             + " characters.\n Please remove "
//             + extraCharLength + " characters from "
//             + fieldDescription + ".");
//      valid=false;
//   }
//   return valid;
//}


/**
 * Validates that something has been entered
 * @param field field object whose length you need to check
 * @param fieldDescription description of the field

 Commented out for I18N - current I18N mechanism does not process .js files.

 */

//function validateRequired (field, fieldDescription)
//{
//   alert('in validateRequired');
//   alert('in validateRequired - fieldvalue=' + field.value);
//   var actualLength = trim(field.value).length;
//   var isValid=true;
//   if (actualLength == 0)
//   {
//      alert ("<util:resString>Please enter a value for </util:resString>"
//             + fieldDescription + "<util:resString>.</util:resString>");
//      isValid=false;
//   }
//   return isValid;
//}

/**
 * Validates that at least one checkbox has been checked
 * @param pForm
   @param pGroupName
 * @param pFieldDescription description of the field

 Commented out for I18N - current I18N mechanism does not process .js files.

 */

//function validateCheckboxRequired(pForm, pGroupName, pFieldDescription)
//{
//
////   alert ('checkboxRequired: top');
//   for (var e = 0; e < pForm.elements.length; e++)
//   {
//      var formElement = pForm.elements[e];
////      alert ('checkboxRequired: checking element=' + formElement.name);
//      if (
//         (formElement.type == 'checkbox')
//         && (formElement.type != 'hidden')
//         && (formElement.name == pGroupName)
//         )
//      {
////         alert ('checkboxRequired: passed the trio - type=' + formElement.type + ' and Name=' + formElement.name + ' and groupNmae=' + pGroupName);
//         if (formElement.checked)
//         {
////            alert ('checkboxRequired: it is checked');
//            return true;
//         }
//      }
//   }
//   alert ("Please select at least one " + pFieldDescription + ".");
//   return false;
//}


/**
   This function removes all spaces from a string
   Example Trim(' Me And You ') returns 'MeAndYou'
   @param pUntrimmed
*/
function trim(pUntrimmed)
{
   var Trimmed = '';
   for (var i = 0; i < pUntrimmed.length; i++)
   {
      if (pUntrimmed.charCodeAt(i)!=32)
      {
         // @todo verify this works.  untrimmed[i] may be undefined
         Trimmed += pUntrimmed[i];
      }
   }
   return Trimmed;
}

function trimString (str)
{
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

/**
function for NN2 and NN3 and IE3 which removes leading and
trailing spaces from a string:
*/
function trimStringOldBrowser (str)
{
  while (str.charAt(0) == ' ')
    str = str.substring(1);
  while (str.charAt(str.length - 1) == ' ')
    str = str.substring(0, str.length - 1);
  return str;
}


/**
 * Validates an Email address.
 *
 * Ensures email address is in form of
 *   a.b.c.d.e.@foo.bar.com
 *   a.b.c.d.e@192.168.0.1
 *
 * 'a.b.c.d' means any "OK for email character"
 */
function isValidEmail(pEmailAddress)
{
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(pEmailAddress);
}
