function trim(s){return s.replace(/(^\s+)|(\s+$)/g,'');}
function clickLi() {
	var string = "";
	var categorySelector = document.getElementById("CategorySelector");
	if(categorySelector != null) {
		var category = document.getElementsByTagName("li")
		for(var i = 0; i < category.length; i++) {
			category[i].onclick = function() {
				for(var j = 0; j < category.length; j++) {
					if(this.parentNode.id == category[j].parentNode.id) {
						if(trim(category[j].className) == "" || trim(category[j].className) == "Selected") {
							category[j].className = "";
						} else {
							category[j].className = "IsFather";
						}
					}
				}
				this.className = this.className + " Selected";
				string=changeCategory(this.id);
				if(this.parentNode.id == "firstCategory") {
					document.getElementById("firstCategoryText").value = this.id;
					document.getElementById("secondCategory").innerHTML = "";
					document.getElementById("thirdCategory").innerHTML = "";
					document.getElementById("secondCategory").innerHTML = string;
				}
				if(this.parentNode.id == "secondCategory") {
					document.getElementById("secondCategoryText").value = this.id;
					document.getElementById("thirdCategory").innerHTML = string;
				}
				if(this.parentNode.id == "thirdCategory") {
					document.getElementById("thirdCategoryText").value = this.id;
				}
			}
		}
	}
}

function changeCategory(id) {
	var xmlRequest = createXMLHttpRequest();
	var url = "ajax.php?action=get_category_xml&category_id=" + id;
	xmlRequest.open("get", url, false);
	xmlRequest.send(null);
	var string = "";
	if(xmlRequest.readyState == 4 && xmlRequest.status == 200) {
		var xmlDocument = xmlRequest.responseXML.documentElement;
		var category = xmlDocument.getElementsByTagName("category")
		for(var i = 0; i < category.length; i++) {
			for(var j = 0; j < category[i].childNodes.length; j++) {
				if(category[i].childNodes.item(j).nodeType == 1) {
					if(category[i].childNodes.item(j).nodeName == "category_id") {
						string += "<li id='" + category[i].childNodes.item(j).childNodes.item(0).data + "'";
					}
					if(category[i].childNodes.item(j).nodeName == "has_child") {
						if(category[i].childNodes.item(j).childNodes.item(0).data == "true") {
							string += " class='IsFather' onmouseover='clickLi()'>";
						} else {
							string += " onmouseover='clickLi()'>";
						}
					}
					if(category[i].childNodes.item(j).nodeName == "category_name") {
						string += category[i].childNodes.item(j).childNodes.item(0).data + "</li> \n";
					}
				}
			}
		}
	}
	return string;
}

