var posy = 0;
var posx = 0;

var sPrixMini = 0;
var sPrixMaxi = 0;

var anchor = "imgSuite";

/** tableaux prix/steps **/
var priceList = new Array();

/** spécifique à l'étape 2 **/

function critStep2(){
	for(i=1;i<vTC.length;i++){
  	crit = eval("document.getElementById('critere_"+vTC[i][0]+"')");
  	if(crit!=null) crit.onclick = selectProd;
	}
}

function critStep2bis(){
	for(i=1;i<vTC.length;i++)	{
  	crit = eval("document.getElementById('critere_"+vTC[i][0]+"')");
  	if(crit!=null) crit.onclick = selectProdbis;
	}
}

function selectProd(num){
	var nbCriteresIndiff = 0;
	var piNbProduct = 0
	for (var i = 1 ; i < vTC.length ; i++)	{
		var crit = eval("document.getElementById('critere_" +i+"')");
		if(crit!=null){
	        if(crit.checked) {
	            crit.value=1;
	            vTC[i][2] = crit.value;  // valeur selectionné du critère           
	        }else {
	            crit.value=0;
	            vTC[i][2] = crit.value;  // valeur selectionné du critère
	        }
	    }
	}
			
	for (var iP = 1 ; iP < vTP.length ; iP++)	{
		var vsLigneProd = '';
		var vbSelectP = true ;  // selection du produit dans la liste finale
		for (var iCP = 1 ; iCP < vTC.length ; iCP++){
			var vCritereSelect = vTC[iCP][2] ;
			var vCritereProduct = vTP[iP][3][iCP-1];
			if ( vCritereSelect != '0' &&  (vCritereProduct.indexOf(vCritereSelect) < 0) )
					vbSelectP = false;
		}

		vsClassProduct = '';
		if (vbSelectP){
			/** renseigner la classe selected sur chaque produit identifié, et lister les produits pour le post **/		
			prod = eval("document.getElementById('prodNum_"+vTP[iP][1]+"')");	
			if(prod.style.display == 'none'){
				prod.style.display = '';	
			}
			//alert("vbSelect"+" "+prod.style.display);
			// Ajout traitement des nombres de produits "ok"
			piNbProduct++;
		}else	{
			comp = eval("document.getElementById('compare_"+vTP[iP][1]+"')");
            if (comp!=null && comp.checked == true) {
                  comp.checked = false;
            }
			prod = eval("document.getElementById('prodNum_"+vTP[iP][1]+"')");
			if(prod)prod.style.display = 'none';
			//alert("NON vbSelect"+" "+prod.style.display);
		}	
	}
	if(document.getElementById('nbresults'))document.getElementById('nbresults').innerHTML = piNbProduct;

}


function selectProdbis(){
  selectProd(2);	
}

/** création des paliers et du select des prix **/
function createSelectsPrix(){
var aList = new Array();
aList[0] = 0;
for(i=1;i<vTP.length;i++){
	aList[i] = parseFloat(vTP[i][2]);
}
aList.splice('0','1');
sPrixMini = aList.min();
sPrixMaxi = aList.max();
var tmp  = 1;

if(palier==0) palier=parseInt((sPrixMaxi-sPrixMini)/10);
if(palier==0) palier=parseInt(sPrixMaxi/2);

var tmpPrixMin=0;
var tmpPrixMaxi=0;
while(tmpPrixMin<(sPrixMini-palier)){
    tmpPrixMin+=palier;
}
while(tmpPrixMaxi<=sPrixMaxi){
    tmpPrixMaxi+=palier;
}
sPrixMini=tmpPrixMin;
sPrixMaxi=tmpPrixMaxi;
tmp  = parseInt((sPrixMaxi - sPrixMini)  / palier);

var inter = Math.floor(tmp);

var pSelectTo = eval(document.getElementById('selectMin'));
var pSelectToMax = eval(document.getElementById('selectMax'));

var sPrixTmp = parseFloat(sPrixMini);

for(i=0;i<inter;i++){
		pValue = i;
	if(i==0)	{
		priceList[0] = sPrixMini;	
		pText = sPrixMini;
	}
	else	{
		sPrixTmp+=palier;
		priceList[i] = parseInt(sPrixTmp);		
		pText = sPrixTmp;	
	}
	var newMot, newMot2;
	newMot = new Option(pText,pValue,false,false);
	newMot2 = new Option(pText,pValue,false,false);
  if(pSelectToMax!=null && pSelectTo!=null){
      pSelectToMax.options[pSelectToMax.length] = newMot;
      pSelectTo.options[pSelectTo.length] = newMot2;
  }
}

if(priceList[priceList.length-1]<sPrixMaxi){
	pValue = priceList.length;
	priceList[priceList.length] = sPrixMaxi;
	pText = sPrixMaxi;
	
	var newMot, newMot2;
	newMot = new Option(pText,pValue,false,false);
	newMot2 = new Option(pText,pValue,false,false);
	if(pSelectToMax!=null && pSelectTo!=null){
      pSelectToMax.options[pSelectToMax.length] = newMot;
      pSelectTo.options[pSelectTo.length] = newMot2;
  }	
}

/** obtenir des valeurs par défaut au chargement? **/
if(pSelectTo!=null)if(pSelectTo.options[0]!=null){
    pSelectTo.options[0].selected =true;
    pSelectToMax.options[0].selected =true;
}	

}

