//-----------------------------------------
// MAWFormCheck.js contains selected functions from Netscape FormCheck.js
// Check whether string s is empty.

// whitespace characters
var whitespace = " \t\n\r";

// 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."

var defaultEmptyOK = false


// ----------------------------------------------------------

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}



// ----------------------------------------------------------
// 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 == 3) return defaultEmptyOK;
       else return (isInteger.arguments[4] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// ----------------------------------------------------------
// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


// ----------------------------------------------------------
// 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 ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}


// ----------------------------------------------------------
// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


// 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);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}

// ----------------------------------------------------------
// 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)
{   theField.focus()
    theField.select()
    alert(s)
    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 == 3) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[3] == 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);
    return ((num >= a) && (num <= b));
}


// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // check conatins no spaces?
    if (s.indexOf(" ") != -1) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


// isValidCompName (STRING s [, BOOLEAN emptyOK])
// 
// * contains only lower or upper case letters, numbers, hyphens, spaces
// * no spaces
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isValidCompName (s)
{
	if (isEmpty(s)) 
       if (isValidCompName.arguments.length == 1) return defaultEmptyOK;
       else return (isValidCompName.arguments[1] == true);
    
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // only valid chars
    var i = 0;
    var sLength = s.length;
    while (i < sLength)
    {
		if ( s.charAt(i) != "-" && s.charAt(i) != " " && !(isDigit(s.charAt(i))) && !(isLetter(s.charAt(i))) ) return false;
		i++;
    }

	return true;
}


// isValidUserId (STRING s [, BOOLEAN emptyOK])
// 
// * 4-10 chars in length
// * lower or upper case letters and numbers
// * no spaces
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isValidUserId (s)
{
	if (isEmpty(s)) 
       if (isValidUserId.arguments.length == 1) return defaultEmptyOK;
       else return (isValidUserId.arguments[1] == true);
    
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // is length 4-30
    if (s.length < 4 || s.length > 30) return false;

    // is s alphanumeric - i.e. contains letters or digits only?
    if (!isAlphanumeric(s)) return false;
    
	return true;
}


// isCommonURL (STRING s [, BOOLEAN emptyOK])
// 
// Difficult to validate as valid URLs can vary greatly:
// * for time being check: 
// * begins http://
// * contains no \ chars.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isCommonURL (s)
{   if (isEmpty(s)) 
       if (isCommonURL.arguments.length == 1) return defaultEmptyOK;
       else return (isCommonURL.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
	// does it begin http
    if (s.substr(0,7) != "http://") return false;

	// it should not contain any back slashes "\"
    if (s.indexOf("\\") != -1) return false;

    return true;
}



// isValidPostCode (STRING postCode)
// 
// * check post code is CA1 - CA28 or LA5 - LA23

function isValidPostCode (postCode)
{
	var s;
	
	if (isEmpty(postCode)) 
       if (isValidPostCode.arguments.length == 1) return defaultEmptyOK;
       else return (isValidPostCode.arguments[1] == true);
    
    // is s whitespace?
    if (isWhitespace(postCode)) return false;
    
	var arrSplitPostCode = postCode.split(" ")
	if (arrSplitPostCode.length <= 1) return false;
	
	// Check first part is CA1 - CA28 or LA5 - LA23
//	if ( !((arrSplitPostCode[0].toUpperCase()>="CA1" && arrSplitPostCode[0].toUpperCase()<="CA28") || (arrSplitPostCode[0].toUpperCase()>="LA5" && arrSplitPostCode[0].toUpperCase()<="LA23")) ) return false;
	s = arrSplitPostCode[0].toUpperCase();
	if ( s.substr(0,2) == "CA" )
		{if ( s.substr(2) >= 1  &&  s.substr(2) <= 28) return true;};
	if ( s.substr(0,2) == "LA" )
		{if ( s.substr(2) >= 5  &&  s.substr(2) <= 23) return true;};
	
	return false;
}


// isValidDate (DATE theDate [, BOOLEAN emptyOK])
// 
// * check that date is of the form dd/mm/yyyy

function isValidDate (theDate)
{
	if (isEmpty(theDate)) 
       if (isValidDate.arguments.length == 1) return defaultEmptyOK;
       else return (isValidDate.arguments[1] == true);
    
	// check for 2 "/" delimeters
	var arrSplitDate = theDate.split("/")

	if (arrSplitDate.length != 3) return false;

	for (i = 0; i < 3; i++)
	{
		arrSplitDate[i] = stripWhitespace(arrSplitDate[i]);
	}

	if (!isDate(arrSplitDate[2], arrSplitDate[1], arrSplitDate[0])) return false;
	
	
	return true;
}



function stripLeadingZeros (s)

{   var i = 0;
    while ( (i < s.length) && (s.charAt(i)=="0") )
       i++;
    
    return s.substring (i, s.length);
}


// 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) daysInMonth 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
}


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 ValidSearchDates(strFromDate, strToDate)
{
	if (!isWhitespace(strFromDate))
		{
		// check for 2 "/" delimeters
		var arrSplitDate = strFromDate.split("/");
		if (arrSplitDate.length != 3) return false;
		if (!isDate(arrSplitDate[2], arrSplitDate[1], arrSplitDate[0])) return false;
		}
	if (!isWhitespace(strToDate))
		{
		// check for 2 "/" delimeters
		var arrSplitDate = strToDate.split("/");
		if (arrSplitDate.length != 3) return false;
		if (!isDate(arrSplitDate[2], arrSplitDate[1], arrSplitDate[0])) return false;
		}
	return true;
}




// 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 == 2) || (s.length == 4));
}



// 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, 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 );
}



// 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) >= 0) ) );
}



// 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.
    month = stripLeadingZeros(month);
    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);
    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;
}



// 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 {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}


// 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;
}


// 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);
}


// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripTrailingWhitespace (s)

{   var i;

    i = s.length - 1;
    while ((i >= 0) && charInString (s.charAt(i), whitespace))
       i--;
    
    return s.substring (0, i+1);
}




// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripWhitespace (s)

{   return stripCharsInBag (s, whitespace)
}


