
function rf(){return false; }

function _get(ident){ return document.getElementById(ident); }

TRejonChoose	= function(name,id,pl,pi,wj)
{	
	this.div=_c('div');	
	//this.div.style=((pl==1) && (wj==0))?'float:left':'';
	//this.div.style.float='left';
	//this.div.width='150px';
	this.div.className=((pl==1) && (wj==0))?'float':'';
	this.a = _c('a');
	this.a.choosed=false;
	this.a.id=id;
	this.a.href="#";
	this.a.apC(_cTxt(name));
	this.a.onclick=function(){
		this.reset();
		this.setChoose(!this.choosed)
		return false;
	}
	this.a.setChoose=function(stan){
		this.choosed=stan;
		this.className=(stan)? 'ckd':'';
	}
	this.div.apC(this.a);
	this.a.reset=function(){}
	this.result=this.div;
	return this;
}

TRejonlist			= function(ident){
	
	this.elements = Array();
	this.ident=_get(ident);
	this.ident.list=this;
	this.add=function(obj){
		this.ident.appendChild(obj.result);
		this.elements[this.elements.length]=obj;
	}
	this.clear=function(){
		this.ident.innerHTML='';
	}
}

TRejonlist.prototype.getAjax = function(url){
	var tmp=this;
	advAJAX.get({
    	url: url,
    	onSuccess : function(obj) {
    		var dane=null;
    		eval(obj.responseText);
    		tmp.create(dane); 
    		tmp=null;
     		},
    	onLoading : function(obj){
    		tmp.ident.innerHTML='<img src="/images/loading4.gif">';
    	},	
    	onError : function(obj) { alert("Error: " + obj.status); }
	});
}
TRejonlist.prototype.get = function(pi,pl,wj){
	if(pi<0){ pl=1; wj=0; }
	else{
		this.woj=_get('wojOgl');
		if(this.woj!=null){
			if(pl==1) if(this.woj.value!='') wj=this.woj.value;
			if(pl==3 ) pl=1;			
		}
	}
	var url='/kupon/pobierzrejony/';
	if(pi>0) url+='pi/'+pi+'/pl/0/';
	else
		if(pl>0){
			url+=(wj>0)? 'pl/1/wj/'+wj+'/':'pl/'+pl+'/wj/0/';
		}
	this.getAjax(url);
}



TRejonlist.prototype.create=function(dane){
	if(dane==null) return;
	this.clear();
	var tmp;
	if(dane.result!=null){
		this.liczba=dane.result.length;
		this.rejon='';
		this.rejonObj=_get('rejon');
		if(this.rejonObj!=null){
			//alert('Rejon: '+this.rejonObj.value);
			this.rejon=this.rejonObj.value;
			//alert(this.rejonObj.value);
		}
		//alert(this.liczba);
		
		//for(i in dane.result)
		
		
		for(var i=0;i<dane.result.length;i++)
		{
			//alert(this.liczba);
			//alert(dane.result[i]);
			if(dane.result[i]!=undefined){
				if (typeof(dane.result[i])=='function') continue;
				//--
				tmp_org=new TRejonChoose(dane.result[i].nazwa,dane.result[i].id,dane.pl,dane.pi,dane.wj);
				tmp=tmp_org.a;
				tmp.dane=dane.result[i];
				tmp.parent=this;
				tmp.onclick=function(){		
					child=this.parent.elements;				
					for (var j = 0; j < child.length; j++) {
						child[j].a.setChoose(false);
					}
					this.setChoose(true);				
					if((dane.pl==1) && (dane.wj==0)){
						this.parent.get(0,3,this.id);
						_get('wojOgl').value=this.id;
					}else{
						 _get('rejon').value=this.id;
						 _get('rejon').dane=this.dane;
						 _get('wojOgl').value='';
						 if(_get('data_r')!=null){
						 	_get('data_r').value='';
						 }
						 
						 finalizacja();
					}				
					return false;	
				}			
			}
			this.add(tmp_org);
			if(this.liczba==1) tmp.onclick();
		}
		//alert(this.rejon);
		if(this.rejon!=''){		
				
			if(_get(this.rejon)!=null) _get(this.rejon).onclick();
		}
	}	
}


function initRejon(){
	var r= new TRejonlist('rejony');
	_get('rejony').list.get(163,0,0);
}
