/////////////////////////////////////////////////////////////////////////
// Rainier Pacific Validation Library
//
// This common library of Javascript functions is called by all form
// pages for client-side validation purposes.

// NEED:
//   validation for SS number (ddd-dd-dddd or ddd/dd/dddd or ddddddddd)

// Initialize global variables  /////////////////////////////////////////////
// These need to be reset on the page each time validation is performed.
var errText = "";    // Contains error messages that are displayed with alert.
var valid = true;    // Flag to determine whether data is valid. If any data is
                     // invalid, this gets set to false, and the error message
                     // is displayed at the end of the validation process.
var firstFocus = ""; // Container for name of first field that returns invalid.
                     // We'll bring focus to this field for the user.


// Validation strings //////////////////////////////////////////////////////
// These are sets of allowed characters used in validation

// validAlph - allowable alphabetical characters
var validAlph = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";

// validAlphPlus - allowable alphabetical characters plus punctuation
var validAlphPlus = validAlph + ",./:'()&# -_ ";

// validDigits - allowable digit characters
var validDigits = "0123456789";

// validDigitsPlus - allowable digit characters plus punctuation
var validDigitsPlus = validDigits + "-. ()/";

// validText - allowable alph and digit characters combined
var validText = validAlph + validDigits;

// validTextPlus - allowable alph and digit characters plus punctuation
var validTextPlus = validText + ",./:'()&# -_@ ";


/////////////////////////////////////////////////////////////////////////
//  checkNoPO
//
//  abstract:      Ensures that provided address does not contain a
//                 PO Box by searching for "PO" and "P.O." at the beginning
//                 of the address string.
//
//  parameters:    address - provided postal address
//
//  return values: true - if address does not contain PO Box
//                 false - if address does contain PO Box
//
///////////////////////////////////////////////////
function checkNoPO(address) {

  // make sure address doesn't contain PO Box
  var first4 = address.substring(0,4);
  var first2 = address.substring(0,2);

  if (first4 == "P.O." || first4 == "p.o." || first2 == "PO") {
    return false;
  }

  return true;

}




/////////////////////////////////////////////////////////////////////////
//  checkValidEmail
//
//  abstract:      Checks whether provided email address is correctly
//                 formatted following these rules (a@b.c):
//                 -Must have a user name before the @
//                 -Must have an @
//                 -Must have at least one dot after the @
//                 -Must have at least one character after the dot
//
//  parameters:    email - provided email address
//                 req - set to true or 1 if this field is required
//
//  return values: true - if email is correctly formatted
//                 false - if email is not correctly formatted
//
///////////////////////////////////////////////////
function checkValidEmail(email,req) {

  // if required, ensure field is not null
  if (req && !email) return false;

  // make sure address doesn't contain any unwanted characters
  if (!checkValidTextPlus(email)) return false;

  // break apart address so we can check for validity of each part
  var whereAt = email.indexOf("@");          // look for @
  var userName = email.substring(0,whereAt); // extract user name
  var whereDot = email.lastIndexOf(".");     // look for last dot
  var tld = email.substring(whereDot+1);     // extract tld

  // check validity
  if (whereAt < 0 || whereDot < 0) return false; // no @ or dot
  if (!userName || !tld) return false;           // invalid user name or TLD

  return true;

}


/////////////////////////////////////////////////////////////////////////
//  checkNotEmpty
//
//  abstract:      Checks whether provided text is not empty string.
//
//  parameters:    text - provided text
//
//  return values: true - if text is not empty string
//                 false - if text is empty string
//
///////////////////////////////////////////////////
function checkNotEmpty(text) {

  if (!text) return false;
  return true;

}


/////////////////////////////////////////////////////////////////////////
//  checkValidAlph
//
//  abstract:      Checks whether provided text consists of allowed
//                 characters A-Z, a-z. If this field is required, use
//                 max and min length parameters to ensure a value has
//                 been entered--set minlength to 1.
//
//  parameters:    text - provided text
//                 maxlength - maximum length limit (optional)
//                 minlength - minimum length limit (optional)
//
//  return values: true - if text is valid
//                 false - if text is invalid
//
///////////////////////////////////////////////////
function checkValidAlph(text,maxlength,minlength) {

  // check for invalid characters
  for (var i=0; i<text.length; i++) {
    if (validAlph.indexOf(text.charAt(i)) == -1) return false;
  }

  // check for invalid length
  if (!checkValidLength(text,maxlength,minlength)) return false;

  return true;

}


/////////////////////////////////////////////////////////////////////////
//  checkValidAlphPlus
//
//  abstract:      Checks whether provided text consists of allowed
//                 characters A-Z, a-z. If this field is required, use
//                 max and min length parameters to ensure a value has
//                 been entered--set minlength to 1.
//
//  parameters:    text - provided text
//                 maxlength - maximum length limit (optional)
//                 minlength - minimum length limit (optional)
//
//  return values: true - if text is valid
//                 false - if text is invalid
//
///////////////////////////////////////////////////
function checkValidAlphPlus(text,maxlength,minlength) {

  // check for invalid characters
  for (var i=0; i<text.length; i++) {
    if (validAlphPlus.indexOf(text.charAt(i)) == -1) return false;
  }

  // check for invalid length
  if (!checkValidLength(text,maxlength,minlength)) return false;

  return true;

}