// Ajout des infos critères Variant   Liste des différents steps de critères
function AddCV ( pId, pLibelle){
	vTCV[pId] = new Array;
	vTCV[pId][0] = pId;
	vTCV[pId][1] = pLibelle;
}


// Ajout des infos critères
function AddC ( pId , pParamCritere){
	vTC[pId] = new Array;
	vTC[pId][0] = pId;
	vTC[pId][1] = pParamCritere;
	vTC[pId][2] = '0'; // Valeur du critère selectionné
}


// Ajout des infos produits
function AddP ( pId , psName, PfPrix , pParamProduct ){
	vTP[pId] = new Array;
	vTP[pId][0] = pId;
	vTP[pId][1] = psName;
	vTP[pId][2] = PfPrix;
	vTP[pId][3] = pParamProduct;
}


function OnLoadPage(){	
	if(document.getElementById('palier'))document.getElementById('palier').value = palier;
	createSelectsPrix();// construction des options pour chaque select de prix
	if(document.getElementById('zonePrixMini')!=null)document.getElementById('zonePrixMini').innerHTML = sPrixMini +"\&euro\;";
	if(document.getElementById('zonePrixMaxi')!=null)document.getElementById('zonePrixMaxi').innerHTML = sPrixMaxi +"\&euro\;";
	BuildSelectCritere();// construction des options pour chaque select de critère
}

function recupCritSDV(mf){
 var critere_0 = '';
 var critere_1 = '';
 var critere_2 = '';
 var critere_3 = '';
 var critere_4 = '';
 var critere_5 = '';
 var critere_6 = '';
 var critere_prix = '';
  for(i=0;i<7;i++){
	for(j=0;j<100;j++){
	if(document.getElementById('mycritere_'+i+'_'+j)) 
	{
 	if(document.getElementById('mycritere_'+i+'_'+j).checked)
 	{
 		if(i==0)
 		{
 			critere_0= critere_0 + document.getElementById('mycritere_'+i+'_'+j).value + "#";
 		}
 		if(i==1)
 		{
 			critere_1= critere_1 + document.getElementById('mycritere_'+i+'_'+j).value + "#";
 		}
 		if(i==2)
 		{
 			critere_2= critere_2 + document.getElementById('mycritere_'+i+'_'+j).value + "#";
 		}
 		if(i==3)
 		{
 			critere_3= critere_3 + document.getElementById('mycritere_'+i+'_'+j).value + "#";
 		}
 		if(i==4)
 		{
 			critere_4= critere_4 + document.getElementById('mycritere_'+i+'_'+j).value + "#";
 		}
 		if(i==5)
 		{
 			critere_5= critere_5 + document.getElementById('mycritere_'+i+'_'+j).value + "#";
 		}
 		if(i==6)
 		{
 			critere_6= critere_6 + document.getElementById('mycritere_'+i+'_'+j).value + "#";
 		}
 	}
 	}
 	}
 	}

 	for(j=0;j<100;j++)
 	{
		if(document.getElementById('mycritere_prix_'+j)) 
		{
 		if(document.getElementById('mycritere_prix_'+j).checked)
 		{
 			critere_prix= critere_prix + document.getElementById('mycritere_prix_'+j).value + "#";
 		}
		}
	}

	document.getElementById('out_critere_0').value = critere_0;
	document.getElementById('out_critere_1').value = critere_1;
	document.getElementById('out_critere_2').value = critere_2;
	document.getElementById('out_critere_3').value = critere_3;
	document.getElementById('out_critere_4').value = critere_4;
	document.getElementById('out_critere_5').value = critere_5;
	document.getElementById('out_critere_6').value = critere_6;
	document.getElementById('out_critere_prix').value = critere_prix;
 }

/**** validation du formulaire de step 1 ****/
function validCritSDV(mf){
  var nbProd = 0 ;
	var liste_produits = '';;
		var critere_1 = '';
		var critere_2 = '';
		var critere_3 = '';
		var critere_4 = '';
		var critere_5 = '';
		var critere_6 = '';
	if(document.getElementById("mycritere_1")) critere_1 = document.getElementById("mycritere_1").value;
	if(document.getElementById("mycritere_2")) critere_2 = document.getElementById("mycritere_2").value;
	if(document.getElementById("mycritere_3")) critere_3 = document.getElementById("mycritere_3").value;
	if(document.getElementById("mycritere_4")) critere_4 = document.getElementById("mycritere_4").value;
	if(document.getElementById("mycritere_5")) critere_5 = document.getElementById("mycritere_5").value;
	if(document.getElementById("mycritere_6")) critere_6 = document.getElementById("mycritere_6").value;
  for( var i=1;i<vTP.length;i++)
  {
  var bonProduit=true;
  if(critere_1!='' && critere_1!='0')
  {
    if(critere_1 != vTP[i][3][0]) bonProduit=false;
  }
  if(critere_2!='' && critere_2!='0')
  {
    if(critere_2 != vTP[i][3][1]) bonProduit=false;
  }
  if(critere_3!='' && critere_3!='0')
  {
    if(critere_3 != vTP[i][3][2]) bonProduit=false;
  }
  if(critere_4!='' && critere_4!='0')
  {
    if(critere_4 != vTP[i][3][3]) bonProduit=false;
  }
  if(critere_5!='' && critere_5!='0')
  {
    if(critere_5 != vTP[i][3][4]) bonProduit=false;
  }
  if(critere_6!='' && critere_6!='0')
  {
    if(critere_6 != vTP[i][3][5]) bonProduit=false;
  }  
  if(bonProduit) 
  {liste_produits = liste_produits+vTP[i][1]+"#";
  nbProd++;
  }

  }
			
		document.getElementById('liste_produits').value = liste_produits;
		document.getElementById('nbproduits').value = nbProd;
	/** soumission du formulaire **/
	/**document.forms[mf].submit();**/
	//document.location.href("http://localhost:8000/vad/sdv/resultatRechercheAvancee.jsp?rayon=8161&ssrayon=8164&etapeaac=2");
	//document.href("http://www.google.com");
}


