<!--
	// 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";


/****************************************************************/

// PURPOSE: Check to see if a radio control on a form has 
// one element selected.
function ForceRadio(objField, FieldName)
{
	var i = 0;
    radioOK = false;
	for (i = 0; i < objField.length; i++) 
	{
		if (objField[i].checked) radioOK = true;
	}
	if (!radioOK) 
	{
		alert('Please select an option for' + FieldName)
		return false;
	}
	return true;
}

/****************************************************************/

// PURPOSE:  Check to see if the string passed in is a valid time.
//	A valid time is defined as a string which is postfixed with either
//  "PM" or "AM".  Next it checks to see if there is a colon in the
//  string.  If there is, it makes sure that at least one digit preceeds
//  it and two proceed it.
function IsTime(strTime)
{
	var strTestTime = new String(strTime);
	strTestTime.toUpperCase();

	var bolTime = false;

	if (strTestTime.indexOf("PM",1) != -1 || strTestTime.indexOf("AM",1))
	{
		bolTime = true;
	}

	if (bolTime && strTestTime.indexOf(":",0) == 0)
	{
		bolTime = false;
	}

	var nColonPlace = strTestTime.indexOf(":",1);
	if (bolTime && ((parseInt(nColonPlace) + 5) < (strTestTime.length - 1) || (parseInt(nColonPlace) + 4) > (strTestTime.length - 1)))
	{
		bolTime = false;
	}

	return bolTime;
}

/****************************************************************/

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;
}

/****************************************************************/

// isEmail (Field)
// 
// 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
//note: if blank field is ok that needs to be tested separately
function isEmail(objField)
{
	var strField = new String(objField.value);
	emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[a-zA-Z]$"
	var regex = new RegExp(emailReg);
	if (regex.test(strField)) 
	{
		return true;
	}
	else 
	{
		alert("Please enter a valid E-Mail address")
		return false;
	}
}

/****************************************************************/

// 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, ZeroAllowed)
{
	var strField = new String(objField.value);
	
	if (ZeroAllowed == 'N')
	{
		if (strField == 0) 
		{
			alert(FieldName + " must be non zero");
			objField.focus(); 
			return false;
		}
	}
	if (isWhitespace(strField)) return true;

	var i = 0;

	for (i = 0; i < strField.length; i++)
	{
		if (strField.charAt(i) < '0' || strField.charAt(i) > '9'  ) 
		{
			if ((strField.charAt(i) == '-' && i == 0))
			{
				// Added to ease equality
			}
			else 
			{
				alert(FieldName + " must be a valid numeric entry.  Please do not use spaces, commas, dollar signs or any non-numeric symbols.");
				//	 objField.focus();
				return false;
			}
		}
	}
	return true;
}

/****************************************************************/

// Returns true if the string passed in is a valid money
//  (no alpha characters except a decimal place), 
//   else it displays an error message
function ForceMoney(objField, FieldName, ZeroAllowed)
{
	var strField = new String(objField.value);
	
	if (isWhitespace(strField)) return true;

	if (ZeroAllowed == 'N')
	{
		if (strField == 0) 
		{
			alert(FieldName + " must be non zero");
			// objField.focus(); 
			return false;
		}
	}        

	var i = 0;

	for (i = 0; i < strField.length; i++)
	{
		if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && (strField.charAt(i) != '.') && (strField.charAt(i) != '-')) 
		{
			alert(FieldName + " must be a valid numeric entry.  Please do not use commas or Pound signs or any non-numeric symbols.");
			//objField.focus();
			return false;
		}
	}

	return true;
}

/****************************************************************/

// 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 date number.
	A method is passed in (1 = day, 2 = month).  If the string is
	nonnumeric, false is passed back.  If the day in the date string
	is greater than 31, false is returned.  If the month is greater
	than 12, an error is returned.
*/
function isDateNumber(strNum,method)
{
	var str = new String(strNum);
	var i = 0;

	if (isNaN(parseInt(str)) || parseInt(str) < 0) return false;

	if (method == 1)
	{
		if (parseInt(str) > 31)
		{
			return false;
		}
	}
	if (method == 2)
	{
		if (parseInt(str) > 12)
		{
			return false;
		}
	}

	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 in dd/mm/yyyy format.");
	Field.focus();
}

/****************************************************************/

