//-------- form validation functions

var RegEx_Postal = "^\\d{5}-\\d{4}|\\d{5}|[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d$"
var RegEx_EMail = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$"
var RegEx_Phone = "^\\(?[0-9]{3}\\)?[ -.]?[0-9]{3}[ -.]?[0-9]{4}[ -.]?[0-9]*$"

var RegEx_Phone_Prompt = "S'il vous pla"+String.fromCharCode(238)+"t, entrez un num"+String.fromCharCode(233)+"ro de t"+String.fromCharCode(233)+"l"+String.fromCharCode(233)+"phone valide, y compris le code r"+String.fromCharCode(233)+"gional.\nLes formats accept"+String.fromCharCode(233)+"s sont:\n\n(555)-555-5555\n555-555-5555\n555.555.5555\n\nUne extension mai "+String.fromCharCode(233)+"galement "+String.fromCharCode(234)+"tre ajout"+String.fromCharCode(233)+"s (un ou plusieurs chiffres)."
// &

// Reg Ex references: 
//		http://www.regular-expressions.info/reference.html
//		http://msdn.microsoft.com/en-us/library/1400241x(VS.85).aspx
//
// meta characters:  [\^$.|?*+()	to match these, add \\ in front
//		? match preceding token 0 or more times
//		+ match preceding token 1 or more times
//		. match any character
//		^ match the start of the string
//		$ match the end of the string
//		\d match digit
//		\D match non-digit
//		\w match word character
//		\W match non-word
//		\s match space
//		\S match non-space
//		[^ negate match (caret inside [



function ValidateForm (theForm)
{
	//*** client side form validation
	//*** auto field validation 
	for (var i=0;i<theForm.elements.length;i++)
	{
		var e = theForm.elements[i]
		if (e.getAttribute("required") == "true")
			if (NotFilled(e))
				return false
		switch (e.getAttribute("validation"))
		{
			case "number":
				if (NotNumeric(e))
					return false
				break
			case "email":
				if (NotEmail(e))
					return false
				break
			case "date":
				if (NotDate(e))
					return false
				break
			case "postal":
				if (NotPostalZip(e))
					return false
				break
			case "phone":
				if (NotPhoneNumber(e))
					return false
				break
		}
	}
	return true
}


function NotFilled (fld)
{
	if (fld.type == "checkbox")
		if (!fld.checked)
		{
			alert ("Case doit "+String.fromCharCode(234)+"tre coch"+String.fromCharCode(233)+"e.")
			fld.focus ()
			fld.select ()
			return true
		}
	if (fld.value == "")
	{
		alert ("Champs obligatoires est manquant. S'il vous pla"+String.fromCharCode(238)+"t entrer.")
		fld.focus ()
		return true
	}
	return false
}

function NotNumeric (fld)
{
	if (fld.value == "")
		return false
	if (isNaN(fld.value))
	{
		alert ("S'il vous pla"+String.fromCharCode(238)+"t, entrez un num"+String.fromCharCode(233)+"ro.")
		FocusField (fld)
		return true
	}
	return false
}

function NotDate (fld, cantBeBlank)
{
	if (fld.value == "")
		if (cantBeBlank)
		{
			alert ("S'il vous pla"+String.fromCharCode(238)+"t entrer une date soit comme MMM JJ AAAA (ex.: le 31 janvier 2000), ou MM / JJ / AAAA (ex.: 1/31/2000).")
			FocusField (fld)
			return true
		}
		else
			return false
	try
	{
		var d = new Date (fld.value)
		if (isNaN(d))
		{
			alert ("S'il vous pla"+String.fromCharCode(238)+"t entrer une date soit comme MMM JJ AAAA (ex.: le 31 janvier 2000), ou MM / JJ / AAAA (ex.: 1/31/2000).")
			FocusField (fld)
			return true
		}
		if (d.getFullYear() < 1900)
		{
			alert ("S'il vous pla"+String.fromCharCode(238)+"t entrer une ann"+String.fromCharCode(233)+"e 1900 ou une version ult"+String.fromCharCode(233)+"rieure.")
			FocusField (fld)
			return true
		}
		return false
	}
	catch (e)	{	}
	alert ("S'il vous pla"+String.fromCharCode(238)+"t entrer une date soit comme MMM JJ AAAA (ex.: JAN 31 2000), ou MM/JJ/AAAA (ex.: 1/31/2000) .")
	FocusField (fld)
	return true
}

function NotPostalZip (fld)
{
	if ((fld.value != "") && (!ValidateRegEx (fld.value, RegEx_Postal)))
	{
		alert ("S'il vous pla"+String.fromCharCode(238)+"t entrer un code postal valide ou code postal.")
		FocusField (fld)
		return true
	}
	return false
}

function NotEmail (fld)
{
	if ((fld.value != "") && (!ValidateRegEx (fld.value, RegEx_EMail)))
	{
		alert ("S'il vous pla"+String.fromCharCode(238)+"t entrer une adresse email valide.")
		FocusField (fld)
		return true
	}
	return false
}

function NotPhoneNumber (fld)
{
	if ((fld.value != "") && (!ValidateRegEx (fld.value, RegEx_Phone)))
	{
		alert (RegEx_Phone_Prompt)
		FocusField (fld)
		return true
	}
	return false
}

function ValidateRegEx (strSearch, expr)
{
	try
	{
		var regEx = new RegExp (expr, "i")
	}
	catch (e)
	{
		alert ('regular expression error: ' + e)
	}
	var arr = strSearch.match(regEx)
	return ((arr != null) && (arr.length == 1) && (strSearch == arr[0]))
}

function FocusField (fld)
{
	try
	{
		fld.focus ()
		fld.select ()
	}
	catch (e) {}
}
