/*
 * Common generic validation routines
 */


/*
 * Trims trailing and leading spaces in a string
 */
function INTrim(input) {

   var headwsre = /^\s+/;
   var tailwsre = /\s+$/;

   input.value = input.value.replace(headwsre, "");
   input.value = input.value.replace(tailwsre, "");

}

/*
 * Returns a handle to the input specified by 'inputName' contained
 * in the form specified by formName
 */
function getInput(formName, inputName) {

   var form = eval('document.' + formName);
   if (form[inputName]) {
      (form[inputName].type == 'text' || form[inputName].type == 'textarea' || form[inputName].type == 'password') ? INTrim(form[inputName]) : true;
      return form[inputName];
   }

   return null;

}

/*
 * Generic text input validation function. If input has no length,
 * alert message and return false
 */
function stdValidationText(input, msg) {

   if (input) {

      if (!input.value.length) {
         alert(msg);
         return false;
      }

   }

   return true;

}

/*
 * Generic text input maximum length validation function. If input length is
 * greater than max_len, alert message and return false
 */
function stdValidationLength(input, max_len, msg) {

   if (input) {

      if (input.value.length > max_len) {
         alert(msg);
         return false
      }

   }

   return true;

}

function isValidEmail(str)
{
   // try to exclude ones outside of valid single-byte characters:
   // characters below \u0020 (space) are control characters, and
   // the upper limit \u00FF will exclude double-byte characters

   var reInvalidChars = "[^\u0020-\u00FF]";
   var reFormat       = /^[^@ ]+@[^@ ]{2,64}\.[^@ \.-]{2,4}$/;

   if (!reFormat.test(str) || str.search(reInvalidChars) != -1)
   {
      return false;
   }
   return true;
}

function isValidEmailName(str) {
   
   //Testing the email portion before the @
   var re  =  /^([a-zA-Z0-9_\.\-]+)$/;
   if (!re.test(str)) 
   {
      return false;
   }
   return true;
}

function isValidEmailAddressLength(str){

    //Testing to make sure that the full email address does
    //not exceed 97 characters
    if(str.length < 97 && str.length != 0 )
    {
        return true;
    }
}


//  a check for new/edit Members and Contacts
//  validation on server side is done as an entire name
//  meaning first, middle, or last
//  this was added to make sure a new member can't just have
//  the middle name only.
function CheckMiddleNameOnly(firstName, middleName, lastName)
{

    // check if middle initial or name only was entered
     if (middleName.length > 0)
       {  
          if(firstName.length == 0 && lastName.length == 0)
           {
              alert(g_ENTERFIRSTANDORLASTNAME);
              return false;
           }
       }

}

/*
* Name:         isFieldTextSafe
* Description:  This method loops through all controls on a form and checks for unsafe text.
*               The default is to only check text fields. Additionally, you can pass an array
*               of the name of the fields to check and those shall be the only ones checked.
*/
function isFieldTextSafe(bCheckAll, controlNameArray)
{
  var str = '';
  var re = /^((?!<script>|<\/script>|<script\/>).)*$/i;

  //If specific field names passed in, only check those
  if(controlNameArray!=null)
  {
    for(var x = 0; x < controlNameArray.length; x++)
    {
      var elemSpecific = document.forms[0].elements;

      for(var i = 0; i < elemSpecific.length; i++)
      { 
        if(elemSpecific[i].name==controlNameArray[x])
        {
          if (!re.test(elemSpecific[i].value))
          {
            alert(g_INVALIDCHARSSENTERED);
            return false;
          }
        }
      }               
    }

    return true;
  }
  //Specific fields not specified
  var elem = document.forms[0].elements;

  for(var i = 0; i < elem.length; i++)
  {
    if(!bCheckAll)
    {
      if(elem[i].type=="text")
      {
        if (!re.test(elem[i].value))
        { 
          alert(g_INVALIDCHARSSENTERED);
          return false;
        }
      }
    }
    else
    {
      if (!re.test(elem[i].value))
      {
        alert(g_INVALIDCHARSSENTERED);
        return false;
      }
    }          
  }
  return true;
}