/* PURPOSE: Checks to see if the string is a valid date.  A valid
	date is defined as any of the following:

		DD/MM/YY, DD/DD/YYYY, D/M/YY, D/M/YYYY,
		DD-MM-YY, DD-MM-YYYY, D-M-YY, D-M-YYYY
*/
function ForceDate(strDate,strField)
{
	var str = new String(strDate.value);

	if (isWhitespace(str)) 
	{
		return true;
		// if the field is empty, just return true...
	}

	var i = 0, count = str.length, j = 0;
	while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
	{
		i++;
	}
	
	if (i == count || i > 2) 
	{
		PromptErrorMsg(strDate,strField);
		return false;
	}

	var addOne = false;
	if (i == 2) addOne = true;

	if (!isDateNumber(str.substring(0,i),1)) 
	{
		PromptErrorMsg(strDate,strField);
		return false;
	}

	j = i+1;
	i = 0;

	while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
	{
		i++;
	}
	
	if (i+j == count || i > 2) 
	{
		PromptErrorMsg(strDate,strField);
		return false;
	}

	if (!isDateNumber(str.substring(j,i+j),2)) 
	{
		PromptErrorMsg(strDate,strField);
		return false;
	}

	j = i+3;
	i = 0;

	if (addOne) j++;

	while (i+j < count) { i++; }

	if (i != 2 && i != 4) 
	{
		PromptErrorMsg(strDate,strField);
		return false;
	}

	if (!isDateNumber(str.substring(j,i+j),3)) 
	{
		PromptErrorMsg(strDate,strField);
		return false;
	}

	return true;
}


/****************************************************************/

// This function ensures that a field is greater than or equal to the
// Length passed in.  You must call this function with the element
// name in your form (for example: "ForceMinLength(document.forms[0].txtElement)"
// as opposed to "ForceMinLength(document.forms[0].txtElement.value)"
// If the field's value is too small, an error message is displayed
// and false is returned, else true is returned.
function ForceMinLength(objField, nLength, strWarning)
{
	var strField = new String(objField.value);

	if (strField.length < nLength) 
	{
		alert(strWarning);
		objField.focus();
		return false;
	}
	else
	{
		return true;
	}
}

/****************************************************************/

// This function ensures that a field is less than or equal to the
// Length passed in.  You must call this function with the element
// name in your form (for example: "ForceMaxLength(document.forms[0].txtElement)"
// as opposed to "ForceMaxLength(document.forms[0].txtElement.value)"
// If the field's length is too large, an error message is displayed
// and false is returned, else true is returned.
function ForceMaxLength(objField, nLength, strWarning)
{
	var strField = new String(objField.value);

	if (strField.length > nLength) 
	{
		alert(strWarning);
		objField.focus();
		return false;
	}
	else
	{
		return true;
	}
}

/****************************************************************/

// This function ensures that a pair of passed strings are the same
// Most useful for when a user is changing a password
// You must call this function with the element names in your form 
// (for example: 
// "ForceMatchString(document.forms[0].txtElement1, document.forms[0].txtElement2, "error message")"
// If the strings do not match an error message is displayed
// and false is returned, else true is returned.
function ForceMatchString(objField1, objField2, strWarning)
{
	var strField1 = new String(objField1.value);
	var strField2 = new String(objField2.value);

	if (String(strField1) != String(strField2)) 
	{
		alert(strWarning);
		objField2.value = "";
		objField2.focus();

		return false;
	} 
	else
	{
		return true;
	}
}

/******************************************************************/

//function to check that a numeric value is within a given range
//You must call this function with the element names from the form
//e.g. ForceRange(document.frmFormName.elementname,minimum,maximum,"field name")
function ForceRange(objField,strMinimum,strMaximum,strFieldName)
{
  var strField = new String(objField.value);
	
	if (!isWhitespace(strMinimum)) 
	{
		if (strField < strMinimum) 
		{
		    alert(strFieldName + " cannot be less than " + strMinimum)
			objField.value = "";
			objField.focus();
			return false;
		}
	} 

	if (!isWhitespace(strMaximum)) 
	{
		if (strField > strMaximum) 
		{
		    alert(strFieldName + " cannot be greater than " + strMaximum)
			objField.value = "";
			objField.focus();
			return false;
		}
	} 
	
	return true;
}

/************************************/

