
function changeSearchKind () {
	var searchKind = document.getElementById("searchKind");	
	resetForm();
	if(searchKind.value==1) {
		showSearchByContinent();
	} else if (searchKind.value==2) {
		showSearchByCategory();
	} else if (searchKind.value==3) {
		showSearchByLogin();
	} else {
		hideAll();	
	}
}

function resetForm() {
	var searchContinent = document.getElementById("searchContinent");
	var searchCountry = document.getElementById("searchCountry");
	var searchCity = document.getElementById("searchCity");
	var searchParent = document.getElementById("searchParent");
	var searchChild = document.getElementById("searchChild");
	var searchLogin = document.getElementById("searchLogin");
	var trCountry = document.getElementById("trCountry");
	var trCity = document.getElementById("trCity");
	var trChild = document.getElementById("trChild");	
	var txtSubmit = document.getElementById("txtSubmitPF");	


	searchCountryinnerHTML = "<option value='wybierz'>wszystkie</option>";
	select_innerHTML((searchCountry),searchCountryinnerHTML);
		
	searchCityinnerHTML = "<option value='wybierz'>wszystkie</option>";
	select_innerHTML((searchCity),searchCityinnerHTML);
	
	searchParentinnerHTML = "<option value='wybierz'>wszystkie</option>";
	select_innerHTML((searchParent),searchParentinnerHTML);
	
	searchChildinnerHTML = "<option value='wybierz'>wszystkie</option>";
	select_innerHTML((searchChild),searchChildinnerHTML);
	
	searchLogin.value = "";
	
	searchContinent.disabled = true;
	searchCountry.disabled = true;
	searchCity.disabled = true;
	searchParent.disabled = true;
	searchChild.disabled = true;
	searchLogin.disabled = true;
	txtSubmit.disabled = true;
	
	searchCountry.style.display = "none";
	searchCity.style.display = "none";
	trCountry.style.display = "none";
	trCity.style.display = "none";	
	trChild.style.display = "none";
	searchChild.style.display = "none";
	txtSubmit.style.display = "none";
}

function hideAll() {
	var searchByContinent = document.getElementById("searchByContinent");
	var searchByCategory = document.getElementById("searchByCategory");
	var searchByLogin = document.getElementById("searchByLogin");	
	
	searchByContinent.style.display = "none";
	searchByCategory.style.display = "none";
	searchByLogin.style.display = "none";	
}

function showSearchByContinent () {
	var searchByContinent = document.getElementById("searchByContinent");
	var searchByCategory = document.getElementById("searchByCategory");
	var searchByLogin = document.getElementById("searchByLogin");	
	
	searchByContinent.style.display = "";
	searchByCategory.style.display = "none";
	searchByLogin.style.display = "none";	
	
	getContinents();
}



function getContinents() {
	var searchContinent = document.getElementById("searchContinent");
	var txtSubmit = document.getElementById("txtSubmitPF");
	
	searchContinentinnerHTML = "<option value='1'>Afryka</option><option value='2'>Ameryka Północna</option><option value='3'>Ameryka Południowa</option><option value='4'>Australia</option><option value='5'>Azja</option><option value='6' selected='selected'>Europa</option>";
	select_innerHTML((searchContinent),searchContinentinnerHTML);	
		
	searchContinent.disabled = false;

	txtSubmit.style.display = "";
	txtSubmit.disabled = false;		
	changeSearchContinent();		
}

function changeSearchContinent() {
	var searchContinent = document.getElementById("searchContinent");	
	var searchCountry = document.getElementById("searchCountry");	
	var searchCity = document.getElementById("searchCity");	
	var trCity = document.getElementById("trCity");	
	var trCountry = document.getElementById("trCountry");	
	var txtSubmit = document.getElementById("txtSubmitPF");


	searchCountry.disabled = true;
	searchCity.disabled = true;
	searchCity.style.display = "none";	
	trCity.style.display = "none";	


	if (searchContinent.value != "wybierz") {
		var oXmlHttp = zXmlHttp.createRequest();
		
		var loadingIco = document.getElementById("loadingIco");	
		loadingIco.style.display = "";
		
		oXmlHttp.open("get", "ajaxphp/placeFinder.php?ContinentId" + "=" + encodeURIComponent(searchContinent.value), true);
		 oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
			
				if (oXmlHttp.status == 200) {
					var data = JSON.parse(oXmlHttp.responseText);
					showContinents(data);
					loadingIco.style.display = "none";	
					txtSubmit.style.display = "";
					txtSubmit.disabled = false;						
				} else {
					var msg = "Bd podczas prby poczenia z serwerem.";
				};
			}
	} 
  	oXmlHttp.send(null);
	} else {
		searchCountry.disabled = true;
		searchCountry.style.display = "none";	
		trCountry.style.display = "none";
		txtSubmit.style.display = "none";
		txtSubmit.disabled = true;						
		
		
	}
	
};

