//function Detail(id)
//{
//	window.open ("fiches/" + id + ".htm", "Fiche", "height=660 width=830 scrollbars=yes");
//}

function Detail(id)
{
	window.location.href=id;
//	window.open(id , "Fiche", "height=660 width=830 scrollbars=yes");
}

function ExtraireNombre(chaine){
// Pour l'Alimentation
	strNombre = "";
	CurrentChar = ""
	i = 0;
	j = 0;
	while(i < chaine.length){
		j = i + 1;
		CurrentChar = chaine.substr(i,1)
		if(isNaN(CurrentChar) == false && CurrentChar != " "){
			strNombre = strNombre + CurrentChar;
		}
		i = i + 1 ;
	}
	return strNombre;
}

function TriParData0(a0, b0) {
	a0 = a0.Data0;
    b0 = b0.Data0;
    return ((a0 < b0) ? -1 : ((a0 > b0) ? 1 : 0));
}

function TriParData1(a1, b1) {
	a1 = a1.Data1;
    b1 = b1.Data1;
    return ((a1 < b1) ? -1 : ((a1 > b1) ? 1 : 0));
}

function TriParData2(a2, b2) {
	a2 = a2.Data2;
    b2 = b2.Data2;
    return ((a2 < b2) ? -1 : ((a2 > b2) ? 1 : 0));
}

function TabTitre(){
	// Ecriture des titres
	for (var i=0;i<TabLib.length;i++){
		document.getElementById("tablib" + i).innerHTML = "&nbsp;" + TabLib[i] + "&nbsp;";
	}
}
function tri_nombres(a,b)

// Fonction de comparaison à utiliser avec la méthode sort
{ return a-b; }

function EcriteTable(){
	// Calcul du nombre max de lignes
	var NbCell = new Array(LibCellTab0.length,LibCellTab1.length,LibCellTab2.length);
	NbCell.sort(tri_nombres);
	NbLignes = NbCell[NbCell.length -1 ];

	// On recontruit les tableaux HTML's
	for (var i=0;i<TabLib.length;i++){
		strTable = "";
		strTable = strTable + "<table width='100%' cellspacing='0'>";
		strLongueurTR = eval("LibCellTab" + i + ".length");		

			// On boucle sur le nb max de lignes
			for(var j=0;j<NbLignes;j++){
				// Si la cellule est pleine
				if( eval("LibCellTab" + i + "[j]") != null){
					strTable = strTable + "<tr bgcolor='#D1E1F1' valign='top'>";
					strTable = strTable + "<td height='14' width='20' bgcolor='#FFFFFF' class='lienbleu' onclick='Clic("+ eval(i) +",this)'><a class='lienvert' href='#' ><img src='images/radio.gif' width='14' height='14' border='0' align='absmiddle' ></a></td><td height='14' align='left' bgcolor='#FFFFFF' onclick='Clic("+ eval(i) +",this)'><a class='lienbleu' href='#' >" + eval("LibCellTab" + i + "[j]") + "</a></td>";
					strTable = strTable + "</tr>";
				// Ou vide
				}else{
					strTable = strTable + "<tr bgcolor='#D1E1F1' valign='top'>";
					strTable = strTable + "<td height='16' width='20' bgcolor='#FFFFFF' class='lienbleu'></td><td height='14' align='left' bgcolor='#FFFFFF' class='lienbleu'>&nbsp;</td>";
					strTable = strTable + "</tr>";
				}
			}
		strTable =  strTable +  "<table>";

		// on écrit le tableau courant
		document.getElementById("filtreval" + i).innerHTML = strTable;
	}
}

