var repwd = /pwd/
var repwdb = /\*+/
var reWhitespace = /^\s+$/
var reLetter = /^[a-zA-Z]$/
var reLetterOrDigit = /^([a-zA-Z]|\d)$/
var reInteger = /^\d+$/
var rePhone = /^(\d){3}(-){0,1}(\d){3}(-){0,1}(\d){4}(x(\d){1,5}){0,1}/
var reBday = /^(\d){4}(-){0,1}(\d){2}(-){0,1}(\d){2}$/
var reEmail = /^.+\@.+\..+$/
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";
var phoneNumberDelimiters = "()- x";
var validUSPhoneChars = digits + phoneNumberDelimiters;
var ZIPCodeDelimiters = "-";
var ZIPCodeDelimeter = "-"
var validZIPCodeChars = digits + ZIPCodeDelimiters
var digitsInZIPCode1 = 5
var mPrefix = "You did not enter a value into the "
var mSuffix = " field. \nThis is a required field. Please enter it now."
var sUSLastName = "Last Name"
var sUSFirstName = "First Name"
var sPassword = "Password"
var sUSTitle = "Title" 
var sUSAddress = "Street Address"
var sCity = "City"
var sZIPCode = "ZIP Code"
var sPhone = "Phone Number"
var sFax = "Fax Number"
var sDateOfBirth = "Date of Birth"
var sEmail = "Email"
var iStateCode = "This field must be a valid two character U.S. state \nabbreviation (like CA for California). \nPlease reenter it now."
var iZIPCode = "This field must be a 5 digit U.S. ZIP Code (like 94043). \nPlease reenter it now."
var iUSPhone = "This field must be a 10 digit U.S. phone number (like 919-555-1212). \nYou may also add an optional extension (up to 5 digits) (like 919-555-1212x12345).\nPlease reenter it now."
var iEmail = "This field must be a valid email address (like foo@bar.com). \nPlease reenter it now."
var iDay = "This field must be a day number between 1 and 31.  \nPlease reenter it now."
var iMonth = "This field must be a month number between 1 and 12.  \nPlease reenter it now."
var iYear = "This field must be a 4 digit year number greater than 1955.  \nPlease reenter it now."
var iBday = "This field must be formatted YYYY-MM-DD.  \nPlease reenter it now."
var iDatePrefix = "The Day, Month, and Year for "
var iDateSuffix = " do not form a valid date.  Please reenter them now."
var defaultEmptyOK = false

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

var daysInMonth = makeArray(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;

function checkPWD(s)
{
   if (repwd.test(s.value)) {
      alert ("Password search not allowed"); 
      return false;
   }
   if (repwdb.test(s.value)) {
      alert ("* search not allowed");
      return false;
   }

   return true;
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}



function isWhitespace (s)
{   // Is s empty?
    return (isEmpty(s) || reWhitespace.test(s));
}

function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to 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 charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

function stripInitialWhitespace (s)
{   var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    return s.substring (i, s.length);
}

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 isPhone (s)
{   if (isEmpty(s))       
       if (isPhone.arguments.length == 1) return true;
       else return (isPhone.arguments[1] == true);
    return rePhone.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 isUSPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return true;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isPhone(s))
}

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 isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    var num = s - 0;
    return ((num >= a) && (num <= b));
}


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 );
}

function isDate (year, month, day)
{   
    if (year.length != 4) return false;
    if (! (isIntegerInRange (month, 1, 12))) return false;
    if (! (isIntegerInRange (day, 0, 31))) return false;
    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);
    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
    return true;
}

function warnEmpty (theField, s)
{ 
    alert(mPrefix + s + mSuffix);
    //theField.focus();
    return false
}

function warnInvalid (theField, s)
{
    alert(s);
    //theField.focus();
    //theField.select(); 
    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 checkLength(theField, maxlen)
{
   if (theField.value.length > maxlen){
      alert("Warning: you have exceeded the max length (" + maxlen + ") for the Interests field.");
      return 0;
   } else return 1; 
}

function checkZIPCode (theField) {   
     var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
     if (! (isInteger(normalizedZIP) && (normalizedZIP.length == digitsInZIPCode1))) 
         return warnInvalid (theField, iZIPCode);
      else 
         return true;
}

var reUN_year = /(_)(\d){4}$/;
var reUN_space = /\s/;

function checkUploadName( theField ) {
     if (reUN_year.test(theField.value)) {
        if (reUN_space.test(theField.value)) {
           alert('Filename cannot have spaces in it');
           return false;
        }
        return true;  
     }
     alert ('The filename must end with the _YYYY, where YYYY is the year');
     return false;
}

function reformatUSPhone (USPhone)
{   if(USPhone.length > 10) {
      return (reformat (USPhone, "", 3, "-", 3, "-", 4, "x",5))
    } else {
      return (reformat (USPhone, "", 3, "-", 3, "-", 4))
    }
}

function checkUSPhone (theField, emptyOK)
{   if (checkUSPhone.arguments.length == 1) emptyOK = true;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (theField.value == '919-') {
          theField.value = '';
          return true;       }
       if (!isPhone(normalizedPhone, false)) 
          return warnInvalid (theField, iUSPhone);
       else 
       {  
          theField.value = reformatUSPhone(normalizedPhone)
          return true;       }
    }
}

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = true;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else return true;
}

function checkYear (theField, emptyOK)
{   if (checkYear.arguments.length == 1) emptyOK = false;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (theField.value < 1955) 
       return warnInvalid (theField, iYear);
    else return true;
}

function checkMonth (theField, emptyOK)
{   if (checkMonth.arguments.length == 1) emptyOK = false;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isIntegerInRange (theField.value, 1, 12)) 
       return warnInvalid (theField, iMonth);
    else return true;
}

