// JavaScript Document
function createMess(elt, balise, txt) {
	var messInfo = document.createElement(balise);
	var txtNodeMessInfo = document.createTextNode(txt);
	messInfo.className = 'check';
	messInfo.appendChild(txtNodeMessInfo);
	elt.parentNode.appendChild(messInfo);
}

function verifForm(elt) {
	var PasswordLenghtMin = 4;
	var classObligatoire = "obligatoire";
	var baliseMessInfo = "span";

	var InputPasswordMessInfo = PasswordLenghtMin+InputPasswordMessInfoLang;
	
	// déclarations
	var firstFalse = new Array(10000,'');
	var modele = /^[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9\.\-_]+\.[a-zA-Z]{2,5}$/i;
	
	// suppression des messages existants
	$$('#'+elt.getAttribute('id')+' .check').each( function (s) {
		s.parentNode.removeChild(s);
	});
	
	// check des obligatoires
	$$('#'+elt.getAttribute('id')+' .obligatoire').each( function(o, index) {
		o.identify();
		var txt = '';

		// check des input
		if(	o.down('input') && 
			(o.down('input').type != 'hidden') && 
			(o.down('input').type != 'submit') && 
			(o.down('input').type != 'radio') && 
			(o.down('input').type != 'checkbox')) {
			var e = o.down('input');
			if(e.value.length == 0) {
				//alert('input');
				firstFalse[0] = Math.min(index, firstFalse[0]);
				if(index == firstFalse[0]) firstFalse[1] = e;
				switch(e.type) {
					case 'text': 		txt = InputTxtMessInfo;		break;
					case 'password':	txt = InputTxtMessInfo;		break;
					case 'file':		txt = InputFileMessInfo;	break;
				}
						
			} else if ((e.value.length < PasswordLenghtMin) && (e.type == 'password')) {
				firstFalse[0] = Math.min(index, firstFalse[0]);
				if(index == firstFalse[0]) firstFalse[1] = e;
				txt = InputPasswordMessInfo;
			}
			
			if(txt != '') createMess(e, baliseMessInfo, txt); 
		}
		
		// check des radio et des checkbox
		if(o.down('input') && ((o.down('input').type == 'radio') || (o.down('input').type == 'checkbox'))) {
			//alert('radio');
			var e = o.down('input');
			var check = false;
			$$('input[name='+e.name+']').each( function (r) {
				if(r.checked) check = true;
			});
			if (!check) {
				firstFalse[0] = Math.min(index, firstFalse[0]);
				if(index == firstFalse[0]) firstFalse[1] = e;
				createMess($$('input[name='+e.name+']')[$$('input[name='+e.name+']').length - 1], baliseMessInfo, InputRadioMessInfo);
			}
		}
		
		// check des select
		if(o.down('select')) {
			//alert('select');
			var e = o.down('select');
			if (e.options[e.selectedIndex].value == '') {
				firstFalse[0] = Math.min(index, firstFalse[0]);
				if(index == firstFalse[0]) firstFalse[1] = e;
				switch(e.type) {
					case 'select-one': default :	txt = SelectSingleMessInfo;		break;
					case 'select-multiple':			txt = SelectMultiMessInfo;		break;
				}
				createMess(e, baliseMessInfo, txt); 
			}
		}

		// check des textarea
		if(o.down('textarea')) {
			//alert('textarea');
			var e = o.down('textarea');
			if(e.value.length == 0) {
				firstFalse[0] = Math.min(index, firstFalse[0]); 
				if(index == firstFalse[0]) firstFalse[1] = e;
				createMess(e, baliseMessInfo, InputTxtMessInfo);
			}
		}
		
	});
	// check des champs mail obligatoires ou non
	// ils doivent être bien formés
	$$('#'+elt.getAttribute('id')+' input[name~=courrielz]').each( function(o) {
		
		if(o.value.length != 0) {
			if((o.value != '') && (!modele.test(o.value))) {
				// FIXME : index n'existe pas ici
				//firstFalse[0] = Math.min(index, firstFalse[0]);
				if(firstFalse[0] == 10000) firstFalse[1] = o; //manu a mis un o a la place du e				
				createMess(o, baliseMessInfo, InputMailMessInfo); 
			}
		}
	});
	
	// action finale
	if(firstFalse[1] != '') {
		firstFalse[1].focus();
		return false;
	} else {
		return true;
	}
}

function prepareForm() {
	$$('form').each( function(f) {
		if(!f.getAttribute('id').match('recherche')) {
			// dans le cas d'un formulaire d'une référence PRODUIT
			// il faut checker uniquement les champs de la ligne du bouton
			if(f.className && (f.className.toLowerCase().match('ajoutpanier')) ){
				$$('#'+f.id+' tr').each( function (tr) {
					tr.identify();
					$$('#'+tr.id+' input[type="submit"]').each( function (i) {
						i.tr = tr;
						i.formulaire = f;
						i.onclick = function() {
							return verifForm(this.tr);
							this.formulaire.submit();
						}
					});
				});
			} else {
				var checkform = true;
				if (f.submit.length > 0)		checkform = false;
				if (f.onsubmit != undefined)	checkform = false;
						
				if(checkform) f.onsubmit = function() { return verifForm(this); }
			}
		}
	});
}

function addLoadListenerFormAuto(func) {
   if (window.addEventListener) {
      window.addEventListener("load", func, false);
   } else if (document.addEventListener) {
      document.addEventListener("load", func, false);
   } else if (window.attachEvent) {
      window.attachEvent("onload", func);
   }
}


