function refreshSelects(){
	var color_index = document.getElementById("ASColor").selectedIndex;
	var taille_index = document.getElementById("ASSize").selectedIndex;
	var type_index = document.getElementById("ASType").selectedIndex;
	var tri_index = document.getElementById("ASTri").selectedIndex;

	var color = "";
	if(color_index > 0)
		color = document.getElementById("ASColor").options[color_index].value;
	
	var size = "";
	if(taille_index > 0)
		size = document.getElementById("ASSize").options[taille_index].value;
	
	var type = "";
	if(type_index > 0)
		type = document.getElementById("ASType").options[type_index].value;
	
	var tri = "";
	if(tri_index > 0)
		type = document.getElementById("ASTri").options[tri_index].value;
	
	
	var storeId = document.getElementById("storeId").value;
	var catalogId = document.getElementById("catalogId").value;
	var langId = document.getElementById("langId").value;
	
	var params = "ASType="+type+"&ASSize="+size+"&ASColor="+color+"&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&ASTri="+tri;
	
	document.getElementById("ASColor").disabled = true;
	document.getElementById("ASSize").disabled = true;
	document.getElementById("ASType").disabled = true;
	document.getElementById("ASTri").disabled = true;
	
	makeContentRequest('LuceneAdvancedSearchView',params);
}

function refreshType(){
	document.getElementById("ASSize").selectedIndex = 0;
	document.getElementById("ASColor").selectedIndex = 0;
	refreshSelects();
}

function makeContentRequest(url,params) {
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	
	http_request.onreadystatechange = function() { processContentRequestResult(http_request,url,params); };
	http_request.open('POST', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(encodeURI(params));
}	
	
	
		
function processContentRequestResult(http_request, url, params) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var html = http_request.responseText;				
			var div = document.getElementById("encartRechercheAvancee");
			if(div){
				div.innerHTML=decodeURI(html);
				sortArraySize("ASSize");
				sortArrayColor("ASColor");
			}
		}
	}
}
// fonction tri selon le prix
function sortByPrice(tabSearch, tri){
	 var temp = new Array();
	 var result = new Array();
	     for(var j=0 ; j< tabSearch.length ; j++){
		     temp [j] = new Array(tabSearch[j][1],tabSearch[j][0]);
	     }
     temp.sort();
     if(tri == 1)
	    	temp.reverse();
     var k =0 ;
	     for(var ind = 0 ; ind< temp.length ; ind++){
		     	for(var j=0 ; j < tabSearch.length ; j++ ){
			     		if (tabSearch[j][0] == temp[ind][1] ){
				     			result[k] = tabSearch[j];
				     			k++;
			     		}
		     	}
	     }
   	     return result;
}
// fonction qui recupere la valeur du bouton radio checked
function getCheckedValue(){
     if(document.search_articles.tri[0].checked)
     	return document.search_articles.tri[0].value;
     	else
     	return document.search_articles.tri[1].value;
}
function updateDataByTypeDisplay(){
	document.getElementById("select_color").options[0].selected = true;
	document.getElementById("select_height").options[0].selected = true;
	updateDataDisplay()
}
function updateDataDisplay(){
	var show_color = true;
	var show_height = true;
	var show_type = true;

	var color_index = document.getElementById("select_color").selectedIndex;
	var taille_index = document.getElementById("select_height").selectedIndex;
	var type_index = document.getElementById("select_type").selectedIndex;

	var color_selected = "";
	if(color_index > 0)
	color_selected = document.getElementById("select_color").options[color_index].value;
	
	var height_selected = "";
	if(taille_index > 0)
	height_selected = document.getElementById("select_height").options[taille_index].value;
	
	var type_selected = "" ;
	if(type_index > 0)
	type_selected = document.getElementById("select_type").options[type_index].value;
	
	tabResultSearch = new Array();
	var index = 0;
	var tabColors = new Array();
	var tabSizes = new Array();
	
	for(var i=0; i<tabProduct.length;i++){
		var items = tabProduct[i][1];
		var show_type = true;
		
    	if(type_selected != ""){
			if(tabProduct[i][2] != type_selected){
			 	show_type = false;
		 	} 
	    } 
	    var hasItems = true;
		for(var j=0; j<items.length;j++){
			var show_color = true;
			var show_height = true;
			var iId = tabProduct[i][0]+"-"+items[j];
			
			if(color_selected != "" ) {
				if(tabInfo[iId][2] != color_selected){
	             	show_color = false; 
	            } 
	    	} 
	    	if(show_type){
				if(!tabColors.contains(tabInfo[iId][2])){
				
					tabColors[tabColors.length] = new Array(tabInfo[iId][2],tabInfo[iId][1],color_selected);
				}
				if(show_color){
					if(!tabSizes.contains(tabInfo[iId][4])){
						tabSizes[tabSizes.length] = new Array(tabInfo[iId][4],tabInfo[iId][3],height_selected);
					}
				}
			}
	    	
	    	
		    if(height_selected != ""){
				if(tabInfo[iId][4] != height_selected){
				 	show_height = false;
			 	} 
		    }  
		    if(hasItems){
			    if(show_color && show_height && show_type)   { 
			    	tabResultSearch[index] = new Array(tabProduct[i][0],tabInfo[iId][5],tabInfo[iId][6],tabInfo[iId][7],tabInfo[iId][8],tabInfo[iId][9],tabInfo[iId][10],tabInfo[iId][11],tabInfo[iId][12],tabInfo[iId][13],tabInfo[iId][2],tabInfo[iId][4],tabProduct[i][2],tabInfo[iId][14],tabInfo[iId][15]);
			    	index++;
			    	hasItems = false;
			    }
		    }
		}
		
	}
	fillSelect("select_color",tabColors);
	fillSelect("select_height",tabSizes);
	
	updateData(tabResultSearch);
}


