// Onload des fonctions
function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if(oldonload) oldonload();
			func();
		}
	}
}

// Methode push() pour IE5
Array.prototype.push = function(value) {
	this[this.length] = value;
}

// Récupération d'éléments par type et nom de classe
document.getElementsByClassName = function(className, p) {
  var children = (document.all) ? document.all : document.getElementsByTagName('*');
  var elements = new Array();
  
  for(var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for(var j = 0; j < classNames.length; j++) {
			if(p == 'partial') {
      	if(classNames[j].indexOf(className) != -1) {
					elements.push(child);
					break;
	      }
			} else {
      	if(classNames[j] == className) {
					elements.push(child);
					break;
  	    }
			}
    }
  }

  return elements;
}

var TRU = new Object();

// Traitement des classes
TRU.cName = {
	addClass : function(obj, cName) {
		obj.className += obj.className? ' '+cName : cName;
	},
	removeClass : function(obj, cName) {
		var rep = obj.className.match(' '+cName) ? ' '+cName : cName;
		obj.className = obj.className.replace(rep,'');
	}
}

// Menu déroulant
TRU.menuDrop = function() {
	if(!document.getElementById || !document.getElementsByTagName) return false;
	var menu = document.getElementById('menu');
	if(!menu) return false;
	var items = menu.getElementsByTagName('li');
	if(!items[0]) return false;
	var lg = items.length;
	var timer1, timer2 = null;

	for(var i = 0; i < lg; i++) {
		if(items[i].className == 'catalogue') {
			var lk = items[i].firstChild;
			var tg = items[i].getElementsByTagName('ul')[0];
			items[i].onmouseover = function() {
				if(lk.nodeName == 'A') TRU.cName.addClass(lk, 'actif');
				TRU.cName.addClass(tg, 'actif');
			}
			items[i].onfocus = items[i].onmouseover;
			items[i].onmouseout = function() {
				timer1 = setTimeout(function() {TRU.cName.removeClass(tg, 'actif');}, 300);
				timer2 = setTimeout(function() {TRU.cName.removeClass(lk, 'actif');}, 300);
			}
			items[i].onblur = items[i].onmouseout;
		}
	}
}

// Liste de produits
TRU.setListe = function() {
	if(!document.getElementById || !document.getElementsByTagName) return false;
	var liste = document.getElementById('liste');
	if(!liste) return false;
	var items = liste.getElementsByTagName('li');
	var tg1 = document.getElementById('produits');
	var tg2 = document.getElementById('extra');
	if(!items[0] || !tg1 || !tg2) return false;
	var lg = items.length;

	for(var i = 0; i < lg; i++) {
		var cl = (items[i].className) ? items[i].className : '';
		if(i == 0 && cl.indexOf('actif') != -1) {
			TRU.cName.addClass(tg1, 'alt');
		} else if(i == (lg - 1) && cl.indexOf('actif') != -1) {
			TRU.cName.addClass(tg2, 'alt');
		}
		items[i].onmouseover = function() {
			TRU.cName.addClass(this, 'shover');
		}
		items[i].onmouseout = function() {
			TRU.cName.removeClass(this, 'shover');
		}
		items[i].onclick = function() {
			var lk = this.getElementsByTagName('a')[0];
			if(lk) document.location = lk.href;
		}
	}
}

// Liste selection
TRU.setSelection = function() {
	if(!document.getElementById || !document.getElementsByTagName) return false;
	var liste = document.getElementById('F_selection');
	if(!liste) return false;
	var items = liste.getElementsByTagName('td');
	if(!items[0]) return false;
	var lg = items.length;

	for(var i = 0; i < lg; i++) {
		var cl = (items[i].className) ? items[i].className : '';
		if(cl.indexOf('T_dp') != -1) {
			items[i].onmouseover = function() {
				TRU.cName.addClass(this, 'shover');
			}
			items[i].onmouseout = function() {
				TRU.cName.removeClass(this, 'shover');
			}
			items[i].onclick = function() {
				var lk = this.getElementsByTagName('a')[0];
				if(lk) var lkc = lk.className;
				if(lkc.indexOf('pop-') != -1) {
					var attrs = 'top=100, left=100, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes, status=yes';
					var motif = /[0-9]+/g;
					var dim = lk.className.match(motif);
					var w = 'width='+dim[0]+', ';
					var h = 'height='+dim[1]+', ';
					window.open(lk.href, 'fiche', w+h+attrs);
					return false;
				} else {
					document.location = lk.href;
				}
			}
		}
	}
}

// Ouverture popup (liens)
TRU.openPop = function() {
	if(!document.getElementsByClassName) return false;
	var lks = document.getElementsByClassName('pop-', 'partial');
	var lg = lks.length;
	if(lg < 1) return false;
	var title = {'mdp' : 'motdepasse', 'desinscription': 'desinscription'};
	var attrs = 'top=100, left=100, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes, status=yes';
	var motif = /[0-9]+/g;

	for(var i = 0; i < lg; i++) {
		lks[i].onclick = function() {
			var dim = this.className.match(motif);
			var w = 'width='+dim[0]+', ';
			var h = 'height='+dim[1]+', ';
			var t = this.href.split('.')[0];
			window.open(this.href, title[t], w+h+attrs);
			return false;
		}
	}
}

