function js_moveover(frombox,tobox)
{
	var fromboxLength = frombox.length;

	var opt_id, p_option;
	
	for (opt_id = 0; opt_id < fromboxLength; opt_id++)
	{
		p_option = frombox.options[opt_id];
		if (p_option.selected)
		{
			addOption (tobox, p_option.text, p_option.value);
		}
	}
}

function addOption (tobox, text, value)
{
	var boxLength = tobox.length;
	var i, thisitem;
	var isNew = true;
	if (boxLength != 0) {
		for (i = 0; i < boxLength; i++) {
			thisitem = tobox.options[i].value;
			if (thisitem == value) {
				isNew = false;
				break;
			}
		}
	} 
	if (isNew) {
		var newoption = new Option(text, value, false, false);
		tobox.options[boxLength++] = newoption;
	}
}

/*
function js_moveover(frombox,tobox)
{
	var boxLength = tobox.length;
	var selectedItem = frombox.selectedIndex;
	var selectedText = frombox.options[selectedItem].text;
	var selectedValue = frombox.options[selectedItem].value;

	if (selectedValue >= 0)
	{
		var i;
		var isNew = true;
		if (boxLength != 0) {
			for (i = 0; i < boxLength; i++) {
				thisitem = tobox.options[i].value;
				if (thisitem == selectedValue) {
					isNew = false;
					break;
				}
			}
		} 
		if (isNew) {
			newoption = new Option(selectedText, selectedValue, false, false);
			tobox.options[boxLength] = newoption;
		}
//		frombox.selectedIndex=-1;
	}
}
*/


function js_moveover_mult(frombox,tobox)
{
	var boxLength = tobox.length;
	var selectedItem = frombox.selectedIndex;
	var selectedText = frombox.options[selectedItem].text;
	var selectedValue = frombox.options[selectedItem].value;
	var i;

	newoption = new Option(selectedText, selectedValue, false, false);
	tobox.options[boxLength] = newoption;
	frombox.selectedIndex=-1;
}


function js_removeme(tobox) {
	var boxLength = tobox.length;
	arrSelected = new Array();
	var count = 0;
	for (i = 0; i < boxLength; i++) {
		if (tobox.options[i].selected) {
			arrSelected[count] = i;
		}
		count++;
	}
	var x;
	for (i = boxLength - 1; i >= 0; i--) {
		for (x = arrSelected.length - 1; x >= 0; x--) {
			if (i == arrSelected[x]) {
			tobox.options[i] = null;
		   }
		}
	}
	tobox.focus();
}


function reorder(formfield,index,to) {
	var list = formfield;
	var total = list.options.length-1;
	if (index == -1) return false;
	if (to == +1 && index == total) return false;
	if (to == -1 && index == 0) return false;
	var items = new Array;
	var values = new Array;
	
	for (i = total; i >= 0; i--) {
		items[i] = list.options[i].text;
		values[i] = list.options[i].value;
	}

	for (i = total; i >= 0; i--) {
		if (index == i) {
			list.options[i + to] = new Option(items[i],values[i + to], 0, 1);
			list.options[i] = new Option(items[i + to], values[i]);
			i--;
		}
		else {
			list.options[i] = new Option(items[i], values[i]);
		}
	}
	list.focus();
}

function js_submitform(docform, resultstring) {
	var listnumber=js_submitform.arguments.length;
	var theList = "";

	for (var j = 2; j < listnumber; ++j) {
		var list=js_submitform.arguments[j];
		if (list.length > 0) {
			// start with a "?" to make it look like a real query-string
			for (i = 0; i <= list.length-1; i++) {
				theList += list.options[i].value;
				// a "&" only BETWEEN the items, so not at the end
				if (i != list.length-1) theList += "@@@";
				else if (j != listnumber - 1) theList += "&&&";

			}
		}
		else if (j != listnumber - 1) {
			theList += "&&&";
		}
	}
	resultstring.value = theList;
	docform.submit();
}

function textCounter(field,  maxlimit) {
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
}



//========= Select districts, linked to the Street =======
var g_AllStreetsBox, g_DistrictsFromBox, g_allDistrictsBox, g_TimerID;

function showStreetDistricts(frombox, districtsFromBox, allDistrictsBox)
{
	g_AllStreetsBox = frombox;
	g_DistrictsFromBox = districtsFromBox;
	g_allDistrictsBox = allDistrictsBox;

	clearTimeout(g_TimerID);
	g_TimerID = setTimeout (doShowStreetDistricts, 300);
}

function doShowStreetDistricts()//(frombox, districtsFromBox, allDistrictsBox)
{
	var fromboxLength = g_AllStreetsBox.length;
	var streetValue;

	var opt_id, p_option;

	var districtOptions = g_DistrictsFromBox.options;
	while (districtOptions.length > 0)
	{
		districtOptions.remove(0);
	}
	
	for (opt_id = 0; opt_id < fromboxLength; opt_id++)
	{
		p_option = g_AllStreetsBox.options[opt_id];
		if (p_option.selected)
		{
			streetValue = p_option.value;
			var districts = streetValue.split(";");
			if (districts.length == 2)
			{
				districts = districts[1].split(",");
				var districtId, i, districtName;
				for (i = 0; i < districts.length; i++)
				{
					districtId = districts[i];
					districtName = getDistrictName(districtId,g_allDistrictsBox);
					if (districtName != "")
					{
						addOption (g_DistrictsFromBox, districtName, districtId);
					}
				}
			}
		}

	}
}

function getDistrictName (districtId, allDistrictsBox)
{
	var fromboxLength = allDistrictsBox.length;

	var opt_id, p_option;
	
	for (opt_id = 0; opt_id < fromboxLength; opt_id++)
	{
		p_option = allDistrictsBox.options[opt_id];
		if (p_option.value == districtId)
		{
			return p_option.text;
		}
	}
	return '';
}