// JavaScript Document
<!-- 
/**
 * Classe RadioActiveChamps
 * Desactive un groupe de champs lorsqu'une option de bouton radio est cochee. Active le groupe de champs lorsqu'elle est décochée. Cela est utile pour un formulaire dont un certain nombre d'options sont proposées dont une option qui vaut "Autres". Puis avec champ à compléter si cette option est sélectionnée.
 *
 * Exemple d'utilisation: 
 * var activeChamps;
 * function initialise () {
 * 	activeChamps = new RadioActiveChamps('fact','adresse_liv',2,'liv_nom','liv_rue','liv_ville','liv_inti_postal','liv_pays');
 * }
 * window.onload = initialise;
 * 
 * @author : xavier.ottolini[at]adelis.com
 * @param argument 0 :  chaine de caractere contenant l'attribut id du formulaire ou son attribut name 
 * @param argument 1 :  chaine de caractere contenant le nom du bouton radio qui active ou desactive les champs
 * @param argument 2 :  entier contenant l'indice du bouton radio qui active ou desactive les champs
 * @param argument 3...n : nom d'un champ a desactiver
 */

function RadioActiveChamps(){
	var f;
	var a = RadioActiveChamps.arguments;
	var boutonRadio;
	var boutonLevier = -1;
	var fields;

	try {	
		f=document.getElementById(a[0]);
		f.elements==null;
	} catch (e) {
		var formsLists= document.forms;
		f=formsLists[a[0]];
		f.elements==null;
	}

	try {
		this.fields = f.elements;
                if(this.fields[a[1]].length) {
		  if(this.fields[a[1]][0].type == "radio") {
			this.boutonRadio = this.fields[a[1]];
			this.boutonRadio.boutonLevier = a[2];
			this.boutonRadio[this.boutonRadio.boutonLevier].arrayOfFields = new Array();

			for(var i = a.length; i> 3; i--) {
				this.boutonRadio[this.boutonRadio.boutonLevier].arrayOfFields[a.length - i] = a[a.length -i + 3];
			}

			for(var i = this.boutonRadio.length; i> 0; i--) {
				this.boutonRadio[i-1].onclick = desactiveChamps;
				this.boutonRadio[i-1].f= f;
				this.boutonRadio[i-1].boutonsName = a[1];
				this.boutonRadio[i-1].boutonLevier = a[2];
			}


                  } else {
			throw e
                  }
		} else {
		  if(this.fields[a[1]].type == "radio") {
			this.boutonRadio = this.fields[a[1]];
			this.boutonRadio.f = f;
			this.boutonRadio.arrayOfFields = new Array();

			for(var i = a.length; i> 3; i--) {
				this.boutonRadio[this.boutonRadio].arrayOfFields[a.length - i] = a[a.length - i + 3];
			}

			this.boutonRadio.onclick = desactiveChamps;
                  } else {
			throw e
                  }                   
		}
	} catch (e) {
		alert("Le champ \"" + a[1] + "\" n'existe pas.");
	}

	this.initializeFields = initialiseChamps;
	this.initializeFields();
}

/**
 * Methode desactiveChamps
 * Active ou desactive la liste des champs lors du changement de valeur de la case a cocher
 */
function desactiveChamps() {
	var nomChampCourant = "";

	try {
		var champBoutonLevier = this.f.elements[this.boutonsName][this.boutonLevier];

		for (key in champBoutonLevier.arrayOfFields)   {
			nomChampCourant = champBoutonLevier.arrayOfFields[key];
			if(champBoutonLevier.checked) {
				this.f.elements[nomChampCourant].removeAttribute("disabled");
				this.f.elements[nomChampCourant].style.color="000000";
				this.f.elements[nomChampCourant].style.backgroundColor="FFFFFF";
			} else {
				var att = document.createAttribute("disabled");
				att.value="true";
				this.f.elements[nomChampCourant].setAttributeNode(att);
				this.f.elements[nomChampCourant].style.color="A1A1A1";
				this.f.elements[nomChampCourant].style.backgroundColor="F7F7F7";
			}
		}
		
	} catch (e) {
		alert("Le champ \"" + nomChampCourant + "\" 'existe pas.");
	}
}

/**
 * Methode initialiseChamps
 * Active ou desactive la liste des champs lors du chargement de la page en fonction de la valeur de la case a cocher
 */
function initialiseChamps() {
	var nomChampCourant = "";
	var champCourant = null;

	try {
		if(this.boutonRadio.length) {
			for (key in this.boutonRadio[this.boutonRadio.boutonLevier].arrayOfFields)   {
				nomChampCourant = this.boutonRadio[this.boutonRadio.boutonLevier].arrayOfFields[key];
				champCourant = this.boutonRadio[this.boutonRadio.boutonLevier].f.elements[nomChampCourant];

				if(this.boutonRadio[this.boutonRadio.boutonLevier].checked) {
					champCourant.removeAttribute("disabled");
				} else {
					var att = document.createAttribute("disabled");
					att.value="true";
					champCourant.setAttributeNode(att);
					champCourant.style.color="A1A1A1";
					champCourant.style.backgroundColor="F7F7F7";
				}
			}
		} else {
			for (key in this.boutonRadio.arrayOfFields)   {
				nomChampCourant  = this.boutonRadio.arrayOfFields[key];
				champCourant = this.boutonRadio.f.elements[nomChampCourant];
				champCourant.setAttribute("disabled",!this.boutonRadio.checked);
			}
		}
	} catch (e) {
		alert("Le champ \"" + nomChampCourant + "\" n'existe pas.");
	}
}
//-->