// Funktio: haeToimialat()
// Argumentit: -
// Paluuarvo: -
function haepalvelutvalikko() {
	// Tyhjennetään lista
	document.getElementById('alitoimi').options.length = 0;
	var id = document.getElementById('toimiala').options[document.getElementById('toimiala').selectedIndex].value;
	var xmlHttp;
	var sivu = "ajax/haepalvelut.php?id=" + id + "&v=1";
	document.getElementById('alitoimi').style.display = 'block';

	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		// IE
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				// Virhe, tehdään php-haku
				return false;
			}
		}
	}

	// Kun xml-requestin tila vaihtuu
	xmlHttp.onreadystatechange = function() {

		// Haku on käynnissä
		if (xmlHttp.readyState == 2) {
			// Tyhjennetään lista
			document.getElementById('alitoimi').options.length = 0;

			// Lisätään teksti
			document.getElementById('alitoimi').options[0] = new Option('Ladataan..');
		}
		// Haku on valmis
		else if (xmlHttp.readyState == 4) {
			// Tyhjennetään lista
			document.getElementById('alitoimi').options.length = 0;

			// Toimialat arrayksi
			var alitoimialat = xmlHttp.responseText.split("\n");
			var opt = new Option("Valitse", "0");
			document.getElementById('alitoimi').options.add(opt);
			// Käydään toimialat läpi
			for (i in alitoimialat) {
				// id ja ala erikseen
				ala = alitoimialat[i].split("\t");
				// Lisätään ne listaan
				var opt = new Option(ala[1], ala[0])
				document.getElementById('alitoimi').options.add(opt);

			} // for

			// Valitaan oikea indexi
			// document.getElementById('toimiala').selectedIndex = 0;

		} // if
	} // function

	// Lähetetään pyyntö
	xmlHttp.open("GET", sivu, true);
	xmlHttp.send(null);

	// Ei anneta formin edetä
	return false;
}

// Funktio: haeGetArvo()
// Parametrit: nimi - Halutun arvon nimi
// Paluuarvo: Haettu Get-arvo tai ''
function haeGetArvo(nimi) {
	// Onko "alitoimialat" GET-muuttuja asetettu
	var get = document.location.search;
	var index = get.indexOf(nimi + '=');
	var arvo = '';

	// Löytyikö
	if (index != -1) {
		// Poistetaan alku
		get = get.substr(index + nimi.length + 1, get.length);

		// Lopun indexi muistiin
		var index = get.indexOf('&');

		// Löytyikö
		if (index == -1) {
			// Lopussa ei ole mitään ylimääräistä
			arvo = get;
		}
		else {
			// Poistetaan turhat merkit lopusta
			arvo = get.substr(0, index);
		}
	}

	// Palautetaan arvo
	return arvo;
}
