function getFromUrl(what) {
	var form = document.getElementById("urlRef");
	if (form.value!="") {
		var url = form.value;
	} else {
		var url = document.URL;
	}

	var result_arr = new Array();
	var result;
	result_arr = url.split(what+"=",2);
	if (result_arr[1]) {		
		result = result_arr[1].split("&",1);
	}
	if (result == "") {
		result = 0;	
	}
	
	return result;	
} 
function showChild (data) {
		var selectForm = document.getElementById("txtChild");	
		var zawartosc;
		zawartosc = "<option value='wybierz'>wybierz</option>";
	
	for (var i=0; i < data.categories.length; i++) {

		zawartosc += "<option value='" + data.categories[i].id + "'";
	
		var childId = getFromUrl("childId");

		if (data.categories[i].id==childId) {
//		if (data.categories[i].id==<? echo ($_GET['childId'] != "") ? $_GET['childId'] : 0; ?>) {			
	        zawartosc += "selected=\"selected\"";   
        }
        zawartosc += ">" + data.categories[i].name + "</option>";		
        
	}

	select_innerHTML(selectForm,zawartosc);
    
};
function changeParent() {

    var txtField = document.getElementById("txtParent");
	
	if (txtField.value != "wybierz") {

		var oXmlHttp = zXmlHttp.createRequest();
		oXmlHttp.open("get", "/ajaxphp/catalogForm.php?parentId" + "=" + encodeURIComponent(txtField.value), true);
		 oXmlHttp.onreadystatechange = function () {

		loading_data = document.getElementById('loading_child');
		loading_data.innerHTML = "<img src=\"../../img/icons/loadingbar.gif\" style=\"width:150px; height: 15px;\"/>";
		loading_data.style.display = "";	

			if (oXmlHttp.readyState == 4) {

				if (oXmlHttp.status == 200) {
					var data = JSON.parse(oXmlHttp.responseText);
					loading_data.style.display = 'none';

					showChild(data);		
							
					var txtChild = document.getElementById("txtChild");
					txtChild.style.display="";					
				} else {
					var msg = "Bd podczas prby poczenia z serwerem.";
				}				
			}
		}
    oXmlHttp.send(null);		
    } else {
		var txtChild = document.getElementById("txtChild");
		txtChild.style.display="none";
	}	
};
function loadChild() {
		var oXmlHttp = zXmlHttp.createRequest();
		var parentId = getFromUrl("parentId");
		oXmlHttp.open("get", "/ajaxphp/catalogForm.php?parentId" + "=" + parentId, true);
		
		oXmlHttp.onreadystatechange = function () {

		var txtChild = document.getElementById('txtChild');
		txtChild.style.display = 'none';
		loading_data = document.getElementById('loading_child');
		loading_data.innerHTML = "<img src=\"../../img/icons/loadingbar.gif\" style=\"width:150px; height: 15px;\"/>";
		loading_data.style.display = "";	

		if (oXmlHttp.readyState == 4) {		
				if (oXmlHttp.status == 200) {
					var data = JSON.parse(oXmlHttp.responseText);
					loading_data.style.display = 'none';
					txtChild.style.display = '';						
					showChild(data);
					var txtChild = document.getElementById("txtChild");
					txtChild.disabled=false;					
				} else {
					var msg = "Blad podczas próby polaczenia z serwerem.";
				}				
		}
        }
		oXmlHttp.send(null);	
        
}; 


