/////////////////////////////////////////////////
//
// Fonctions Javascript de validation
//
/////////////////////////////////////////////////

/*
les var msg_xxx (alert) sont positionnées par l'appelant (multilingue via php)
Sur le ou les chp, positionne le ou les attribs à la value correspondante.
*/
function set_attribut(form, chps, attribs, values)
{
	var OK = true;
	var el, type

	if (typeof chps == 'string') {
		_set_attribut_1_chp(form, chps, attribs, values);
	}
	else {
		for (var i=0; i<chps.length; i++) {
			_set_attribut_1_chp(form, chps[i], attribs, values);
		}  // for (var i in chps) {
	}
}

function _set_attribut_1_chp(form, chp, attribs, values)
{
	var OK = true;
	var el, type
	if (typeof form.elements[chp] == "object") {
		el = form.elements[chp]
		type = el.type
		if ((type == "select-multiple") || (type == "select-one") || (typeof el.length == "undefined")) {
			// un seul champ de ce nom
			if (typeof attribs == 'string') {
				eval('el.' + attribs +" = values;");
			}
			else {
				// plusieurs attributs
				for (var i=0; i<attribs.length; i++) {
					eval('el.' + attribs[i] +" = values["+ i +"];");
				}  // for (var i in chps) {
			}

		} 
		else {
			// plusieurs champs de ce nom
			for (var j = 0; j < el.length; j++) {
				if (typeof attribs == 'string') {
					eval('el[j].' + attribs +" = values;");
				}
				else {
					// plusieurs attributs
					for (var i=0; i<attribs.length; i++) {
						eval('el[j].' + attribs[i] +" = values["+ i +"];");
					}  // for (var i in chps) {
				}
			} 
		}  // plusieurs champs de ce nom
	}  // (typeof form.elements[chp] == "object") 
}

// Vérifie que les chps_obl ne sont pas vides.
// met le backgroundColor des chps vides à bgcolorErr.
// positionne le focus() sur le premier d'entre eux
// fait un alert() et retourne false
function _verif_chp_oblig(form, chps_obl)
{
	var OK = true;
	var chp_obl, el, el_1, elFocus;

	//for (var i in chps_obl) {
	for (var i=0; i<chps_obl.length; i++) {
		chp_obl = chps_obl[i]
		if (typeof form.elements[chp_obl] == "object") {
			el = form.elements[chp_obl]
			var type = el.type
			if ((type == "select-multiple") || (type == "select-one") || (typeof el.length == "undefined")) {
				// un seul champ de ce nom
				if (! _verif_1_chp_oblig(el)) {
					if (OK) 
						elFocus = el
					OK = false
					// break;
				}
			} 
			else {
				// plusieurs champs de ce nom
				el_1 = el[0]
				if (el_1.type == 'checkbox' || el_1.type == 'radio') {
					// combien de cases cochée ???
					if (_VerifCheckBox(form, chp_obl) == 0) {
						el_1.style.backgroundColor = bgcolorErr
						if (OK) 
							elFocus = el_1
						OK = false
					}
				} 
				else {
					// tester chaque champ
					for (var j = 0; j < el.length; j++) {
						if (! _verif_1_chp_oblig(el[j])) {
							if (OK) 
								elFocus = el[j]
							OK = false
						}
					} 
				}  // (el_1.type != 'checkbox')
			}  // plusieurs champs de ce nom
		}  // (typeof form.elements[chp_obl] == "object") 
	}  // for (var i in chps_obl) {

	if (! OK) {
		elFocus.focus()
		alert(msg_chp_oblig)
	}
	return OK
}

function _verif_1_chp_oblig(el)
{
	var c = null
	var OK = true
	switch (el.type) {
	case "text" :
	case "password" :
	case "textarea" :
	case "file" :
		c = el.value;
		break;
	case "select-one" :
		c = el.options[el.selectedIndex].value;
		break;
	case "select-multiple" :
		if (el.selectedIndex == -1) 
			OK = false;
		break;
	case "checkbox" :  // 1 seul checkbox [non checké]
	case "radio" :  // 1 seul radio [non checké]
		if (! el.checked) 
			OK = false;
		break;
	}
	if (OK && (c != null)) {
		c = c.replace(/\s/, ''); // remplace les blancs par du vide
		if (c == '' || c == 0 || c == '0') 
			OK = false
	}
	if (! OK) 
		el.style.backgroundColor = bgcolorErr

	return OK
}

			
// Exemple : ret = _verif_chp_num(this.form, [['nb','>0'], 'nb_1']);
function _verif_chp_num(form, chps_num)
{
	var nmChp, comp
	var OK = true

	//for (var i in chps_num) {
	for (var i=0; i<chps_num.length; i++) {
		var x = chps_num[i]
		if (typeof x == 'string') {
			// 'nomChp'
			nmChp = chps_num[i]
			comp = ''
		} 
		else {
			// un test supplémentaire : comp
			nmChp = x[0]
			comp = x[1]
		}
		if (typeof form.elements[nmChp] == "object") {
			if (typeof form.elements[nmChp].length == "undefined") {
				// 1 seul champ de ce nom
				if (! _verif_1_chp_num(form.elements[nmChp], comp)) {
					OK = false
					break;
				}
			} 
			else {
				// plusieurs champs de ce nom
				for (var j = 0; j < form.elements[nmChp].length; j++) {
					if (! _verif_1_chp_num(form.elements[nmChp][j], comp)) {
						OK = false
						break;
					}
				}
			}  // plusieurs champs de ce nom
		}  // le chp existe
	}  // boucle sur la liste des chps
	return OK
}