// Ouverture popup (submit sur catalogue et cheques cadeaux)
TRU.openConfirm = function() {
	if(!document.getElementsByTagName || !document.getElementById) return false;
	var verif = document.getElementsByTagName('body')[0];
	var verifC = verif.className;
	var attrs = 'width=500, height= 150, top=100, left=100, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes, status=yes';
	if(verifC == 'catalogue') {
		var btn = document.getElementById('F_ajouter');
		if(!btn) return false;
		window.open('ajout_confirm_produit.php', 'confirmation', attrs);
	} else if(verifC == 'cheques') {
		var btn = document.getElementById('F_ajouter');
		if(!btn) return false;
		window.open('ajout_confirm_cheque.php', 'confirmation', attrs);
	}
}

// Gestion des formulaires horaires
TRU.toggleHoraire = function() {
	if(!document.getElementById) return false;
	var c1 = document.getElementById('F_contact1');
	var c2 = document.getElementById('F_contact2');
	var h1 = document.getElementById('horaire1');
	var h2 = document.getElementById('horaire2');
	if(!c1 || !c2 || !h1 || !h2) return false;

	h1.style.display = h2.style.display = 'none';

	c1.onchange = function() {
		h1.style.display = (this.value == 7) ? 'none' : 'block';
	}
	c2.onchange = function() {
		h2.style.display = (this.value == 7) ? 'none' : 'block';
	}
}

// Gestion des cheques cadeaux pour ma selection
TRU.toggleCheques = function() {
	if(!document.getElementById || !document.getElementsByTagName) return false;
	var liste = document.getElementById('F_selection');
	var cheques = document.getElementById('T_cheques');
	if(!liste || !cheques) return false;
	var items = liste.getElementsByTagName('td');

	var tg = (cheques.parentNode.nextSibling.nodeType == 1) ? cheques.parentNode.nextSibling : cheques.parentNode.nextSibling.nextSibling;
	if(tg.nodeName == 'TR' && cheques.className != 'actif') {
		cheques.firstChild.nodeValue = 'Ajouter des cartes cadeaux';
		tg.style.display = 'none';
		cheques.onclick = function() {
			cheques.firstChild.nodeValue = 'Cartes cadeaux';
			TRU.cName.addClass(cheques, 'actif');
			if(document.all && window.opera == null) {
				tg.style.display = 'block';
			} else {
				tg.style.display = 'table-row';
			}
		}
	}
}

// Validation formulaire
TRU.validForm = new Object();

TRU.validForm.motifs = {
	wp : /^\s*$/,
	em : /^[A-Z0-9._%-]+@[A-Z0-9.-]+[^.]+\.[A-Z]{2,4}$/i,
	nb : /^[0-9\s]*$/,
	it : /^\s*[0-9]+\s*$/,
	an : /^[A-Z0-9]{6,}$/i,
	cp : /^\s*[0-9]{5}\s*$/,
	tl : /^[0-9,\.\+\/\(\)\s-]+$/,
	sl : -1,
	mg : 'mg',
	cl : 'erreur'
}

TRU.validForm.content = {
	origin : new Object(),
	msg : new Object(),
	ssi : new Object(),
	wp : new Object(),
	em : new Object(),
	nb : new Object(),
	it : new Object(),
	an : new Object(),
	cp : new Object(),
	tl : new Object(),
	ch : new Object(),
	sl : new Object()
}

// validation formulaire
TRU.validForm.inspector = function() {
	if(!document.getElementById(TRU.validForm.name)) return;
	TRU.validForm.base = document.getElementById(TRU.validForm.name);
	for(var field in TRU.validForm.erreurs) {
		TRU.validForm.get.origine(field);
		TRU.validForm.get.erreurs(field);
		if(TRU.validForm.erreurs[field].indexOf('ch=') != -1) {
			for(var i = 0; i < TRU.validForm.base.elements[field].length; i++) {
				TRU.validForm.base.elements[field][i].onchange = function() {TRU.validForm.check(this);}
				TRU.validForm.base.elements[field][i].onclick = function() {TRU.validForm.check(this);}
			}
		} else {
			TRU.validForm.base.elements[field].onchange = function() {TRU.validForm.check(this);}
		}
	}

	TRU.validForm.base.onsubmit = function() {
		TRU.validForm.reponse = 0;
		var c = 0;
		for(var field in TRU.validForm.erreurs) {
			if(TRU.validForm.erreurs[field].indexOf('ch=') != -1) {
				for(var i = 0; i < TRU.validForm.base.elements[field].length; i++) {
					TRU.validForm.check(TRU.validForm.base.elements[field][i]);
					if(TRU.validForm.reponse > 0 && c == 0) {TRU.validForm.base.elements[field][0].focus(); c++;}
				}
			} else {
				TRU.validForm.check(TRU.validForm.base.elements[field]);
				if(TRU.validForm.reponse > 0 && c == 0) {TRU.validForm.base.elements[field].focus(); c++;}
			}
		}
		if(TRU.validForm.reponse > 0) {
			return false;
		} else {
			TRU.openConfirm();
			return true;
		}
	}
}