function showContinents (data) {
	var searchCountry = document.getElementById("searchCountry");	
	var trCountry = document.getElementById("trCountry");			
	var zawartosc;
	zawartosc = "<option value='wybierz'>wszystkie</option>";

	if (data.countries.length==0) {
				searchCountry.disabled = false;
				zawartosc = "<option value='wybierz'>brak krajów</option>";		
	} else {	
		for (var i=0; i < data.countries.length; i++) {
		 zawartosc += "<option value='" + data.countries[i].id + "'>" + data.countries[i].name + "</option>";		
		searchCountry.disabled = false;		 
		}
	}

	searchCountry.innerHTML = zawartosc;	

	select_innerHTML((searchCountry),zawartosc);	
	searchCountry.style.display = "";		
	trCountry.style.display = "";	
}

function changeSearchCountry() {
	var searchCountry = document.getElementById("searchCountry");	
	var searchCity = document.getElementById("searchCity");	
	var trCity = document.getElementById("trCity");		
	var txtSubmit = document.getElementById("txtSubmitPF");	
	txtSubmit.disabled = true;			
	searchCity.disabled = true;
	
	if (searchCountry.value != "wybierz") {
		var oXmlHttp = zXmlHttp.createRequest();
		
		var loadingIco = document.getElementById("loadingIco");
		
		loadingIco.style.display = "";
		
		oXmlHttp.open("get", "ajaxphp/placeFinder.php?CountryId" + "=" + encodeURIComponent(searchCountry.value), true);
		 oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
			
				if (oXmlHttp.status == 200) {
					var data = JSON.parse(oXmlHttp.responseText);
					showCities(data);

					txtSubmit.style.display = "";
					txtSubmit.disabled = false;				
					loadingIco.style.display = "none";
				} else {
					var msg = "Bd podczas prby poczenia z serwerem.";
				};
			}
	} 
  	oXmlHttp.send(null);
	} else {
		txtSubmit.style.display = "none";	
		trCity.style.display = "none";
		searchCity.style.display = "none";
	}
	
};

function showCities (data) {
	var searchCity = document.getElementById("searchCity");	
	var trCity = document.getElementById("trCity");			
		
	var zawartosc;
	zawartosc = "<option value='wybierz'>wszystkie</option>";
	
	if (data.cities.length==0) {
				searchCity.disabled = true;
				zawartosc = "<option value='wybierz'>brak miast</option>";		
	} else {		
		for (var i=0; i < data.cities.length; i++) {
			 zawartosc += "<option value='" + data.cities[i].id + "'>" + data.cities[i].name + "</option>";		
		searchCity.disabled = false;		 			 
		}
	}
	searchCity.innerHTML = zawartosc;	
	select_innerHTML((searchCity),zawartosc);	
	
	searchCity.style.display = "";			
	trCity.style.display = "";	
}
function showSearchByCategory () {
	var searchByContinent = document.getElementById("searchByContinent");
	var searchByCategory = document.getElementById("searchByCategory");
	var searchByLogin = document.getElementById("searchByLogin");	
	
	searchByContinent.style.display = "none";
	searchByCategory.style.display = "";
	searchByLogin.style.display = "none";		
	getCategories();
	
}
function showSearchByLogin () {
	var searchByContinent = document.getElementById("searchByContinent");
	var searchByCategory = document.getElementById("searchByCategory");
	var searchByLogin = document.getElementById("searchByLogin");	
	var txtSubmit = document.getElementById("txtSubmitPF");	

	txtSubmit.style.display = "";
	searchByContinent.style.display = "none";
	searchByCategory.style.display = "none";
	searchByLogin.style.display = "";		
	
	var searchLogin = document.getElementById("searchLogin");	
	searchLogin.disabled = false;
	txtSubmit.disabled = false;
	
}

