//-----------------[ Trieur ]------------------
/*
Creation de l'objet
*/
Trieur = function( aDefinition )
{
	this.iChoix = 0;
	this.aDefault = new Array();
	
	this.aResult = new Array();
	this.aResult[0] = new Array();
	this.aResult[1] = new Array();
	this.aResult[2] = new Array();
	
	this.aSelect = new Array();
	this.db = aDefinition;
}
//-----------------[ addOption ]------------------
/*
Enregistre un critére de tri
*/
Trieur.prototype.addOption = function(id,aNameDefinition,oSelect)
{
	this.aSelect[id] = {'definition':aNameDefinition,'select':oSelect,'id':id};
	this.aSelect[id].select.trieur = this;
	this.aSelect[id].select.id = id;

	this.aSelect[id].select.onchange = function(){ this.trieur.onchange(this.id) }
	if(id==0)this.setOptionName( id );

}
//-----------------[ setOptionName ]------------------
/*
Trouve les valeurs possibles pour un select en fonction de tous les critéres précedents
*/
Trieur.prototype.setOptionName = function(id , array)
{
	var i,iOption = 0;
	
	
	this.setEmpty(id);
	for(i in this.aSelect[id].definition ){
		if( array == undefined || inArray(i ,array ) ){
			this.aSelect[id].select.options[iOption] = new Option( this.aSelect[id].definition[i] , i );
			iOption++;
		}
	}
	if( this.aSelect[id].select.options.length == 1){
		this.aSelect[id].select.options[0].selected  = true;
	}
	this.aSelect[id].select.onchange();

}
//-----------------[ setEmpty ]------------------
/*
Vide un select
*/
Trieur.prototype.setEmpty= function(id){
	var i;
	if( this.aSelect[id].select.options.length ==0) return;
	for(i=this.aSelect[id].select.options.length - 1; i>=0;i--) this.aSelect[id].select.options[i] = null;
}
//-----------------[ getRelation ]------------------
/*
Obtient les options  en fonction de tous les critéres précedents
*/
Trieur.prototype.getRelation = function(id)
{
	var i,j,bAccept,aTemp=new Array();
	var nextId = new Number(id)+1;
	for(i in this.db ){
		bAccept = true
		for( j=0; j<=id; j++){
			if( ! inArray( this.db[i][j] , this.getValue(j) ) ) bAccept = false;
		}
		if(bAccept && ! inArray(this.db[i][nextId] , aTemp ) )aTemp.push( this.db[i][nextId] );
	}
	
	return aTemp
}
//-----------------[ onchange ]------------------
Trieur.prototype.onchange= function(id){
	var nextId = new Number(id)+1;
	if( nextId < this.aSelect.length ) this.setOptionName(nextId , this.getRelation(id) );
}
//-----------------[ getValue ]------------------
Trieur.prototype.getValue = function(id)
{
	var i,aReturn = new Array();
	for(i=0;i<this.aSelect[id].select.options.length;i++ ){
		if( this.aSelect[id].select.options[i].selected ) aReturn.push( this.aSelect[id].select.options[i].value );
	}

	return aReturn;
}

//-----------------[ setDefault ]------------------
Trieur.prototype.setDefault = function()
{
	var i,j;
	for(i=0;i<this.aSelect.length;i++ ){
		if( this.aDefault[i] != undefined &&  this.aDefault[i] !='')
			for(j=0;j< this.aSelect[i].select.options.length;j++ ) 
				if( this.aSelect[i].select.options[j].value == this.aDefault[i]  ) this.aSelect[i].select.options[j].selected = true;
			this.aSelect[i].select.onchange();
	}
}
///////////////////////////////////////////////////
//	FUNCTIONS DE RESULTATS
///////////////////////////////////////////////////

	//-----------------[ saveResult ]------------------
	Trieur.prototype.saveResult = function()
	{
		var i;
		for( i=0;i<this.aSelect.length;i++ ){
			this.aResult[ this.iChoix ][i] = this.getValue( i )
		}
	}
	//-----------------[ getStringValue ]------------------
	Trieur.prototype.getStringValue = function( id , aResult )
	{
		var i;
		if (aResult == undefined ) aResult = this.getValue(id);
		for(i in aResult ) aResult[i] = this.aSelect[id].definition[ aResult[i] ];
		return aResult.join(' ou ')
	}
	//-----------------[ getStringResult ]------------------
	Trieur.prototype.getStringResult = function( iChoix )
	{
		var i,sReturn='';
		for( i=0;i<this.aResult[iChoix].length;i++ ){
			sReturn = sReturn + this.getStringValue(i , this.aResult[iChoix][i] )+'<br>';
		}
		return sReturn
	}
	//-----------------[ getNumericResult ]------------------
	Trieur.prototype.getNumericResult = function()
	{
		var i,aResult=new Array();
		for(i in this.aResult[ this.iChoix ] ) {
			if( !isNaN(i) ) {
				aResult[i] =  this.getValue( i ).join(',');
			}
		}
		return aResult.join(';')
	}

//-----------------[ inArray ]------------------
function inArray (value, array)
{
	var s = new String(""+array.join("")+"");
	return ( s.indexOf(""+value+"")!= -1 );
}