function addWindowOnloadAfter(theFunction) {
	var oldFunction = window.onload;
	if (typeof oldFunction != 'function') {
		window.onload = theFunction;
	}
	else
	{
		var newFunction = function() {
			oldFunction();
			theFunction();
		};
		window.onload = newFunction;
	}
}

function addWindowOnloadBefore(theFunction) {
	var oldFunction = window.onload;
	if (typeof oldFunction != 'function') {
		window.onload = theFunction;
	}
	else
	{
		var newFunction = function() {
			theFunction();
			oldFunction();
		};
		window.onload = newFunction;
	}
}

function addOnClickAfter(theDOM, theFunction) {
	var oldFunction = theDOM.onclick;
	if (typeof oldFunction != 'function') {
		theDOM.onclick = theFunction;
	}
	else
	{
		var newFunction = function() {
			oldFunction();
			theFunction();
		};
		theDOM.onclick = newFunction;
	}
}

function addOnClickBefore(theDOM, theFunction) {
	var oldFunction = theDOM.onclick;
	if (typeof oldFunction != 'function') {
		theDOM.onclick = theFunction;
	}
	else
	{
		var newFunction = function() {
			theFunction();
			oldFunction();
		};
		theDOM.onclick = newFunction;
	}
}

/*
function fillSelect(selectDOM, optionsArray, optionSelected, callback)
{
	while (selectDOM.options.length > 1)
	{
		selectDOM.remove(1);
	}
	for (theOption in optionsArray)
	{
		if (optionsArray.length <= theOption)
			break;
		if (optionsArray[theOption] == undefined)
			continue;
		var newOption = document.createElement("option");
		if (typeof(callback) == "function")
		{
			newOption.value = callback(theOption);
		}
		else
		{
			newOption.value = theOption;
		}
		newOption.text = optionsArray[theOption];
		if (newOption.value == optionSelected)
		{
			newOption.selected = true;
		}
		if (document.all && !window.opera)
		{
			selectDOM.add(newOption);
		}
		else
		{
			selectDOM.add(newOption, null);
		}
	}
}
*/
function fillSelect(selectDOM, optionsArray, optionSelected, minCount, callback)
{
	// window.alert("fillSelect: " + selectDOM.id + ", " + typeof(optionsArray) + " :: " + optionsArray.length +", " + optionSelected + ", " + minCount + ", " + callback);
	// Backwards compatibility: Fix!
	if (typeof(minCount) == "function")
	{
		callback = minCount;
		minCount = 1;
	}
	if (minCount == undefined)
	{
		minCount = 1;
	}
	while (selectDOM.options.length > minCount)
	{
		selectDOM.remove(minCount);
	}
	for (theOption in optionsArray)
	{
		// window.alert(optionsArray[theOption]);
		if (typeof(callback) == "function")
		{
			var theOptionReal = callback(theOption);
		}
		else
		{
			var theOptionReal = theOption;
		}
		/*
		if (optionsArray.length <= theOptionReal)
			break;
		*/
		if (optionsArray[theOption] == undefined)
			continue;
		if (typeof(optionsArray[theOption]) == "function")
			continue;
		fillSelectAddOption(selectDOM, theOption, optionsArray[theOption], optionSelected)
	}
}

function fillSelectAddOption(selectDOM, value, text, optionSelected)
{
	var newOption = document.createElement("option");
	newOption.value = value;
	newOption.text = text;
	if (value == optionSelected)
	{
		newOption.selected = true;
	}
	if (document.all && !window.opera)
	{
		selectDOM.add(newOption);
	}
	else
	{
		selectDOM.add(newOption, null);
	}
}


function fillSelect2(selectDOM, optionsArray) {
	while (selectDOM.options.length > 1) {
		selectDOM.remove(1);
	}
	for (theOption in optionsArray) {
		if (optionsArray.length <= theOption)
			break;
		if (optionsArray[theOption] == undefined)
			continue;
		var newOption = document.createElement("option");
		newOption.value = theOption;
		newOption.text = optionsArray[theOption];
		if (document.all && !window.opera)
		{
			selectDOM.add(newOption);
		}
		else
		{
			selectDOM.add(newOption, null);
		}
	}
}	

