//V2.0 2004-11-26 MP
// (c) 2004-2006 Ovitech

function clearList(field) {
	//clears an entire list
	field = getObject(field);
	for (var i = field.length - 1 ; i >= 0 ; i--) {
		listremove(field, i);
	}
}

function getObject(objectId) {
	//returns the object connected to the name
	if(window.document.getElementById && window.document.getElementById(objectId)) return window.document.getElementById(objectId);
	else if (window.document.all && window.document.all(objectId))
	return window.document.all(objectId);
	else if (window.document.layers && window.document.layers[objectId])
	return window.document.layers[objectId];
	else return null;
}

function getValue(field) {
	//gets the value of a field
	field = getObject(field);
	if (field!=null){
		return field.value;
	}else{
		return null;
	}
}

function inArray(needle, hayStack, begin, caseSensitive) {
	//checks if the substring is in the string.
	//checks if the string is in the begin if specified
	//returns true or false
	if (!caseSensitive) {
		needle = needle.toLowerCase();
		hayStack = hayStack.toLowerCase();
	}
	var beginpoint = hayStack.search(needle);
	if ((begin) && (beginpoint == 0)) return true;
	else if ((!begin) && (beginpoint != -1)) return true;
	else return false;
}

function selectList(list, i) {
	unselectList(list);
	var list = getObject(list);
	if (list.options[i] != null) {
		list.options[i].selected = true;
	}
}

function setValue(field, value) {
	//sets the value of a field

	if (typeof(value) == 'undefined') value = '';
	field = getObject(field);
	if (field!=null){
		field.value = value;
	}
}

function subSelectArray(arraySrc, validArray) {
	arrayRes = Array();
	for (i = 0 ; i < validArray.length ; i++) {
		arrayRes[arrayRes.length] = arraySrc[validArray[i]];
	}
	return arrayRes;
}

function subSetArray(needle, hayStack, begin, caseSensitive) {
	//checks if the string is in the array of strings in the hayStack
	//checks if the string is in the begin is specified
	//returns the array of indexes
	var returnArray = Array();
	for (var i = 0 ; i < hayStack.length ; i++) {
		if (needle == '') returnArray[returnArray.length] = i;
		else if (inArray(needle, hayStack[i], begin, caseSensitive)) returnArray[returnArray.length] = i;
	}
	return returnArray;
}

function unselectList(list) {
	var list = getObject(list);
	for (var i = 0 ; i < list.length ; i++) {
		list.options[i].selected = false;
	}
}

//add an entry to the list
function listadd(field, val, text, i) {
	if (typeof(i) == 'undefined') var i = field.length;
//	var regexp = "/" + "\"" + "/g";
//	var replacestring = "";
//	text = text.replace(regexp, replacestring);
	field.options[i] = new Option(text);
	field.options[i].value = val;
}

function listpresentcheck(field, val, text) {
	//check if it is in the list
	//returns the index number if the item is present else returns false
	//val is the value of the item that should be removed
	//text is the text of the item that should be removed
	//either text or value is filled
	if ((typeof(val) == 'undefined')||(val== "")) {
		var active = text;
		var act = "text";
	}
	else if ((typeof(text) == 'undefined')||(text== "")) {
		var active = val;
		var act = "value";
	}
	if ((typeof(active) == 'undefined')||(active== "")) return -1;
	else {
		var len = field.length;
		for (var i = 0 ; i < len ; i++) {
			eval('var tempactive = field.options[i].' + act);
			if (tempactive == active) return i;
		}
		return -1;
	}
}

function listremove(field, i) {
	//clears an item of the fieldlist
	//i is the index of the item to be removed
	field.options[i] = null;
}

function listsort(field, dir) {
	//sort the list alphabetically
	//sorts a list
	var len = field.length;
	var ok = false;
	while (!ok) {
		var found = false;
		if (dir == "up")
		for (var i = 0 ; i < (len - 1) ; i++) {
			if (field.options[i].text.toUpperCase() > field.options[(i + 1)].text.toUpperCase()) {
				listswitch(field,i);
				found = true;
			}
		}
		if (dir == "down")
		for (var i = 0 ; i < (len - 1) ; i++) {
			if (field.options[i].text.toUpperCase() < field.options[(i + 1)].text.toUpperCase()) {
				listswitch(field,i);
				found = true;
			}
		}
		if (!found) ok = true;
	}
}

function listswitch(field, i) {
	//i is the number of the field that should be switched with its next item

	var texti = field.options[i].text;
	var vali = field.options[i].value;
	var seli = field.options[i].selected;
	field.options[i].text = field.options[(i + 1)].text;
	field.options[i].value = field.options[(i + 1)].value;
	field.options[i].selected = field.options[(i + 1)].selected;
	field.options[(i + 1)].text = texti;
	field.options[(i + 1)].value = vali;
	field.options[(i + 1)].selected = seli;
}

function listUpdate(field, arrval, arrtext, doupdate) {
	//fieldid is the id of the list
	//arrval is an array of values that should be in the list
	//arrtext is an array of strind that should be in the list (corresponding with arrval)
	//check if there are as much text-tems as values
	field = getObject(field);
	if (arrval.length != arrtext.length) {
		alert('size is not the same');
//		alert(arrval.length +"="+ arrtext.length);
//		alert('listupdate: the number of text-items and number of values does not correspond');
	}
	else {
		if ((typeof(doupdate) == "undefined") || (doupdate == "")) doupdate = false;
		//for each item in the list look if the value is in the array
		var len = field.length;
		var len2 = arrval.length;
		if (!doupdate) {
			for (i = (len - 1) ; i >= 0 ; i--) {
				listremove(field, i);
			}
			for (var i = 0 ; i < len2 ; i++) {
				listadd(field, arrval[i], arrtext[i], i);
			}
		}
		else {
			if (len != 0) {
				for (var i = (len - 1) ; i >= 0 ; i--) {
					var found = false;
					for (var j = 0 ; j < len2 ; j++) {
						if (field.options[i].value == arrval[j]) found = true;
					}
					if (!found) listremove(field, i)
				}
			}
			for (var i = 0 ; i < len2 ; i++) {
				//check if the field exists
				var index = listpresentcheck(field, arrval[i]);
				if (index == -1) listadd(field, arrval[i], arrtext[i]);
			}
		}
		//sort the list
		//listsort(field, "up");
	}
}