// This file contains the data validation JavaScript functions
// It is included in the HTML pages with forms that need these
// data validation routines.


// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";



/****************************************************************/

function replaceAll (s, fromStr, toStr)
{
	var new_s = s;
	for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++)
	{
		new_s = new_s.replace (fromStr, toStr);
	}
	return new_s;
}

/****************************************************************/

/* PURPOSE:  Since we are using the single tick mark as the
	string delimiter to construct our SQL queries, a string with
	a tick mark in it will cause a SQL error.  Therefore we replace
	all "'" with "''", which eliminates the possibility of a SQL error.
*/

function sqlSafe (s)
{
	var new_s = s;
	new_s = replaceAll (new_s, "'", "|");
	new_s = replaceAll (new_s, "|", "''");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "''");
	return new_s;
}

/****************************************************************/

function makeSafe (i)
{
	i.value = sqlSafe (i.value);
}

/****************************************************************/

// 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)

{   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;
}

/****************************************************************/


// 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
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (objField)
{
	var strField = new String(objField.value);
	var CanSubmit = false;

	// is s whitespace?
	if (isWhitespace(strField)) CanSubmit = false;
	else {
    
		// there must be >= 1 character before @, so we
		// start looking at character position 1 
		// (i.e. second character)
		var i = 1;
		var sLength = strField.length;

		// look for @
		while ((i < sLength) && (strField.charAt(i) != "@"))
		{ i++
		}

		if ((i >= sLength) || (strField.charAt(i) != "@")) CanSubmit = false;
		else i += 2;

		// look for a full stop after the @
		while ((i < sLength) && (strField.charAt(i) != "."))
		{ i++
		}

		// there must be at least one character after the full stop
		if ((i >= sLength - 1) || (strField.charAt(i) != ".")) CanSubmit = false;
		else CanSubmit = true;
	}

  	if (!CanSubmit) alert("You have not entered a valid email address");

	return CanSubmit
}

/****************************************************************/

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function ForceEntry(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField)) {
		alert("You need to enter information for " + FieldName);
		objField.focus();
		objField.select();
		return false;
	}

	return true;
}

/****************************************************************/

// Returns true if the string passed in is a valid number
// (no alpha characters), else it displays an error message

function ForceNumber(objField, FieldName){
	var strField = new String(objField.value);
	var blnNumber = true;
	var i = 0;

	if (isWhitespace(strField)) {
		blnNumber = false;
	} else {
			if (!isNumber(strField)) blnNumber = false;
	}
	if (!blnNumber) {
		alert(FieldName + " must be a valid numeric entry. Please do not use commas or dollar signs or any non-numeric symbols.");
		objField.focus();
	}
	
	return blnNumber;
}

		
/****************************************************************/

// Right trims the string...  Useful for SQL datatypes of CHAR

function RTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0

	for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
		c = str.charAt(i);
		if (whitespace.indexOf(c) == -1)
			endpos = i;
	}

	return str.substring(0,endpos+1);
}

/****************************************************************/

/* PURPOSE: Returns true if the string is a valid number.
*/

function isNumber(strNum){
	var str = new String(strNum);
	var i = 0;

	for (i = 0; i < str.length; i++){
		if (str.charAt(i) < '0' || str.charAt(i) > '9') return false;
	}

	return true;
}

/****************************************************************/

// Displays an alert box with the passed in string...

function PromptErrorMsg(Field,strError){
	alert("You have entered an invalid date for " + strError + ".\nPlease make sure your date format is YYYY-MM-DD.");
	Field.focus();
}


//Checks for a valide time in HH:MM formnat
function isTime(value){
   var hasMeridian = false;
   var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
   if (!re.test(value)) { return false; }
   var values = value.split(":");
   if ( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) { return false; }
   if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) { return false; }
   if (values.length > 2) {
      if ( (parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) ) { return false; }
   }
   return true;
}


// Alerts the user if the string passed in is not a valid time in HH:MM format
function ForceTime(objField, FieldName)
{
	var strField = new String(objField.value);
	if (!isTime(strField)) {
		alert("You need to enter information for " + FieldName);
		objField.focus();
		objField.select();
		return false;
	}

	return true;
}

/****************************************************************/

/* PURPOSE: Checks to see if the string is a valid date. A valid
date is defined as YYYY-MM-DD
*/
function ForceDate(objDate,strField){
	var CanSubmit = true;
	var largeMonth = [1,3,5,7,8,10,12];
	var isLeapYear = 0;
	var maxDay = new Number(30);;
	// parse date string into its components
	var datePat = /(\d{4})-(\d{1,2})-(\d{1,2})/; // checks for date in yyy-mm-dd format
	var dateArray = objDate.value.match(datePat);
	if (dateArray == null){
		CanSubmit = false;
	}else{
		var Year = dateArray[1];
		var Month = dateArray[2];
		var Day = dateArray[3];
		if (Month.charAt(0) == "0") Month = Month.charAt(1);
		if (Day.charAt(0) == "0") Day = Day.charAt(1);

		//Check the day, month and year are in range
		if (Day < 1 || Month > 12 || Month < 1 || Year < 1) CanSubmit = false;

		//Check the day is not too large for the month
		if (CanSubmit) {

			//Check for a leap year
			if ((Year % 4 == 0) && (Year % 100 != 0)) isLeapYear = 1;
			if (Month == 2) maxDay = (28 + isLeapYear);
			for(i=0;i<largeMonth.length;i++){
				if (Month == largeMonth[i]) maxDay = 31;
			}
			if (Day > maxDay) CanSubmit = false;
		}
		
	}
	if (!CanSubmit) alert("The " + strField + " must be valid and in the format YYYY-MM-DD");
	return CanSubmit;
}