debug = false

disableOption = function (idSelect, valueOption) {
	if (debug) console.log('Appel disableOption Select : ' + idSelect + ' option : ' + valueOption);
	var options = $(idSelect).immediateDescendants();
	for (var i=0; i < options.length; i++) {
		var monOption = options[i];
		if( monOption.value == valueOption ) {
			monOptionObj = $(monOption);
			$(monOption).addClassName('hidden');
			monOptionObj.setAttribute('disabled', 'disabled');	
			if (debug) console.log('	Test disableOption Select : ' + idSelect + ' option : ' + valueOption);		
		}
	}
}
selectOption = function (idSelect, valueOption) {
	var options = $(idSelect).immediateDescendants();
	for (var i=0; i < options.length; i++) {
		var monOption = options[i];	
		if( (monOption.value == valueOption) ) {
			monOptionObj = $(monOption);
			$(monOption).addClassName('selected');
			monOptionObj.setAttribute('selected', 'selected');			
		}
	}
}
// source : http://www.bobbyvandersluis.com/articles/unobtrusivedynamicselect.php
function dynamicSelect(id1, id2) {
     if (document.getElementById && document.getElementsByTagName) {
         var sel1 = document.getElementById(id1);
         var sel2 = document.getElementById(id2);
         var clone = sel2.cloneNode(true);
         var clonedOptions = clone.getElementsByTagName("option");
         refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
         sel1.onchange = function() {
             refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
         };
     }
 }
 function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
     while (sel2.options.length) {
         sel2.remove(0);
     }
     var pattern1 = /( |^)(select)( |$)/;
     var pattern2 = new RegExp("( |^)(" +
          sel1.options[sel1.selectedIndex].value + ")( |$)");
     for (var i = 0; i < clonedOptions.length; i++) {
         if (clonedOptions[i].className.match(pattern1) ||
              clonedOptions[i].className.match(pattern2)) {
             sel2.appendChild(clonedOptions[i].cloneNode(true));
         }
     }
 }
 
 // simple duplication de la fonction precedente avec ajout d'un parametre : voir si l'on peut refactoriser

function dynamicSelect2(id1, id2, id3) {
	// Feature test to see if there is enough W3C DOM support
	if (document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		var sel3 = document.getElementById(id3);	
		// Clone the dynamic select box
		var clone = sel2.cloneNode(true);
		var clone2 = sel3.cloneNode(true);
		// Obtain references to all cloned options 
		var clonedOptions = clone.getElementsByTagName("option");
		var clonedOptions2 = clone2.getElementsByTagName("option");
		// Onload init: call a generic function to display the related options in the dynamic select box
		refreshDynamicSelectOptions2(sel1, sel2, sel3, clonedOptions, clonedOptions2);
		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box	
		sel1.onchange = function() {
			refreshDynamicSelectOptions2(sel1, sel2, sel3, clonedOptions, clonedOptions2);
		};
	}
}
function refreshDynamicSelectOptions2(sel1, sel2, sel3, clonedOptions, clonedOptions2) {
	// Delete all options of the dynamic select box	
	while (sel3.options.length) { sel3.remove(0); }
	while (sel2.options.length) { sel2.remove(0); }
	// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
	var pattern1 = /( |^)(select)( |$)/;
	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
	// Iterate through all cloned options
	for (var i = 0; i < clonedOptions.length; i++) {
		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
			// Clone the option from the hidden option pool and append it to the dynamic select box
			sel2.appendChild(clonedOptions[i].cloneNode(true));
		}
	}
	for (var i = 0; i < clonedOptions2.length; i++) {
		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
		if (clonedOptions2[i].className.match(pattern1) || clonedOptions2[i].className.match(pattern2)) {
			// Clone the option from the hidden option pool and append it to the dynamic select box
			sel3.appendChild(clonedOptions2[i].cloneNode(true));
		}
	}
}


/*	Copyright Robert Nyman, http://www.robertnyman.com */
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\s)" + strAttributeValue + "(\s|$)") : null;
	var oCurrent;
	var oAttribute;
	for(var i=0; i<arrElements.length; i++){
		oCurrent = arrElements[i];
		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
		if(typeof oAttribute == "string" && oAttribute.length > 0){
			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
				arrReturnElements.push(oCurrent);
			}
		}
	}
	return arrReturnElements;
}

var MethodsJ = {	
	enableChilds: function(element) {
		if (debug) console.log('enableChilds ' + element.getAttribute('id'));
		element = $(element);		
		var options = element.immediateDescendants();
		for (var i=0; i < options.length; i++) {
			var monOption = options[i];			
			monOptionObj = $(monOption);
			monOptionObj.show();	
			if (debug) console.log('enableChild ' + monOptionObj.getAttribute('value'));				
			monOptionObj.removeClassName('disabled');			
			monOptionObj.removeAttribute('disabled');
		}
		return element;
	},
	selectOption: function(element, valueOption) {
		element = $(element);		
		var options = element.immediateDescendants();
		for (var i=0; i < options.length; i++) {
			var monOption = options[i];
			monOptionObj = $(monOption);
			if( monOption.value == valueOption ) {				
				$(monOption).addClassName('selected');
				monOptionObj.setAttribute('selected', 'selected');	
			} else {
				$(monOption).removeClassName('selected');
				monOptionObj.removeAttribute('selected');	
			}
		}
		return element;
	},
	/* retourne le label d'un champ en fonction de son id : for="id" */
	getMyLabel: function(element) {
		element = $(element);	
		nomElement = element.getAttribute('id');			
		/* recherche le label ayant le for=element pour le disabled */		
		var forMe = getElementsByAttribute(document, 'label', 'for', nomElement);	
		return forMe[0];
	  },	
	disableJ: function(element) {		
	    element = $(element);
		element.addClassName('disabled');
	    element.disabled = true;	
		if (element.getMyLabel()) element.getMyLabel().addClassName('disabled');		
		element.getMyLabel();
		return element;
	  },
	enableJ: function(element) {
	    element = $(element);
	    element.blur();
	    element.disabled = false;
		element.removeClassName('disabled');
		if (element.getMyLabel()) element.getMyLabel().removeClassName('disabled');
	    return element;
	  }
}
Element.addMethods(MethodsJ);