/////////////////////////////////////////////////////////////////////////
//  checkValidDigits
//
//  abstract:      Checks whether provided text consists of allowed
//                 characters 0-9. If this field is required, use
//                 max and min length parameters to ensure a value has
//                 been entered--set minlength to 1.
//
//  parameters:    text - provided text
//                 maxlength - maximum length limit (optional)
//                 minlength - minimum length limit (optional)
//
//  return values: true - if text is valid
//                 false - if text is invalid
//
///////////////////////////////////////////////////
function checkValidDigits(text,maxlength,minlength) {

  // check for invalid characters
  for (var i=0; i<text.length; i++) {
    if (validDigits.indexOf(text.charAt(i)) == -1) return false;
  }

  // check for invalid length
  if (!checkValidLength(text,maxlength,minlength)) return false;

  return true;

}


/////////////////////////////////////////////////////////////////////////
//  checkValidDigitsPlus
//
//  abstract:      Checks whether provided text consists of allowed
//                 characters 0-9 and simple punctuation. If this field is required, use
//                 max and min length parameters to ensure a value has
//                 been entered--set minlength to 1.
//
//  parameters:    text - provided text
//                 maxlength - maximum length limit (optional)
//                 minlength - minimum length limit (optional)
//
//  return values: true - if text is valid
//                 false - if text is invalid
//
///////////////////////////////////////////////////
function checkValidDigitsPlus(text,maxlength,minlength) {

  // check for invalid characters
  for (var i=0; i<text.length; i++) {
    if (validDigitsPlus.indexOf(text.charAt(i)) == -1) return false;
  }

  // check for invalid length
  if (!checkValidLength(text,maxlength,minlength)) return false;

  return true;

}


/////////////////////////////////////////////////////////////////////////
//  checkValidText
//
//  abstract:      Checks whether provided text consists of allowed
//                 characters A-Z, a-z, and 0-9. If this field is required, use
//                 max and min length parameters to ensure a value has
//                 been entered--set minlength to 1.
//
//  parameters:    text - provided text
//                 maxlength - maximum length limit (optional)
//                 minlength - minimum length limit (optional)
//
//  return values: true - if text is valid
//                 false - if text is invalid
//
///////////////////////////////////////////////////
function checkValidText(text,maxlength,minlength) {

  // check for invalid characters
  for (var i=0; i<text.length; i++) {
    if (validText.indexOf(text.charAt(i)) == -1) return false;
  }

  // check for invalid length
  if (!checkValidLength(text,maxlength,minlength)) return false;

  return true;

}


/////////////////////////////////////////////////////////////////////////
//  checkValidTextPlus
//
//  abstract:      Checks whether provided text consists of allowed
//                 characters A-Z, a-z, 0-9, and common punctuation.
//                 If this field is required, use
//                 max and min length parameters to ensure a value has
//                 been entered--set minlength to 1.
//
//  parameters:    text - provided text
//                 maxlength - maximum length limit (optional)
//                 minlength - minimum length limit (optional)
//
//  return values: true - if text is valid
//                 false - if text is invalid
//
///////////////////////////////////////////////////
function checkValidTextPlus(text,maxlength,minlength) {

  // check for invalid characters
  for (var i=0; i<text.length; i++) {
    if (validTextPlus.indexOf(text.charAt(i)) == -1) return false;
  }

  // check for invalid length
  if (!checkValidLength(text,maxlength,minlength)) return false;

  return true;

}


/////////////////////////////////////////////////////////////////////////
//  checkValidLength
//
//  abstract:      Checks whether provided text is within
//                 max/min parameters. Can be used to ensure a value has
//                 been entered in required fields--set minlength to 1.
//
//  parameters:    text - provided text
//                 maxlength - maximum length limit (optional)
//                 minlength - minimum length limit (optional)
//
//  return values: true - if text is within max/min parameters
//                 false - if text is outside max/min parameters
//
///////////////////////////////////////////////////
function checkValidLength(text,maxlength,minlength) {

  // since maxlength might not be specified, check these separately
  if (maxlength && text.length > maxlength) {
    return false;
  }
  if (minlength && text.length < minlength) {
    return false;
  }

  return true;

}


/////////////////////////////////////////////////////////////////////////
//  checkRadioSelected
//
//  abstract:      Checks whether the user has selected one in a group
//                 of radio buttons that have the same name. Also works
//                 with checkboxes if at least one must be selected.
//
//  parameters:    field - radio/checkbox button object
//
//  return values: true - if at least one radio button/checkbox is selected
//                 false - if no radio button/checkbox is selected
//
///////////////////////////////////////////////////
function checkRadioSelected(field) {

  // Create flag, switch to true if a button is selected,
  // then return this flag instead of implicitly returning
  // either true or false.
  var isSel = false;

  // loop through radio button elements
  for (var i=0; i<field.length; i++) {
    if (field[i].checked == true) isSel = true;
  }

  return isSel;

}







