RegionySelector=function(woj_id,pow_id,gmn_id,options){
	this.woj_id=parseInt(woj_id);
	this.pow_id=parseInt(pow_id);
	this.gmn_id=parseInt(gmn_id);
	this.regiony=regiony;
	this.Config={
		'woj_id_name':'woj_id',
		'pow_id_name':'pow_id',
		'gmn_id_name':'gmn_id',
		'sep':'',
		'cls':'',
		'null_woj_label':'-- Wybierz województwo --',
		'null_pow_label':'-- Wybierz powiat --',
		'null_gmn_label':'-- Wybierz gminę--'
	}
	if(options)
    {
		jQuery.extend(this.Config, options);
	}
	this.selector={
		'woj_id':null,
		'pow_id':null,
		'gmn_id':null
	}
	
	this.init = function(box){
		var tmp_this=this;
		this.selector.woj_id=$('<select id="' + this.Config.woj_id_name + '" name="' + this.Config.woj_id_name + '" class="' + this.Config.cls + '"></select>').change(function(){tmp_this.onWojChange()});		
		$('#'+box).append(this.selector.woj_id);
		if(this.Config.sep!='')$('#'+box).append($(this.Config.sep));		
		$('#'+box).append($('<input type="hidden" name="' + this.Config.pow_id_name + '" value="-1" />'));
		this.selector.pow_id=$('<select id="' + this.Config.pow_id_name + '" name="' + this.Config.pow_id_name + '" class="' + this.Config.cls + '"></select>').hide().change(function(){tmp_this.onPowChange()});
		$('#'+box).append(this.selector.pow_id);
		if(this.Config.sep!='')$('#'+box).append($(this.Config.sep));
		$('#'+box).append($('<input type="hidden" name="' + this.Config.gmn_id_name + '" value="-1" />'));
		this.selector.gmn_id=$('<select id="' + this.Config.gmn_id_name + '" name="' + this.Config.gmn_id_name + '" class="' + this.Config.cls + '"></select>').hide();
		$('#'+box).append(this.selector.gmn_id);
		
		this.addOptions(this.selector.woj_id,this.regiony,this.woj_id,this.Config.null_woj_label);
		this.onWojChange(1);
		this.onPowChange(1);		
	}
	this.onWojChange = function(init){
		this.woj_id=this.selector.woj_id.val();
		if(init==undefined)this.pow_id=0;
		if(init==undefined)this.gmn_id=0;
		this.selector.gmn_id.hide().empty();
		this.selector.pow_id.hide().empty();
		if(this.woj_id>0 && this.regiony[this.woj_id][2]!=undefined){
			this.addOptions(this.selector.pow_id,this.regiony[this.woj_id][2],this.pow_id,this.Config.null_pow_label);
			this.selector.pow_id.show();
		}
	}
	this.onPowChange = function(init){
		this.pow_id=this.selector.pow_id.val();
		if(init==undefined)this.gmn_id=0;
		this.selector.gmn_id.hide().empty();		
		if(this.pow_id>0 && this.regiony[this.woj_id][2][this.pow_id][2]!=undefined){
			this.addOptions(this.selector.gmn_id,this.regiony[this.woj_id][2][this.pow_id][2],this.gmn_id,this.Config.null_gmn_label);
			this.selector.gmn_id.show();
		}
	}
	this.addOptions = function(selector,options,selected, null_info){
		selector.children().remove();
		if(null_info!=undefined&&null_info!='') selector.append('<option value="0">' + null_info + '</option>');
		for (i in options) {
			if (i == selected) {
				selector.append('<option value="' + i + '" selected="selected">' + options[i][1] + '</option>');
			}
			else {
				selector.append('<option value="' + i + '">' + options[i][1] + '</option>');
			}
		}			
	}
}