function GetNoOfDays(objField,objField2)
{
	var tmpDate = objField.value.split('/');
	date1 = new Date();
	date2 = new Date();

	// date1.setTime(ObjDate1.value);

	date1.setYear(tmpDate[2]);
	date1.setMonth((tmpDate[1]-1));
	date1.setDate (tmpDate[0]);
	
	var tmpDate = objField2.value.split('/');
	date2.setYear(tmpDate[2]);
	date2.setMonth((tmpDate[1]-1));
	date2.setDate (tmpDate[0]);

	var diff = (date1.getTime() - date2.getTime());
	var nodays
	nodays = Math.floor ((diff /(1000 * 60 * 60 * 24))+1);

	return nodays;
} 

function ForceFuture(objField,strField)
{
	var tmpDate = objField.value.split('/');
	date1 = new Date();
	date2 = new Date();

	date1.setYear(tmpDate[2]);
	date1.setMonth((tmpDate[1]-1));
	date1.setDate (tmpDate[0]);

	var diff = (date1.getTime() - date2.getTime());
	var nodays
	nodays = Math.floor ((diff /(1000 * 60 * 60 * 24))+1);

	if ( nodays < 1 ) 
	{
		alert("The date you have entered for " + strField + " is in the past.");
		return false;
	}
	else 
	{
		return true;
	}
}

/****************************************************************/

// ForceWord (objField,"strName")
// 
//  objField can only contain characters A-Z, a-z, 0-9 and _
//note: if blank field is not ok that needs to be tested separately
function ForceWord(objField,strName)
{
	var strField = new String(objField.value);
	wordReg = "^[\\w]*$";
	var regex = new RegExp(wordReg);
	if (regex.test(strField)) 
	{
		return true;
	}
	else
	{
		alert(strName + " should only contain letters, numbers and the underscore character")
		objField.focus();
		return false;
	}
}


/****************************************************************/

// ForceTime (objField,"strName") 
// 
// Times must be pass in the format XX:YY:YY where X is between 0-23 and y 0-59
function ForceTime(strTime,strField) 
{
	var myTime = new String (strTime.value);
	var time_array = myTime.split(":");
	var CanSubmit;
	CanSubmit= false;

	if (time_array.length != 3)
	{
		alert("Please enter a valid Time for " + strField);
		return false;
		CanSubmit = false; 
	} 
	else 
	{
		CanSubmit = true; 
	}

	if (CanSubmit) 
	{
		for (var loop=0; loop < 3; loop++)
		{
			if (CanSubmit)
			{
				for (i = 0; i < time_array[loop].length; i++) 
				{

					if (time_array[loop].charAt(i) < '0' || time_array[loop].charAt(i) > '9'  ) CanSubmit = false;
					if (CanSubmit) 
					{
						if (loop == 0 )
						{
							if ( Number(time_array[loop]) > Number("23") ) 
							{
								alert("Please enter a valid Time for " + strField);
								return false;
								CanSubmit = false; 
							}
						} 
						else 
						{
							if ( Number(time_array[loop]) > Number("59") )  
							{
								alert("Please enter a valid Time for " + strField);
								return false;
								CanSubmit = false; 
							}
						}
					}
				} // for
			} // if
		} // for
	}

	if (!CanSubmit) 
	{
		alert("Please enter a valid Time for " + strField);
		return false;
	}
   
   return true;
}


/****************************************************************/

// ForcePhone(objField, FieldName, ZeroAllowed)
// 
// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message
function ForcePhone(objField, FieldName)
{
	var strField = new String(objField.value);
	
	if (strField == 0) 
	{
		alert(FieldName + " must be non zero");
		objField.focus(); 
		return false;
	}
	if (isWhitespace(strField)) return false;

	var i = 0;

	for (i = 0; i < strField.length; i++)
	{
		if (strField.charAt(i) < '0' || strField.charAt(i) > '9'  ) 
		{
			if ((strField.charAt(i) == '(' || strField.charAt(i) == ')' || strField.charAt(i) == '+' || strField.charAt(i) == ' '))
			{
				// Added to ease equality
			}
			else 
			{
				alert(FieldName + " must be a valid telephone number.  Please do not use commas, dollar signs or any non-numeric symbols.");
				return false;
			}
		}
	}
	
	return true;
}

/****************************************************************/

// ForceLength(objField, FieldName, MinLength)
// 
// Forces minimum length of filed
function ForceLength(objField, FieldName, MinLength)
{
	if (objField.value.length < MinLength)
	{
		alert(FieldName + " needs to be at least " + MinLength + " characters in length.");
		return false;
	} 
	return true;	
}
// -->