function EcrireListeTri(){
	strForm = "";
	strForm = strForm + "<table height=\"29\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"650\" align=\"left\" valign=\"middle\"> <span class=\"lien2\"><strong>Tri </strong>:</span>"
	strForm = strForm + "<select name=\"tri\" class=\"input\" onChange=\"SelectTri(tri.options[tri.selectedIndex].value)\">";
	for (var i=0;i<TabLib.length;i++){
		strForm = strForm + "<option value=\"" + TabLibRes[i] + "\">" + TabLibRes[i] + "</option>";
	}
	strForm = strForm +  "</select></td></tr></table>";

	// on le écrit le formulaire de tri
	document.getElementById("Layer_TRI").innerHTML = strForm;
}
function Compare0(a,b) {
	if (a.Titre < b.Titre) { return -1; }
	if (a.Titre > b.Titre) { return 1; }
	return 0;
}
function Compare1(a,b) {
	if (GetNumMois(a.Data1) < GetNumMois(b.Data1)) { return -1; }
	if (GetNumMois(a.Data1) > GetNumMois(b.Data1)) { return 1; }
	return 0;
}
function Compare2(a,b) {
	if (a.Data2 < b.Data2) { return -1; }
	if (a.Data2 > b.Data2) { return 1; }
	return 0;
}

function SelectTri(strVal){
	AfficheVal(strVal);
}

function simplePreload()
{
  if(document.images)
  {
    if(!document.imageArray) document.imageArray = new Array();
    var i,j = document.imageArray.length, args = simplePreload.arguments;
    
    for(var i=0; i<args.length; i++)
    {
      if (args[i].indexOf("#")!= 0)
      {
        document.imageArray[j] = new Image;
        document.imageArray[j++].src = args[i];
      }
    }
  }
}

simplePreload('images/radio_over.gif','images/radio.gif','images/radio_annul.gif')

function ClickImage(objet){
	var image_click = objet.getElementsByTagName("IMG");
	image_radio_click = image_click[0];
	var image_click	= "images/radio_annul.gif";
	image_radio_click.src = image_click;
}

function Annuler(){
	ValSel[0] = "";
	ValSel[1] = "";
	ValSel[2] = "";

	// Mise à jour Data3 listes de valeurs
	ReinitListeVal(0);
	ReinitListeVal(1);
	ReinitListeVal(2);
	MM_findObj("Res").innerHTML = "";
	MM_findObj("nbres").innerHTML = "";

	// On rétinit les styles
	itemA = document.getElementsByTagName("A");
	for(var i=0;i<itemA.length;i++){
		if(itemA[i].className = "inactive"){
			itemA[i].className = "lienbleu";
		}
	
	}
}

function ChangeStyle(index,cell)
{
	// new
	cell = cell.parentNode.getElementsByTagName("TD");

	// On recherche la balise A
	var itemA = cell[1].getElementsByTagName("A");

	ClickImage(cell[0]);
	
	if (itemA.length > 0){	
		var item = itemA[0];
		if (ValSel[index] == item.innerHTML)
		{
			ValSel[index] = "";
			cell[1].style.backgroundColor = "#FFFFFF";
		}
		else
		{
			ValSel[index] = item.innerHTML;
			
			// On remonte sur le tableau pour tout déselectionner
			var TheTab;
			// new
			TheTab = cell[0].parentNode.parentNode;
					
			var itemTR = TheTab.getElementsByTagName("TR")
			var i
			for (i=0; i < itemTR.length; i++)
			{
				// Ligne
				var item = itemTR[i];
		
				// Récupération de la seconde cellule de la ligne
				var itemTD = item.getElementsByTagName("TD");
				itemTD[1].style.backgroundColor = "#FFFFFF";
				
				// Radiobouton normal				
				var image_normal = itemTD[0].getElementsByTagName("IMG");

				if (image_normal.length > 0) 
				{
					image_radio_normal = image_normal[0];
					image_radio_normal.src = "images/radio_annul.gif";
				}	
			}
			
			// Radiobouton 
			var image = cell[0].getElementsByTagName("IMG");
			image_radio = image[0];
			image_radio.src = "images/radio_valid.gif";
		}
	}
}