// recuperation de donnees
TRU.validForm.get = {
	target : function(nom) {
		var id = TRU.validForm.motifs.mg+nom;
		var tg = document.getElementById(id);
		if(!tg) return false;
		return tg;
	},
	
	origine : function(field) {
		TRU.validForm.content.origin[field] = TRU.validForm.get.target(field).firstChild.nodeValue;
	},
	
	erreurs : function(field) {
		TRU.validForm.content.msg[field] = new Object();
		TRU.validForm.content.ssi[field] = new Object();
		var tab = TRU.validForm.erreurs[field].split('|');
		for(var i = 0; i < tab.length; i++) {
			var a = tab[i].split('=');
			if(a[0] == 'rq')
				TRU.validForm.content.ssi[field] = a[1];
			else
				TRU.validForm.content.msg[field][a[0]] = a[1];
		}
	}
}

TRU.validForm.check = function(field) {
	var nm = field.name;
	for(var er in TRU.validForm.content.msg[nm]) {
		if(TRU.validForm.cd_type == '||' && TRU.validForm.cd_id && TRU.validForm.cd_msg) {
			TRU.validForm.checks.use('wp', field);
			TRU.validForm.checks.use('wp', TRU.validForm.base[TRU.validForm.content.ssi[nm]]); 
			if(TRU.validForm.content.wp[nm] == 1 && TRU.validForm.content.wp[TRU.validForm.content.ssi[nm]] == 1)
				TRU.validForm.state('defaut', 1, TRU.validForm.cd_msg, 'conditionnel');
			else
				TRU.validForm.state('defaut', 0, '', 'conditionnel');
		}
		TRU.validForm.checks.use(er, field);
		TRU.validForm.state(nm, TRU.validForm.content[er][nm], TRU.validForm.content.msg[nm][er]);
		if(TRU.validForm.content[er][nm] == 1) break;
	}
}

TRU.validForm.state = function(nom, a, msg, cd) {
	if(cd) {
		var tg = TRU.validForm.get.target(TRU.validForm.cd_id);
		var id = 'er'+TRU.validForm.cd_id;
	} else {
		var tg = TRU.validForm.get.target(nom);
		var id = 'er'+nom;
	}
	var el = document.getElementById(id);
	TRU.validForm.reponse = TRU.validForm.reponse + a;
	if(a == 1) {
		if(!el) {
			var elt = document.createElement('strong');
			var txt = document.createTextNode(msg);
			elt.appendChild(txt);
			if(cd)
				elt.setAttribute('id', 'er'+TRU.validForm.cd_id);
			else
				elt.setAttribute('id', 'er'+nom);
			tg.appendChild(elt);
		} else {
			el.firstChild.nodeValue = msg;
		}
		tg.className = TRU.validForm.motifs.cl;
	} else {
		if(el) tg.removeChild(el);
		tg.className = '';
	}
}

// controles
TRU.validForm.checks = {
	use : function(motif, field) {
		var nm = field.name;
		if(motif == 'ch') TRU.validForm.checks.group(field, nm);
		else if(motif == 'sl') TRU.validForm.checks.list(field, nm);
		else TRU.validForm.checks.text(field, nm, motif);
	},

	text : function(field, nm, motif) {
		var vl = field.value;
		if(motif == 'wp') {
			TRU.validForm.content[motif][nm] = TRU.validForm.motifs[motif].test(vl) ? 1 : 0;
			if(TRU.validForm.content[motif][nm] == 0) TRU.validForm.content[motif][nm] = (vl.length == 0) ? 1 : 0;
		} else {
			if(TRU.validForm.content.ssi[nm] == 'f') {
				if(TRU.validForm.motifs.wp.test(vl) == false)
					TRU.validForm.content[motif][nm] = TRU.validForm.motifs[motif].test(vl) ? 0 : 1;
				else
					TRU.validForm.content[motif][nm] = 0;
			} else {
				TRU.validForm.content[motif][nm] = TRU.validForm.motifs[motif].test(vl) ? 0 : 1;
			}
		}
	},

	group : function(field, nm) {
		var gr = TRU.validForm.base.elements[nm];
		var lg = gr.length;
		var sl = 0;
		TRU.validForm.content.ch[nm] = 0;
		if(field.checked == false) {
			for(var i = 0; i < lg; i++) {
				if(gr[i].checked == false) sl++;
			}
			if(sl == lg) TRU.validForm.content.ch[nm] = 1;
		}
	},

	list : function(field, nm) {
		if(field.value == TRU.validForm.motifs.sl)
			TRU.validForm.content.sl[nm] = 1;
		else
			TRU.validForm.content.sl[nm] = 0;
	}
}

// Chargement des fonctions
addLoadEvent(TRU.menuDrop);
addLoadEvent(TRU.setListe);
addLoadEvent(TRU.setSelection);
addLoadEvent(TRU.toggleHoraire);
addLoadEvent(TRU.toggleCheques);
addLoadEvent(TRU.validForm.inspector);
addLoadEvent(TRU.openPop);