function fillSelect(id,tab){
	var len = document.getElementById(id).length; 
	if(tab.length > 0){
		for(var i=len; i>0;i--){
			document.getElementById(id).options[i] = null;
		}
		for(var i=0; i<tab.length ;i++){
		 	var element = new Option(tab[i][1],tab[i][0],false,(tab[i][0]==tab[i][2]));
		 	document.getElementById(id).options[document.getElementById(id).length] = element;
		}
	}
}


function updateData(tab){
	var result ="" ;
	var sort_selected = "0" ;
	
	var OrdreApparition = document.getElementById("OrdreApparition").selectedIndex;
	if(document.getElementById("OrdreApparition"))
		sort_selected = document.getElementById("OrdreApparition").options[OrdreApparition].value;
	tab = sortByPrice(tab, sort_selected);
	for(var i=0; i<tab.length;i++){
	
		result +="<div class=\"item\">";
		result +="	<div class=\"pic\">";
		result +="		<div class=\"cadrepic\">";
		result +="		<img id=\"visuel"+(i+1)+"\" alt=\""+tab[i][2]+"\" src=\""+tab[i][13]+"\"/>";
		result +="				<a title=\""+tab[i][2]+"\" href=\""+tab[i][3]+"\" onmouseout=\"MM_swapImgRestore();\" onmouseover=\"MM_swapImage('visuel"+(i+1)+"','','"+tab[i][14]+"',1); \"><span class=\"off\">Voir le produit</span></a>";
		result +="				<div class=\"CoinsArrondis\"></div>";
		result +="				<span title=\""+tab[i][2]+"\"></span>";
		result +="		</div>";
		result +="	</div>";
		result +="	<div class=\"item_bot\">";
		result +="	<h5>"+tab[i][4]+"</h5>";
		result +="	<ul>";
		result +="		<li class=\""+tab[i][5]+"\">"+tab[i][6]+" <span class=\"bar\"></span></li>";
		result +="		<li class=\""+tab[i][7]+"\"> "+tab[i][1]+" </li>";
		result +="		<li>"+tab[i][2]+" </li>";
		result +="	</ul>";
		var cPicto = tab[i][8];
		if(cPicto != null && cPicto != "")
		result +="		<div class=\""+tab[i][8]+"\"><span>"+tab[i][9]+"</span></div>";
		result +="  </div>";
		result +="</div>";
	}
	sortArraySize("select_height");
	sortArrayColor("select_color");
	document.getElementById("searchContent").innerHTML = result;
}