function ReinitListeVal(index)
{
	theObj = MM_findObj("filtreval" + (index));
	var itemTABLE = theObj.getElementsByTagName("TABLE")
	var item = itemTABLE[0];
	var itemTR = item.getElementsByTagName("TR")
	for (var i=0; i< itemTR.length; i++)
	{
		// Récupération de la seconde cellule de la ligne
		var itemLigne 	= itemTR[i];
		var itemTD 	= itemLigne.getElementsByTagName("TD");
		var cell 		= itemTD[0];

		var itemA 	= cell.getElementsByTagName("A");
		var itemIMG = cell.getElementsByTagName("IMG")
		if (itemIMG.length > 0) 
		{
			image_radio_normal = itemIMG[0];
			image_radio_normal.src = "images/radio.gif";
		}
	}
}

function MajListeVal(index)
{

	theObj = MM_findObj("filtreval" + index);
	var itemTABLE = theObj.getElementsByTagName("TABLE");
	var item = itemTABLE[0];
	var itemTR = item.getElementsByTagName("TR")
	for (var i=0; i< itemTR.length; i++)
	{
		// Récupération de la seconde cellule de la ligne
		var itemLigne 	= itemTR[i];
		var itemTD 	= itemLigne.getElementsByTagName("TD");
		var itemA 	= itemTD[1].getElementsByTagName("A");
		var itemIMG 	= itemTD[0].getElementsByTagName("IMG")
		if (itemA.length > 0){	
			var itemA0 = itemA[0];

			// Parcours Data3 solutions
			bOk = false;
			for (var j=0;j<nbProd;j++)
			{
				var Liste;
				Liste = ListeTabVal[index];
				if (Liste[ListeSel[j]] == itemA0.innerHTML)
				{
					bOk = true;
					break;
				}
			}
			if (!bOk)
			{

				itemTD[1].style.textDecoration= "none";
				// Radiobouton normal
				if (ValSel[index] != itemA0.innerHTML)
				{
					var image_normal = itemTD[0].getElementsByTagName("IMG");
					if (image_normal.length > 0) 
					{
						image_radio_normal = image_normal[0];
						image_radio_normal.src = "images/radio_annul.gif";
//						itemA0.className = "inactive";	
					}
					
				}	

			}
			else
			{
				itemTD[1].style.textDecoration= "none";
				if (ValSel[index] != itemA0.innerHTML)
				{
					var image_normal = itemTD[0].getElementsByTagName("IMG");
					if (image_normal.length > 0) 
					{
						image_radio_normal = image_normal[0];
						image_radio_normal.src = "images/radio_over_green.gif";
//						itemA0.className = 'lienbleu';
					}
				}
			}

		}		
	}
}

// Suppression des doublons ( le comparateur est l'Id de la structure)
function suppDoublons(badArray){
	var correctArray = new Array();
	for(var e=0;e<badArray.length;e++){
		var copyFound=0;
		for(var c=0;c<correctArray.length;c++){
			if(correctArray[c].Id == badArray[e].Id ){copyFound=1};
		}
		if(copyFound){continue};
		correctArray.length++
		correctArray[correctArray.length-1] = badArray[e]
	}
	
	return correctArray;
}

