

var reWhitespace = /^\s+$/

var reLetter = /^[a-zA-Z]$/

var reAlphabetic = /^[a-zA-Z]+$/

var reAlphanumeric = /^[a-zA-Z0-9]+$/

var reNumeric = /^[0-9]+$/

var reDigit = /^\d/

var reLetterOrDigit = /^([a-zA-Z]|\d)$/

var reInteger = /^\d+$/

var reCurrency = /(^\d+\.\d{2}$)|(^\d+$)|(^\.\d{2}$)/

var reEmail = /^.+\@.+\..+$/

var digits = "0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

var whitespace = " \t\n\r";

var phoneNumberDelimiters = "()- ";

var validUSPhoneChars = digits + phoneNumberDelimiters;

var SSNDelimiters = "- ";

var SsnEinDelimiters = "- ";

var validSSNChars = digits + SSNDelimiters;


var digitsInSocialSecurityNumber = 9;

var digitsInUSPhoneNumber = 10;

var digitsMedID = 10;
var digitsPANum = 10;
var digitsNDCCode = 11;

var charsInState = 2;

var ZIPCodeDelimiters = "-";

var ZIPCodeDelimeter = "-";

var validZIPCodeChars = digits + ZIPCodeDelimiters;

var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;

var digitsInWVMed1 = 8;
var digitsInWVMed2 = 10;

var creditCardDelimiters = " ";


var mPrefix = "You did not enter a value into the ";
var mSuffix = " field. This is a required field. Please enter it now.";

var selPrefix = "You did not select a value in the ";
var selSuffix = " field. This is a required field. Please select a value now.";



var sLastName = "Last Name";
var sFirstName = "First Name";
var sUserName = "Logon Name";
var sPassphrase = "Password";
var sPassphrase2 = "Re-entered Password";
var sOrganization = "Organization Name";
var sStreetAddress = "Street Address";
var sCity = "City";
var sStateCode = "State Code";
var sZIPCode = "ZIP Code";
var sPhone = "Phone Number";
var sFax = "Fax Number";
var sEmail = "Email";
var sDate = "Date";
var sDateOfBirth = "Date of Birth";
var sValue = "Value";
var sLogonName = "Logon Name";
var sDateFrom = "From Date";
var sDateTo = "To Date";



var iStateCode = "This field must be a valid two character U.S. state abbreviation (like WV for West Virginia). Please reenter it now.";
var iZIPCode = "This field must be a 5 or 9 digit U.S. ZIP Code (like 94043). Please reenter it now.";
var iMedID = "This field must be a 10 digit Medicaid ID (like 1234567890). Please reenter it now.";
var iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now.";
var iSSN = "This field must be a 9 digit U.S. social security number (like 123 45 6789). Please reenter it now.";
var iEmail = "This field must be a valid email address (like xxx@yyy.com). Please reenter it now.";
var iDay = "This field must be a day number between 1 and 31.  Please reenter it now.";
var iMonth = "This field must be a month number between 1 and 12.  Please reenter it now.";
var iYear = "This field must be a 4 digit year number.  Please reenter it now.";
var i2Year = "This field must be a 2 digit year number.  Please reenter it now.";
var iDatePrefix = "The Day, Month, and Year for ";
var iDateSuffix = " do not form a valid date.  Please reenter them now.";
var iIntRange3 = "This field must be a number between 0 and 999. Please reenter it now.";
var iIntRange7 = "This field must be a number between 1 and 9999999. Please reenter it now.";
var iWVMed = "This field must be a number 8 characters or 10 characters in length. Please reenter it now.";
var iNumeric = "This field must contain only numeric characters.";
var iCurrency = "This field must contain only a currency value (no $).";
var iNoFutureDate = "This date cannot be a date in the future.  Please reenter it now.";
var iToAfterFrom = "The to/thru date must be the same or later than the from date.";
var iLogonName = "This field must have a length between 4 and 15 characters [aA-zZ, 0-9].\nPlease reenter now.";
var iAllowedAnywhere = "Input fields are only allowed to contain the characters a-z, A-Z, 0-9, _ @ & . ' - and embedded blanks.  Please reenter."