function isBday (Bday) {
    if (!isDate(Bday.substr(0,4),Bday.substr(4,2),Bday.substr(6,2))) return false;
    return reBday.test(Bday) }

function reformatBday (Bday) {
   return (reformat (Bday, "", 4, "-", 2, "-", 2)) }

function checkBday (theField)  
{    var normalizedBday = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (!isBday(normalizedBday)) {
          return warnInvalid (theField, iBday);
       } else {
          theField.value = reformatBday(normalizedBday)
          return true;       } 
}

function checkDeleted (theField) {
   if (theField.selectedIndex == 7) 
      alert ("Please be sure to enter a reason in the Notes field for\ndeleting this record since usually deletes are not needed,\ninstead we'd change status to one of the Inactive categories.\nThanks!");
}

function recordDeleted (theField, stamp, notes) {
   if (theField.selectedIndex == 7) {
      notes.value = notes.value + "\nDeleted by " + stamp.value; }
   return true;
}

function checkDate (yearField, monthField, dayField, labelString, OKtoOmitDay)
{    if (checkDate.arguments.length == 4) OKtoOmitDay = false;
    if (theField.length != 4) return false;
    if (!isMonth(monthField.value)) return false;
    if (!isDay(dayField.value)) return false;
    if (isDate (yearField.value, monthField.value, dayField.value))
       return true;
    alert (iDatePrefix + labelString + iDateSuffix)
    return false
}

function check_search_form() {
   var choices=0;
   for(count=0;count < window.document.search_form.showme.length;count++){
      if (window.document.search_form.showme[count].checked == true) {
         choices=choices+1;
      }
   }
   if(choices == 0) {
      alert ("You must check at least one box in the right side of the form"); 
      return false;
   }
   if( (!(isWhitespace(window.document.search_form.search_field1.value))) && (window.document.search_form.test1.selectedIndex == 0)) {
      alert("Incomplete filter1:enter a condition or set search field back to blank");
      return false;
   }
   if((window.document.search_form.search_field1.value == '') && (window.document.search_form.test1.selectedIndex != 0)) { 
      alert("Incomplete filter1:enter a roster field, or set condition back to blank");
      return false;
   }
   if( (!(isWhitespace(window.document.search_form.search_field2.value))) && (window.document.search_form.test2.selectedIndex == 0)) {
      alert("Incomplete filter2:enter a condition or set search field back to blank");
      return false;
   }
   if((window.document.search_form.search_field2.value == '') && (window.document.search_form.test2.selectedIndex != 0)) { 
      alert("Incomplete filter2:enter a roster field, or set condition back to blank");
      return false;
   }
   if( (!(isWhitespace(window.document.search_form.search_field3.value))) && (window.document.search_form.test3.selectedIndex == 0)) {
      alert("Incomplete filter3:enter a condition or set search field back to blank");
      return false;
   }
   if((window.document.search_form.search_field3.value == '') && (window.document.search_form.test3.selectedIndex != 0)) { 
      alert("Incomplete filter3:enter a roster field, or set condition back to blank");
      return false;
   }
   return true;
}

function check_create_letter_form() {
   if( window.document.letters_form.new_letter_name.value == '(new_letter_name)') {
      alert("Enter a name for the new letter.");
      return false;
   }
   if( isWhitespace(window.document.letters_form.new_letter_name.value))  {
      alert("Enter a name for the new letter.");
      return false;
   }
   return true;
}

function checkENewsletter(email1, email2, enewsletter) {
   if (isEmpty(email1.value) && isEmpty(email2.value)) { 
      if (enewsletter.selectedIndex == 0) {
         alert ("Cannot receive eNewsletter if you don't enter a Primary (or Alternate) email addr");
         return false;
      } else {
        return confirm ("Are you sure you don't want to sign up for the email version of our Newsletter (and save the chapter $$)!\nPrinting Jaycall is one of the top 5 expenses for the chapter.\nCancel will take you back to the edit form, where you can select Y for eJaycall and then save..\nOK will save your info without going back to the edit form.");
      }
   }
   if (enewsletter.selectedIndex == 0) { return true }

   if (enewsletter.value == 'Y') { return true }
        return confirm ("Are you sure you don't want to sign up for eJaycall (and save the chapter $$)!\nPrinting Jaycall is one of the top 5 expenses for the chapter.\nCancel will take you back to the edit form, where you can select Y for eJaycall and then save..\nOK will save your info without going back to the edit form.");
}

function checkTitle(theField,s) {
    if (theField.selectedIndex == 4)
       return warnEmpty (theField, s);
    else return true;
}

var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
function rm_from_jm() {
    window.document.mf.rm.selectedIndex = window.document.mf.jm.selectedIndex;}
function ry_from_jy() {
    window.document.mf.ry.value = 1+eval(window.document.mf.jy.value); }
function citycheck() {
    if(window.document.mf.citypick.value != '(pick list)') {
        window.document.mf.city.value = ''; } }
function citycheckb() {
    if(window.document.mf.city.value != '') {
       window.document.mf.citypick.selectedIndex = '12';  } }

function name_ok(last, first) {
   if ( (last.value == ('(last)')) ||
        (last.value == (''))) {
      alert ("Please enter last name (or the first few letters of one)");
      return false;
   }
   if ((first.value == ('(first)')) ||
         (first.value == (''))) {
      alert ("Please enter a first name (or the first few letters of one)");
      return false;
   }
   return true;
}
function clear_field(theField) {
   if ((theField.value == ('(first)')) || (theField.value == ('(last)')) || (theField.value == ('(keyword)'))) {
      theField.value = ''; }
}

