//
//
// 2004/04/15 Yu-Long Kung:
//    - majorly revised/reorganized/added interfances to make it general for all bulletin retrieval
// 2007/04/02 ylk:
//    - added date time reg expression check for new calender input fields
//

// data check
var reWhitespace = /^\s+$/
var reLetter = /^[a-zA-Z]$/
var reAlphabetic = /^[a-zA-Z]+$/
var reAlphanumeric = /^[a-zA-Z0-9]+$/
var reDigit = /^\d/
var reLetterOrDigit = /^([a-zA-Z]|\d)$/
var reInteger = /^\d+$/
var reSignedInteger = /^(\+|-)?\d+$/
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
var reSignedFloat = /^(((\+|-)?\d+(\.\d*)?)|((\+|-)?(\d*\.)?\d+))$/
var reEmail = /^.+\@.+\..+$/
var reRFCEmail=/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
var reDateTime = /^\d{4}\/\d{1,2}\/\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}/
var reDate = /^\d{4}\/\d{1,2}\/\d{1,2}/
var reTime = /^\d{1,2}:\d{1,2}:\d{1,2}/

// VARIABLE DECLARATIONS
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

// whitespace characters as defined by this sample code
var whitespace = " \t\n\r";

// non-digit characters which are allowed in 
// RC Date
var RCDATEDelimiters = "-";

// Date has 8 digits.
// Formatted as YYYY-MM-DD.
var digitsInRCDATE = 8;

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

// m is an abbreviation for "missing"
var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."

// s is an abbreviation for "string"
var sRCDATE = "Date"

// i is an abbreviation for "invalid"
var iDATE = "The 'Date' field must be a valid value (like 1999-09-01). Please reenter it now."
var iDateTime = "The 'Date Time' field must be a valid value (like 2000/01/01 00:00:00). Please reenter it now."
var iLAT = "The 'Latitude' field must be a valid value (like -45.0). Please reenter it now."
var iLATF = "The 'Latitude' field must be a valid float value between -90.0 and 90.0. Please reenter it now."
var iLATI = "The 'Latitude' field must be a valid integer value between -90 and 90. Please reenter it now."
var iLATIP = "The 'Latitude' field must be a valid positive integer value between 0 and 90. Please reenter it now."
var iLON = "The 'Longitude' field must be a valid value (like -100.0). Please reenter it now."
var iLONF = "The 'Longitude' field must be a valid float value between -180.0 and 180.0. Please reenter it now."
var iLONI = "The 'Longitude' field must be a valid integer value between -180 and 180. Please reenter it now."
var iLONIP = "The 'Longitude' field must be a valid positive integer value between 0 and 180. Please reenter it now."
var iSecond = "The 'Second' field must be a number between 0.0 and 59.  Please reenter it now."
var iMinute = "The 'Minute' field must be a number between 0 and 59.  Please reenter it now."
var iHour = "The 'Hour' field must be a number between 0 and 23.  Please reenter it now."
var iDay = "The 'Day' field must be a number between 1 and 31 (28 or 29).  Please reenter it now."
var iMonth = "The 'Month' must be a number between 1 and 12.  Please reenter it now."
var iYear = "The 'Year' field must be a 4 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 iEMail = "Invalid EMail address. Please enter a valid EMail address."

var iDepth = "The 'Depth' field must be a float number between 0 and 700 km.  Please reenter it now."
var iMag = "The 'Magnitude' field must be a float number.  Please reenter it now."
var iYield = "The 'Yield' field must be a float number.  Please reenter it now."
var iNDef = "The 'Num of Def Phases' field must be an integer number.  Please reenter it now."
var iDateTimeRange = "The start date time is greater than or equal to end date time. Please reenter both fields now."


// p is an abbreviation for "prompt"
var pEntryPrompt = "Please enter a "
var pRCDATE = "date value in the YYYY-MM-DD format."
var pLAT = " latitude value like (-)45.0."
var pLON = " longitude value like (-)100.0."
var pDay = "day number between 1 and 31 (29 or 28)."
var pMonth = "month number between 1 and 12."
var pYear = "2 or 4 digit year number."


