function init_filter(action,filter_holder,table_holder,status_holder) {
    if(!filter_holder) filter_holder = document.getElementById('ghdb_filter_div');
    if(!status_holder) status_holder = document.getElementById('ghdb_table_div_status');
    if(!table_holder) table_holder = document.getElementById('ghdb_table_div');
    var x = gh_db_getAtrax(null,null,ghdb_GetData,status_holder,filter_holder);
    x.xmlHttp.open("GET", action + '/filter.js',true);
    x.xmlHttp.send(null);
	function ghdb_GetData(event,handler,filter_holder) {
		var idata = eval( handler.responseText.toString() );
		while(filter_holder.childNodes.length>0){filter_holder.removeChild(filter_holder.lastChild);}
		gh_filter = new ghdb_filter(filter_holder,idata,action,'post',table_holder,status_holder);
	}
    return false;
}

var ghdb_filter = function(filter_holder,idata,action,method,table_holder,status_holder){
	//
	if(typeof(_)!='function'){this._ = function (msgid) { return msgid; }}
	else this._=_;
	this.idata=idata;
	this.limits=new Array();
	this.filters=new Array();
	this.customs=new Array();
	this.filter_holder=filter_holder;
	this.action=action;
	this.method=method;
	this.table_holder=table_holder;
	this.status_holder=status_holder;
	this.table=new ghdb_table_init(this.action,null,this.method,this.table_holder,this.status_holder); 
	this.objectum=new Array();
	f_h=this.filter_holder;
	this.inner_fh=document.createElement("table");f_h.appendChild(this.inner_fh);
	this.custom_parag=document.createElement("div");
	elem_n=document.createTextNode(this._("Additional filters: "));this.custom_parag.appendChild(elem_n);
	this.custom_select=document.createElement("select");this.custom_select.size=1;this.custom_parag.appendChild(this.custom_select);
	elem_b=document.createElement("button");elem_b.onclick=function(){gh_filter.custom_show()};
				butttext=document.createTextNode(this._("add"));elem_b.appendChild(butttext);this.custom_parag.appendChild(elem_b);
	f_h.appendChild(this.custom_parag);
	but_h=document.createElement("div");f_h.appendChild(but_h);
	elem=document.createElement("button");elem.onclick=function(){gh_filter.submit()};
				butttext=document.createTextNode(this._("Use filter"));elem.appendChild(butttext);but_h.appendChild(elem);
	elem=document.createElement("button");elem.onclick=function(){gh_filter.reset()};
				butttext=document.createTextNode(this._("Reset filter"));elem.appendChild(butttext);but_h.appendChild(elem);
	
	this.add(idata);
}

ghdb_filter.prototype.add = function(idata,isCust){
	isCust=isCust || false;
	if("custom" in idata){this.add_custom(idata.custom)}else{
		if ("id" in idata){
			if ((typeof(idata.id)!=='undefined')&&(typeof(idata.type)!=='undefined')&&(typeof(idata.label)!=='undefined')){
			x=this.filters.length;
			this.filters[x]=idata;
			this.f_show(this.filters[x],x,isCust);}
		}
		else{
			for (i in idata){
				if ("custom" in idata[i]){this.add_custom(idata[i].custom)}else{
				if ((typeof(idata[i].id)!=='undefined')&&(typeof(idata[i].type)!=='undefined')&&(typeof(idata[i].label)!=='undefined')){
				x=this.filters.length;
				this.filters[x]=idata[i];
				this.f_show(this.filters[x],x,isCust);}}
			}
		}}
if (this.custom_select.options.length==0) this.custom_parag.style.visibility="hidden";
}


ghdb_filter.prototype.add_custom=function(sh_customs){
	if ("id" in sh_customs)
	{
		if ((typeof(sh_customs.id)!=='undefined')&&(typeof(sh_customs.type)!=='undefined')&&(typeof(sh_customs.label)!=='undefined')){
		z=this.customs.length;
		if (typeof(sh_customs.limit)=='undefined'){if(sh_customs.type=='select'){sh_customs.limit=sh_customs.data.length;}else{sh_customs.limit=1;}};
		this.limits[z]=sh_customs.limit;
		this.customs[z]=sh_customs;
		arr_opt=this.custom_select.options;
		arr_opt[arr_opt.length]=new Option(this.customs[z].label,z);}
	}
	else
	{
		for (i in sh_customs){
		if ((typeof(sh_customs[i].id)!=='undefined')&&(typeof(sh_customs[i].type)!=='undefined')&&(typeof(sh_customs[i].label)!=='undefined'))
		{z=this.customs.length;
		if (typeof(sh_customs[i].limit)=='undefined'){if(sh_customs[i].type=='select'){sh_customs[i].limit=this.as_len(sh_customs[i].data)-2;}else{sh_customs[i].limit=1;}};
		this.limits[z]=sh_customs[i].limit;
		this.customs[z]=sh_customs[i];
		arr_opt=this.custom_select.options;
		arr_opt[arr_opt.length]=new Option(this.customs[z].label,z);}
		}
	}
if (this.custom_select.options.length==0) this.custom_parag.style.visibility="hidden";
}