function AfficheVal(strValTri)
{
	// Critères de choix
	crit0 = ValSel[0]
	crit1 = ValSel[1];
	crit2 = ValSel[2];

	// Parcours des valeurs possibles
	nbProd = 0;
	var StructureDonneesTmp = new Array();
	for (var i=0;i<StructureDonnees.length;i++)
	{	
		styled0 = ""
		stylef0 = ""
		bOk=true;
		
		if (crit0 != "")
		{
			if (crit0.toLowerCase() != StructureDonnees[i].Data0.toLowerCase() )
			{
				bOk=false;
			}
			else
			{
				styled0 = "<b>";
				stylef0 = "</b>";
			}
		}

		styled1 = ""
		stylef1 = ""
		
		if (crit1 != "")
		{
			if (crit1.toLowerCase() != StructureDonnees[i].Data1.toLowerCase() )
			{
				bOk=false;
			}
			else
			{
				styled1 = "<b>";
				stylef1 = "</b>";
			}
		}

		styled2 = ""
		stylef2 = ""
		if (crit2 != "")
		{
			if (crit2.toLowerCase() != StructureDonnees[i].Data2.toLowerCase() )
			{
				bOk=false;
			}
			else
			{
				styled2 = "<b>";
				stylef2 = "</b>";
			}
		}

		if (bOk == true)
		{
			nbProd++;
			ListeSel[nbProd-1] = i;
			if(nbProd%2 == 0){	
				strClass = "EFEFEF";
			}else{
				strClass = "#FFFFFF";				
			}

			// Copie dans la structure temporaire
			{
				StructureDonneesTmp[nbProd-1] = StructureDonnees[i];
			}
		}
	}

	//Supression des doublons
	//StructureDonneesTmp = suppDoublons(StructureDonneesTmp);
	//On compte le nb de résultats
//	nbProd = StructureDonneesTmp.length;

	// Tri de la structure temporaire si nécessaire
	if (strValTri != "")
	{
		if (strValTri == TabLibRes[0])
		{
			StructureDonneesTmp.sort(Compare0);
		}
	}

	// Tableau d'affichage des résultats
	texteproduits 		= "";
	texteapplis 		= "";
	texteaccessoires	= "";
//	textetmp = textetmp + "<DIV id=scrollContent style='z-index:0; width:665;height:140px; overflow: auto;'>";
//	textetmp 			= textetmp + "<table border=0 width=648 cellspacing='0' >";
//	texteapplis 		= texteapplis + "<table border=0 width=648 cellspacing='0' >";
//	texteaccessoires 	= texteaccessoires + "<table border=0 width=648 cellspacing='0' >";

	// Parcours des données à afficher
	nbProd = 0;
	for (i = 0 ; i < StructureDonneesTmp.length ; i++)
	{
		// Le produit est-il déjà inséré ?
		var bInserer = true;
		for (j=0 ; j < i ; j++)
		{
			if (StructureDonneesTmp[j].Id == StructureDonneesTmp[i].Id)
			{
				bInserer = false;
			}
		}
		
		// Si affichage
		if (bInserer)
		{
			// Id du produit
			Id = StructureDonneesTmp[i].Id;
			
			// Libellé du produit
			Lib = StructureDonneesTmp[i].Lib;
			
			// Applications du produits
			strApplis = StructureDonneesTmp[i].Applis;
			
			// Accessoires du produit
			strAccessoires = StructureDonneesTmp[i].Accessoires;
			
			// Affichage des données des produits
			texteproduits = texteproduits + "<tr class='lienbleu'>"
			texteproduits = texteproduits + "<td valign='top' width=665 align=left>" + styled0 + "<img src='images/box_orange.gif' align='absmiddle'>&nbsp;<a class='lienbleu' href='javascript:Detail(\"" + Id + "\")'>" +Lib + "</a>" + stylef0 + "</td>";
			texteproduits = texteproduits + "</tr>";
			
			// affichage des données des accessoires
			if (strApplis != "")
			{
				var TabApplis = strApplis.split("¤¤");
				for (var k =0; k<TabApplis.length;k++)
				{
					var TabDetailAppli  = TabApplis[k].split("##");
					if (TabDetailAppli[0] != "")
					{
						var strCode			= "<td valign='top' width=665 align=left>" + styled0 + "<img src='images/pdf.gif' align='absmiddle'>&nbsp;<a class='lienbleu' href='photo/" + TabDetailAppli[1] +"' target='_blank'>" +TabDetailAppli[0] + "</a>" + stylef0 + "</td>";
						if (texteapplis.indexOf(strCode) == -1)
						{
							texteapplis = texteapplis + "<tr class='lienbleu'>"
							texteapplis = texteapplis + strCode;
							texteapplis = texteapplis + "</tr>";
						}						
					}						
				}
			}

			// affichage des données des accessoires
			if (strAccessoires != "")
			{
				var TabAccess = strAccessoires.split("¤¤");
				for (var k =0; k<TabAccess.length;k++)
				{
					var TabDetailAccess  = TabAccess[k].split("##");
					if (TabDetailAccess[0] != "")
					{
						var strCode			= "<td valign='top' width=665 align=left>" + styled0 + "<img src='images/pdf.gif' align='absmiddle'>&nbsp;<a class='lienbleu' href='photo/" + TabDetailAccess[1] +"' target='_blank'>" +TabDetailAccess[0] + "</a>" + stylef0 + "</td>";
						if (texteaccessoires.indexOf(strCode) == -1)
						{
							texteaccessoires = texteaccessoires + "<tr class='lienbleu'>"
							texteaccessoires = texteaccessoires + strCode;
							texteaccessoires = texteaccessoires + "</tr>";
						}						
					}						
				}
			}

			// Nombre de produits
			nbProd++;
		}
	}
	delete StructureDonneesTmp;
	
	// Création des tableaux
	textetmp 			= "<DIV id=scrollContent style='z-index:0; width:685;height:400px; overflow: auto;'>";
	if (texteproduits != "")
	{
		textetmp 	= textetmp + "<table width='665' cellspacing='0' border='0'>";
		textetmp 	= textetmp + "<tr  class='lienbleu'><td width='200' bgcolor='#cccccc' class='lienbleu'><b>" + nbProd + " " +TabLibRes[0]+"(s)</b></td></tr>"
		textetmp 	= textetmp + "</table>";
		textetmp 	= textetmp + "<table border=0 width=648 cellspacing='0' >";
		textetmp 	= textetmp + texteproduits;
		textetmp 	= textetmp + "</table>";
	}
	
	if (texteapplis != "")
	{
		textetmp	= textetmp + "<br>";
		textetmp 	= textetmp + "<table width='665' cellspacing='0' border='0'>";
		textetmp 	= textetmp + "<tr  class='lienbleu'><td width='200' bgcolor='#cccccc' class='lienbleu'><b>" +TabLibRes[1]+"</b></td></tr>"
		textetmp 	= textetmp + "</table>";
		textetmp 	= textetmp + "<table border=0 width=648 cellspacing='0' >";
		textetmp 	= textetmp + texteapplis;
		textetmp 	= textetmp + "</table>";
	}

	if (texteaccessoires != "")
	{
		textetmp	= textetmp + "<br>";
		textetmp 	= textetmp + "<table width='665' cellspacing='0' border='0'>";
		textetmp 	= textetmp + "<tr  class='lienbleu'><td width='200' bgcolor='#cccccc' class='lienbleu'><b>" +TabLibRes[2]+"</b></td></tr>"
		textetmp 	= textetmp + "</table>";
		textetmp 	= textetmp + "<table border=0 width=648 cellspacing='0' >";
		textetmp 	= textetmp + texteaccessoires;
		textetmp 	= textetmp + "</table>";
	}
	textetmp 			= textetmp + "</div>";
	texte				= textetmp;

	// Ecriture
	MM_findObj("Res").innerHTML = texte;

	if (strValTri == "") {
		// Mise à jour listes de valeurs
		MajListeVal(0);
		MajListeVal(1);
		MajListeVal(2);
	}
}

function Clic(index,cell)
{
	// new

	cell = cell.parentNode.getElementsByTagName("TD");		// FireFox IE
	
	var itemIMG = cell[0].getElementsByTagName("IMG");
	if (itemIMG.length > 0) 
	{
		img = itemIMG[0];
		src = img.src;
		if (src.indexOf("radio_annul.gif") == -1)
		{	
			ChangeStyle(index,cell[0]);
			AfficheVal("");
		}
	}			
}

function Popup(page,titre){
	window.open(page, titre, "toolbar=no,status=no,scrollbars=yes,width=800,height=650");
}
