function SearchPrdTreeItem(id, idprev, name)
{
	this.id = id;
	this.idprev = idprev;
	this.name = name;
}

function SearchPrdTree(idContener, name)
{
	this.contener = document.getElementById(idContener);
	this.items = Array();
	this.controls = Array();
	this.name = name;
	this.counter = 0;
		
	this.Add = function(id, idprev, name)
	{
		this.items.push(new SearchPrdTreeItem(id,idprev,name));
	}
	
	this.Load = function(idprev,currentID)
	{		
		if(currentID != null)
		{				
			this.RemoveControls(currentID.replace("sel",""));
		}
		
		var hasChilds = false;
		this.counter += 1;
		var select = document.createElement("select");
		select.setAttribute('id',"sel"+this.counter);
		select.setAttribute('name',"sel"+this.counter);
		select.onchange = new Function(this.name+'.Load(this.value,this.id);GetAttributes("searchextajax.php?idtree="+GetSelectedIDTree());');
		
		var option = document.createElement("option");
		option.setAttribute('value',-1);
		option.appendChild(document.createTextNode("Wszystkie"));
		select.appendChild(option);
				
		for(var i = 0; i < this.items.length; i++)
		{
			if(this.items[i].idprev == idprev)
			{
				option = document.createElement("option");
				option.setAttribute('id',"opt"+this.items[i].id);
				option.setAttribute('name',"opt"+this.items[i].id);
				option.setAttribute('value',this.items[i].id);							
				option.appendChild(document.createTextNode(this.items[i].name));	
				select.appendChild(option);
				hasChilds = true;					
			}		
		}
		
		if(hasChilds)
		{
			this.controls.push(select);			
		}
		this.Render();
	}
	
	this.SelectControls = function(val)
	{
		var arr = val.split(";");
		
		for(var j = 0; j < arr.length; j++)
		{
			if(arr[j].length > 0)
			{				
				for(var i = 0; i < this.controls.length; i++)
				{
					if(this.controls[i] != null)
					{
						for(var k = 0; k < this.controls[i].options.length; k++)
						{							
							if(this.controls[i].options[k].value == arr[j])
							{								
								this.controls[i].options[k].selected = true;
								break;
							}
						}
					}
				}
			}
		}
	}
	
	this.RemoveControls = function(id)
	{
		var currentID = 0;
		
		for(var i = 0; i < this.controls.length; i++)
		{
			if(this.controls[i] != null)
			{
				currentID = this.controls[i].id.replace("sel","");
				
				if(currentID > id)
				{
					this.controls[i] = null;
				}
			}
		}

		this.ReloadControls();	
	}
	
	this.ReloadControls = function()
	{
		var arr = Array();
		
		for(var i = 0; i < this.controls.length; i++)
		{
			if(this.controls[i] != null)
			{
				arr.push(this.controls[i]);
			}
		}
	
		this.controls = arr;
	}
	
	this.Render = function()
	{
		if (this.contener.hasChildNodes())
		{
			while (this.contener.firstChild) 
			{				
				this.contener.removeChild(this.contener.firstChild);
			}
		}
		
		for(var i = 0; i < this.controls.length; i++)
		{
			if(this.controls[i] != null)
			{
				this.contener.appendChild(this.controls[i]);
				var span = document.createElement("span");
				span.innerHTML = "&nbsp;";
				this.contener.appendChild(span);
			}
		}
	}
}

function SearchType(id, name)
{
	this.id = id;
	this.name = name;
	this.attributes = Array();
}

function SearchAttribute(id, name, checked)
{
	this.id = id;
	this.name = name;
	this.checked = checked;
}

function SearchTypeAtr(idContener, name)
{
	this.contener = document.getElementById(idContener);
	this.items = Array();
	this.controls = Array();
	this.name = name;
		
	this.AddType = function(id, name)
	{
		this.items.push(new SearchType(id,name));
	}
	
	this.AddAttribute = function(idtype, id, name, checked)
	{
		var index = this.FindType(idtype);
		if(index != -1)
		{		
			this.items[index].attributes.push(new SearchAttribute(id,name,checked));
		}
	}
	
	this.FindType = function(id)
	{
		for(var i = 0; i < this.items.length; i++)
		{
			if(this.items[i].id == id)
			{
				return i;
			}
		}
		
		return -1;
	}
	
	this.Render = function()
	{
		var tbl = document.createElement("table");
		var tbody = document.createElement("tbody");
		tbl.border = 0;
		tbl.cellPadding = 4;
		tbl.cellSpacing = 4;	
		
		for(var i = 0; i < this.items.length; i++)
		{			
			//if(i%2 == 0)
			//{
				var row = document.createElement("tr");				
			//}
			var cell = document.createElement("td");
			cell.setAttribute("valign","top");			
			
			var bold = document.createElement("b");
			bold.appendChild(document.createTextNode(this.items[i].name));
			cell.appendChild(bold);
			cell.appendChild(document.createElement("br"));
			
			for(var j = 0; j < this.items[i].attributes.length; j++)
			{
				var chk = document.createElement("input");	
				chk.className = "check";
				chk.setAttribute("type","checkbox");				
				chk.setAttribute("id","chk"+i+""+j);
				chk.setAttribute("name","chk"+i+""+j);	
				chk.setAttribute("value",this.items[i].attributes[j].id);
				
				if(this.items[i].attributes[j].checked)
				{
					
					if(IsMSIE()==true)
					{
						chk.setAttribute('defaultChecked','defaultChecked');
					}
					else
					{
						chk.setAttribute('checked',true);	
					}
					
				}
				
				cell.appendChild(chk);
				cell.appendChild(document.createTextNode(" "+this.items[i].attributes[j].name));				
				cell.appendChild(document.createElement("br"));
			}
			
			row.appendChild(cell);
			//if(i%2 == 0)
			//{
				tbody.appendChild(row);
			//}
		}	
		
		tbl.appendChild(tbody);
		this.contener.appendChild(tbl);		
	}
}

//funkcje ajax
var selectedIDTree = Array();

function GetSelectedIDTree()
{
	var obj = document.getElementsByTagName("SELECT");
	var tmp = "";
	for(var i = 0; i < obj.length; i++)
	{
		if(obj[i].id.substring(0,3)=="sel")
		{
			if(parseInt(obj[i].value) != -1)
			{
				tmp += ""+obj[i].value+",";
			}
		}
	}
	if(tmp.length > 0)
	{
		tmp = tmp.substring(0,tmp.length-1);
	}
	
	return tmp;
}

function GetAttributes(url) 
{
	var http_request = MakeRequest(url);
	if(http_request != null)
	{
		http_request.onreadystatechange = function() {GetDataAttributes(http_request);};
		http_request.open('GET', url, true);
		http_request.send(null);
	}
}
	
function GetDataAttributes(http_request) 
{
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			document.getElementById("searchTypeAtr").innerHTML = http_request.responseText;
		} 
		else {//problem z zapytaniem
		}
	}
}
