// Written for www.hitchhikers.de by Jens Benecke <jens at team.hitchhikers.de>
//
// If you use this script or parts of it, we would like you to link back to
// our page at http://www.hitchhikers.de/
//
// This script is released under the GNU GPL (General Public Licence) 2.0.
// This means (simplified) that you can use it for whatever you want, but
// you MUST publish any changes, improvals or updates / bugfixes and we
// would like to have a copy sent to our address above.
// Also, this comment text and the copyright notice must not be changed.
//
// Thank you.
//
// You can get a full copy of the license at http://www.opensource.org.
//

//
// Diese Skripte wandeln ein SELECT-Element in zwei SELECT-Elemente um,
// wobei im ersten Element die Anfangsbuchstaben und dann im zweiten ZUERST
// die "passenden" Auswahlmoeglichkeiten (und dann der Rest) zu sehen sind.
//
// Das ganze wird nur dann ueberhaupt gemacht, wenn im SELECT-Element mehr
// als 'minanzahl' Elemente auftauchen.
//


function printalphabet(target, def, alle_orte_str) {

	//if(target.options.length<50) return false; // zu kleine Box -> lohnt nicht

	document.write("<SELECT name="+target.name+"filter onChange='return dofilter("+target+", this.value, \""+alle_orte_str+"\");'>\n");
	document.write("<OPTION name=_alle>...\n");
	for(var i=65; i<=81; i++) {
		document.write("<OPTION name="+fromCharCode(i)+">"+fromCharCode(i)+"\n");
	}
	document.write("</SELECT>\n\n");

}


function dofilter(target, buchstabe, alle_orte_str) {

	// Momentan 'unpassende' Werte speichern
	var a = new Array(); var atmp;
	var b = new Array(); var btmp;
	var i = 0;

	for(i=0; i<target.length; i++) {
		atmp = a.push(target.options[i].value);
		btmp = b.push(target.options[i].text);
	}
//	alert("dofilter fillarray: target="+target+", "+a.length+", "+b.length);

	target.length = 0; // Select-Feld leeren

	// 'Alle Orte' einfügen
//	var no0 = new Option();
//	no0.value = a[i]; no0.text = b[i];
//	target.options[target.options.length] = no0;

	for(i=0; i<a.length; i++) {
		if(b[i].substring(0,1) == buchstabe) {
			var no1 = new Option();
			no1.value = a[i]; no1.text = b[i];
			target.options[target.options.length] = no1;
		}
	}

	// Trennlinie einfügen
	var no2 = new Option();
	no2.value = 0; no2.text = '---------';
	target.options[target.options.length] = no2;

	for(i=0; i<a.length; i++) {
		if(b[i].substring(0,1) != buchstabe) {
			var no3 = new Option();
			no3.value = a[i]; no3.text = b[i];
			target.options[target.options.length] = no3;
		}
	}

}