function getCategories() {
	var searchParent = document.getElementById("searchParent");	
	var txtSubmit = document.getElementById("txtSubmitPF");		
	
	getParent();
	searchParent.disabled = false;
	txtSubmit.style.display = "";
	txtSubmit.disabled = false;			
}

function getParent() {
	var oXmlHttp = zXmlHttp.createRequest();
	
	var loadingIco = document.getElementById("loadingIco");
		
	loadingIco.style.display = "";
		
	oXmlHttp.open("get", "ajaxphp/placeFinder.php?getParent", true);
	oXmlHttp.onreadystatechange = function () {
	
		if (oXmlHttp.readyState == 4) {
				
			if (oXmlHttp.status == 200) {
				var data = JSON.parse(oXmlHttp.responseText);
				showParent(data);
				loadingIco.style.display = "none";
			} else {
				var msg = "Bd podczas prby poczenia z serwerem.";
			};
		}
	
	} 
  	oXmlHttp.send(null);

};

function showParent(data) {
	var searchParent = document.getElementById("searchParent");	
	var zawartosc;
	zawartosc = "<option value='wybierz'>wszystkie</option>";

	if (data.parents.length==0) {
				searchParent.disabled = true;
				zawartosc = "<option value='wybierz'>brak kategorii</option>";		
	} else {	
		for (var i=0; i < data.parents.length; i++) {
		if (i==0) {
		 zawartosc += "<option value='" + data.parents[i].parentId + "'>" + data.parents[i].parentName + "</option>"; } 
		 else {
		 zawartosc += "<option selected='selected' value='" + data.parents[i].parentId + "'>" + data.parents[i].parentName + "</option>";			 
		 }
		searchParent.disabled = false;		 
		}
	}
	searchParent.innerHTML = zawartosc;	
	select_innerHTML((searchParent),zawartosc);	
	searchParent.style.display = "";	
	changeSearchParent();
};

function changeSearchParent() {
	var searchParent = document.getElementById("searchParent");	
	var searchChild = document.getElementById("searchChild");	
	var trChild = document.getElementById("trChild");	
	var txtSubmit = document.getElementById("txtSubmitPF");	

	txtSubmit.style.display = "";
	txtSubmit.disabled = false;

	searchChild.disabled = true;
	
	if (searchParent.value != "wybierz") {
		var oXmlHttp = zXmlHttp.createRequest();
		
		var loadingIco = document.getElementById("loadingIco");
		
		loadingIco.style.display = "";
		
		oXmlHttp.open("get", "ajaxphp/placeFinder.php?ParentId" + "=" + encodeURIComponent(searchParent.value), true);
		 oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
			
				if (oXmlHttp.status == 200) {
					var data = JSON.parse(oXmlHttp.responseText);
					showChild(data);
					loadingIco.style.display = "none";
					txtSubmit.style.display = "";
					txtSubmit.disabled = false;						
				} else {
					var msg = "Bd podczas prby poczenia z serwerem.";
				};
			}
	} 
  	oXmlHttp.send(null);
	} else {
		searchChild.disabled = true;
		searchChild.style.display = "none";	
		trChild.style.display = "none";		
	}
}

function showChild(data) {
	var searchChild = document.getElementById("searchChild");	
	var trChild = document.getElementById("trChild");	

	var zawartosc;
	zawartosc = "<option value='wybierz'>wszystkie</option>";

	if (data.childs.length==0) {
				searchChild.disabled = true;
				zawartosc = "<option value='wybierz'>brak podkategorii</option>";		
	} else {	
		for (var i=0; i < data.childs.length; i++) {
			 zawartosc += "<option value='" + data.childs[i].id + "'>" + data.childs[i].name + "</option>";		
			searchChild.disabled = false;		 
		}
	}
	searchChild.innerHTML = zawartosc;
	select_innerHTML((searchChild),zawartosc);		
	searchChild.style.display = "";		
	trChild.style.display = "";		

};

function changeSearchChild() {
	var searchChild = document.getElementById("searchChild");	
	var txtSubmit = document.getElementById("txtSubmitPF");	

	if (searchChild.value != "wybierz") {
		txtSubmit.disabled = false;
		txtSubmit.style.display = "";
	} else {
		txtSubmit.disabled = true;
		txtSubmit.style.display = "none";		
	}

}


