var IE4 = (document.all) ? true : false;
var DOM = (document.getElementById) ? true : false;
var IE5 = (document.getElementById && IE4) ? true : false;
var NS6 = (document.getElementById && !IE4) ? true : false;

if (DOM || IE4) {document.write("<style type='text/css'>.child {display:none}</style>");}
if (NS6) {document.addEventListener("mousemove",nsevent,false);}

function display(dv) {

	if (DOM) {document.getElementById(dv).style.display == "block" ? display_none(dv) : display_block(dv);}
	else if (IE4) {document.all[dv].style.display == "block" ? display_none(dv) : display_block(dv);}
}

function display_block(dv) {

	if (DOM) {document.getElementById(dv).style.display = "block";}
	else if (IE4) {document.all[dv].style.display = "block";}
}

function display_none(dv) {

	if (DOM) {document.getElementById(dv).style.display = "none";}
	else if (IE4) {document.all[dv].style.display = "none";}
}

function display_unique(dv,regexp) {

	re  = new RegExp(regexp);
	tag = IE4 ? document.all.tags("div") : document.getElementsByTagName("div");
	
	for(i=0;i<tag.length;i++)
	
		if(re.test(tag[i].id)) 
		
			display_none(tag[i].id);

	display_block(dv);
}

function nsevent(e) {nsx = e.pageX; nsy = e.pageY;}

function show(dv) {

	if (DOM) {document.getElementById(dv).style.visibility = "visible";}
	else if (IE4) {document.all[dv].style.visibility = "visible";}
}

function hide(dv) {

	if (DOM) {document.getElementById(dv).style.visibility = "hidden";}
	else if (IE4) {document.all[dv].style.visibility = "hidden";}
}

function pos(dv) {
 
	if (IE5) {

		nsx = event.clientX;
		nsy = event.clientY;
		document.getElementById(dv).style.top = nsy-5;
		document.getElementById(dv).style.left = nsx-5;
		document.getElementById(dv).style.visibility = "visible";

	} else if (IE4) {

		nsx = event.clientX; nsy = event.clientY;
		document.all[dv].top = nsy-5;
		document.all[dv].left = nsx-5;
		document.all[dv].style.visibility = "visible";

	} else if (NS6) {

		document.getElementById(dv).style.top = nsy-5;
		document.getElementById(dv).style.left = nsx-5;
		document.getElementById(dv).style.visibility = "visible";
	}
}

function updatehtml(id,thestr) {

	if (IE4) {

		document.all[id].innerHTML = thestr;
	
	} else if (DOM) {
	
		document.getElementById(id).innerHTML = thestr;
	} 
}

function gethtml(id) {

	if (IE4) {

		return document.all[id].innerHTML;
	
	} else if (DOM) {
	
		return document.getElementById(id).innerHTML;
	} 
}

array_changehtml = new Array;

function changehtml(id,str1,str2) {

	if(array_changehtml[id] == 1) {
	
		array_changehtml[id] = 0;
		updatehtml(id,str2);
		
	} else {
	
		array_changehtml[id] = 1;
		updatehtml(id,str1);
	}
}

function uncheck(theform,nom) {

	var field = document.forms[theform];

	for (i = 0; i < field.elements.length; i++) {
	
		if (field.elements[i].name == nom) field.elements[i].checked = false;
	}
}

function check(theform,nom) {

	var field = document.forms[theform];

	for (i = 0; i < field.elements.length; i++) {

		if (field.elements[i].name == nom) field.elements[i].checked = true;
	}
}

function ischeck(theform,name,title) {

	var error = "";
	var field = document.forms[theform];

	for(i=0;i<name.length;i++) {

		for(j=0;j<field.length;j++) {

			if(field.elements[j].name == name[i]  && field.elements[Math.abs(j-1)].name != name[i]) {

				var type = field.elements[j].type;

				if (type == "select-one" || type == "select-multiple") {

					if(field.elements[name[i]].selectedIndex == -1 
					|| field.elements[name[i]].options[field.elements[name[i]].selectedIndex].value == "#") {

						error+= " - "+title[i]+"\n";
						break;
					}

				} else if (type == "radio" || type == "checkbox") {

					var chk = 0;

					for(k=0;k<field.elements[name[i]].length;k++) {

						if(field.elements[name[i]][k].checked == true) chk = 1;
					}

					if(chk == 0) {

						error+= " - "+title[i]+"\n";
						break;
					}

				} else {

					if(field.elements[name[i]].value.length == 0) {

						error+= " - "+title[i]+"\n";
						break;
					}
				}
			}
		}
	}

 	if(error != "") {alert("les éléments suivants sont obligatoires :\n\n"+error);return false;} else {return true;}
}

function isarray(thestr) {

	if(typeof eval("window." + thestr) == "object") return true; else return false;
}

function select(theform,el,thestr) {
	
	if(isarray(thestr)) { 
	
		eval("var thelist = " + thestr + ";");
		var j = 0;
		document.forms[theform].elements[el].length = (thelist.length+2)/2;
	
		for(i=0;i<thelist.length;i=i+2) {
		
			j++;
			document.forms[theform].elements[el].options[j].value = thelist[i+1];
			document.forms[theform].elements[el].options[j].text  = thelist[i];
		}
	}
}

