// JavaScript Document
function updateUtilisation(){
	
	gamme = $('gamme').value;
	
	clear_select('utilisation');
	$('utilisation').appendChild(addOptionOnly("Utilisation", ""));
	if (gamme != ''){

		//recupere partie dynamique (a droite) de l'option

		var url = "/fr/service/ajax/getUtilisation.xhr.php";
		o_options = new Object();
		o_options = {	method: 'get', 
							postBody: 'g='+gamme,
							parameters: 'g='+gamme,
							asynchronous:false,
							onSuccess: function(transport) {
								traiteUtilisation(transport.responseText);
							},
							on404: function(transport) {
								alert("Erreur 404");
							},
							onFailure: function (transport){
								alert("Echec");	
							}
					};
		var request = new Ajax.Request(url,o_options);

	}
	
}

function updateOrgane(){
	
	gamme = $('gamme').value;
	utilisation = $('utilisation').value;
	
	clear_select('organe');
	$('organe').appendChild(addOptionOnly("Organe", ""));
	if (gamme != '' && utilisation!=''){

		//recupere partie dynamique (a droite) de l'option

		var url = "/fr/service/ajax/getOrgane.xhr.php";
		o_options = new Object();
		o_options = {	method: 'get', 
							postBody: 'g='+gamme+'&u='+utilisation,
							parameters: 'g='+gamme+'&u='+utilisation,
							asynchronous:false,
							onSuccess: function(transport) {
								traiteOrgane(transport.responseText);
							},
							on404: function(transport) {
								alert("Erreur 404");
							},
							onFailure: function (transport){
								alert("Echec");	
							}
					};
		var request = new Ajax.Request(url,o_options);

	}
	
}

function traiteUtilisation(str){	
	var tabOpt = str.split('||');
	for(var i=0;i<tabOpt.length;i++){
		if (tabOpt[i].length>0){
			var detail = tabOpt[i].split("##");
			$('utilisation').appendChild(addOptionOnly(detail[0], detail[1]));
		}
	}
}


function traiteOrgane(str){	

	var tabOpt = str.split('||');
	for(var i=0;i<tabOpt.length;i++){
		if (tabOpt[i].length>0){
			var detail = tabOpt[i].split("##");
			$('organe').appendChild(addOptionOnly(detail[0], detail[1]));
		}
	}
}


function clear_select(id_select){
	var sel = document.getElementById(id_select);

	for (var i=sel.options.length; i>=0; i--){
		sel.options[i]=null;
	}
	sel.options.length =0;
}


function addOptionGroup(combo, label){
	var combox 	= document.getElementById(combo);
	var opt   	= document.createElement("OPTGROUP");
	var lab		= document.createAttribute("label");
	lab.nodeValue = label;
	opt.setAttributeNode(lab);
	combox.appendChild(opt);
	return opt;
}

function addOption(optGroup, label, value){
	var option	= document.createElement("OPTION");
	var val		= document.createAttribute("value");
	var txt		= document.createTextNode(label);
	val.nodeValue= value;
	
	option.appendChild(txt);
	option.setAttributeNode(val);	
	optGroup.appendChild(option);
}

//args[2] => valeur ŕ selectionner (selected)
function addOptionOnly(label, value){
	var option	= document.createElement("OPTION");
	if (arguments.length>2){
		if (arguments[2]==value){
			option.setAttribute('SELECTED', 'SELECTED');
			option.selected = "selected";
		}
	}
	
	var val		= document.createAttribute("value");
	var txt		= document.createTextNode(label);
	val.nodeValue= value;
	
	option.appendChild(txt);
	option.setAttributeNode(val);	
	return option;
}


function popups(page, w, h){
	wind = window.open(page, '', 'width='+w+', height='+h+', resizable=1, menubar=0, status=1, location=0, toolbar=0, scrollbars=1');
	var screenW = screen.width;
	var screenH = screen.height;
	wind.moveTo((screenW/2)-(w/2), (screenH/2)-(h/2));
}

//Test si le formulaire est bien complé
function verifForm() {
	if(document.getElementById('gamme').value=="")
	{
		alert("Veuillez choisir une gamme");
		return false;
	}
	if(document.getElementById('utilisation').value=="")
	{
		alert("Veuillez choisir une utilisation");
		return false;
	}
	if(document.getElementById('organe').value=="")
	{
		alert("Veuillez choisir un organe");
		return false;
	}
}

