function dealer(a,trades,distributor){
	
	this.loc=a;

	this.trades=trades;
	this.distributor=(distributor)?"Distributor":"Dealer";
	this.init();
}

dealer.prototype.init=function(){
	

	new Ajax.Request('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetPays', {
	  onSuccess: function(transport){dealer.buildCountry(transport.responseText)}
	});

}


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;

		if (this.distributor=="Distributor"){
			
			this.makeSearch();
		}else{
				new Ajax.Request('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetSub&a0='+a, {
				  onSuccess: function(transport){dealer.buildDepartment(transport.responseText)}
				});

		}

}
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').innerHTML="";
		$("titlesearch").hide();
		$('department').innerHTML=this.buildListBox(a,"lstdepartment","","dealer.makeCity");
		//$('trade').innerHTML="";
		$('city').innerHTML="";
		
	
		//auto next
		if (a.length<=2){
			//this.makeTrades($('department').value);

			this.makeCity($('lstdepartment'));
		}
	
}

/*
dealer.prototype.makeTrades=function(e){
	this.buildTrades();


}

dealer.prototype.buildTrades=function(){
		
		this.makeCity();
		
		var temp=this.trades;
		var a=new Array();
		a.unshift({data:"",label:this.loc["trade"]});
		for (var i=0;i<temp.length;i++){
			
			a.push({data:temp[i]["data"],label:temp[i]["label"]});
		}
		$('trade').innerHTML=this.buildListBox(a,"lsttrade","","dealer.makeCity");
		
		//reset
		
		$('resultsearch').innerHTML="";
		$("titlesearch").hide();
		$('city').innerHTML="";
}
*/
dealer.prototype.makeCity=function(e){

	var a="";
	//e.value;

	new Ajax.Request('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetVilleTrade&a0='+$("lstcountry").value+'&a1='+$("lstdepartment").value+'&a2='+a, {
	  onSuccess: function(transport){dealer.buildCity(transport.responseText)}
	});

}
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="";
		$("titlesearch").hide();

		/*
		if (a.length==2){
			$('lstcity').value=a[1]["data"];
		}
		*/
		
		/*
		//auto next
		if (a.length<=2){
			this.makeSearch();
		}
		if (a.length==1){
			$('lstcity').hide();
		}else{
			$('lstcity').show();
		}
		*/
		
}



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=($("lsttrade")!=undefined)?$("lsttrade").value:"";
	a2="";
	a3=($("lstcity")!=undefined)?$("lstcity").value:"";
	
	//alert('&a0='+$("lstcountry").value+'&a1='+a1+'&a2='+a2+'&a3='+a3);
	new Ajax.Request('../../script/amfphp/gateway2.php?s='+this.distributor+'&m=GetShopTrade&a0='+$("lstcountry").value+'&a1='+a1+'&a2='+a2+'&a3='+a3, {
	  onSuccess: function(transport){dealer.buildSearch(transport.responseText)}
	});
}
dealer.prototype.buildSearch=function(s){
		
		$("titlesearch").show();

		var temp=eval(s);
		var s="<ul>";
		for (var i=0;i<temp.length;i++){
			//{"id":"9","pays":"POLAND","division":"POMORSKIE","nom":"MILITARIA.PL","adresse":"SWIETOJANSKA 84","postal":"81-388","ville":"GDYNIA","telephone":" +48 58 620 14 63 ","fax":"","mail":"gdynia@militaria.pl","site":"www.militaria.pl","t1":"1","t2":"1","t3":"1","t4":"1","t5":"1","t6":"1"}]
			
			

			s+="<li><span class='tsearch'>"+temp[i]["nom"]+"</span><br>";
			
			
		//	if (this.distributor=="Distributor"){
				 s+="<span class='tsearch'>"+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>"
		//	}
			
			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+="</li>";
		}
		
		if (temp.length==0) s+="<li>"+a["noresult"]+"</a>";
		s+="</ul>";
		$('resultsearch').innerHTML=s
}