ghdb_filter.prototype.f_show = function(sh_f,x,isCust)
{	
	isCust=isCust || false;
	newTR=document.createElement("tr");
	this.inner_fh.appendChild(newTR);
	newPtext=document.createElement("td");
	newTR.appendChild(newPtext);
	newP=document.createElement("td");
	newTR.appendChild(newP);
	switch (sh_f.type){
	//show input
	case "input":{
	newF=document.createElement("input");
	newF.type="text";
	newF.name=sh_f.id;
	if(typeof(sh_f.size)!=='undefined'){newF.size=sh_f.size};
	this.objectum[x]=newF;
	if(typeof(sh_f.data) !== 'undefined'){ newF.value=sh_f.data;}
	newL=document.createTextNode(sh_f.label+": ");
	newPtext.appendChild(newL);
	newP.appendChild(newF);
	break;}
	//show select
	case "select":{
	newF=document.createElement("select");
	newF.size=1;
	newF.name=sh_f.id;
	this.objectum[x]=newF;
	newL=document.createTextNode(sh_f.label+": ");
	newPtext.appendChild(newL);
	newP.appendChild(newF);
	arr_opt=newF.options;
	for(i in sh_f.data){arr_opt[arr_opt.length]=new Option(sh_f.data[i],i);
	if ((typeof(sh_f.def) !== 'undefined')&&(sh_f.def==i)) {arr_opt[arr_opt.length-1].selected=true;}}
	break;}
	//show combo
	case "combo":{
	newF=document.createElement("select");
	newF.multiple=true;
	newF.name=sh_f.id;
	if(typeof(sh_f.size)!=='undefined'){newF.size=sh_f.size};
	this.objectum[x]=newF;
	newL=document.createTextNode(sh_f.label+": ");
	newPtext.appendChild(newL);
	newP.appendChild(newF);
	arr_opt=newF.options;
	for(i in sh_f.data){
		arr_opt[arr_opt.length]=new Option(sh_f.data[i],i);
		if (typeof(sh_f.def) !== 'undefined') 
			{for(l in sh_f.def) {if(sh_f.def[l]==i) arr_opt[arr_opt.length-1].selected=true;}}
	}
	break;}
	//show yesno
	case "yesno":{
	newF=document.createElement("select");
	newF.size=1;
	newF.name=sh_f.id;
	this.objectum[x]=newF;
	newL=document.createTextNode(sh_f.label+": ");
	newPtext.appendChild(newL);
	newP.appendChild(newF);
	arr_opt=newF.options;
	arr_opt[arr_opt.length]=new Option(this._("yes"),1);
	arr_opt[arr_opt.length]=new Option(this._("no"),0);
	break;}
	case "cmp":{
	this.objectum[x]=new Array();
	newF1=document.createElement("input");
	newF1.type="text";
	newF1.name=sh_f.id+"_1";
	newF1.onchange=function(){this.style.border="2px inset gray"};
	if(typeof(sh_f.size)!=='undefined'){newF1.size=sh_f.size};
	this.objectum[x][0]=newF1;
	if((typeof(sh_f.data) !== 'undefined')&&(typeof(sh_f.data[0]) !== 'undefined')) {newF1.value=sh_f.data[0];}
	newF2=document.createElement("input");
	newF2.type="text";
	newF2.name=sh_f.id+"_2";
	newF2.onchange=function(){this.style.border="2px inset gray"};
	if(typeof(sh_f.size)!=='undefined'){newF2.size=sh_f.size};
	this.objectum[x][1]=newF2;
	if((typeof(sh_f.data) !== 'undefined')&&(typeof(sh_f.data[1]) !== 'undefined')) {newF2.value=sh_f.data[1];}
	newL=document.createTextNode(sh_f.label+": ");
	newL1=document.createTextNode(this._("from")+" ");
	newL2=document.createTextNode(" "+this._("to")+" ");
	newPtext.appendChild(newL);
	newP.appendChild(newL1);
	newP.appendChild(newF1);
	newP.appendChild(newL2);
	newP.appendChild(newF2);
	break;}
	case "numeric":{
	newF=document.createElement("input");
	newF.type="text";
	newF.name=sh_f.id;
	newF.onchange=function(){this.style.border="2px inset gray"};
	if(typeof(sh_f.size)!=='undefined'){newF.size=sh_f.size};
	this.objectum[x]=newF;
	if(typeof(sh_f.data) !== 'undefined'){ newF.value=sh_f.data;}
	newL=document.createTextNode(sh_f.label+": ");
	newPtext.appendChild(newL);
	newP.appendChild(newF);
	break;}
	}
	if(typeof(sh_f.hint) !== 'undefined') {newP.title=sh_f.hint;} else {newP.title=sh_f.label;}
	if(isCust){
	newD=document.createElement("a");
	newD.href="javascript:void(0)";
	newD.onclick=function(){
		gh_filter.inner_fh.removeChild(this.parentNode.parentNode);
		for(j in gh_filter.objectum){
						if(gh_filter.filters[j].type=="cmp"){sh_found_pnode=gh_filter.objectum[j][1].parentNode;}
						else{sh_found_pnode=gh_filter.objectum[j].parentNode;}
						if (sh_found_pnode==this.parentNode) {
												for(k in gh_filter.customs){
												if (gh_filter.customs[k].id==gh_filter.filters[j].id)
												{gh_filter.customs[k].limit+=1;
												if(gh_filter.customs[k].limit==1){
												arr_opt=gh_filter.custom_select.options;
												arr_opt[arr_opt.length]=new Option(gh_filter.customs[k].label,k);
												gh_filter.custom_parag.style.visibility="visible";}
												}
												}
												gh_filter.filters.splice(j,1);
												gh_filter.objectum.splice(j,1);
												}
						}
				}
	newP.appendChild(newD);
	d_node=document.createTextNode(this._("delete"));
	newD.appendChild(d_node);
	}

}


