var ValidDict = new Object()

//=====================
// Regular Expressions
//=====================

// FR activity code
ValidDict.ApePat = /^\d{3,4}\s*[a-zA-Z]$/

// Postcode
ValidDict.zipPat = /^\d{5}$/

// Integer > 0
ValidDict.IntegerPat = /^0*[1-9]+[0-9]*$/

// Integer >= 0
ValidDict.ZeroIntegerPat = /^\d+$/

// matches 12 or 17.23 or 14281545,00 or ...
ValidDict.AmountPat = /^-?\d+((\.|,)\d{0,2})?$/

// matches 12 or -17.234865 or 14281545,00 or ...
ValidDict.AmountLowPat = /^\d+((\.|,)\d{0,6})?$/

// Email pattern
ValidDict.EmailPat = /^([\w-]+\.)*[\w-]+\@([\w-]+\.)+[a-zA-Z]{2,4}$/

// URL pattern
ValidDict.UrlPat = /^https?:\/\/[^\/:]+(:\d+)?(\/[\S]*)?$/

// Phone number (french local pattern)
//ValidDict.PhonePat = /^0[1-9]([-.\s]?\d{2}){4,6}/

// International phone number
ValidDict.PhonePat = /^(\+?\s*((\d+|\(\d+\))\s*)*)?\d[\d\.\s\-]*\d$/

// Fax number
ValidDict.FaxPat = /^0[1-9]([-.\s]?\d{2}){4,6}$/

// date
ValidDict.DatePat = /^\d{1,2}[\s\/\.-]\d{1,2}[\s\/\.-](20)?\d{2}$/

// date (FullYear)
ValidDict.DateFullPat = /^\d{1,2}[\s\/\.-]\d{1,2}[\s\/\.-](19|20)\d{2}$/

// hour:min:second
ValidDict.HourMinSec = /([01]\d|2[0-3])(:[0-5]\d){1,2}?$/

ValidDict.VatNumberPat = /^\w{2]\d{8,12}$/

//Rate xxx.xx
ValidDict.RatePat = /^\d{1,3}((\.|,)\d{0,2})?$/

//=====================
// Functions
//=====================
// Contrôle du SIRET société
ValidDict.CheckSiret = function(Siret){
	var coCountry=document.getElementsByName("coCountry")
	coCountry = coCountry && coCountry.length>1? coCountry[0] : coCountry
	if (!coCountry.options) coCountry=document.getElementById("coCountry")
	if(coCountry && coCountry.options){
		coCountry = coCountry.options[coCountry.selectedIndex].value
		switch(coCountry){
		case 'FRA':
			var c, k = 0, n = Siret.length, j = 1 - n%2
			if(isNaN(Siret) || Siret == 0 || n != 14) return false
			for(var i = j; i < n + j; i++){
				c = parseInt(Siret.charAt(i-j)) * (i%2 + 1)
				k += c > 9? c - 9 : c
			}
			return(k % 10 == 0)
		//case 'ITA' : //le siret est un entier a 11 chiffres sans regle structurelle interne (Vu avec Roberto)
		//	return (/^\d{7}\s?\d{3}\s?\d$/.test(Siret))
		}
	}
	return true
}

// Contrôle IBAN
ValidDict.CheckIBAN =
	function(IBAN){

		var strKey = IBAN.substring(2,4)

		//Calculate the key
		var lRIB, lConcat, lNb, lIBAN, lCodeNum, lCodeStr, li, lRetenue, lCle, lNbInterm, lStrInterm

		lRIB = cleanIBAN(cleanIBAN(IBAN.substring(4,IBAN.length)))
		lcodepays = cleanIBAN(IBAN.substring(0,2))

		lConcat=lRIB+lcodepays+"00"

		//Break in blocks of 9 digits for modulo calculation
		li=0
		lRetenue=""
		while (li<eval(lConcat.length))
		{
			lStrInterm=lRetenue+lConcat.substring(li, li+9)
			lNbInterm=parseFloat(lStrInterm)
			lCle=lStrInterm % 97
			lRetenue=""+lCle
			li+=9
		}
		lCodeNum=98-(lCle % 97)
		if (lCodeNum<10)
			lCodeStr="0"+lCodeNum
		else
			lCodeStr=""+lCodeNum

		return(lCodeStr == strKey)

		function cleanIBAN(aChaineNombre)
		{
			var li=0, lNb
			var lChaine=""+aChaineNombre
			var lChaineRes=""

			while (li<eval(lChaine.length))
			{
				if (lChaine.charCodeAt(li)<48 || lChaine.charCodeAt(li)>57)
				{
					if (lChaine.charCodeAt(li)>=65 || lChaine.charCodeAt(li)<=90)
					{
						lNb=lChaine.charCodeAt(li)-55

						lChar=""+lNb
						lChaineRes=lChaineRes+lChar
					}
					else
					{
						if (lChaine.charCodeAt(li)>=97 || lChaine.charCodeAt(li)<=122)
						{
							lNb=lChaine.charCodeAt(li)-87
							lChar=""+lNb
							lChaineRes=lChaineRes+lChar
						}
					}
				}
				else
				{
					lChaineRes=lChaineRes+lChaine.substring(li,li+1)
				}
				li++
			}
			return lChaineRes
		}
	}

//=====================
// Validation
//=====================
function validateForm(theForm)
{
	var v, val
	var elArr = theForm.elements
	for(var i = 0; i < elArr.length; i++) if(elArr[i].type != 'hidden'){
		val = String(elArr[i].value).replace(/^\s+|\s+$/g,'')
		v = elArr[i].getAttribute("validator")
		var ValidFct = v == null? null : (v.charAt(0) == '/'? new RegExp(v.substr(1, v.length - 2)) : (v.charAt(v.length - 1) == ')'? eval(v.substr(0, v.length - 2)) : ValidDict[v]))
		if((val == '' && elArr[i].getAttribute("mandatory") == '1') || (val != '' && ValidFct != null && !(typeof(ValidFct) == 'function'? ValidFct(val) : (ValidFct.exec(val) != null))))
		{
			alert(val == ''? 'Required Field' : elArr[i].getAttribute("errorMsg"))
			elArr[i].focus()
			if(elArr[i].type == 'text' || elArr[i].type == 'password') elArr[i].select()
			return false
		}
	}
	return true
}