function _verif_1_chp_num(el, comp)
{
	var c = el.value
	var OK = true

	if (c != '' && isNaN(c)) 
		OK = false
	else if (typeof comp != 'undefined') { 
		if (comp != '') { 
			/*
			var x = 2; var y = 39
			eval("x + y + 1") // returns 42
			*/
			if (! eval("c" + comp)) 
				OK = false
		}
	}

	if (! OK) {
		el.style.backgroundColor = bgcolorErr
		el.focus()
		alert(msg_chp_num)
	}

	return OK
}


// fonctionne aussi pour les radio button
function _VerifCheckBox(form, NomElement)
{
	// retourne le nombre de case cochée de NomElement
	var l=0;
	if (typeof form.elements[NomElement].length == "undefined") {
		// un seul champ de ce nom
		if (form.elements[NomElement].checked)
			l++;
	} 
	else {
		for (var i=0; i<form.elements[NomElement].length; i++)
			if (form.elements[NomElement][i].checked)
				l++;
	}
	return  l;
}

function _verif_chp_phone(form, chps_phone)
{
	var OK = true, chp_phone

	//for (var i in chps_phone) {
	for (var i=0; i<chps_phone.length; i++) {
		chp_phone = chps_phone[i]
		if (typeof form.elements[chp_phone] == "object") {
			var el = form.elements[chp_phone]
			c = el.value
			if (c != '') {
				c = c.replace(/[\s0-9()\-]/g, ''); // autorise : 33 (0) 45-56-75
				if (c != '') {
					el.style.backgroundColor = bgcolorErr
					el.focus()
					alert(msg_chp_tel)
					OK = false
					break;
				}  // la valeur contient des car.interdits
			}  // le chp a une valeur 
		}  // le chp existe
	}  // boucle sur la liste des chps 
	return OK
}

/*
// vérifier que 'nom' sans accent
// à partir de [PHP] array(array('nom', '0-9a-zA-Z-_ '), ...) 
// obtenir [javascript] [['nom', '0-9a-zA-Z-_ '], ...]
		if (! empty($chps_car_allowed)) {
			unset($t);
			foreach ($chps_car_allowed as $ar) {
				$t[] = "['$ar[0]', '$ar[1]']";
			}
			$strTab = implode(", ", $t);
			$cond_js .= "if (ret) ret = _verif_char_allowed(this.form, [$strTab]);";
		}
*/
function _verif_char_allowed(form, tables)
{
	var OK = true
	var chp, chars;
	//for (var i in tables) {
	for (var i=0; i<tables.length; i++) {
		chp = tables[i][0]
		if (typeof form.elements[chp] == "object") {
			var el = form.elements[chp]
			chars = tables[i][1]
			re = new RegExp('['+chars+']', 'g');
			if ((c = el.value.replace(re, '')) != '') {
				el.style.backgroundColor = bgcolorErr
				el.focus()
				alert(msg_car_interdit + c)
				OK = false
				break;
			}  // la valeur contient des car.interdits
		}  // le chp existe
	}  // boucle sur la liste des chps 
	return OK
}


function ResetChp(form, chps)
{
	if (typeof chps == 'string') {
		_Reset_1_chp(form, chps)
	}
	else {
		//for (var i in chps) 
		for (var i=0; i<chps.length; i++) 
			_Reset_1_chp(form, chps[i]);
	}
}

function _Reset_1_chp(form, chp)
{
	if (typeof form.elements[chp].length == "undefined") {
		// un seul champ de ce nom
		form.elements[chp].value = "";	
	} 
	else {
		// plusieurs champs de ce nom
		//for (var i in form.elements[chp]) 
		for (var i=0; i<form.elements[chp].length; i++) 
			form.elements[chp][i].value = "";
	}
}

/*<textarea name="txt" 
	onKeyDown="textCounter(this, 160);" 
	onKeyUp="textCounter(this, 160);">$txt</textarea>
*/
function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit); // trim it!
}