var pEntryPrompt = "Please enter a ";
var pStateCode = "2 character code (like CA).";
var pZIPCode = "5 or 9 digit U.S. ZIP Code (like 94043).";
var pUSPhone = "10 digit U.S. phone number (like 415 555 1212).";
var pSSN = "9 digit U.S. social security number (like 123 45 6789).";
var pEmail = "valid email address (like foo@bar.com).";
var pDay = "day number between 1 and 31.";
var pMonth = "month number between 1 and 12.";
var pYear = "2 or 4 digit year number.";
var pMedID = "10 digit Medicaid ID.";




var defaultEmptyOK = false;



var daysInMonth = new Array(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;


var multiSelectArray = new Array();



var USStateCodeDelimiter = "|";
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP"


function open_windowSizedCommon (url, setHeight, setWidth)
{
 var thisHeight = setHeight;
 var thisWidth = setWidth;
popUpWin = 
window.open(url,"win",'toolbar=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes,height=' + thisHeight
 +',width=' + thisWidth);
}


function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}



function isWhitespace (s)

{   // Is s empty?
    return (isEmpty(s) || reWhitespace.test(s));
}



// Removes all characters which appear in regexp bag from string s.
// NOTES:
// 1) bag must be a regexp which matches single characters in isolation,
//    i.e. A or B or C or D or 1 or 2 ...
//    e.g. /\d/g  or /[a-zA-Z]/g
// 2) make sure to append the 'g' modifier (for global search & replace)
//    at the end of the regexp
//    e.g. /\d/g  or /[a-zA-Z]/g

function stripCharsInRE (s, bag)

{       return s.replace(bag, "")
}



function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}





function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}





function stripWhitespace (s)

{   return stripCharsInBag (s, whitespace)
}





function stripInitialWhitespace (s)

{   var i = 0;

    while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
       i++;
    
    return s.substring (i, s.length);
}


function reformatForXML (s)

{
    
    var returnString = "";
    var re1 = /</g;
    var re2 = />/g;
    var re3 = /&/g;
    
    returnString = s.replace(re1," ");
    returnString = returnString.replace(re2," ");
    returnString = returnString.replace(re3,"and");

    return returnString;    
}


function isLetter (c)
{   return reLetter.test(c)
}




function isDigit (c)
{   return reDigit.test(c)
}





function isLetterOrDigit (c)
{   return reLetterOrDigit.test(c)
}




// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}

function isCurrency (s)

{   var i;

    if (isEmpty(s)) 
       if (isCurrency.arguments.length == 1) return defaultEmptyOK;
       else return (isCurrency.arguments[1] == true);

    return reCurrency.test(s)
}


function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    else {
       return reAlphabetic.test(s)
    }
}

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    else {
       return reAlphanumeric.test(s)
    }
}

function isNumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isNumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isNumeric.arguments[1] == true);

    else {
       return reNumeric.test(s)
    }
}




function reformat (s)

{   var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}






function isSSN (s)
{   if (isEmpty(s)) 
       if (isSSN.arguments.length == 1) return defaultEmptyOK;
       else return (isSSN.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInSocialSecurityNumber)
}


function isUSPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}








function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)))
}

function isWVMed (s)
{  if (isEmpty(s)) 
       if (isWVMed.arguments.length == 1) return defaultEmptyOK;
       else return (isWVMed.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInWVMed1) ||
             (s.length == digitsInWVMed2)))
}

function isLogonName (s)
{  if (isEmpty(s)) 
       if (isLogonName.arguments.length == 1) return defaultEmptyOK;
       else return (isLogonName.arguments[1] == true);
   return ((s.length >= 4) && (s.length <= 15) && (reAlphanumeric.test(s)))
}




function isMedID (s)
{  if (isEmpty(s)) 
       if (isMedID.arguments.length == 1) return defaultEmptyOK;
       else return (isMedID.arguments[1] == true);
   return ((isInteger(s)) && (s.length == digitsMedID))
}


function isStateCode(s)
{   if (isEmpty(s)) 
       if (isStateCode.arguments.length == 1) return defaultEmptyOK;
       else return (isStateCode.arguments[1] == true);
    return ( (USStateCodes.indexOf(s) != -1) &&
             (s.indexOf(USStateCodeDelimiter) == -1) && s.length == charsInState )
}





function isEmail (s)

{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    
    else {
       return reEmail.test(s)
    }
}





function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isInteger(s)) return false;
    return (s.length == 4);
}

function is2Year (s)
{   if (isEmpty(s)) 
       if (is2Year.arguments.length == 1) return defaultEmptyOK;
       else return (is2Year.arguments[1] == true);
    if (!isInteger(s)) return false;
    return (s.length == 2);
}