var cDaysInMonth = makeArray(13);
cDaysInMonth[1] = 31;
cDaysInMonth[2] = 29;   // must programmatically check this
cDaysInMonth[3] = 31;
cDaysInMonth[4] = 30;
cDaysInMonth[5] = 31;
cDaysInMonth[6] = 30;
cDaysInMonth[7] = 31;
cDaysInMonth[8] = 31;
cDaysInMonth[9] = 30;
cDaysInMonth[10] = 31;
cDaysInMonth[11] = 30;
cDaysInMonth[12] = 31;

var dInMonth = 31; // days in month, used in 'isDay interface'

var  monthName= new Array(13);
monthName[1] = 'January';
monthName[2] = 'February'; 
monthName[3] = 'March';
monthName[4] = 'April';
monthName[5] = 'May';
monthName[6] = 'June';
monthName[7] = 'July';
monthName[8] = 'August';
monthName[9] = 'September';
monthName[10] = 'October';
monthName[11] = 'November';
monthName[12] = 'December';


// Global variable defaultEmptyOK defines default return value 
// for many functions when they are passed the empty string. 
// By default, they will return defaultEmptyOK.
//
// defaultEmptyOK is false, which means that by default, 
// these functions will do "strict" validation.  Function
// isInteger, for example, will only return true if it is
// passed a string containing an integer; if it is passed
// the empty string, it will return false.
//
// You can change this default behavior globally (for all 
// functions which use defaultEmptyOK) by changing the value
// of defaultEmptyOK.
//
// Most of these functions have an optional argument emptyOK
// which allows you to override the default behavior for 
// the duration of a function call.
//
// This functionality is useful because it is possible to
// say "if the user puts anything in this field, it must
// be an integer (or a phone number, or a string, etc.), 
// but it's OK to leave the field empty too."
// This is the case for fields which are optional but which
// must have a certain kind of content if filled in.
var defaultEmptyOK = false



//------------------ JS interfaces ----------------------
// Attempting to make this library run on Navigator 2.0,
// so I'm supplying this array creation routine as per
// JavaScript 1.0 documentation.  If you're using 
// Navigator 3.0 or later, you don't need to do this;
// you can use the Array constructor instead.
function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) cDaysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

// Check whether string s is empty.
function isEmpty(s){   
   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
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, "")
}

// Removes all characters which appear in string bag from string 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;
}

// Removes all characters which do NOT appear in string bag 
// from string s.
function stripCharsNotInBag (s, bag){
   var i;
   var returnString = "";

   // Search through string's characters one by one.
   // If character is 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;
}

// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
function stripWhitespace (s){
   return stripCharsInBag (s, whitespace)
}

// WORKAROUND FUNCTION FOR NAVIGATOR 2.0.2 COMPATIBILITY.
//
// The below function *should* be unnecessary.  In general,
// avoid using it.  Use the standard method indexOf instead.
//
// However, because of an apparent bug in indexOf on 
// Navigator 2.0.2, the below loop does not work as the
// body of stripInitialWhitespace:
//
// while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
//   i++;
//
// ... so we provide this workaround function charInString
// instead.
//
// charInString (CHARACTER c, STRING s)
//
// Returns true if single character c (actually a string)
// is contained within string s.
function charInString (c, s){
   for (i = 0; i < s.length; i++){
      if (s.charAt(i) == c) return true;
   }
   return false
}

// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
function stripInitialWhitespace (s){
   var i = 0;

   while ((i < s.length) && charInString (s.charAt(i), whitespace)){
      i++;
   }
   return s.substring (i, s.length);
}


// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.
function isLetter (c){
   return reLetter.test(c)
}

// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c){
   return reDigit.test(c)
}

// Returns true if character c is a letter or digit.
function isLetterOrDigit (c){
   return reLetterOrDigit.test(c)
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// 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)
}

// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true 
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      true
function isSignedInteger (s){
   if (isEmpty(s)){
      if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
      else return (isSignedInteger.arguments[1] == true);
   }else{
      return reSignedInteger.test(s)
   }
}

// isPositiveInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer > 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isPositiveInteger (s){
   var secondArg = defaultEmptyOK;

   if (isPositiveInteger.arguments.length > 1){
      secondArg = isPositiveInteger.arguments[1];
   }

   // The next line is a bit byzantine.  What it means is:
   // a) s must be a signed integer, AND
   // b) one of the following must be true:
   //    i)  s is empty and we are supposed to return true for
   //        empty strings
   //    ii) this is a positive, not negative, number
   return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) > 0) ) );
}

// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer >= 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isNonnegativeInteger (s){
   var secondArg = defaultEmptyOK;

   if (isNonnegativeInteger.arguments.length > 1){
       secondArg = isNonnegativeInteger.arguments[1];
   }

   // The next line is a bit byzantine.  What it means is:
   // a) s must be a signed integer, AND
   // b) one of the following must be true:
   //    i)  s is empty and we are supposed to return true for
   //        empty strings
   //    ii) this is a number >= 0
   return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) >= 0) ) );
}

// isNegativeInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer < 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isNegativeInteger (s){
   var secondArg = defaultEmptyOK;

   if (isNegativeInteger.arguments.length > 1){
      secondArg = isNegativeInteger.arguments[1];
   }
   // The next line is a bit byzantine.  What it means is:
   // a) s must be a signed integer, AND
   // b) one of the following must be true:
   //    i)  s is empty and we are supposed to return true for
   //        empty strings
   //    ii) this is a negative, not positive, number
   return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) < 0) ) );
}

// isNonpositiveInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer <= 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isNonpositiveInteger (s){
   var secondArg = defaultEmptyOK;

   if (isNonpositiveInteger.arguments.length > 1){
      secondArg = isNonpositiveInteger.arguments[1];
   }

   // The next line is a bit byzantine.  What it means is:
   // a) s must be a signed integer, AND
   // b) one of the following must be true:
   //    i)  s is empty and we are supposed to return true for
   //        empty strings
   //    ii) this is a number <= 0
   return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) <= 0) ) );
}

// isFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is an unsigned floating point (real) number. 
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isFloat (s){
   if (isEmpty(s)){ 
      if (isFloat.arguments.length == 1) return defaultEmptyOK;
      else return (isFloat.arguments[1] == true);
   }
   return reFloat.test(s)
}

// isSignedFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is a signed or unsigned floating point 
// (real) number. First character is allowed to be + or -.
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isSignedInteger, then call isSignedFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isSignedFloat (s){
   if (isEmpty(s)){ 
      if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
      else return (isSignedFloat.arguments[1] == true);
   }else{
      return reSignedFloat.test(s)
   }
}

// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.
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)
   }
}

// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and numbers only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.
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)
   }
}

// reformat (TARGETSTRING, STRING, INTEGER, STRING, INTEGER ... )       
//
// Handy function for arbitrarily inserting formatting characters
// or delimiters of various kinds within TARGETSTRING.
//
// reformat takes one named argument, a string s, and any number
// of other arguments.  The other arguments must be integers or
// strings.  These other arguments specify how string s is to be
// reformatted and how and where other strings are to be inserted
// into it.
//
// reformat processes the other arguments in order one by one.
// * If the argument is an integer, reformat appends that number 
//   of sequential characters from s to the resultString.
// * If the argument is a string, reformat appends the string
//   to the resultString.
//
// NOTE: The first argument after TARGETSTRING must be a string.
// (It can be empty.)  The second argument must be an integer.
// Thereafter, integers and strings must alternate.  This is to
// provide backward compatibility to Navigator 2.0.2 JavaScript
// by avoiding use of the typeof operator.
//
// It is the caller's responsibility to make sure that we do not
// try to copy more characters from s than s.length.
//
// EXAMPLES:
//
// * To reformat a 10-digit U.S. phone number from "1234567890"
//   to "(123) 456-7890" make this function call:
//   reformat("1234567890", "(", 3, ") ", 3, "-", 4)
//
// * To reformat a 9-digit U.S. Social Security number from
//   "123456789" to "123-45-6789" make this function call:
//   reformat("123456789", "", 3, "-", 2, "-", 4)
//
// HINT:
//
// If you have a string which is already delimited in one way
// (example: a phone number delimited with spaces as "123 456 7890")
// and you want to delimit it in another way using function reformat,
// call function stripCharsNotInBag to remove the unwanted 
// characters, THEN call function reformat to delimit as desired.
//
// EXAMPLE:
//
// reformat (stripCharsNotInBag ("123 456 7890", digits),
//           "(", 3, ") ", 3, "-", 4)
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;
}