/**** validation du formulaire de step 1 ****/
function validCrit(mf){
  var nbProd = document.getElementById("nbproduits").value ;
	var liste_produits = document.getElementById("liste_produits").value;
			//alert("LISTE PRODUITS "+" "+liste_produits);
	if(document.getElementById("critere_1"))var critere_1 = document.getElementById("critere_1").value;
	if(document.getElementById("critere_2"))var critere_2 = document.getElementById("critere_2").value;
	if(document.getElementById("critere_3"))var critere_3 = document.getElementById("critere_3").value;
	if(document.getElementById("critere_4"))var critere_4 = document.getElementById("critere_4").value;
	if(document.getElementById("critere_5"))var critere_5 = document.getElementById("critere_5").value;
	if(document.getElementById("critere_6"))var critere_6 = document.getElementById("critere_6").value;
	
	if(document.getElementById("selectMin"))var prixMin = priceList[document.getElementById("selectMin").value];
	if(document.getElementById("selectMax"))var prixMax = priceList[document.getElementById("selectMax").value];
	
	if(document.getElementById('palier'))document.getElementById('palier').value = palier;
	
	/** soumission du formulaire **/
	document.forms[mf].submit();
}


function BuildSelectCritere(){
	for (var i = 1 ; i < vTC.length ; i++)	{
		if(document.getElementById('critere_'+i))		{
			/** étudier le "createElement" à insérer dans les zones ?**/
			var vSelectCritere = document.getElementById('critere_'+i);
		}else	{
			var vSelectCritere = '';
		}

		var vTabCriterVariant = vTC[i][1];

		if(vSelectCritere !='')		{
			/** indifférent à 0**/
			AddToSelect( vSelectCritere, '0','Indifférent');
			
			/** les autres options **/
			for (var vIndiceCV= 1 ; vIndiceCV < vTabCriterVariant.length ; vIndiceCV++)		{
				AddToSelect( vSelectCritere
				, vTabCriterVariant[vIndiceCV][0]
				, vTabCriterVariant[vIndiceCV][1]
				);
			}
		}
	}
}


var piNbProduct = 0;
var piPrixMin = 0;
var piPrixMax = 0;

function BuildSelectListprix(){
	var results = new Array();
	results = eval(calcListeProd(1,'1'));
	changeMin();
	changeMax();
	vsListeProd = results['1'];
	var vHTML_BilanProduit =  results['2'];
	/** renseignement des zones d'affichage **/
	document.getElementById("nbResult").innerHTML = vHTML_BilanProduit ;
	document.getElementById("nbproduits").value = vHTML_BilanProduit ;
	document.getElementById('liste_produits').value =vsListeProd;
	if(document.getElementById('sliderPrix1'))document.getElementById('sliderPrix1').innerHTML = priceList[document.getElementById('selectMin').value]+"&euro;";
	if(document.getElementById('sliderPrix2'))document.getElementById('sliderPrix2').innerHTML = priceList[document.getElementById('selectMax').value]+"&euro;";
	posIndicateur();
}

function BuildSelectListeProduitSDV(){	
alert('RONAN1');
	var results = new Array();
	results = eval(calcListeProd());
alert('RONAN2');	
	var vsListeProd = results['1'];

	/** renseignement des zones d'affichage **/
	
	document.getElementById("nbproduits").value = vHTML_BilanProduit ;
	document.getElementById('liste_produits').value = vsListeProd;
alert('RONAN3');	
}

function BuildSelectListeProduit(){	
	if(document.getElementById('selectMin')!=null)document.getElementById('selectMin').value = getNumFromPrice(sPrixMini);//results['min'];
	if(document.getElementById('selectMax')!=null)document.getElementById('selectMax').value = getNumFromPrice(sPrixMaxi);//results['max'];
	var results = new Array();
	results = eval(calcListeProd());
	if(results['3']==parseInt(vTC.length-1))	{
		document.getElementById('selectMin').value = getNumFromPrice(sPrixMini);
		document.getElementById('selectMax').value = getNumFromPrice(sPrixMaxi);
		changeMin();		
		changeMax();
	}	
	
	var vsListeProd = results['1'];
	var vHTML_BilanProduit =  results['2'];		
	
	/** renseignement des zones d'affichage **/
	document.getElementById("nbResult").innerHTML = vHTML_BilanProduit ;
	
	document.getElementById("nbproduits").value = vHTML_BilanProduit ;
	document.getElementById('liste_produits').value = vsListeProd;
	
	changeMin();
	changeMax();
	if(vHTML_BilanProduit==0)	{
		if(document.getElementById('sliderPrix1'))document.getElementById('sliderPrix1').style.display ='none';
		if(document.getElementById('sliderPrix2'))document.getElementById('sliderPrix2').style.display ='none';
		if(document.getElementById('indicateurPrix'))document.getElementById('indicateurPrix').style.display ='none';
		if(document.getElementById('bt_suite'))document.getElementById('bt_suite').style.display = 'none';
	}else	{
		if(document.getElementById('sliderPrix1'))document.getElementById('sliderPrix1').style.display ='block';
		if(document.getElementById('sliderPrix2'))document.getElementById('sliderPrix2').style.display ='block';
		if(document.getElementById('indicateurPrix'))document.getElementById('indicateurPrix').style.display ='block';
		if(document.getElementById('bt_suite'))document.getElementById('bt_suite').style.display = 'block';
		posIndicateur();
	}
	if(document.getElementById('sliderPrix1'))document.getElementById('sliderPrix1').innerHTML = priceList[document.getElementById('selectMin').value]+"&euro;";
	if(document.getElementById('sliderPrix2'))document.getElementById('sliderPrix2').innerHTML = priceList[document.getElementById('selectMax').value]+"&euro;";
}