function isIntegerInRange (s, a, b)
{
   if (isEmpty(s))
   { 
         if (isIntegerInRange.arguments.length == 1)
         {  
            return defaultEmptyOK;
         }else{ 
            return ((isIntegerInRange.arguments[1] != 1) && 
            (isIntegerInRange.arguments[1] == true)); 
            }
    }

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    
    var num = parseInt (s, 10);
    return ((num >= a) && (num <= b));
}





function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}





function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}



// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}



// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day 
// form a valid date.
// 

function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year, 10);
    var intMonth = parseInt(month, 10);
    var intDay = parseInt(day, 10);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}


function is2Date (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (is2Year(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year, 10);
    var intMonth = parseInt(month, 10);
    var intDay = parseInt(day, 10);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}




/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */


// Display prompt string s in status bar.

function prompt (s)
{   window.status = s
}



// Display data entry prompt string s in status bar.

function promptEntry (s)
{   window.status = pEntryPrompt + s
}




// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}

function warnSelectEmpty (theField, s)
{   theField.focus();
    alert(selPrefix + s + selSuffix);
    return false;
}

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid_OLD (theField, s)
{   theField.focus();
    theField.select();
    alert(s);
    return false
}

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.
// If theField.type is unable to accept a focus() and select(),
// then add to the message whatever info we can give the user.
// type can be one of: text|password|checkbox|radio|submit|reset|file|hidden|image|button

function warnInvalid (theField, s)
{
    if (theField.type != "hidden")
    {
        theField.focus();
        theField.select();
        alert(s);
    }
    if (theField.type == "hidden")
    {
        s2 = new String (s + "\n Field name ["+theField.name+"]  Field value ["+theField.value+"]");
        alert(s2);
    }
    return false
}

function warnInvalidSelectList (theField, s)
{   theField.focus();
    alert(s);
    return false
}





function checkString (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    else return true;
}


function checkForSelect(theField, s)
{
  var checkArray = new Array();
  checkArray = getMultiSelectValues (theField);

  if (checkArray.length == 0) 
       return warnSelectEmpty (theField, s);
  if (checkArray.length == 1 && (getSelectValue(theField) == ""))
        return warnSelectEmpty (theField, s);
   else return true;
        
}


function checkStateCode (theField, emptyOK)
{   if (checkStateCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  theField.value = theField.value.toUpperCase();
       if (!isStateCode(theField.value, false)) 
          return warnInvalid (theField, iStateCode);
       else return true;
    }
}

function reformatZIPCode (ZIPString)
{   if (ZIPString.length == 5) return ZIPString;
    else return (reformat (ZIPString, "", 5, "-", 4));
}

function make2Digits (theValue)
{   if (theValue.length == 2) return theValue;
        else return ("0" + theValue.toString());
}

function addPadding (s, maxLength, addHere, padChar)
{   
    var thisAddHere = addHere.toUpperCase();    
        for (var i=s.length; i < maxLength; i++)
        {
                if (thisAddHere = "RIGHT")
                {
                  s = s+padChar;      
                }else{
                  s = padChar + s;
                }       
        }
        
        return s;
}

function caseToUpper (theField)
{   
	theField.value = theField.value.toUpperCase()
	return true;
}

function caseToLower (theField)
{   
	theField.value = theField.value.toLowerCase()
	return true;
}

function checkEitherOrBothSelect(field1, field2, iBothEmpty, iBothFilled)
{
	
	if(field1.value.length == 0 && field2.value.length == 0)
		return(warnInvalidSelectList(field1, iBothEmpty)); 
	if(field1.value.length >= 1 && field2.value.length >= 1)
		return(warnInvalidSelectList(field1, iBothFilled));
		else return true;

}

function checkEitherSelect(field1, field2, iBothEmpty)
{
	
	if(field1.value.length == 0 && field2.value.length == 0)
		return(warnInvalidSelectList(field1, iBothEmpty)); 
		else return true;

}

function checkZIPCode (theField, emptyOK)
{   if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
      if (!isZIPCode(normalizedZIP, false)) 
         return warnInvalid (theField, iZIPCode);
      else 
      {  // if you don't want to insert a hyphen, comment next line out
         theField.value = reformatZIPCode(normalizedZIP)
         return true;
      }
    }
}

