function dealer(a,trades,distributor){
	this.loc=a;
	this.trades=trades;
	this.distributor=(distributor)?"Distributor":"Dealer";
	this.init();
	this.patch="";
	this.patch2="";
	this.countrySelected="";
	
	this.depChoix=new Array();
}

dealer.prototype.init=function(){

makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetPays',"buildCountry",this,'')
}


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').innerHTML=this.buildListBox(a,"lstcountry","","dealer.makeDepartment");
}
dealer.prototype.makeDepartment=function(e){
	
	
		var a=e.value;
		
			this.countrySelected=a;
		
		switch(a){
			case "ENGLAND":
			case "SCOTLAND":
			case "WALES":
			case "NORTHERN IRELAND":
			case "REP OF IRELAND":
			case "ISLE OF MAN":
			case "CHANNEL ISLANDS":
				this.patch="uk";
				break;
			default:
				this.patch="";
				break;
		}
		
		switch(a){
			case "REP OF IRELAND":
				this.patch2="ireland";
				break;
			default:
				this.patch2="";
				break;
		}
		
		
		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();
		
		
		if (this.patch!="uk"){
			a.push({data:"",label:this.loc["department"]});
		}else{
			a.push({data:"",label:this.loc["county"]});
		}
		
			
			
		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').innerHTML="";
			
		hideDiv("titlesearch");

		$('department').innerHTML=this.buildListBox(a,"lstdepartment","","dealer.makeCity");
		//$('trade').innerHTML="";
		$('city').innerHTML="";
		
		
			this.depChoix=new Array();
			this.depChoix.push($('department').innerHTML);
		
	
		//auto next
		if (this.patch!="uk" || this.patch2=="ireland"){
			if (a.length<=2){
				this.makeCity($('lstdepartment'));
			}
		}else{
			
			makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetPostCode&a0='+this.countrySelected,"buildPostcode",this,'')
		}
		
		
	
}


dealer.prototype.buildPostcode=function(s){
		
		var temp=eval(s);
		var a=new Array();
		

		a.push({data:"",label:this.loc["postcode"]});

		
			
			
		for (var i=0;i<temp.length;i++){
			a.push({data:temp[i]["postal"],label:temp[i]["postal"]});
		}
		/*
		if (a.length>1){
			if (a[1]["label"]=="") a[1]["label"]=this.loc["all"];
		}	
		*/
		
					this.depChoix.push(" <span class='choix'>"+this.loc["choix"]+"</span> ");
					this.depChoix.push(this.buildListBox(a,"lstpostcode","","dealer.makePostcode"));
								
		$('department').innerHTML=this.depChoix[0]+this.depChoix[1]+this.depChoix[2];
}


dealer.prototype.makeCity=function(e){

	


	var a="";

				makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetVilleTrade&a0='+$("lstcountry").value+'&a1='+$("lstdepartment").value+'&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').innerHTML=this.buildListBox(a,"lstcity","","dealer.makeSearch");

		//reset
		
		$('resultsearch').innerHTML="";
		hideDiv("titlesearch");

}



dealer.prototype.makePostcode=function(e){

$('city').innerHTML="";
	a1=($("lstpostcode")!=undefined)?$("lstpostcode").value:"";

	a2="";
	a3=($("lstcity")!=undefined)?$("lstcity").value:"";
	makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetShopTradePostcode&a0='+$("lstcountry").value+'&a1='+a1+'&a2='+a2+'&a3='+a3,"buildSearch",this,'')
	
	
}




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")!=undefined)?$("lstdepartment").value:"";

	a2="";
	a3=($("lstcity")!=undefined)?$("lstcity").value:"";
	makeAjax('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetShopTrade&a0='+$("lstcountry").value+'&a1='+a1+'&a2='+a2+'&a3='+a3,"buildSearch",this,'')
	
}
dealer.prototype.buildSearch=function(s){

		
		showDiv("titlesearch");

		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+="<span class=''>"+this.loc["trade"]+":    "
				for (var j=0;j<6;j++){
					if (temp[i]["t"+(j+1)]=="1") s+=" "+this.trades[j]["label"]+",  ";
				}
				s=s.substring(0,s.length-3);
				s+="</span><br>"

			
					if (this.patch!="uk"){
						s+=temp[i]["adresse"]+"<br>"+temp[i]["postal"]+"<br>"+temp[i]["ville"]+"<br>"+temp[i]["pays"];
					}else{
						s+=temp[i]["adresse"]+"<br>"+temp[i]["ville"]+"<br>"+temp[i]["division"]+" "+temp[i]["postal"]+"<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').innerHTML=s
}