// isSSN (STRING s [, BOOLEAN emptyOK])
// 
// isSSN returns true if string s is a valid U.S. Social
// Security Number.  Must be 9 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
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)
}

// isRCDATE (STRING s [, BOOLEAN emptyOK])
// 
// isRCDATE returns true if string s is a valid date
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isRCDATE (s){
   if (isEmpty(s)){ 
      if (isRCDATE.arguments.length == 1) return defaultEmptyOK;
      else return (isRCDATE.arguments[1] == true);
   }
   return (isInteger(s) && s.length == digitsInRCDATE)
}

function isLAT (s){ 
   if (isEmpty(s)) {
      if (isLAT.arguments.length == 1) return defaultEmptyOK;
      else return (isLAT.arguments[1] == true);
   }
   return (isSignedFloat(s))
}

function isLON (s){
   if (isEmpty(s)){
      if (isLON.arguments.length == 1) return defaultEmptyOK;
      else return (isLON.arguments[1] == true);
   }
   return (isSignedFloat(s))
}

// isYear (STRING s [, BOOLEAN emptyOK])
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 2 or 4 digits only.
// 
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but 
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isYear (s){
   if (isEmpty(s)){ 
      if (isYear.arguments.length == 1) return defaultEmptyOK;
      else return (isYear.arguments[1] == true);
   }
   if (!isNonnegativeInteger(s)) return false;
   return (s.length == 4);
}

// check LeapYear
//
function isLeapYear (s){
   if(isYear(s) ){
      if ( (s % 4 == 0) && ((s % 100 != 0) || (s % 400 == 0)) ){
         return true;
      }
   }
   return false;
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// 
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isIntegerInRange (s, a, b){
   if (isEmpty(s)){
      if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
      else return (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;

   // Now, explicitly change the type to integer via parseInt
   // so that the comparison code below will work both on 
   // JavaScript 1.2 (which typechecks in equality comparisons)
   // and JavaScript 1.1 and before (which doesn't).
   var num = parseInt (s,10);
   return ((num >= a) && (num <= b));
}

// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isMonth (s){
   if (isEmpty(s)){
      if (isMonth.arguments.length == 1) return defaultEmptyOK;
      else return (isMonth.arguments[1] == true);
   }
   return isIntegerInRange (s, 1, 12);
}

// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isDay (s){
   if (isEmpty(s)){
      if (isDay.arguments.length == 1) return defaultEmptyOK;
      else return (isDay.arguments[1] == true); 
   }  
   return isIntegerInRange (s, 1, dInMonth);
}

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

// daysInMonth (INTEGER year, INTEGER month)
// 
// Given integer argument year,
// returns number of days in February of that year.
function daysInMonth (year,month){
   if(month==2){
      return daysInFebruary(year);
   }
   return cDaysInMonth[month];
}


// 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){
   // 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);

   if (!isYear(intYear)) return false;
   if (!isMonth(intMonth)) return false;
   if (intDay > daysInMonth(intYear,intMonth)) 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
}

// 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 (theField, s){
   alert(s)
   theField.focus()
   theField.select()
   return false
}


/* >>> FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */
// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
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);
   }
   return true;
}

// Check that string theField.value is a valid Year.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
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);
   }
   return true;
}

// Check that string theField.value is a valid Month.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
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);
   }
   return true;
}

// Check that string theField.value is a valid Day.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
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);
   }
   return true;
}


// checkDate (yearField, monthField, dayField, STRING labelString [, OKtoOmitDay==false])
//
// Check that yearField.value, monthField.value, and dayField.value 
// form a valid date.
//
// If they don't, labelString (the name of the date, like "Birth Date")
// is displayed to tell the user which date field is invalid.
//
// If it is OK for the day field to be empty, set optional argument
// OKtoOmitDay to true.  It defaults to false.
function checkDate (yearField, monthField, dayField, 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 (!isYear(yearField.value)) return warnInvalid (yearField, iYear);
   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 (isDate (yearField.value, monthField.value, dayField.value)){
       return true;
   }
   alert (iDatePrefix + labelString + iDateSuffix)
   return false
}

