var Tab_Motos = new Array();
var Tab_Etats = new Array();
var Tab_Marques = new Array();
var Tab_Cylindrees = new Array();
var Nb_Pages = 0;
var Filtre_Ouverture;
var Nb_Motos;

var Max_Motos_Page = 5;

var MotoXML;

function ImportXML() 
{ 
	var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined'); 
	var ie = (typeof window.ActiveXObject != 'undefined'); 

 	if (moz) 
	{ 
   		MotoXML = document.implementation.createDocument("", "", null); 
		MotoXML.load('Photos_Motos/Motos_a_vendre.xml'); 
   		MotoXML.onload = ChargeMotos; 
 	} 
	else if (ie) 
	{ 
   		MotoXML = new ActiveXObject("Microsoft.XMLDOM"); 
   		MotoXML.async = false; 
   		MotoXML.load('Photos_Motos/Motos_a_vendre.xml'); 
		ChargeMotos();
 	} 
	else
	{
		document.location = "MotosAutresNavigateurs.htm";
	}
}

function ChargeMotos()
{
    	var Miniature;
    	var Ref;
    	var Moto;
    	var Couleur;
    	var Annee;
    	var Kms;
    	var Prix;
    	var Etat;
	var Marque;
	var Procedure;
	var Cylindree;

	Nb_Motos = MotoXML.getElementsByTagName("Req_motos_WEB").length;
	
	for (var i=0; i<Nb_Motos; i++)
    	{
		Miniature = "Photos_Motos/" + MotoXML.getElementsByTagName("Photo0").item(i).firstChild.data;
        		Ref = MotoXML.getElementsByTagName("Reference").item(i).firstChild.data;
        		Moto = MotoXML.getElementsByTagName("Moto").item(i).firstChild.data;
        		Couleur = MotoXML.getElementsByTagName("Couleur").item(i).firstChild.data;
        		Annee = MotoXML.getElementsByTagName("Annee").item(i).firstChild.data;
		Kms = MotoXML.getElementsByTagName("Kilometrage").item(i).firstChild.data;
        		(Kms ==0) ? Kms = "N.C." : Kms += " kms";
        		Prix = MotoXML.getElementsByTagName("Prix").item(i).firstChild.data;
        		(Prix ==0) ? Prix = "N.C." : Prix += " €";		
		Etat = MotoXML.getElementsByTagName("Etat").item(i).firstChild.data;
        		Marque = MotoXML.getElementsByTagName("Marque").item(i).firstChild.data;
        		Procedure =MotoXML.getElementsByTagName("Procedure").item(i).firstChild.data;
		Cylindree = MotoXML.getElementsByTagName("Cylindree").item(i).firstChild.data;

               	Tab_Motos[i] =  "<TR align=CENTER><TD width=80 height=60><A HREF=javascript:DetailMotos(\"" + i + "\")><IMG SRC=\"" + Miniature + "\" border=0 width='80' height='60' alt=\"Cliquez pour plus de d&eacute;tails\" /></A></TD>";
	 	Tab_Motos[i] += "<TD width=180 height=60><FONT face='Comic sans MS' size=2>" + Moto + "</FONT></TD>";
	  	Tab_Motos[i] += "<TD width=80 height=60><FONT face='Comic sans MS' size=2>" + Couleur + "</FONT></TD>";
	  	Tab_Motos[i] += "<TD width=65 height=60><FONT face='Comic sans MS' size=2>" + Annee+ "</FONT></TD>";
	  	Tab_Motos[i] += "<TD width=80 height=60><FONT face='Comic sans MS' size=2>" + Kms + "</FONT></TD>";
	  	Tab_Motos[i] += "<TD width=70 height=60><FONT face='Comic sans MS' size=2>" + Prix + "</FONT></TD>";
	  	Tab_Motos[i] += "<TD width=90 height=60><FONT face='Comic sans MS' size=2>" + Etat + Procedure + "</A></FONT></TD><TD width=60><A HREF=javascript:DetailMotos(\"" + i  + "\")><IMG SRC='Images/Details.gif' width=60 border=0 alt=\"Cliquez pour plus de d&eacute;tails\" /></A></TD></TR>";

		(Etat.indexOf("/")==-1) ? Tab_Etats[i]=Etat : Tab_Etats[i] = Etat.substring(0, Etat.indexOf("/")-1);
		Tab_Marques[i] = Marque;
		Tab_Cylindrees[i] = Cylindree;
	}
	
	switch (Filtre_Ouverture)
	{
		case "Occasions":
		{
			Filtre_Ouverture = "Occasion";
			break;
		}
		case "Epaves":
		{
			Filtre_Ouverture = "Accidentée";
			break;
		}
		case "Pieces":
		{
			Filtre_Ouverture = "Pour pièces";
			break;
		}
		default:
		{
			Filtre_Ouverture = "Toutes";
			break;
		} 
	}

	AfficheListes(Filtre_Ouverture, "Toutes");
	FiltreMotos(Filtre_Ouverture, "Toutes", "Toutes", 1);
}