function checkWVMed (theField, maxLength, addHere, padChar, emptyOK)
{   
        
        if (checkWVMed.arguments.length == 4) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { var normalizedWVMed = stripCharsInBag(theField.value, whitespace);
      if (!isWVMed(normalizedWVMed, false)) 
         return warnInvalid (theField, iWVMed);
      else 
      {  
         theField.value = addPadding(normalizedWVMed, maxLength, addHere, padChar);
         return true;
      }
    }
}

function checkMedID (theField, emptyOK)
{   if (checkMedID.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { 
      if (!isMedID(theField.value, false)) 
         return warnInvalid (theField, iMedID);
      else 
      {  
         return true;
      }
    }
}

function checkLogonName (theField, emptyOK)
{   if (checkLogonName.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { 
      if (!isLogonName(theField.value, false)) 
         return warnInvalid (theField, iLogonName);
      else 
      {  
         return true;
      }
    }
}



function reformatUSPhone (USPhone)
{   return (reformat (USPhone, "", 3, "-", 3, "-", 4))
}





function checkUSPhone (theField, emptyOK)
{   if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (!isUSPhoneNumber(normalizedPhone, false)) 
          return warnInvalid (theField, iUSPhone);
       else 
       {  // if you don't want to reformat as (123) 456-789, comment next line out
          theField.value = reformatUSPhone(normalizedPhone)
          return true;
       }
    }
}









function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else 
    {
        theField.value = stripWhitespace(theField.value);
        return true;
    }
}


function cleanForXML (theField)
{
    theField.value = reformatForXML(theField.value);
    return true; 
}



function reformatSSN (SSN)
{   return (reformat (SSN, "", 3, "-", 2, "-", 4))
}




function checkSSN (theField, emptyOK)
{   if (checkSSN.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  var normalizedSSN = stripCharsInBag(theField.value, SSNDelimiters)
       if (!isSSN(normalizedSSN, false)) 
          return warnInvalid (theField, iSSN);
       else 
       {  // if you don't want to reformats as 123-456-7890, comment next line out
          theField.value = reformatSSN(normalizedSSN)
          return true;
       }
    }
}


function check2Year (theField, emptyOK)
{   if (check2Year.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!is2Year(theField.value, false)) 
       return warnInvalid (theField, i2Year);
    else return true;
}


function checkYear (theField, emptyOK)
{   if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear(theField.value, false)) 
       return warnInvalid (theField, iYear);
    else return true;
}




function checkMonth (theField, emptyOK)
{   if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isMonth(theField.value, false)) 
       return warnInvalid (theField, iMonth);
    else return true;
}




function checkDay (theField, emptyOK)
{   if (checkDay.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isDay(theField.value, false)) 
       return warnInvalid (theField, iDay);
    else return true;
}


function check2Date (yearField, monthField, dayField, labelString, OKtoOmitDay)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (check2Date.arguments.length == 4) OKtoOmitDay = false;
    if (!is2Year(yearField.value)) return warnInvalid (yearField, i2Year);
    if (!isMonth(monthField.value)) return warnInvalid (monthField, iMonth);
    if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true;
    else if (!isDay(dayField.value)) 
       return warnInvalid (dayField, iDay);
    if (is2Date (yearField.value, monthField.value, dayField.value))
       return true;
    alert (iDatePrefix + labelString + iDateSuffix)
    

    return false
}


function checkDate (monthField, dayField, yearField, labelString, OKtoOmitDay)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkDate.arguments.length == 4) OKtoOmitDay = false;
    if (!isMonth(monthField.value)) return warnInvalid (monthField, iMonth);
    if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true;
    else if (!isDay(dayField.value)) 
       return warnInvalid (dayField, iDay);
    if (!isYear(yearField.value)) return warnInvalid (yearField, iYear);
    if (isDate (yearField.value, monthField.value, dayField.value))
    {
       monthField.value = make2Digits(monthField.value);
       dayField.value = make2Digits(dayField.value);
       return true;
    }
    
    monthField.focus()
    monthField.select()
    alert (iDatePrefix + labelString + iDateSuffix)
    return false
}

function checkQuantity (quantField, a, b, s)
{
    if (!isIntegerInRange(quantField.value, a, b))
        {
            return warnInvalid (quantField, s)
         }
return true;
}

function checkInvalid(theTestField, theFocusField, iString)
{
   // allow the input of another field for focus
   // in case the TestField is a hidden field (which doesn't allow focus) 
   if(theTestField.value.length >0)
   {
   return true;
        }else {
                return(warnInvalid(theFocusField,iString));
                
        }     
}

