function createHttpRequest(){
  var xmlHttp;
  try{
    // Utworzenie obiektu XMLHttpRequest (silnik Gecko, Webkit, Presco)
    xmlHttp=new XMLHttpRequest();
  } catch(e) {
    // Wyłapuje błąd jeśli JavaScript nie posiada obiektu XMLHttpRequest
    try {
      // Utworzenie obiektu ActiveXObject, który jest zawarty w kontrolce ActiveX IE
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        // Utworzenie obiektu ActiveXObject, dla innych wersji IE
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        // Wyświetlenie błędu o braku obsługi obiektu XMLHttpRequest
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  // zwrócenie obiektu
  return xmlHttp;
}

function sortCompare(a, b) {
	var arra = a.split(":");
	var arrb = b.split(":");

	if (arra.length != 2 || arrb.length != 2) {
		if (arra.length == arrb.length) {
			return 0;
		} else if (arra.length != 2) {
			return 1;
		} else {
			return -1;
		}
	} else if (arra[1] == arrb[1]) {
		return 0;
	} else if (arra[1] < arrb[1]) {
		return -1;
	} else {
		return 1;
	}
}

function updateLocalityList() {
	var list = document.getElementById("localityList");
	var provinceList = document.getElementById("provinceList");
	if (list != null) {
		var request = createHttpRequest();
		request.onreadystatechange = function() {
			if(request.readyState==4) {
				if (request.responseText != null) {
					var lines = request.responseText.split(";");
					
					lines.sort(sortCompare);
					list.options.length = 0;
					for(var i = 0; i < lines.length; i++) {
						var idValue = lines[i].split(":");
						if (i > 0) {
							idValue[0] = provinceList.options[provinceList.selectedIndex].innerHTML + " - " + idValue[1];
						}
						if (idValue.length == 2) {
							var option = document.createElement("option");
							option.value = idValue[0];
							option.innerHTML = idValue[1];
							try {
								list.add(option, null);
							} catch(ex) {
								list.add(new Option(idValue[1], idValue[0]));
							}
						}
					}

				}
			}
		}
		request.open("GET", "tercservice.php?type=locality&provinceId="
			+ provinceList.options[provinceList.selectedIndex].value, true);
		request.send(null);
	}
}

function updateProvinceList() {
	var list = document.getElementById("provinceList");
	if (list != null) {
		var request = createHttpRequest();
		request.onreadystatechange = function() {
			if(request.readyState==4) {
				if (request.responseText != null) {
					var lines = request.responseText.split(";");
					list.options.length = 0;
					for(var i = 0; i < lines.length; i++) {
						var idValue = lines[i].split(":");
						if (idValue.length == 2) {
							var option = document.createElement("option");
							option.value = idValue[0];
							option.innerHTML = idValue[1];
							try {
								list.add(option, null);
							} catch(ex) {
								list.add(new Option(idValue[1], idValue[0]));
							}
						}
					}
					updateLocalityList();
				}
			}
		}
		request.open("GET", "tercservice.php", true);
		request.send(null);
	}
}