function changeMin(){
	if(document.getElementById('gutterPrix1'))	{
	var curValue = parseInt(document.getElementById('selectMin').value);
	var gutter = document.getElementById('gutterPrix1');
	var slider = document.getElementById('sliderPrix1');
	var txt = document.getElementById('texteMin');
		slider.style.left = (curValue * gutter.optionDistance) - Math.floor(slider.offsetWidth/2) + "px";
		if(txt)txt.innerHTML = priceList[curValue];
	}
}

function changeMax(){
if(document.all){
	var largeur = 415;
}else{
	var largeur = 419;
}
	if(document.getElementById('gutterPrix2') && document.getElementById('gutterPrix1'))	{
	var curValue = parseInt(document.getElementById('selectMax').value);
	var gutter = document.getElementById('gutterPrix2');
	var slider = document.getElementById('sliderPrix2');
	var txt = document.getElementById('texteMax');
		slider.style.left = (curValue * gutter.optionDistance) - Math.floor(slider.offsetWidth/2) + "px";
		if(curValue == getNumFromPrice(priceList.max())) slider.style.left = largeur + "px";
		if(txt)txt.innerHTML = priceList[curValue];
	}
}



function posIndicateur(){
	posy = lib_getImageYfromTop(anchor);
	posx = lib_getImageXfromLeft(anchor);

	if(document.getElementById('gutterPrix2') && document.getElementById('gutterPrix1'))	{
		var coordx1 = document.getElementById('sliderPrix1').style.left;
		var coordy1 = document.getElementById('sliderPrix1').style.top;
		var coordx2 = document.getElementById('sliderPrix2').style.left;
		coordx1 = coordx1.replace('px','');
		coordy1 = coordy1.replace('px','');
		coordx2 = coordx2.replace('px','');
		coordx1 = parseInt(coordx1);
		coordx2 = parseInt(coordx2);
		coordy1 = parseInt(coordy1);		

		var patchPosX = 0;
		var patchPosY = 0;
		if(!document.all && document.getElementById)		{
  		patchPosX = 274;
  		patchPosY = -303;
  		patchWidth = -2;
		}else		{
  		patchPosX = 274;		
  		patchPosY = -303;
  		patchWidth = -1;
		}
		var largeur = coordx2 - coordx1;

		document.getElementById('indicateurPrix').style.width = Math.abs(Math.abs(largeur) + patchWidth) +"px";
		document.getElementById('indicateurPrix').style.left =  patchPosX + parseInt(coordx1)+"px";
		document.getElementById('indicateurPrix').style.top = parseInt(posy)+patchPosY+"px";
		document.getElementById('indicateurPrix').style.display = 'block';
	}
}

function posIndicateurResize(){
	posy = lib_getImageYfromTop(anchor);
	posx = lib_getImageXfromLeft(anchor);
	posIndicateur();
}


/** gestion des flèches pour les critères **/
function moveTo(numCrit,data,nb){
	var curValue = eval("document.getElementById('critere_"+numCrit+"').value");
	if(nb)curValue = nb-1;
	var gutter = eval("document.getElementById('gutter"+numCrit+"')");
	var tabEquiv = new Array();
	var vTab = vTC[numCrit][1];
	tabEquiv[0] = 0;
	for (var vIndiceCV= 1 ; vIndiceCV < vTab.length ; vIndiceCV++)	{
		tabEquiv[vIndiceCV] = vTab[vIndiceCV][0];
	}

	var input = eval("document.getElementById('critere_"+numCrit+"')");
	var slider = eval("document.getElementById('slider"+numCrit+"')");
	var txt = eval("document.getElementById('infos"+numCrit+"')");

	switch (data)	{
		case 'droite':		
			for(i = 0; i<tabEquiv.length;i++)	{
				if(tabEquiv[i]==curValue)	{
					if(i<(tabEquiv.length-1))	{
						var nCurValue = parseInt(tabEquiv[i+1]);
						slider.style.left = (parseInt(i+1) * gutter.optionDistance) - Math.floor(slider.offsetWidth/2) + "px";
						if(txt)txt.innerHTML = gutter.options[parseInt(i+1)].text;
					}else	{
						var nCurValue = tabEquiv[i];
						slider.style.left = ((i) * gutter.optionDistance) - Math.floor(slider.offsetWidth/2) + "px";
						if(txt)txt.innerHTML = gutter.options[i].text;
					}
				}
			}
			input.value = nCurValue;

			BuildSelectListeProduit();
			posIndicateur();		
		break;
		case 'gauche':
			for(i = 0; i<tabEquiv.length;i++)	{
				if(tabEquiv[i]==curValue && curValue!=0)	{
					var nCurValue = parseInt(tabEquiv[i-1]);
					slider.style.left = (parseInt(i-1) * gutter.optionDistance) - Math.floor(slider.offsetWidth/2) + "px";
					if(txt)txt.innerHTML = gutter.options[parseInt(i-1)].text;
				}else if(tabEquiv[i]==curValue && curValue==0)	{
					var nCurValue = parseInt(tabEquiv[i]);
					slider.style.left = (parseInt(i) * gutter.optionDistance) - Math.floor(slider.offsetWidth/2) + "px";
					if(txt)txt.innerHTML = gutter.options[parseInt(i)].text;
				}
			}
			input.value = nCurValue;
			BuildSelectListeProduit();
		break;
	}
}