function checkExplain(theSelectField, theExplainField, sMustExplainValue, sLabelString)
{
        if(getSelectValue(theSelectField) == sMustExplainValue)
        {
                return(
                checkString(theExplainField, sLabelString)
                );    
        }else{
                return true;
        }        
        
}

function checkNeed1OtherField(theTestField, theFirstField, sLabelString)
{
        var normalizedTestField = stripCharsInBag(theTestField.value, whitespace);
        if(normalizedTestField.length > 0)
        {
                return(
                checkString(theFirstField, sLabelString)                
                );    
        }else{
                return true;
        }        
        
}

function checkNeed2OtherFields(theTestField, theFirstField, theSecondField, sLabelString)
{
        var normalizedTestField = stripCharsInBag(theTestField.value, whitespace);
        if(normalizedTestField.length > 0)
        {
                return(
                checkString(theFirstField, sLabelString)&&
                checkString(theSecondField, sLabelString)
                );    
        }else{
                return true;
        }        
        
}

function checkNeed3OtherFields(theTestField, theFirstField, theSecondField,theThirdField, sLabelString)
{
        var normalizedTestField = stripCharsInBag(theTestField.value, whitespace);
        if(normalizedTestField.length > 0)
        {
                return(
                checkString(theFirstField, sLabelString)&&
                checkString(theSecondField, sLabelString)&&
                checkString(theThirdField, sLabelString)
                );    
        }else{
                return true;
        }        
        
}

function checkNeed4OtherFields(theTestField, theFirstField, theSecondField, theThirdField, theFourthField, sLabelString)
{
        var normalizedTestField = stripCharsInBag(theTestField.value, whitespace);
        if(normalizedTestField.length > 0)
        {
                return(
                checkString(theFirstField, sLabelString)&&
                checkString(theSecondField, sLabelString)&&
                checkString(theThirdField, sLabelString)&&
                checkString(theFourthField, sLabelString)
                );    
        }else{
                return true;
        }        
        
}

function checkDateNoFuture(theMonthField, theDayField, theYearField, theCurrentDate, sLabelString, iString, OKtoOmit)
{
    if (checkDateNoFuture.arguments.length == 6) OKtoOmit = false;
    var theField = (theMonthField.value + theDayField.value + theYearField.value);
    if ((OKtoOmit == true) && (isEmpty(theField))) return true;

    if( checkDate(theMonthField, theDayField, theYearField, sLabelString))
    {
        var testDate = (theYearField.value + theMonthField.value + theDayField.value);
        
        if( parseInt(testDate, 10) > parseInt(theCurrentDate, 10) )
        {
            return warnInvalid(theMonthField, iString);
            
        }else{ return true;}
        
    }else{ return false;}
    
    
}

function checkSequenceDates(theFirstMonth, theFirstDay, theFirstYear, 
                                    theSecondMonth, theSecondDay, theSecondYear, iString)
{
 var firstDate = (theFirstYear.value + theFirstMonth.value + theFirstDay.value);
 var secondDate = (theSecondYear.value + theSecondMonth.value + theSecondDay.value);
 
 if( parseInt(firstDate, 10) > parseInt(secondDate, 10) )
        {
            return warnInvalid(theFirstMonth, iString);
            
        }else{ return true;}   
    
}

function checkNeedDate(theSelectField, theMonthField, theDayField, theYearField, sNeedsDateValue, sLabelString)
{
        if(getSelectValue(theSelectField) == sNeedsDateValue)
        {
                return(
                checkDate(theMonthField, theDayField, theYearField, sLabelString)
                );    
        }else{
                return true;
        }        
        
}

function checkNeedDateNoFuture(theSelectField, theMonthField, theDayField, theYearField, sNeedsDateValue, sLabelString, theCurrentDate, iString, OKtoOmit)
{
        if(getSelectValue(theSelectField) == sNeedsDateValue)
        {
                return(
                checkDateNoFuture(theMonthField, theDayField, theYearField, theCurrentDate, sLabelString, iString, OKtoOmit)
                );    
        }else{
                return true;
        }        
        
}
function checkNeedDateAnyValue(theTestField, theMonthField, theDayField, theYearField, sLabelString)
{
        var normalizedTestField = stripCharsInBag(theTestField.value, whitespace);
        if(normalizedTestField.length > 0)
        {
                return(
                checkDate(theMonthField, theDayField, theYearField, sLabelString)
                );    
        }else{
                return true;
        }        
        
}
function checkNeedDateAnyValueNoFuture(theTestField, theMonthField, theDayField, theYearField, sLabelString, theCurrentDate, iString)
{
        var normalizedTestField = stripCharsInBag(theTestField.value, whitespace);
        if(normalizedTestField.length > 0)
        {
                return(
                checkDateNoFuture(theMonthField, theDayField, theYearField, theCurrentDate, sLabelString, iString)
                );    
        }else{
                return true;
        }        
        
}
function checkNumeric(theField,emptyOK)
{
    if (checkNumeric.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isInteger(theField.value)) 
       return warnInvalid (theField, iNumeric);
    else return true;
        
        
}