function selectinit(theform,el) {

	for(i=0;i<el.length;i++) {
	
		document.forms[theform].elements[el[i]].length = 1;
		document.forms[theform].elements[el[i]].options[0].value = "";
		document.forms[theform].elements[el[i]].options[0].text = "--- Sélectionnez ici ---";
		document.forms[theform].elements[el[i]].options[0].selected = "true";
	}
}

function option(thestr,thearray1,thearray2) {

	if(!thearray2) thearray2 = thearray1;

	for(i=0;i<thearray1.length;i++) {
	
		document.write("<option value=\"" + thearray1[i] + "\"");
		document.write(thearray1[i] == thestr ? "selected" : "");
		document.write(">" + thearray2[i] + "</option>");
	}
}

function tree(thestr,el,target,thenode) {

	var color = "#D4D4D4";
	var i = 0;
	var j = 0;
	var s = "";

	if(!el.length) el      = new Array("","","");
	if(!target)    target  = "document";
	if(!thenode)   thenode = "";
	
	eval("var thelist = " + thestr + ";");
	
	document.write("<div id='" + thestr + "' class='child'>");
		
	for(i=0;i<thelist.length;i=i+2) {
		
		j++;
		s = thestr + "_" + j;
		
		if(isarray(s)) { 

			spanid = "spanid_" + s;

			document.write("<table border='0' cellpadding='0' cellspacing='0'><tr class=texte valign=top><td>" + thenode + "</td><td><span id='" + spanid + "'>" + el[0] + "</span></td><td style='cursor: hand' ");
			document.write("onmouseover=\"this.style.backgroundColor = '" + color + "'\" ");
			document.write("onmouseout=\"this.style.backgroundColor = ''\" ");
			document.write("onclick=\"display('" + s + "');");
			document.write("changehtml('" + spanid + "','" + el[1] + "','" + el[0] + "');");
			document.write(thelist[i+1].length > 5 ? target + ".location.href=unescape('" + escape(thelist[i+1]) + "')\">" : "\">");
			document.write(thelist[i] + "</td></tr></table>");
			
			tree(s,el,target,thenode+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		
		} else {

			document.write("<table border='0' cellpadding='0' cellspacing='0'><tr class=texte valign=middle><td>" + thenode + "</td><td>" + el[2] + "</td><td style='cursor: hand' ");
			document.write("onmouseover=\"this.style.backgroundColor = '" + color + "'\" ");
			document.write("onmouseout=\"this.style.backgroundColor = ''\" ");
			document.write("onclick=\"" + target + ".location.href=unescape('" + escape(thelist[i+1]) + "')\">");
			document.write(thelist[i] + "</td></tr></table>");
		}
	}
	
	document.write("</div>");	
}

function menu(thestr,target,thearray,theindex) {

	var color = "#D4D4D4";
	var i = 0;
	var j = 0;
	var str = "";
	
	if(!target)   target   = "document";
	if(!thearray) thearray = thestr;
	if(!theindex) theindex = 1;
	
	eval("var thelist = " + thestr + ";");	
	
	for(i=0;i<thelist.length;i=i+2) {

		j++;
		div_name = thestr + "_" + j;

		if(isarray(div_name)) {
		
			str += "<tr class=fclair style='cursor: hand' "
			str += "onmouseover=\"this.style.backgroundColor = '" + color + "'\" ";
			str += "onmouseout=\"this.style.backgroundColor = ''\" ";
			str += "onclick=\"pos('" + div_name + "')\">";
			str += "<td><table width=100% cellpadding='0' cellspacing='0' border='0'><tr><td class=texte>";
			str += thelist[i];
			str += "</td><td class=texte align=right>&nbsp;&gt;&gt;</td></tr></table></td></tr>";
		
			thearray += "," + div_name;
			menu(div_name,target,thearray,theindex+1);
		    thearray = thearray.substring(0,thearray.lastIndexOf(","));	
				
		} else  {
		
			str += "<tr class=fclair style='cursor: hand' ";
			str += "onmouseover=\"this.style.backgroundColor = '" + color + "'\" ";
			str += "onmouseout=\"this.style.backgroundColor = ''\" ";
			str += "onclick=\"" + target + ".location.href=unescape('" + escape(thelist[i+1]) + "')\">";
			str += "<td class=texte>" + thelist[i] + "</td></tr>";
		}
	}	

	document.write("<div id='" + thestr + "' ");
	document.write("onmouseover=menushow('" + thearray + "') ");
	document.write("onmouseout=menuhide('" + thearray + "') ");
	document.write("style='position:absolute;visibility:hidden;z-index:" + theindex + "'>");
	document.write("<table cellpadding='0' cellspacing='0' border='0' bgcolor=" + color + "><tr><td>");
	document.write("<table cellpadding=4 cellspacing=1 border='0'>");
	document.write(str);
	document.write("</table>");	
	document.write("</td></tr></table>");
	document.write("</div>");
}

function menushow(thearray) {

	thearray = thearray.split(",");

	for(i=0;i<thearray.length;i++) show(thearray[i]);
}

function menuhide(thearray) {

	thearray = thearray.split(",");

	for(i=0;i<thearray.length;i++) hide(thearray[i]);
}

function popit(u,W,H) {
	window.open(u,"migal","top=10, left=10, width=" + W + ", height=" + H + ", scrollbars=yes, status=no, resizable=yes");
}
