var http_request = false;
var url_address = "./php/request.php";
var listIndex = 0;

//specify URL and handler function to handle results
function makeRequest(url, handler)
{
	disableDropdowns();
	http_request = false;
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
	   http_request = new XMLHttpRequest();
	   if (http_request.overrideMimeType)
	      http_request.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject)
	{ // IE
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request)
	{
		alert('Nie można stworzyć instancji obiektu XMLHTTP');
		return false;
	}
	http_request.onreadystatechange = function() { checkRequest(handler); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function checkRequest(handler)
{
	if(http_request.readyState == 4)
	{
	   if(http_request.status == 200) handler();
	   else
	   {
	      alert("http_request.status="+http_request.status);
	   	return false;
	   }
	}
	else
	{
		//indicate loading process
	}
	return false;
}

//handlers
function updateDropdown(index)
{
	if(typeof(dropdowns)=="undefined") {alert("dropdowns are missing!"); return;}
	if(index == null)//recurrence
	{
		listIndex++;
		updateList(http_request, dropdowns[listIndex]);
	}
	else listIndex = index;
	if(listIndex < dropdowns.length-1) //if not all list updated yet, request for data for next dropdown
	{
	   var url_req = url_address+'?table='+listIndex;
	   for(i=0; i<dropdowns.length-1; i++) url_req += "&fk"+i+"="+dropdowns[i].value;
	   waitingNotice(dropdowns[listIndex+1]); //show msg on the next dropdown
	   makeRequest(url_req, updateDropdown);
	}
	else
		enableDropdowns();
}

//reads requested XML data and updates dropdown list
function updateList(http_request, dropdown)
{
	var xmldoc = http_request.responseXML;
	var arr_names = new Array();
	var arr_ids = new Array();
	
	var item = xmldoc.getElementsByTagName('item').item(0);
	if(item != null)
	   do
	   {
	      arr_ids.push(item.getAttribute("id"));
	      arr_names.push(item.getAttribute("name"));
	      item = item.nextSibling;
	   }
	   while(item != null)

	if(dropdown.type)
	{
		dropdown.length = 0;
		dropdown.options[0] = new Option("Wszystkie",0);
		for(i=0; i<arr_names.length; i++)
			dropdown.options[i+1] = new Option(arr_names[i],arr_ids[i]);
	}
}

function enableDropdowns() {changeDropdowns(false);}
function disableDropdowns(){changeDropdowns(true);}

function changeDropdowns(flag)
{
	for(i=0; i<dropdowns.length; i++)
		dropdowns[i].disabled = flag;
	document.getElementById("searchform").elements["submit1"].disabled = flag;
	document.getElementById("searchcodeform").elements["submit2"].disabled = flag;
}

function waitingNotice(dropdown)
{
	if(dropdown.type)
	{
		dropdown.length = 0;
		dropdown.options[0] = new Option("Poczekaj...",0);
	}
}