function checkCurrency(theField,emptyOK)
{
    if (checkCurrency.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isCurrency(theField.value)) 
       return warnInvalid (theField, iCurrency);
    else return true;                
}

function checkEitherOr(field1, field2, iBothEmpty, iBothFilled)
{
	
	if(field1.value.length == 0 && field2.value.length == 0)
		return(warnInvalid(field1, iBothEmpty)); 
	if(field1.value.length >= 1 && field2.value.length >= 1)
		return(warnInvalid(field1, iBothFilled));
		else return true;

}

function checkEitherOrNeitherOK(field1, field2, iBothFilled)
{
	if(field1.value.length >= 1 && field2.value.length >= 1)
		return(warnInvalidSelectList(field1, iBothFilled));
		else return true;

}

// Get checked value from radio button.

function getRadioButtonValue (radio)
{   for (var i = 0; i < radio.length; i++)
    {   if (radio[i].checked) { break }
    }
    return radio[i].value
}

// Get selected values from multi Select element 

function getMultiSelectValues (multiSelect)
{   
    for (var i = 0; i < multiSelectArray.length ; i++)
        {
            multiSelectArray[i] = "";
        }
        
    for (var i = 0; i < multiSelect.length; i++)
        {   if (multiSelect[i].selected)
            { 
                 multiSelectArray[i] = multiSelect.options[i].value;  
         
            }
        }
    return multiSelectArray;
}

// Get selected value from Select element 

function getSelectValue (selectField)
{   
    
    for (var i = 0; i < selectField.length; i++)
        {   if (selectField[i].selected)
            { 
                 break;  
         
            }
        }
    return selectField[i].value;
}

// Utility functions

function moveNextField( theField, maxlength, nextFieldName )
{
    //alert('Here');
        if(theField.value.length == maxlength)
        {
        	theField.form.elements[nextFieldName].focus();
        }else{
        		theField.form.elements[theField.name].focus();
        	}			
}

function fillDate(fromMonth, fromDay, fromYear, toMonth, toDay, toYear)
{
	if(toMonth.value == "" && toDay.value == "" && toYear.value == "")
	{
		toMonth.value = fromMonth.value;
		toDay.value = fromDay.value;
		toYear.value = fromYear.value;
	}
}






// check list of allowed characters; return false if any input field (type text or hidden?) fails.
// type can be: text|password|checkbox|radio|submit|reset|file|hidden|image|button
// 20041217 - added underscore as valid character on any form per Scott Twigg request.
function checkForDisallowedCharacters(theForm)
{
    if (!theForm.elements) return true;
    var elArr = theForm.elements;
    for(var i = 0; i < elArr.length; i++) {
       with(elArr[i]) {
          var delimPat = /^[a-zA-Z0-9\.\ \&\_\-\'\@]*$/;
          if (!elArr[i].value) continue;
          var gotIt = delimPat.exec(value);
          if (!gotIt) { 
             return(warnInvalid(elArr[i],iAllowedAnywhere));                           
           //  elArr[i].select();
           //  elArr[i].focus(); 
             return false;
          }
       }
    }
    return true;
}

// remove leading and trailing blanks in all input fields of form, replace values.
function trimLeadingTrailingSpaces(theForm)
{
    if (!theForm.elements) return true;
    var elArr = theForm.elements; 
    var patt = /^\s*(\S.*\S)*?\s*$/;
    for(var i = 0; i < elArr.length; i++) {
       with(elArr[i]) {
           etype=elArr[i].type;		// text|password|checkbox|radio|submit|reset|file|hidden|image|button
		   if (etype=='submit' || etype=='reset')
               continue;
           if (!elArr[i].value) continue;
           elArr[i].value = value.replace(patt, "$1");       
       }
    }
}