function AfficheListes(Filtre_Etat, Filtre_Marque)
{
	var Tab_Lst_Marques = new Array();
	var Tab_Lst_Cylindrees = new Array();

	for(var i=0; i<Nb_Motos; i++)
	{		
		if (Filtre_Etat=="Toutes" || Filtre_Etat==Tab_Etats[i])
		{
			var Ajoute = true;

			for (var j=0; j<Tab_Lst_Marques.length; j++)
			{
				if (Tab_Lst_Marques[j]==Tab_Marques[i])
				{
					Ajoute = false;
					break;
				}
			}
			if (Ajoute){Tab_Lst_Marques.push(Tab_Marques[i]);}
		}

		if ((Filtre_Etat=="Toutes" || Filtre_Etat==Tab_Etats[i]) && (Filtre_Marque=="Toutes" || Filtre_Marque==Tab_Marques[i]))
		{
			var Ajoute = true;
			for (var j=0; j<Tab_Lst_Cylindrees.length; j++)
			{
				if (Tab_Lst_Cylindrees[j] == Tab_Cylindrees[i])
				{
					Ajoute = false;
					break;
				}
			}
			if (Ajoute){Tab_Lst_Cylindrees.push(Tab_Cylindrees[i]);}
		}
	}
	Tab_Lst_Marques.sort();
	Tab_Lst_Cylindrees.sort(( function(n1,n2) { return n1-n2; } ));


	var Listes = "<FONT face='comic sans ms'>Etat <SELECT NAME='Lst_Etat' onChange=\"AfficheListes(Lst_Etat.value, Lst_Marque.value);\"><OPTION VALUE='Toutes'>Toutes";
	Listes += "<OPTION VALUE='Occasion'";
	Listes += (Filtre_Etat == "Occasion") ? " selected>Occasions" : ">Occasions";
	Listes += "<OPTION VALUE='Accidentée'";
	Listes += (Filtre_Etat == "Accidentée") ? " selected>Accident&eacute;es" : ">Accident&eacute;es";
	Listes += "<OPTION VALUE='Pour pièces'";
	Listes += (Filtre_Etat == "Pour pièces") ? " selected>Pour pi&egrave;ces" : ">Pour pi&egrave;ces";
	Listes += "</SELECT>&nbsp;&nbsp;"
	Listes += "Marque <SELECT NAME='Lst_Marque' onChange=\"AfficheListes(Lst_Etat.value, Lst_Marque.value);\"><OPTION VALUE=\"Toutes\">Toutes";

	for (i = 0; i < Tab_Lst_Marques.length; i ++)
	{
		Listes += "<OPTION VALUE=\"" + Tab_Lst_Marques[i];
		Listes += (Tab_Lst_Marques[i]==Filtre_Marque) ? "\" selected>" : "\">";
		Listes += Tab_Lst_Marques[i];
	}
	Listes += "</SELECT>&nbsp;&nbsp;";	

	Listes += "Cylindr&eacute;e <SELECT NAME='Lst_Cylindree'><OPTION VALUE=\"Toutes\">Toutes";
	for (i = 0; i < Tab_Lst_Cylindrees.length; i ++)
	{
		Listes += "<OPTION VALUE=\"" + Tab_Lst_Cylindrees[i] + "\">" + Tab_Lst_Cylindrees[i];
	}	

	Listes += "</SELECT>&nbsp;&nbsp;&nbsp;<INPUT type=button value=\"Rechercher\" onClick=\"FiltreMotos(Lst_Etat.value, Lst_Marque.value, Lst_Cylindree.value, 1)\">";
	Listes += "<BR><BR>";

	document.getElementById("Resultat_Marques").innerHTML = Listes;
}

