<!--
// JavaScript library for form field validation

//var whitespace = " \t\n\r";
var whitespace = "\s";
var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."


var sEmail = "Email"

var iEmail = "This field must be a valid email address (like foo@bar.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 2 or 4 digit year number.  Please reenter it now."
var iYear4 = "This 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 iDate = "This field must be a valid date. Please reenter it now."
// Jas 15/02/2000
var iDate3 = "Please check all three input boxes contain the correct values which are in the form of DD/MM/YYYY."

var iInteger = "This field must be a whole number value. Please reenter it now."
var iPositiveInteger = "This field must be a positive whole number value. Please reenter it now."
var iAlphabetic = "This field must contain only letters. Please reenter it now."
var iNumber = "This field must contain only numeric values. Please reenter it now."
// Jas 28/02/2000 checkYear() also checks if correct days have been entered for a feb
var iFeb = "This date does not contain the correct number of days in February"

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 pYear4 = "4 digit year number."


var numDaysEntered;
var enteredMonth;

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 isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

//Jas 11/02/2000
function formatDate(dd,mm,yyyy){
    if(isEmpty(dd.value)||isEmpty(mm.value)||isEmpty(yyyy.value)){
       	return "";
    } else {    
       return  dd.value + "/" + mm.value + "/" + yyyy.value;
    }
}

function isWhitespace (s)

{   var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    return true;
}

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {
        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++)
    {
        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 isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}


function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}




function isInteger (s)

{   var i;

    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);


    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    return 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];

        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}


function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}


function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}


function isNegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNegativeInteger.arguments.length > 1)
        secondArg = isNegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) );
}


function isNonpositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonpositiveInteger.arguments.length > 1)
        secondArg = isNonpositiveInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) <= 0) ) );
}


function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s))
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == ".") return false;

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == ".") && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    return true;
}


function isSignedFloat (s)

{   if (isEmpty(s))
       if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedFloat.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedFloat.arguments.length > 1)
            secondArg = isSignedFloat.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;
        return (isFloat(s.substring(startPos, s.length), secondArg))
    }
}


function isAlphabetic (s)

{   var i;

    if (isEmpty(s))
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);


    for (i = 0; i < s.length; i++)
    {
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
}

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s))
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    return true;
}

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 isvalidEmailChar (s)
{   var i;

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) || (c=='@') || (c=='.') || (c=='_') || (c=='-') || (c=='+')) ) {
       	return false;
		}
    }

    return true;
}


function isEmail (s)
{   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);

    if (isWhitespace(s)) return false;
    if (!isvalidEmailChar(s)) return false;

    atOffset = s.lastIndexOf('@');

    if ( atOffset < 1 )
        return false;
    else {
 	dotOffset = s.indexOf('.', atOffset);

      if ( dotOffset < atOffset + 2 ||
         dotOffset > s.length - 2 ) {
         return false;
      }
   }
   return true;
}


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));
}

// Phil 29-02-00 Needed to take in only 4 digit date not 2!
function isYear4 (s)
{   if (isEmpty(s))
       if (isYear4.arguments.length == 1) return defaultEmptyOK;
       else return (isYear4.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return (s.length == 4);
}


function isIntegerInRange (s, a, b)
{   if (isEmpty(s))
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    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);
}


function daysInFebruary (year)
{
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day)
{
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    if (intDay > daysInMonth[intMonth]) return false;

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}


function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

//NAB
//aw
//function checkNumber (theField, emptyOK)
//{   if (checkNumber.arguments.length == 1) emptyOK = defaultEmptyOK;
function checkNumber (theField, s, emptyOK)
{   if (checkNumber.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isSignedFloat(theField.value, false))
       return warnInvalid (theField, iNumber);
    else return true;
}

//NAB
function checkInteger (theField, emptyOK)
{   if (checkInteger.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isSignedInteger(theField.value, false))
       return warnInvalid (theField, iInteger);
    else return true;
}

//NAB
function checkPositiveInteger (theField, emptyOK)
{   if (checkPositiveInteger.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isInteger(theField.value, false))
       return warnInvalid (theField, iPositiveInteger);
    else return true;
}

//NAB
function checkAlphabetic (theField, emptyOK)
{   if (checkAlphabetic.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isAlphabetic(theField.value, false))
       return warnInvalid (theField, iAlphabetic);
    else return true;
}

function checkString (theField, s, emptyOK)
{
    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 checkEmail (theField, s, 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 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);
       
    var intMonth = parseInt(enteredMonth);

    if (intMonth == 2 && numDaysEntered > daysInFebruary(theField.value)) {
        intMonth = "";
        numDaysEntered = "";
        return warnInvalid (theField, iFeb);
    } else {
        intMonth = "";
        numDaysEntered = "";
        return true;
    }
}

// Phil 29-02-00 Date input with 4 digit not 2!
function checkYear4 (theField, emptyOK)
{   if (checkYear4.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear4(theField.value, false))
       return warnInvalid (theField, iYear4);
       
    var intMonth = parseInt(enteredMonth);

    if (intMonth == 2 && numDaysEntered > daysInFebruary(theField.value)) {
        intMonth = "";
        numDaysEntered = "";
        return warnInvalid (theField, iFeb);
    } else {
        intMonth = "";
        numDaysEntered = "";
        return true;
    }
}


function checkMonth (theField, emptyOK)
{   if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    
    enteredMonth = theField.value;    
    
    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;
    
    numDaysEntered = theField.value;    
    
    if (!isDay(theField.value, false))
       return warnInvalid (theField, iDay);
    else return true;
}


function checkDate (theField, emptyOK)
{   if (checkDate.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isValidDate(theField.value, false))
       return warnInvalid (theField, iDate);
    else return true;
}

// Jas 15/02/2000 Checks the fields that contain 3 separate entries
function checkDate3 (theField, emptyOK)
{   if (checkDate3.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isValidDate(theField.value, false))
       return false;
    else return true;
}


function isValidDate(s) {	
  d = new Date(s)
  if (isNaN( d.valueOf() ))
    return false;

  sArray = s.split("/")
  if (sArray.length > 3)
    return false;

  md1 = sArray[0];
  md2 = sArray[1];
  y = sArray[2];

  if ( (md1 > 31) || (md1 < 0) || (md2 > 31) || (md2 < 0) || (y < 0) || (y > 99999) )
     return false;

  if ( (md1 > 12) && (md2 > 12))
    return false;

    return true;
}

function checkDateFormat(s) { 

    var i = 0;
    var j = 0; 
    var date = s.value;
    while ((i < date.length) && (j != 2)) {
	var c = date.charAt(i);
	if (c == "/") {
	    j++;
        }
	if (j == 2) {
	    var k = i + 5;
	    if (k == date.length) {
		return true;
	    }
	}
	i++;
    } 
    return false;		
}  

//returns whether a date is in the past or not
//!!!!! will remove one from month as js month is 0 indexed !!!!!
function isPastDate(day,month,year) {
    var date = new Date(year,month-1,day);
    var today = new Date();
    
    if (date.getTime() < today.getTime()) {
        return true;
    } else {
        return false;        
    }
}