// checkRCDate (dateField, STRING labelString [, OKtoOmitDay==false])
//
// Check that dateField.value form a valid date.
//
// If they don't, labelString (the name of the date, like "Birth Date")
// is displayed to tell the user which date field is invalid.
//
// If it is OK for the day field to be empty, set optional argument
// OKtoOmitDay to true.  It defaults to false.
function checkRCDate (dateField, labelString, OKtoOmitDay){
   // Next line is needed on NN3 to avoid "undefined is not a number" error
   // in equality comparison below.
   if (checkRCDate.arguments.length <= 2) OKtoOmitDay = false;
   var datev = dateField.value;

   var ind1 = datev.indexOf("-");
   var yearv = datev.substring(0,ind1);
   var ind2 = datev.lastIndexOf("-");
   var ind3 = datev.length;
   var monthv = datev.substring(ind1+1,ind2);
   var dayv = datev.substring(ind2+1,ind3);

   var intYear = parseInt(yearv,10);
   var intMonth = parseInt(monthv,10);
   var intDay = parseInt(dayv,10);
   var dim;

   if (!isYear(yearv)) return warnInvalid (dateField, iYear);
   if (!isMonth(monthv)) return warnInvalid (dateField, iMonth);

   dim=daysInMonth(intYear,intMonth);
   if (intDay > dim ){
      return warnInvalid (dateField, "The 'Day' field must be a number between 1 and "+dim+".  Please reenter it now."); 
   }
   return true
}

function checkLAT (theField){ 
   var flat=parseFloat(theField.value);

   if ( isNaN(flat) ){
       return warnInvalid (theField, iLAT);
   }
   if (flat < -90 || flat > 90){
       return warnInvalid (theField, iLATF);
   }
   return true;
}

function checkLON (theField){
   var flon=parseFloat(theField.value);
   if ( isNaN(flon) ){
       return warnInvalid (theField, iLON);
   }
   if (flon < -180 || flon > 180){
       return warnInvalid (theField, iLONF);
   }
   return true;
}

function checkFloat (theField,stringLabel){
   var fv=parseFloat(theField.value);
   if ( isNaN(fv) ){
       return warnInvalid (theField, stringLabel);
   }
   return true;
}

function checkInt (theField,stringLabel){
   var iv=parseInt(theField.value);
   if ( isNaN(iv) || !reInteger.test(theField.value) ){
       return warnInvalid (theField, stringLabel);
   }
   return true;
}

function checkRCYear(theField){
   var iyr=parseInt(theField.value,10);

   if( isNaN(iyr) || !reDigit.test(theField.value) || (iyr < 1000) ){
      return warnInvalid(theField,iYear);
   }
   return true;
}

function checkRCMonth(theField){
   var imon=parseInt(theField.value,10);
   if( isNaN(imon) || imon < 1 || imon > 12){
       return warnInvalid (theField,iMonth);
   }
   return true;
}

function checkRCDay(theField,yearField,monthField){
   var iyear=parseInt(yearField.value,10);
   var imon=parseInt(monthField.value,10);
   var iday=parseInt(theField.value,10);
   var dim=daysInMonth(iyear,imon);
   if(iday < 1 || iday > dim ){
       return warnInvalid (theField,"The 'Day' field must be a number between 1 and "+dim+".  Please reenter it now.");
   }
   return true;
}

function checkRCHour(theField){
   var ihr=parseInt(theField.value,10);
   if( isNaN(ihr) || ihr < 0 || ihr > 24){
       return warnInvalid (theField,iHour);
   }
   return true;
}

function checkRCMinute(theField){
   var iminute=parseInt(theField.value,10);
   if( isNaN(iminute) || iminute < 0 || iminute > 59){
       return warnInvalid (theField,iMinute);
   }
   return true;
}

function checkRCSecond(theField){
   var isec=parseInt(theField.value,10);
   if( isNaN(isec) || isec < 0 || isec > 59){
       return warnInvalid (theField,iSecond);
   }
   return true;
}

