function SspJs (id_container, sep, ssps_obj, codage, options){
	this.selects = [];
	this.codages = [];
	this.ssp_ids = []; 
	this.ssp_noms = []; 
	this.separateur = sep;
	this.ssp_elmts = [];
	this.options = options;
	this.lastSelectedIndex = [];
	
	

	if (ssps_obj){
		var box = document.getElementById(id_container);
		if (box){
			var splitLength = 0;
			for (var i=0; i<ssps_obj.length; i++){
				var split = ssps_obj[i].nom.split(this.separateur);
				if (split.length == 1){
					return false;
				}
				if (i == 0){
					splitLength = split.length;
				} else if (split.length != splitLength) {
					return false;
				}
				for (var j=0; j<split.length; j++){
					if (!this.selects[j]){
						var nom = null;
						for (var k=0; k<codage.length; k++){
							if (codage[k][0] == split[j].substr(0, 2)){
								nom = codage[k][1];
								this.codages.push(codage[k][0]);
								indexCodage = k;
								break;
							}
						}
						if (nom == null){
							nom = split[j].substr(0, 1);
							this.codages.push(split[j].substr(0, 2));
							indexCodage = -1;
						}
						
						this.lastSelectedIndex[j] = 0;
						this.selects[j] = {select: document.createElement('select'), label: nom, unique: [], codage: indexCodage};
						this.selects[j].select.name = 'ssp_js_' + split[j].substr(0, 1) + '_' + j;
						this.selects[j].select.id = 'id_ssp_js_' + split[j].substr(0, 1);
						var inst = this;
						this.selects[j].select.onchange = function(){
							inst.updateOthersSelects(this.name.substr(this.name.lastIndexOf('_') +1));
						};

						var option = document.createElement('option');
						option.value = '';
						option.innerHTML = ' - ';
						this.selects[j].select.appendChild(option);
					}

					if (!this.ssp_elmts[j]){
						this.ssp_elmts[j] = {all: []};
					}
					if (this.ssp_elmts[j].all.indexOf(ssps_obj[i].nom) == -1){
						this.ssp_elmts[j].all.push(ssps_obj[i].nom);
						if (!this.ssp_elmts[j][split[j].substr(2)]){
							this.ssp_elmts[j][split[j].substr(2)] = [];
						}
						if (!this.ssp_elmts[j][split[j].substr(2)][j]){
							this.ssp_elmts[j][split[j].substr(2)][j] = [];
						}

						this.ssp_elmts[j][split[j].substr(2)].push(ssps_obj[i].nom);
					}
					
					
					if (this.selects[j].unique.indexOf(split[j].substr(2)) != -1){
						continue;
					}
					this.selects[j].unique[this.selects[j].unique.length] = split[j].substr(2);
					
					var option = document.createElement('option');
					option.value = split[j].substr(2);
					option.innerHTML = split[j].substr(2);
					this.selects[j].select.appendChild(option);
				}
				if (this.ssp_noms.indexOf(ssps_obj[i].nom) != -1){
					return false;
				}
				this.ssp_noms.push(ssps_obj[i].nom);
				this.ssp_ids.push(ssps_obj[i].id);
			}

			box.innerHTML = '';
			var inputHidden = document.createElement('input');
			inputHidden.type = 'hidden';
			inputHidden.name = 'idssp';
			inputHidden.id = 'idssp';
			inputHidden.value = '';
			box.appendChild(inputHidden);
			
			for (var i=0; i<this.selects.length; i++){
				var elmtContainer = document.createElement('span');
				if (this.options.elmtContainerClassName != ''){
					elmtContainer.className = this.options.elmtContainerClassName;
				}
				
				var label = document.createElement('label');
				label.setAttribute('for', this.selects[i].select.id);
				label.innerHTML = this.selects[i].label;
				elmtContainer.appendChild(label);
				elmtContainer.appendChild(this.selects[i].select);
				box.appendChild(elmtContainer);
			}
			
			
			var btn = document.getElementById('btn_ssp');
			if (btn){
				var onclickAttr = btn.getAttribute('onclick');
				var inst = this;
				btn.onclick = function(){
					if (document.getElementById("idssp").value == ""){
						alert(inst.options.msgSelectSsp ? inst.options.msgSelectSsp : 'Merci de choisir le sous-produit désiré.');
						return false;
					}
					eval('function onclk(){ ' + (typeof onclickAttr == 'function' ? onclickAttr() : onclickAttr) + ' }; onclk();')
				}
			}			
		}
	}
	
	
	this.getSelectsCombinedDatas = function (selectIndex){
		var selectsCombinedDatas = null;
		
		// Pour chaque select...
		for (var i=0; i<this.selects.length; i++){
			if (selectIndex == i){
				continue;
			}
			
			// Ayant un selectedIndex > 0
			if (this.selects[i].select.selectedIndex > 0){
				var selectsDatas = {noms: [], elmts: []};
				var val = this.selects[i].select.options[this.selects[i].select.selectedIndex].value;
				
				// On boucle sur les entrées... 
				for (var j in this.ssp_elmts[i][val]){
					// Si elles sont des chaines de caractères
					if (typeof this.ssp_elmts[i][val][j] == 'string'){
						// On ajoute l'entrée au tableau selectsDatas
						selectsDatas.noms.push(this.ssp_elmts[i][val][j]);

						var split = this.ssp_elmts[i][val][j].split(this.separateur);
						for (var k=0; k<split.length; k++){
							if (!selectsDatas.elmts[k]){
								selectsDatas.elmts[k] = [];
							}
							if (selectsDatas.elmts[k].indexOf(split[k].substr(2)) == -1){
								selectsDatas.elmts[k].push(split[k].substr(2));
							}
						}
					}
				}

				// Si le tableau selectsCombinedDatas[i] (de ce select) n'existe pas...
				if (selectsCombinedDatas == null){
					// On le crée avec le contenu du tableau selectsDatas
					selectsCombinedDatas = selectsDatas;
				}
				// Si le tableau selectsCombinedDatas[i] (de ce select) existe ...
				else {
					// On boucle sur les entrées...
					for (var j in selectsCombinedDatas.elmts){
						// Si elles sont des chaines de caractères...
						if (typeof selectsCombinedDatas.elmts[j] != 'function'){
							for (var k in selectsCombinedDatas.elmts[j]){
								if (typeof selectsCombinedDatas.elmts[j][k] == 'string'){
									// Pour vérifier quelles sont bien en commun
									if (selectsDatas.elmts[j].indexOf(selectsCombinedDatas.elmts[j][k]) == -1){
										selectsCombinedDatas.elmts[j].splice(k, 1);
									}
								}
							}
						}
					}
				}
			}
		}
		return selectsCombinedDatas;
	};
	
	
	this.hideOption = function (option){
		option.className = 'sspJsHIDDEN';
		if (this.options.disabled){
			option.disabled = true;
		}
		if (this.options.disabledColor && this.options.disabledColor != ''){
			option.style.color = this.options.disabledColor;
		}
	};
	
	
	this.showOption = function (option){
		option.className = '';
		if (this.options.disabled){
			option.disabled = false;
		}
		if (this.options.disabledColor && this.options.disabledColor != ''){
			option.style.color = '#000';
		}
	};
	
	
	
	
	
	this.updateOthersSelects = function (selectIndex){			

		if (this.selects[selectIndex].select.selectedIndex == 0){
			for (var i=0; i<this.selects.length; i++){
				this.selects[i].select.selectedIndex = 0;
			}
		}
		else {
			var thisSelect = this.selects[selectIndex].select;
			if (thisSelect.options[thisSelect.selectedIndex].disabled || thisSelect.options[thisSelect.selectedIndex].className == 'sspJsHIDDEN'){  
				thisSelect.selectedIndex = this.lastSelectedIndex[selectIndex];
				return false;
			}
		}
		
		
		// Pour chaque select				
		for (var i=0; i<this.selects.length; i++){
			this.lastSelectedIndex[i] = this.selects[i].select.selectedIndex;
			
			var selectsCombinedDatas = this.getSelectsCombinedDatas(i);
			if (selectsCombinedDatas == null){
				for (var j=0; j<this.selects[i].select.options.length; j++){
					if (this.selects[i].select.options[j].value != ''){
						this.showOption(this.selects[i].select.options[j]);
					}
				}
				continue;
			}

			if (selectsCombinedDatas.elmts[i].length == 1){
				if (i == selectIndex && this.selects[selectIndex].select.selectedIndex == 0){
					for (var i=0; i<this.selects.length; i++){
						for (var j=0; j<this.selects[i].select.options.length; j++){
							if (this.selects[i].select.options[j].value != ''){
								this.showOption(this.selects[i].select.options[j]);
							}
						}
					}
				}
				else if (this.selects[i].select.selectedIndex <= 0 || this.selects[i].select.options[this.selects[i].select.selectedIndex].value != selectsCombinedDatas.elmts[i][0]) {
					this.selectByVal(this.selects[i].select, selectsCombinedDatas.elmts[i][0], i);
					this.updateOthersSelects(i);
				}
			}

			for (var j=0; j<this.selects[i].select.options.length; j++){
				if (this.selects[i].select.options[j].value != ''){
					if (selectsCombinedDatas.elmts[i].indexOf(this.selects[i].select.options[j].value) == -1){
						this.hideOption(this.selects[i].select.options[j]);
					} else {
						this.showOption(this.selects[i].select.options[j]);
					}
				}
			}
		}

		tousSelectionnes = true;
		for (var i=0; i<this.selects.length; i++){
			if (this.selects[i].select.selectedIndex <= 0){
				tousSelectionnes = false;
			}
		}

		if (tousSelectionnes){
			var ssp_nom = '';
			for (var i=0; i<this.selects.length; i++){
				if (i > 0){
					ssp_nom += this.separateur;
				}
				ssp_nom += this.codages[i] + this.selects[i].select.options[this.selects[i].select.selectedIndex].value;
			}
			var index = this.ssp_noms.indexOf(ssp_nom);
			if (index == -1){
				document.getElementById('idssp').value = '';
				alert('Le sous produit choisi n\'existe pas. Les champs vont être réinitialisé.');
				for (var i=0; i<this.selects.length; i++){
					this.selects[i].select.selectedIndex = 0;
				}
			}
			else {
				var ssp_id = this.ssp_ids[index];
				document.getElementById('idssp').value = ssp_id;
			}
		} 
		else {
			document.getElementById('idssp').value = '';
		}
	};


	this.selectByVal = function (select, value, selectIndex){
		for (var i=0; i<select.options.length; i++){
			if (select.options[i].value == value){
				select.selectedIndex = i;
				this.lastSelectedIndex[selectIndex] = i;
			//	this.showOption(select.options[i]);
				break;
			}
		}
	};
}


// Pour notre cher IE
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}
// Fin de : Pour notre cher IE