function calcListeProd(num,squizz){
// Gestion des filtre de prix
	if(!isUndefined(squizz))	{
  	var viPrixMin =	document.getElementById('selectMin').value;
  	var viPrixMax =	document.getElementById('selectMax').value;
	}	else	{
  	var viPrixMin =	getNumFromPrice(sPrixMini);
  	var viPrixMax =	getNumFromPrice(sPrixMaxi);	
	}
	var nbCriteresIndiff = 0;
	var vTabCriterVariant = new Array;
	if(viPrixMax == "")	{
		viPrixMax = priceList.length-1;
	}
	///// Initialisation des valeurs globales
	vbFiltrePrix = false;
	var vsListeProd = '';
	var listePrix = new Array();
	///// Initialisation des valeurs globales
	piNbProduct = 0; // Initialisation du nombre de produit
	piPrixMin = viPrixMin;
	piPrixMax = viPrixMax;
	if ( (priceList[piPrixMin] != priceList[viPrixMin]) ||(priceList[piPrixMax] != priceList[viPrixMax]) ) vbFiltrePrix  = true
	for (var i = 1 ; i < vTC.length ; i++)	{
		  var viCritereSelect = GetLastSelected ( document.getElementById('critere_' +i ))
			vsClass  = ''
			if (viCritereSelect != 0)  vsClass  = 'Selected'
			vTC[i][2] = viCritereSelect;  // valeur selectionné du critère
  		TabCriterVariant = vTC[i][1];
  		if(viCritereSelect==0)nbCriteresIndiff+=1;
	}
	for (var iP = 1 ; iP < vTP.length ; iP++)	{
		var vsLigneProd = '';
		var vbSelectP = true ;  // selection du produit dans la liste finale

		for (var iCP = 1 ; iCP < vTC.length ; iCP++)	{
			var vCritereSelect = vTC[iCP][2] ;
			var vCritereProduct = vTP[iP][3][iCP-1];
			if ( vCritereSelect != '0' &&  (vCritereProduct.indexOf(vCritereSelect) < 0) )
					vbSelectP = false;
		}

		vsClassProduct = '';
		if (vbSelectP){
			/** prix du produit courant de la boucle**/
			var vfPrix = parseFloat(vTP[iP][2]) ;
			/** première fois pour tester le prix **/
			if(!isUndefined(squizz))	{
				if (!vbFiltrePrix || ((parseFloat(vfPrix) >= parseFloat(priceList[viPrixMin]) &&  (parseFloat(vfPrix)<= parseFloat(priceList[viPrixMax])))))			{
					/** si on est dans la fourchette définie par le double slider de prix **/
					if((parseFloat(vfPrix) >= parseFloat(priceList[viPrixMin])) && (parseFloat(vfPrix) <= parseFloat(priceList[viPrixMax])))	{
						/** renseigner la classe selected sur chaque produit identifié, et lister les produits pour le post **/
						listePrix[listePrix.length] = parseFloat(vTP[iP][2]);
						vsLigneProd = vTP[iP][1]+ "#";
						piNbProduct++;
					}
				}
			}else			{
				if(!vbFiltrePrix)		{
				listePrix[listePrix.length] = vfPrix;			
				vsLigneProd = vTP[iP][1]+ "#";
				piNbProduct++;
				}
			}
		}
		if(vbFiltrePrix)		{
			//définition des valeurs pour les fois suivantes
			piPrixMin = viPrixMin;
			piPrixMax = viPrixMax;
		}
		/** ajout à la liste du produit répondant aux critères **/
		vsListeProd +=   vsLigneProd ;
	}

	if(listePrix.length>0 && isUndefined(squizz))	{
		piPrixMin = listePrix.min();
		piPrixMax = listePrix.max();		
		for(i=0;i<priceList.length;i++)		{
			if(parseFloat(priceList[i])<=parseFloat(piPrixMin))	{
			vPrixMin = i;
			}			
		}
		for(i=priceList.length;i>0;i--)		{
			if(priceList[i]>=piPrixMax)		{
			 vPrixMax = i;
			}
		}
		piPrixMin = vPrixMin;
		piPrixMax = vPrixMax;
	}
	// MAJ des prix
	if(piPrixMin!='' && isUndefined(squizz)){
		document.getElementById('selectMin').value = piPrixMin;
	}
	if(piPrixMax!='' && isUndefined(squizz))	{
		document.getElementById('selectMax' ).value = piPrixMax;
	}
	latotale = new Array();
	latotale['1'] = vsListeProd;
	latotale['2'] = piNbProduct;
	latotale['3'] = nbCriteresIndiff;
	latotale['min'] = piPrixMin;
	latotale['max'] = piPrixMax;
	
	switch (num){
	
	default:
		return latotale;
	break;	
	}
}