function checkRange(minv,maxv,descField,theField){
   if(minv > maxv){
      alert("Error: "+descField+" field minimum "+minv+" > maximum "+maxv+". Please re-enter it now"); 
      theField.focus();
      return false;
   }
   return true;
}

function checkDepth(theField){
   var idep=parseFloat(theField.value);
   if( isNaN(idep) || idep < 0 || idep > 700){
       return warnInvalid (theField,iDepth);
   }
   return true;
}

function enableField(ckbox,field_1,field_2){
   field_1.disabled=!ckbox.checked;
   ckbox.checked? field_1.style.color="#000000" : field_1.style.color="#C2C2C2";
   field_2.disabled=!ckbox.checked;
   ckbox.checked? field_2.style.color="#000000" : field_2.style.color="#C2C2C2";
   if(!field_1.disabled){
      field_1.focus();
   }
   return true;
}

function enable3Field(ckbox,field_1,field_2,field_3){
   field_1.disabled=!ckbox.checked;
   ckbox.checked? field_1.style.color="#000000" : field_1.style.color="#C2C2C2";
   field_2.disabled=!ckbox.checked;
   ckbox.checked? field_2.style.color="#000000" : field_2.style.color="#C2C2C2";
   field_3.disabled=!ckbox.checked;
   ckbox.checked? field_3.style.color="#000000" : field_3.style.color="#C2C2C2";
   if(!field_1.disabled){
      field_1.focus();
   }
   return true;
}

function assign_lat(f,minv,maxv){
   f.min_lat.value=minv;
   f.max_lat.value=maxv;
}

function assign_lon(f,minv,maxv){
   f.min_lon.value=minv;
   f.max_lon.value=maxv;
}


function assign_start_date(f,y,m,d){
   f.start_year.value=y;
   f.start_month.value=m;
   f.start_day.value=d;
}

function assign_end_date(f,y,m,d){
   f.end_year.value=y;
   f.end_month.value=m;
   f.end_day.value=d;
}

function select_start_date(f,y,m,d){
   var i;
   for(i=0;i<f.start_year.length;i++){
      if(f.start_year.options[i].value == y ){
         f.start_year.selectedIndex=i;
      }
   }

   f.start_month.selectedIndex=parseInt(m,10)-1;

   updateDayOption(f.start_year,f.start_month,f.start_day);

   f.start_day.selectedIndex=parseInt(d,10)-1;
}

function select_end_date(f,y,m,d){
   var i;
   for(i=0;i<f.end_year.length;i++){
      if(f.end_year.options[i].value == y ){
         f.end_year.selectedIndex=i;
      }
   }
   
   f.end_month.selectedIndex=parseInt(m,10)-1;

   updateDayOption(f.end_year,f.end_month,f.end_day);

   f.end_day.selectedIndex=parseInt(d,10)-1;
}


function updateDayOption(yopt,mopt,dopt){
   var yidx=yopt.selectedIndex;
   var midx=mopt.selectedIndex;
   var didx=dopt.selectedIndex;

   var m=parseInt(mopt.options[midx].value,10);
   var y=yopt.options[yidx].value;

   var dom=cDaysInMonth[m];

   if(m==2){
      if(!isLeapYear(y)){
         dom=28
      }
   }
   var dlen=dopt.length;

   if(dlen > dom){
      dopt.length=dom;
      if(didx >= dom ){
         dopt.selectedIndex=dom-1;
      }
   }else if (dlen < dom ){
      dopt.length=dom;
      for(i=dlen;i<dom;i++){
         dopt.options[i]=new Option(i+1,i+1);
      }
   }
}

function checkEMail(theField){
   if( ( ! isEmpty(theField.value) ) && reRFCEmail.test(theField.value) ){
      return true;
   }
   return warnInvalid (theField, iEMail);
}

function checkDateTime(theField){
   if( ( ! isEmpty(theField.value) ) && reDateTime.test(theField.value) ){
      return true;
   }
   return warnInvalid (theField, iDateTime);
}

function checkDateTimeRange(startdt, enddt ){
   var sdt=new Date( Date.parse(startdt.value) );
   var edt=new Date( Date.parse(enddt.value) );
   if( sdt < edt ){
      return true;
   }
   return warnInvalid (startdt, iDateTimeRange);
}

