function dealer(a){
	this.loc=a;
	this.distributor="";
	//this.init();
}

dealer.prototype.init=function(type){
	
	
		
	$("#country").html("");
	$("#city").html("");
	$("#department").html("");
	$("#resultsearch").html("");
	
	
	switch(type){
		case "1":
		   this.distributor="Dealer";
		   	makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetPays',"buildCountry",this,'')
			break;
		case "2":
		   this.distributor="Shoponline";
		   	makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetShop',"buildShop",this,'')
			break;
		case "3":
		   this.distributor="Distributor";
		   	makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetPays',"buildCountry",this,'')
			break;

	}

	
	
	

}


dealer.prototype.buildCountry=function(s){	
		var temp=eval(s);
		var a=new Array()
		a.push({data:"",label:this.loc["country"]});
		for (var i=0;i<temp.length;i++){
			a.push({data:temp[i]["pays"],label:temp[i]["pays"]});
		}
		$("#country").html(this.buildListBox(a,"lstcountry","","dealer.makeDepartment"));
}
dealer.prototype.makeDepartment=function(e){
		var a=e.value;
		if (this.distributor=="Distributor"){
			this.makeSearch();
		}else{
			makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetSub&a0='+a,"buildDepartment",this,'')
		}
}
dealer.prototype.buildDepartment=function(s){


		var temp=eval(s);
		var a=new Array()
		a.push({data:"",label:this.loc["department"]});
	
		for (var i=0;i<temp.length;i++){
			a.push({data:temp[i]["division"],label:temp[i]["division"]});
		}
		
		if (a.length>1){
			if (a[1]["label"]=="") a[1]["label"]=this.loc["all"];
		}
	
		//reset
		$("#resultsearch").html("");
			

		$("#department").html(this.buildListBox(a,"lstdepartment","","dealer.makeCity"));
		$("#resultsearch").html("")
		$("#city").html("");
		
	
		//auto next
		if (a.length<=2){
			this.makeCity($("#lstdepartment"));
		}
	
}

dealer.prototype.makeCity=function(e){
	 var a="";
     makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetVilleTrade&a0='+$("#lstcountry").val()+'&a1='+$("#lstdepartment").val()+'&a2='+a,"buildCity",this,'')
}
dealer.prototype.buildCity=function(s){
		

		var temp=eval(s);
		var a=new Array()
		a.push({data:"",label:this.loc["city"]});
		for (var i=0;i<temp.length;i++){
			
			a.push({data:temp[i]["ville"],label:temp[i]["ville"]});
		}
		$("#city").html(this.buildListBox(a,"lstcity","","dealer.makeSearch"));

		//reset
		
		$("#resultsearch").html("")
	

}



dealer.prototype.buildListBox=function(a,id,selected,action,arg){
	
		arg=(arg==undefined)?"":","+arg;
		var maker=(action==undefined || action=='')?"":" onChange=\"javascript:"+action+"(this"+arg+")\" ";
		var s='<select name="'+id+'" id="'+id+'" '+maker+' >';
		for (var i=0;i<a.length;i++){

		  if (selected!=a[i]["data"]){
         	 s+='<option value="'+a[i]["data"]+'" >'+a[i]["label"]+'</option>';
		  }else{
			 s+='<option value="'+a[i]["data"]+'"  selected="selected">'+a[i]["label"]+'</option>';
			  
		  }
		 
		}
		s+='</select>';
		return s;
}


dealer.prototype.makeSearch=function(){
	
	a1=($("#lstdepartment").val()!=undefined)?$("#lstdepartment").val():"";
	a2="";
	a3=($("#lstcity").val()!=undefined)?$("#lstcity").val():"";
	
	makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetShopTrade&a0='+$("#lstcountry").val()+'&a1='+a1+'&a2='+a2+'&a3='+a3,"buildSearch",this,'')
	
}
dealer.prototype.buildSearch=function(s){

		

		var temp=eval(s);
		var s="<ul>";
		for (var i=0;i<temp.length;i++){
			

			s+="<li><span class='tsearch'>"+temp[i]["nom"]+"</span><br>";



			
			s+=temp[i]["adresse"]+"<br>"+temp[i]["postal"]+"<br>"+temp[i]["ville"]+"<br>"+temp[i]["pays"];
					if (temp[i]["telephone"]!="") s+="<br>"+temp[i]["telephone"];
					if (temp[i]["fax"]!="") s+="<br>"+temp[i]["fax"];
					if (temp[i]["mail"]!="") s+="<br><a href='mailto:"+temp[i]["mail"]+"'>"+temp[i]["mail"]+"</a>";
					if (temp[i]["site"]!="") s+="<br><a href='http://"+temp[i]["site"]+"' target='_blank' >"+temp[i]["site"]+"</a>";
					
					s+="<div id='separateur'></div>";
			s+="</li>";
		}
		
		if (temp.length==0) s+="<li>"+a["noresult"]+"</a>";
		s+="</ul>";
		$("#resultsearch").html(s)
}
dealer.prototype.buildShop=function(s){

		

		var temp=eval(s);
		var s="<ul>";
		for (var i=0;i<temp.length;i++){
			
		
			s+="<li><table border='0' cellspacing='0' cellpadding='0'><tr><td>";
			
			s+="<img src='../../files/files_shoponline/"+temp[i]["chemin"]+"' /></td><td>";
			s+="<span class='tsearch'>"+temp[i]["titre"]+"</span><br>";
			if (temp[i]["lien"]!="") s+="<a href='http://"+temp[i]["lien"]+"' target='_blank' >"+temp[i]["lien"]+"</a>";	
			s+="<div id='separateur'></div>";
			s+="</td></tr></table>";
			s+="</li>";
		}
		
		if (temp.length==0) s+="<li>"+a["noresult"]+"</a>";
		s+="</ul>";
		
		//alert(s);
		$("#resultsearch").html(s)
}