function FiltreMotos(Filtre_Etat, Filtre_Marque, Filtre_Cylindree, Num_Page)
{
	var Tab_Result_Motos = new Array();

	for(var i=0; i<Nb_Motos; i ++)
	{
		if ((Filtre_Etat=="Toutes" || Filtre_Etat==Tab_Etats[i]) && (Filtre_Marque=="Toutes" || Filtre_Marque==Tab_Marques[i]) && (Filtre_Cylindree=="Toutes" || Filtre_Cylindree==Tab_Cylindrees[i]))
		{
			Tab_Result_Motos.push(Tab_Motos[i]);
		}
	}

	var Nb_Resultat = Tab_Result_Motos.length;
	var Nb_Pages = Math.ceil(Nb_Resultat / Max_Motos_Page);
	var i_min = (Num_Page * Max_Motos_Page) - Max_Motos_Page;
	var i_max = i_min + Max_Motos_Page;
	if (i_max > Nb_Resultat) { i_max = Nb_Resultat;}

	var Result = "<TABLE align=CENTER  cellspacing=0 cellpadding=1 border=1 bgcolor='#CCCBBA'><FONT face='Comic sans MS' size=2>";
    	Result += "<TR align=CENTER valign=bottom><TD width=80><B>...</B></TD><TD width=180><FONT face='Comic sans MS' size=2><B>Moto</B></FONT></TD><TD width=80><FONT face='Comic sans MS' size=2><B>Couleur</B></FONT></TD>";
    	Result += "<TD width=65><FONT face='Comic sans MS' size=2><B>Ann&eacute;e</B></FONT></TD><TD width=80><FONT face='Comic sans MS' size=2><B>Kilom&egrave;trage</B></FONT></TD><TD width=70><FONT face='Comic sans MS' size=2><B>Prix</B></FONT></TD><TD width=90><FONT face='Comic sans MS' size=2><B>Etat</B></FONT></TD><TD width=60><B>...</B></TD></TR>";

	if (Nb_Resultat == 0)
	{
		NoMotos(Filtre_Marque, Filtre_Etat);	
	}
	else
	{	
		for (var i=i_min ; i<i_max; i++)
		{
			Result += Tab_Result_Motos[i];
		}
		 Result += "</TABLE><BR>"; 
		
		for (var i=1; i<Nb_Pages+1; i++)
		{
			if (Nb_Pages ==1 ){break;}
			if (i == 1 && Num_Page > 1)
			{
				Result += "<A HREF=\"\" OnClick=\"FiltreMotos('" + Filtre_Etat + "','" + Filtre_Marque + "','" + Filtre_Cylindree + "'," +(Num_Page - 1) + ");return false\"><FONT face=arial size=2 color=royalblue><<</FONT></A>&nbsp;&nbsp;&nbsp;&nbsp;";	
			}
			Result += "<A HREF='' OnClick=\"FiltreMotos('" + Filtre_Etat + "','" + Filtre_Marque + "','" + Filtre_Cylindree + "'," + i + ");return false\" >";
			if (i == Num_Page)
			{
				Result += "</A><FONT face='arial' size='4' color='red'><B>" + i + "</B></FONT></A>&nbsp;";
			}
			else
			{
				Result += "<FONT face='arial' size='2' color='royalblue'>" + i + "</FONT></A>&nbsp;";
			}
			if (i == Nb_Pages  && Num_Page < Nb_Pages)
			{
				Result += "&nbsp;&nbsp;&nbsp;<A HREF=\"\" OnClick=\"FiltreMotos('" + Filtre_Etat + "','" + Filtre_Marque + "','" + Filtre_Cylindree + "'," + (Num_Page + 1) + ");return false\"><FONT face=arial size=2 color=royalblue>>></FONT></A>";	
			}
		}

		document.getElementById("Resultat_Motos").innerHTML = Result;
	}
	
}

function NoMotos(Marque, Etat)
{
	try
	{
		switch (Etat)
		{
			case 'Occasions': {Msg = "d'occasion";break;}
			case 'Accidentées': {Msg = "accidentée";break;}
			case 'Pour pièces': {Msg = "pour pièces";break;}
		}
		if (Etat != 'Toutes')
		{
			alert("Nous sommes désolés, mais nous n'avons pas de moto " +  Msg + " en ce moment.");
			document.form.Lst_Etat.value = "Toutes";
			AfficheListes("Toutes", "Toutes");
			FiltreMotos("Toutes", "Toutes", "Toutes", 1);

		}
	}
	catch(error)
	{
		document.location="Motos.htm";
	}
}

function DetailMotos(Ref)
{	
	window.open("Details_Motos.php?" + Ref, "", "width=700, height=560, toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=0, copyhistory=0, menuBar=0,left=" + ((screen.width - 700)/2) + ",top=" + ((screen.height - 550)/2) ); 
}
