/**
 * Laurent Nardin Consulting & Dev. Informatique
 *
 * Description: various client-side data validation scripts
 * Creation date: 03.02.04
 * Last modification: 26.07.04	added chkIsAmountFld(), chkIsTimeFld()
 *                    11.08.04	chkIsAlNumFld() modification: now accepts ',' & ';' characters
 *                    01.09.04	checkTextFld(), chkIsAlNumFld() & chkIsValidAddressFld() 
 *                              modification: now accepts 'ë' character
 *                    11.10.04	chkIsAlNumFld() now accepts '!' and '?' characters
 * 			 		  15.10.04	accept accent caps for checkTextFld() and chkIsAlNumFld()
 *
 * File name: validation.js
 * 
 * @version 0.6
 * @copyright 2003, 2004
 **/
 
/*	checkTextFld
 *
 *  Check if the data is composed of pure alphabetic characters
 *  Check for the length of this data
 * 
 *  txt: the text to check
 *  minLength: minimum length of the text
 *  maxLength: maximum length of the text
 *  allowQuotes: single quotes allowed if true, not allowed if false
 */ 
function checkTextFld(txt, minLength, maxLength, allowQuotes)
{
	var pattern;
	if (allowQuotes)
	{
		pattern = "^([ \'a-zA-ZçéèàâôûîêëäöüÉÈÀÂÔÛÎÊËÄÏÖÜ\-]){" + minLength + "," + maxLength + "}$";
	}
	else
	{
		pattern = "^([ a-zA-ZçéèàâôûîêëäöüÉÈÀÂÔÛÎÊËÄÏÖÜ\-]){" + minLength + "," + maxLength + "}$";
	}
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*	checkEmailFld
 *
 *  Check if the data is composed of a valid e-mail address
 * 
 *  txt: the text to check
 */ 
function checkEmailFld(txt)
{
	var pattern;
	pattern = "^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+[\.][-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$";
	
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*  chkDateFld
 *  Check if the data is composing a valid european date
 *  (dd-mm-yyyy) or (dd/mm/yyyy) or (dd.mm.yyyy)
 * 
 *  txt: the date to check
 *  emptyAllowed: if true, an empty date is allowed, not allowed otherwise
 */ 
 function chkDateFld(txt, emptyAllowed)
 {
 	if(emptyAllowed && txt.length==0)
	{
		return true;
	}
	var pattern = "[0-9]{1,2}[-/\.][0-9]{1,2}[-/\.][0-9]{4}";
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

/*  chkIsNumericFld
 *  Check if the data is composed of a valid numeric field
 * 
 *  txt: the numeric value to check
 *  minLength: minimum length of the text
 *  maxLength: maximum length of the text
 */ 
 function chkIsNumericFld(txt, minLength, maxLength)
 {
	var pattern = "^([0-9]){" + minLength + "," + maxLength + "}$";
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

/*  chkIsTimeFld
 *  Check if the data is composed of a valid time field ("12:00")
 * 
 *  txt: the time value to check
 *  emptyAllowed: true->an empty value is allowed, false->not allowed
 */ 
 function chkIsTimeFld(txt, emptyAllowed)
 {
 	if(txt.length==0 && emptyAllowed)
		return true;
	var pattern = "^([0-9]{1,2}):([0-9]{2})$";
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

/*  chkIsAmountFld
 *  Check if the data is composed of a valid amount field
 * 
 *  txt: the numeric value to check
 *  minLength: minimum length of the integral part
 *  maxLength: maximum length of the integral part
 *  The fractional part which is optional must have a length of 2 digits
 *  Example of valid value '120', '135.45'
 */ 
 function chkIsAmountFld(txt, minLength, maxLength)
 {
	var pattern = "^(([0-9]{" + minLength + "," + maxLength + "})[.]([0-9]{2})|([0-9]{" + minLength + "," + maxLength + "}))$";
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

/*  chkIsAlNumFld
 *  Check if the data is composed of alpha numeric characters
 *  Line feed and cariage return accepted
 *  Check for the length of this data
 * 
 *  txt: the text to check
 *  minLength: minimum length of the text
 *  maxLength: maximum length of the text
 *  quotesAllowed: single quotes allowed if true, not allowed if false
 */ 
 function chkIsAlNumFld(txt, minLength, maxLength, quotesAllowed)
 {
	var pattern;
	if(quotesAllowed)
	{
		pattern = "^([ a-zA-Z0-9çéèàâôûîêëäöüÉÈÀÂÔÛÎÊËÄÏÖÜ\'.,;!?/\:\-]){" + minLength + "," + maxLength + "}$";
	}
	else
	{
		pattern = "^([ a-zA-Z0-9çéèàâôûîêëäöüÉÈÀÂÔÛÎÊËÄÏÖÜ.,;!?/\:\-]){" + minLength + "," + maxLength + "}$";
	}
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
 }
 
/*  chkLengthFld
 *  Check only for the length validity of the field
 *  Line feed and cariage return accepted, a multi-line field is accepted
 *  Check for the length of this data
 * 
 *  $txt: the text to check
 *  $minLength: minimum length of the text
 *  $maxLength: maximum length of the text
 */ 
 function chkLengthFld(txt, minLength, maxLength)
 {
	var pattern = "^(.|[\r\n]){" + minLength + "," + maxLength + "}$";

	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

/*
 *  Check if the data is a well formed phone field
 *  Check for the length of this data
 * 
 *  $txt: the text to check
 *  $minLength: minimum length of the text
 *  $maxLength: maximum length of the text
 */ 
 function chkIsValidPhoneFld(txt, minLength, maxLength)
 {
	var pattern = "^([0-9.,-/()\+\040]){" + minLength + "," + maxLength + "}$";
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

/*  chkIsValidAddressFld
 *  Check if the data is a well formed address field
 *  Check for the length of this data
 * 
 *  txt: the text to check
 *  minLength: minimum length of the text
 *  maxLength: maximum length of the text
 *  quotesAllowed: single quotes allowed if true, not allowed if false
 */ 
 function chkIsValidAddressFld(txt, minLength, maxLength, quotesAllowed)
 {
	var pattern;
	if(quotesAllowed)
	{
		pattern = "^([ (),;:\.\'a-zA-Z0-9çéèàâôûîêëäöü\-]){" + minLength + "," + maxLength + "}$";
	}
	else
	{
		pattern = "^([ (),;:\.a-zA-Z0-9çéèàâôûîêëäöü\-]){" + minLength + "," + maxLength + "}$";
	}
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

/*  chkIsValidPostCodeFld
 *  Check if the data is a well formed postal code field
 *  Check for the length of this data
 * 
 *  txt: the text to check
 *  minLength: minimum length of the text
 *  maxLength: maximum length of the text
 */ 
 function chkIsValidPostCodeFld(txt, minLength, maxLength)
 {
	var pattern = "^([0-9]){" + minLength + "," + maxLength + "}$";
	var exp = new RegExp(pattern);
	if (exp.test(txt))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