ghdb_filter.prototype.custom_show=function(){
sh_sel_val=this.custom_select.options[this.custom_select.selectedIndex].value;
sh_sel_ind=this.custom_select.selectedIndex;
if(this.customs[sh_sel_val].limit>0){this.customs[sh_sel_val].limit-=1;this.add(this.customs[sh_sel_val],true);}
if(this.customs[sh_sel_val].limit==0){this.custom_select.remove(sh_sel_ind);}
if (this.custom_select.options.length==0) this.custom_parag.style.visibility="hidden";
}


ghdb_filter.prototype.submit= function(){
param_line="";do_flag=1;
	for (i in this.filters){
		if(i!=0) param_line+="&";
		switch (this.filters[i].type){
			case "input":{
				param_line+=this.filters[i].id+"[]=";
				param_line+=this.objectum[i].value;
			break;
			}
			case "select":{
				param_line+=this.filters[i].id+"[]=";
				param_line+=this.objectum[i].value;
			break;
			}
			case "combo":{
			arr_options=this.objectum[i].options;
			flag=1;
			for (j=0;j<=arr_options.length-1;j++){
					if (arr_options[j].selected) {
					if (flag==1){flag=0;}else{param_line+="&";}
					param_line+=this.filters[i].id+"[]=";
					param_line+=arr_options[j].value;
				}
			}
			break;
			}
			case "yesno":{
			param_line+=this.filters[i].id+"[]=";
			param_line+=this.objectum[i].value;
			break;
			}
			case "cmp":{
			if (/[^\d\$]/.test(this.objectum[i][0].value)){do_flag=0;this.objectum[i][0].style.border="2px solid red";alert(this._("This field must contain a number"));}
			if (/[^\d\$]/.test(this.objectum[i][1].value)){do_flag=0;this.objectum[i][1].style.border="2px solid red";alert(this._("This field must contain a number"));}
			param_line+=this.filters[i].id+"[]="+this.objectum[i][0].value+"|"+this.objectum[i][1].value;
			break;
			}
			case "numeric":{
				if (/[^\d\$]/.test(this.objectum[i].value)){do_flag=0;this.objectum[i].style.border="2px solid red";alert("This field must contain a number");}
				param_line+=this.filters[i].id+"[]=";
				param_line+=this.objectum[i].value;
			break;
			}
		}
	}
if (do_flag) this.table=new ghdb_table_init(this.action,param_line,this.method,this.table_holder,this.status_holder); 
}

ghdb_filter.prototype.reset=function(){
	for (i in this.customs){this.customs[i].limit=this.limits[i];}
	while(this.filter_holder.childNodes.length>0){this.filter_holder.removeChild(this.filter_holder.lastChild);}
	this.filters=new Array();
	this.customs=new Array();
	this.objectum=new Array();
	f_h=this.filter_holder;
	this.inner_fh=document.createElement("table");f_h.appendChild(this.inner_fh);
	this.custom_parag=document.createElement("div");
	elem_n=document.createTextNode(this._("Additional filters: "));this.custom_parag.appendChild(elem_n);
	this.custom_select=document.createElement("select");this.custom_select.size=1;this.custom_parag.appendChild(this.custom_select);
	elem_b=document.createElement("button");elem_b.onclick=function(){gh_filter.custom_show()};
				butttext=document.createTextNode(this._("add"));elem_b.appendChild(butttext);this.custom_parag.appendChild(elem_b);
	f_h.appendChild(this.custom_parag);
	elem=document.createElement("button");elem.onclick=function(){gh_filter.submit()};
				butttext=document.createTextNode(this._("Use filter"));elem.appendChild(butttext);f_h.appendChild(elem);
	elem=document.createElement("button");elem.onclick=function(){gh_filter.reset()};
				butttext=document.createTextNode(this._("Reset filter"));elem.appendChild(butttext);f_h.appendChild(elem);
	this.add(this.idata);
}

ghdb_filter.prototype.as_len = function(arr){
len=0;
for (j in arr){len+=1;}
return len;
}