// fonction tri selon le prix
function sortByPrice(tabSearch, tri){
	 var temp = new Array();
	 var result = new Array();
     for(var j=0 ; j< tabSearch.length ; j++){
	     temp [j] = new Array(tabSearch[j][1],tabSearch[j][0]);
     }
     temp.sort(testsort);
     if(tri == 1)
	    	temp.reverse(testsort);
     var k =0 ;
	     for(var ind = 0 ; ind< temp.length ; ind++){
		     	for(var j=0 ; j < tabSearch.length ; j++ ){
			     		if (tabSearch[j][0] == temp[ind][1] ){
				     			result[k] = tabSearch[j];
				     			k++;
			     		}
		     	}
	     }
   	     return result;
}

function testsort(a,b){
	return parseFloat(a)-parseFloat(b);
}

/*Recherche*/

function testTaille(a,b){
	if(a[0] != b[0]){
		return a[0]-b[0];
	}else{
		return a[1].charCodeAt(0)-b[1].charCodeAt(0)
	}
}
// Fonction de tri d'une lisbox selon les critere (Tour de poitrine / Bonnet)
function sortArraySize(id_select){
	var Liste = new Array();
	Obj = document.getElementById(id_select);
	var selected = Obj.selectedIndex;
	for (i = 1; i < Obj.options.length; i++) {
		Liste[i-1] = new Array();
		Liste[i-1][0] = Obj.options[i].text;
		Liste[i-1][1] = Obj.options[i].value;
		
		if(i == selected){
			Liste[i-1][2] = true;
		}else{
			Liste[i-1][2] = false;
		}
	}
	
	if (Liste.length > 0) {
		Liste = triAlphaNum(Liste);
		for (i = 1; i < Obj.options.length; i++) {
			Obj.options[i].text = Liste[i-1][0];
			Obj.options[i].value = Liste[i-1][1];
			Obj.options[i].selected = Liste[i-1][2];
		}
	}
}

// Fonction de tri d'une lisbox selon les couleurs
function sortArrayColor(id_select){
	var Liste = new Array();
	Obj = document.getElementById(id_select);
	
	for (i = 1; i < Obj.options.length; i++) {
		Liste[i-1] = new Array();
		Liste[i-1][0] = Obj.options[i].text;
		Liste[i-1][1] = Obj.options[i].value;
		Liste[i-1][2] = Obj.options[i].selected;
	}
	
	if (Liste.length > 0) {
		Liste.sort();
		for (i = 1; i < Obj.options.length; i++) {
			Obj.options[i].text = Liste[i-1][0];
			Obj.options[i].value = Liste[i-1][1];
			Obj.options[i].selected = Liste[i-1][2];
		}
	}
}

// fonction de tri alphanumerique
function triAlphaNum(liste_taille){
	var liste_resultat = new Array();
	for(var i=0 ; i < liste_taille.length ; i++){
		liste_resultat[i] = new Array();
		if(isNaN(liste_taille[i][0])){
			liste_resultat[i][0] = liste_taille[i][0].substring(0,liste_taille[i][0].length-1);
			liste_resultat[i][1] = liste_taille[i][0].substring(liste_taille[i][0].length-1);
			liste_resultat[i][2] = liste_taille[i][1];
			liste_resultat[i][3] = liste_taille[i][2];
		}else{
			liste_resultat[i][0] = liste_taille[i][0];
			liste_resultat[i][1] = "";
			liste_resultat[i][2] = liste_taille[i][1];
			liste_resultat[i][3] = liste_taille[i][2];
		}
		
	}
	liste_resultat.sort(testTaille);
	
	
	var liste_resultat2 = new Array();
	for (var i = 0; i < liste_taille.length; i++) {
		liste_resultat2[i] = new Array();
		liste_resultat2[i][0] = liste_resultat[i][0]+ liste_resultat[i][1];
		liste_resultat2[i][1] = liste_resultat[i][2];
		liste_resultat2[i][2] = liste_resultat[i][3];
	}
	return liste_resultat2;
}

Array.prototype.contains = function (element) {
	for (var i = 0; i < this.length; i++) {
		if (this[i][0] == element) {
			return true;
		}
	}
	return false;
}
/* Fin Recherche*/