function getNumFromPrice(price){
	for (i=0;i<priceList.length;i++)	{
		if(priceList[i] == price)return i;
	}
}

////////////////////////////////////////////

// Ajoute une option au select
// avec les params : valeur et texte de l'option

function AddToSelect(pSelectTo, pValue, pText ){
var newMot;
newMot = new Option(pText,pValue,false,false);
if(pSelectTo.options!=null){
    pSelectTo.options[pSelectTo.length] = newMot;
}
}

// Renvoi la valeur du dernier élement sélectionné
// renvoi "null" si aucune valeur n'est sélectionnée

function GetLastSelected(pSelect){
var vResult="null";
vResult=pSelect.value;
return vResult;
}

/************** événements et positionnements ***********/
function handleEvent(obj, event, func) {
	try {
		obj.addEventListener(event, func, false);
	} catch (e) {
		if (typeof eval("obj.on"+event) == "function") {
			var existing = obj['on'+event];
			obj['on'+event] = function () { existing(); func(); };
		} else {
			obj['on'+event] = func;
		}
	}
}


function findPosX(obj){
	if (!obj) { return; }
	var curleft = 0;
	if (obj.offsetParent)	{
		while (obj.offsetParent)	{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	if (!obj) { return; }
	var curtop = 0;
	if (obj.offsetParent)	{
		while (obj.offsetParent)		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/*** fonctions génériques sur les strings et array ***/
function isNull(a) {
    return typeof a == 'object' && !a;
}
function isUndefined(a) {
    return typeof a == 'undefined';
}

function ExtendArray() {
Array.prototype.min = Array_GetMin;
Array.prototype.max = Array_GetMax;
}

function Array_GetMin() {
var Min = this[0];
if (isNaN(Min*1) && !IsDate(Min)) return '';
for (var i=0; i<this.length; i++) if (this[i]*1<Min) Min = this[i];
return Min;
}

function Array_GetMax() {
var Max = this[0];
if (isNaN(Max*1) && !IsDate(Max)) return '';
for (var i=0; i<this.length; i++) if (this[i]*1>Max) Max = this[i];
return Max;
}

/** gestion des sliders **/

function SliderSelect() {
  	var self = this;
  	self.instantiate = function () {
		posy = lib_getImageYfromTop(anchor);
		posx = lib_getImageXfromLeft(anchor);

		ExtendArray();
		OnLoadPage();
    if(document.getElementById('module_aide_bas')!=null){
        var allSelectsPrix = document.getElementById('module_aide_bas').getElementsByTagName('select');
        for (var o = allSelectsPrix.length-1; o >= 0; o--) {
                self.slidifyPrix(allSelectsPrix[o],o);
        }
		}
    if(document.getElementById('module_aide_content')!=null){
        var allSelects = document.getElementById('module_aide_content').getElementsByTagName('select');
        for (var i = allSelects.length-1; i >= 0; i--) {
                self.slidify(allSelects[i],i);
        }
    }
		
	}
	
	/*****************************/
	/*** les slides des prix  ***/
	/****************************/

	self.slidifyPrix = function (selectBox,num) {
		var selectBoxWidth  = selectBox.offsetWidth;
		num = num+1;
		var containerDiv         = document.createElement('div');
		containerDiv.id			 = "zonePrix"+num;
		containerDiv.className   = "SliderContainerPrix";
		containerDiv.style.width = selectBoxWidth + "px";

		var theGutter                = document.createElement('div');
			theGutter.id 			 = "gutterPrix"+num;
			theGutter.className      = "gutterPrix"+num;
			theGutter.options        = selectBox.options;
			theGutter.numOptions     = selectBox.options.length;
			theGutter.optionDistance = Math.floor(selectBoxWidth/(theGutter.numOptions-1));
			theGutter.defaultOption  = (selectBox.selectedIndex)?selectBox.selectedIndex:Math.floor(selectBox.length/2);
			if(num==1){
				theGutter.defaultOption = 0;
			}
			else	{
				theGutter.defaultOption = parseInt(selectBox.length-1);
			}

		var worstName = selectBox.options[0].name;
		var bestName  = selectBox.options[theGutter.numOptions-1].name;
		var theSlider           = document.createElement('span');
		theSlider.className = "sliderPrix"+num;
		theSlider.id 		= "sliderPrix"+num;
		var theInput            = document.createElement('input');
		theInput.id         = selectBox.id;
		theInput.name       = selectBox.name;
		theInput.type       = "hidden";
		theInput.value      = theGutter.defaultOption;//selectBox.options[theGutter.defaultOption].value;
		theGutter.appendChild(theSlider);

		for (i = 0; i < theGutter.numOptions; i++)		{
			var placeHolder = document.createElement('span');
			placeHolder.className  = 'placeHolderPrix'+num;
			placeHolder.style.left = Math.min(selectBoxWidth, (i * theGutter.optionDistance))+ "px";
			if(i==(theGutter.numOptions-1))placeHolder.style.left = selectBoxWidth+ "px";
			theGutter.appendChild(placeHolder);
		}

		containerDiv.appendChild(theGutter);
		containerDiv.appendChild(theInput);
		self.theInput = theInput;
		self.theInput.value=theInput.value ;

		handleEvent(theGutter, "mousedown", self.registerSliderPrix);
		handleEvent(theGutter, "mouseup",   self.unregisterSliderPrix);
		selectBox.parentNode.replaceChild(containerDiv, selectBox);

		if(num==2){
			containerDiv.style.marginLeft = "-430px";
		}
		theSlider.style.left = (Math.min(selectBoxWidth, (theGutter.defaultOption * theGutter.optionDistance)) - Math.floor(theSlider.offsetWidth/2)) + "px";
		BuildSelectListprix();
	}

	self.registerSliderPrix = function (e) {
		if (!e) e = window.event;
		if (self.activeGutter) {
			self.activeGutter.onmousemove = null;
		}

		self.activeGutter = this;
		self.activeSlider = this.childNodes[0];
		self.activeInput  = this.parentNode.childNodes[1];
		handleEvent(document, "mousemove", self.mousemovePrix);
		handleEvent(document, "mouseup",   self.unregisterSliderPrix);
		self.mousemovePrix(e);
		e.cancelBubble = true;
		return false;
	}

	self.unregisterSliderPrix = function (e) {
		if (!self.activeGutter) { return; }

		var minValue = document.getElementById('selectMin').value;
		var maxValue = document.getElementById('selectMax').value;
		if(self.activeSlider.id == 'sliderPrix1')		{
			if(parseInt(self.activeGutter.options[self.activeGutter.optionNum].value) < parseInt(maxValue))		{
				self.activeSlider.style.left = (Math.min(self.activeGutter.offsetWidth, (self.activeGutter.optionNum * self.activeGutter.optionDistance)) - Math.floor(self.activeSlider.offsetWidth/2)) + "px";
				self.activeInput.value = self.activeGutter.options[self.activeGutter.optionNum].value;
			}
			else	{
				self.activeSlider.style.left = ((parseInt(maxValue-1)) * self.activeGutter.optionDistance) - Math.floor(self.activeSlider.offsetWidth/2) + "px";
				self.activeInput.value = parseInt(maxValue-1);

			}
		}else	{
			if(parseInt(self.activeGutter.options[self.activeGutter.optionNum].value) > parseInt(minValue))		{
				self.activeSlider.style.left = (Math.min(self.activeGutter.offsetWidth, (self.activeGutter.optionNum * self.activeGutter.optionDistance)) - Math.floor(self.activeSlider.offsetWidth/2)) + "px";
				self.activeInput.value = self.activeGutter.options[self.activeGutter.optionNum].value;
			}
			else	{
				self.activeSlider.style.left = ((parseInt(minValue)+1) * self.activeGutter.optionDistance) - Math.floor(self.activeSlider.offsetWidth/2) + "px";
				self.activeInput.value = parseInt(minValue)+1;
			}
		}
		if(self.activeGutter.id =='gutterPrix1')document.getElementById('sliderPrix1').innerHTML = priceList[self.activeGutter.options[self.activeGutter.optionNum].value]+"&euro;";
		if(self.activeGutter.id =='gutterPrix2')document.getElementById('sliderPrix2').innerHTML = priceList[self.activeGutter.options[self.activeGutter.optionNum].value]+"&euro;";

		if(self.activeGutter.id =='gutterPrix1')document.getElementById('selectMin').value= self.activeInput.value;
		if(self.activeGutter.id =='gutterPrix2')document.getElementById('selectMax').value= self.activeInput.value;
		BuildSelectListprix();
		document.onmousemove = null;
		self.activeGutter  = null;
		self.activeSlider  = null;
	}


	self.mousemovePrix = function (e) {
		if (!e) {
			e = window.event;
			e.returnValue = false;
		}
		if (!self.activeGutter) { return; }

		self.offset = Math.min(
				Math.max(0, (e.clientX - findPosX(self.activeGutter))),
				self.activeGutter.offsetWidth
			);

		var currentPos = self.offset;

		var snapTo     = currentPos % self.activeGutter.optionDistance;
		self.activeGutter.optionNum = Math.round(currentPos / self.activeGutter.optionDistance);

		if (snapTo <= 10) {
			currentPos = currentPos - snapTo;
		} else if ((self.activeGutter.optionDistance - snapTo) <= 10) {
			currentPos = Math.min(self.activeGutter.offsetWidth, currentPos + (self.activeGutter.optionDistance - snapTo));
		}

		if(self.activeSlider.id=='sliderPrix1' || self.activeSlider.id == 'sliderPrix2')	{
		self.activeSlider.style.left = (currentPos - Math.floor(self.activeSlider.offsetWidth/2)) + "px";
		self.activeSlider.innerHTML = priceList[self.activeGutter.options[self.activeGutter.optionNum].value]+"&euro;";
		self.activeInput.value = self.activeGutter.options[self.activeGutter.optionNum].value;
		BuildSelectListprix();
		}

	}
	/*** fin des slides des prix ***/
	
	/** "sliders simples" **/
	self.slidify = function (selectBox,num) {
		var selectBoxWidth  = selectBox.offsetWidth;
		num = num+1;
		var containerDiv         = document.createElement('div');
		containerDiv.className   = "SliderContainer";
		containerDiv.style.width = selectBoxWidth + "px";
		var theGutter                = document.createElement('div');
		theGutter.className      = "gutter";
		theGutter.id			 = "gutter"+num
		theGutter.options        = selectBox.options;
		theGutter.numOptions     = selectBox.options.length;
		theGutter.optionDistance = Math.floor(selectBoxWidth/(theGutter.numOptions-1));			
		theGutter.defaultOption  = (selectBox.selectedIndex)?selectBox.selectedIndex:0;
		theGutter.num = num;			
		var worstName = selectBox.options[0].name;
		var bestName  = selectBox.options[theGutter.numOptions-1].name;
		
		var theSlider           = document.createElement('span');
		theSlider.className = "slider";
		theSlider.id = "slider"+num;

		var theInput            = document.createElement('input');
		theInput.id         = selectBox.id;
		theInput.name       = selectBox.name;
		theInput.type       = "hidden";
		theInput.value      = selectBox.options[theGutter.defaultOption].value;

		theGutter.appendChild(theSlider);

		var placeHolder = document.createElement('span');
			placeHolder.className  = 'placeHolder';
			theGutter.appendChild(placeHolder);

		for (i = 1; i < theGutter.numOptions; i++)		{
			  var placeHolder = document.createElement('span');
				placeHolder.className  = 'placeHolder';
				placeHolder.style.left = Math.min(selectBoxWidth, (i * theGutter.optionDistance)) + "px";

			theGutter.appendChild(placeHolder);
		}

		containerDiv.appendChild(theGutter);
		containerDiv.appendChild(theInput);
		self.theInput = theInput;		

		handleEvent(theGutter, "mousedown", self.registerSlider);
		handleEvent(theGutter, "mouseup",   self.unregisterSlider);

		selectBox.parentNode.replaceChild(containerDiv, selectBox);

		theSlider.style.left = (Math.min(selectBoxWidth, (theGutter.defaultOption * theGutter.optionDistance)) - Math.floor(theSlider.offsetWidth/2)) + "px";
		theSlider.style.top ="0px";

		if(critArray[num]!=0)		{
			moveTo(num,'droite',critArray[num]);	
		}	else{
			BuildSelectListeProduit();
		}	
	}
	

	self.registerSlider = function (e) {
		if (!e) e = window.event;

		if (self.activeGutter) {
			self.activeGutter.onmousemove = null;
		}

		self.activeGutter = this;
		self.activeSlider = this.childNodes[0];
		self.activeInput  = this.parentNode.childNodes[1];
		handleEvent(document, "mousemove", self.mousemove);
		handleEvent(document, "mouseup",   self.unregisterSlider);
		self.mousemove(e);
		e.cancelBubble = true;
		return false;
	}

	self.unregisterSlider = function (e) {
		if (!self.activeGutter) { return; }
		self.activeSlider.style.left = (Math.min(self.activeGutter.offsetWidth, (self.activeGutter.optionNum * self.activeGutter.optionDistance)) - Math.floor(self.activeSlider.offsetWidth/2)) + "px";
		self.activeInput.value = self.activeGutter.options[self.activeGutter.optionNum].value;
		var tmp = self.activeSlider.id.replace('slider','');

		if(self.activeGutter.id!='gutterPrix1' && self.activeGutter.id!='gutterPrix2')	{
		  document.getElementById('infos'+tmp).innerHTML = self.activeGutter.options[self.activeGutter.optionNum].text;
		}

		document.onmousemove = null;
		self.activeGutter  = null;
		self.activeSlider  = null;
	}

	self.mousemove = function (e) {
		if (!e) {
			e = window.event;
			e.returnValue = false;
		}
		if (!self.activeGutter) { return; }

		self.offset = Math.min(
			Math.max(0, (e.clientX - findPosX(self.activeGutter))),
			self.activeGutter.offsetWidth
			);

		var currentPos = self.offset;
		var snapTo     = currentPos % self.activeGutter.optionDistance;
		self.activeGutter.optionNum = Math.round(currentPos / self.activeGutter.optionDistance);

		if (snapTo <= 10) {
			currentPos = currentPos - snapTo;
		} else if (self.activeGutter.optionDistance - snapTo <= 10) {
			currentPos = Math.min(self.activeGutter.offsetWidth, currentPos + (self.activeGutter.optionDistance - snapTo));
		}

		self.activeSlider.style.left = (currentPos - Math.floor(self.activeSlider.offsetWidth/2)) + "px";
		self.activeInput.value = self.activeGutter.options[self.activeGutter.optionNum].value;
		BuildSelectListeProduit();
		current = document.getElementById('infos'+self.activeGutter.num);
		var tmp = self.activeGutter.options[self.activeGutter.optionNum].text
		current.innerHTML = tmp;

		document.getElementById('sliderPrix1').innerHTML = document.getElementById('texteMin').innerHTML+"&euro;";
		document.getElementById('sliderPrix2').innerHTML = document.getElementById('texteMax').innerHTML+"&euro;";
	}
	handleEvent(window, "load", self.instantiate);	
}
handleEvent(window, "load", posIndicateur);
