<!-- hide from old browsers

/******************************************************************************
* Project      : DOIT
* Name         : default.js
* Description  : Default java script functions for common wishes
* Author       : Patrick Husi <patrick.husi@arpage.ch>
* Copyright    : (c) 2005 by Arpage AG
*                CH-8700 Kuesnacht
*                All rights reserved
* Version      : $Id: default.js,v 1.5 2004/03/22 13:03:28 husi Exp $
******************************************************************************/

/*************************************************************************************************************
*** popup win funcions ***
**************************************************************************************************************/

var lastWin = null;
var lastLoc = null;
var lastTimeoutId = null;
function openPopupWin(loc, winType, parentElementId, specWidth, specHeight) {
	var features = "";
	var left = 120, top = 10, width = 630, height = 690;
	switch (winType) {
		case "image" :
			features = "scrollbars=no,";
			left = 220; top = 80; width = 440; height = 610;
			break;
		case "image-no-catalogue" :
			winType = "image";
			features = "scrollbars=no,";
			left = 540; top = 20; width = 400; height = 520;
			break;
		case "imageonly" :
			features = "scrollbars=no,";
			left = 20; top = 20; width = 390; height = 390;
			winType = "_blank";
			break;
		case "select-catalogue" :
			winType = "edit";
			features = "scrollbars=no,";
			left = 200; top = 10; width = 580; height = 630;
			break;
		case "select-image" :
			pWinType = "edit";
			features = "scrollbars=yes,";
			left = 180; top = 40; width = 520; height = 630;
			break;
		case "status" :
			winType = "status";
			features = "scrollbars=no,";
			left = 240; top = 100; width = 480; height = 370;
			break;
		case "phase" :
			winType = "edit";
			features = "scrollbars=yes,";
			left = 1; top = 1; width = 550; height = 580;
			break;
		case "lecture" :
			winType = "edit";
			features = "scrollbars=yes,";
			left = 120; top = 10; width = 630; height = 690;
			break;
		case "instruction" :
			winType = "instruction";
			features = "scrollbars=yes,";
			left = 10; top = 10; width = 800; height = 690;
			break;
		case "translation" :
			winType = "translation";
			features = "scrollbars=yes,";
			left = 20; top = 20; width = 800; height = 690;
			break;
		case "help" :
			features = "scrollbars=yes,";
			left = 200; top = 100; width = 480; height = 500;
			break;
		case "notepad" :
			features = "scrollbars=no,";
			left = 220; top = 160; width = 480; height = 460;
			break;
		case "print" :
			features = "scrollbars=no,";
			left = 220; top = 80; width = 740; height = 610;
			break;
		default :
			alert("wrong type " + winType + "!");
			winType = "edit";
			features = "scrollbars=yes,";
			break;
	} // switch
	
	// Position adjustments
	if (specWidth != null)
		width = specWidth;
	if (specHeight != null)
		height = specHeight;
	if (parentElementId != null) {
		var dX = 0, dY = 0;
		if (window.screenX != null) {
			dX += window.screenX;
			dY += window.screenY;
		}
		if (window.pageXOffset != null) {
			dX -= window.pageXOffset;
			dY -= window.pageYOffset;
		} else if (document.body.scrollTop != null) {
			dX -= document.body.scrollLeft;
			dY -= document.body.scrollTop;
		}
		var position = getPosition(document.getElementById(parentElementId));
		left = position.x + dX + 50; top = position.y + dY + 130;
	}
	if (left + width > screen.availWidth) left = screen.availWidth - width;
	if (top + height > screen.availHeight) top = screen.availHeight - height;
	if (left < 0) left = 0;
	if (top < 0) top = 0;
	
	var myWin = window.open(loc, winType, 'dependent=no,resizable=yes,status=no,' + features + "left=" + left + ",top=" + top + ",width=" + width + ",height=" + height);
	if (myWin == null)
		return true;
	myWin.moveTo(left, top);
	myWin.resizeTo(width, height);
	if (myWin.focus != null)
		myWin.focus();
		
	// Checking if reload was ok, due bug in safari browser
	lastWin = myWin;
	lastLoc = loc;
	if (lastTimeoutId != null)
		clearTimeout(lastTimeoutId);
	lastTimeoutId = setTimeout('checkLocation()', 1000);
	
	return false;
} // openPopupWin

// Checking if popupp has realy been reloaded
function checkLocation() {
	if (lastTimeoutId != null) {
		clearTimeout(lastTimeoutId);
		lastTimeoutId = null;
	}
	if (lastWin == null || lastWin.closed || lastWin.location.href == "about:blank")
		return;
	if (lastWin.location.href != lastLoc)
		window.location.reload();
}

// Closing a popup window
function closeWindow(openerURL, keepInCurrentWindow) {
	if (keepInCurrentWindow != null && keepInCurrentWindow == 'true') {
		if (openerURL != '')
			location.href = openerURL.replace(/&amp;/gi, "&");
		else
			location.reload(true);
	} else {
		if (window.opener != null) {
			if (openerURL != null) {
				if (openerURL != '') {
					window.opener.location.href = openerURL.replace(/&amp;/gi, "&");
				} else {
					if (window.opener.reloadMe != null) {
						window.opener.reloadMe();
					} else {
						window.opener.location.reload(true);
					}
				}
			} // if
			window.opener.focus();
		} // if
		close();
	} // if
} // closeWindow

// Reloading current document
var reloadURL = "";
function reloadMe() {
	if (reloadURL != null && reloadURL != '') {
		var oldHref = location.href.replace(/#.*$/, '');
		var newHref = reloadURL.replace(/#.*$/, '');
		location.replace(reloadURL);
		if (oldHref.indexOf(newHref) != -1)
			location.reload(true);
	} else {
		location.reload(true);
	}
}

// Loads a new page in the current window
var ignoreClick = false;
function loadPage(page, target) {
	if (ignoreClick) {
		ignoreClick = false;
		return;
	}
	if (target == null || target == '')
		location.href = page;
	else
		window.open(page, target);
}

/*************************************************************************************************************
*** div, id and content funcions ***
**************************************************************************************************************/

// Gets the div object reference
function getRefToDiv(divID) {
	if (document.layers) // Netscape layers
		return document.layers[divID];
	if (document.getElementById) // DOM; IE5, NS6, Mozilla, Opera
		return document.getElementById(divID);
	if (document.all) // Proprietary DOM; IE4
		return document.all[divID];
	if (document[divID]) // Netscape alternative
		return document[divID];
	return false;
}

// Sets the content of a div element
function setDivContent(myReference, newContent, nameOfIframe) {
	if (myReference == null)
		return;
	if (typeof(myReference.innerHTML) != null) {
		// used by the IE series, Konqueror, Opera 7+ and Gecko browsers
		myReference.innerHTML = newContent;
	} else if (myReference.document && myReference.document != window.document) {
		// used by layers browsers
		myReference.document.open();
		myReference.document.write(newContent);
		myReference.document.close();
	} else if (window.frames && window.frames.length && window.frames[nameOfIframe]) {
		// used by browsers like Opera 6-
		// if we attempt to rewrite the iframe content before
		// it has loaded we will only produce errors
		myReference = window.frames[nameOfIframe].window;
		myReference.document.open();
		myReference.document.write(newContent);
		myReference.document.close();
	}
}

// Display status text
function doStatus(text) {
	if (text == null)
		text = "";
	self.defaultStatus = text;
	self.status = text;
	return true;
} // doStatus

// Gets the absolute position of an object
function getPosition(element) {
	var elem = element, x=0, y=0;

	// Going through tag and parent tags
	while ((typeof(elem) == "object") && (typeof(elem.tagName) != "null")) {
		y += elem.offsetTop;
		x += elem.offsetLeft;
		if (elem.tagName.toUpperCase() == "BODY")
			break;
		if (typeof(elem) == "object" && typeof(elem.offsetParent) == "object")
			elem = elem.offsetParent;
	}

	/* Objekt mit x und y zurueckgeben */
	position = new Object();
	position.x = x;
	position.y = y;
	return position;
}

/*************************************************************************************************************
*** tabs and register ***
**************************************************************************************************************/

// shows or hides a tab
function setTabVisibility(activeTab, tabsArray, tabName) {
	if (tabName == null || tabName == '')
		tabName = "content";
	var newActiveElementTab = document.getElementById("tab-" + activeTab);
	if (newActiveElementTab == null || newActiveElementTab.className == tabName + "-tabdisabled")
		return;
		
	for (var i = 0; i < tabsArray.length; i++) {
		var currentTab = tabsArray[i];
		var elementTab = document.getElementById("tab-" + currentTab);
		var elementContent = document.getElementById("content-" + currentTab);
		if (elementTab == null || elementTab.className == tabName + "-tabdisabled") {
			if (elementContent != null)
				elementContent.style.display = "none";
			continue;
		}
		if (activeTab == currentTab) {
			elementTab.className = tabName + "-tabsel";
			elementContent.style.display = "";
		} else {
			elementTab.className = tabName + "-tab";
			elementContent.style.display = "none";
		}
	}
} // setTabVisibility

function showElements(elements) {
	for (var i = 0; i < elements.length; i++) {
		var element = document.getElementById(elements[i]);
		if (element != null)
			element.style.display = "";
	}
} // showElements

function hideElements(elements) {
	for (var i = 0; i < elements.length; i++) {
		var element = document.getElementById(elements[i]);
		if (element != null)
			element.style.display = "none";
	}
} // hideElements

/*************************************************************************************************************
*** form validation ***
**************************************************************************************************************/

// Checks an input text field
function checkTextField(formName, fieldName, commentFieldId, minLen, maxLen, regExp) {
	// parsing and checking for errors
	if (document.forms[formName] == null) {
		alert("form \"" + formName + "\" doesn't exist");
		return false;
	}
	var field = document.forms[formName].elements[fieldName];
	var isError = !isNaN(parseInt(minLen)) && field.value.length < parseInt(minLen) || !isNaN(parseInt(maxLen)) && field.value.length > parseInt(maxLen);
	if (regExp != null && regExp != '') {
		var checkFieldRegExp = new RegExp(regExp);
		isError = isError || !field.value.match(checkFieldRegExp);
	}
	// errors field
	var errorsField = document.forms[formName].elements['errors'];
	if (errorsField != null) {
		if (errorsField.value.indexOf('-' + fieldName + '-') != -1) {
			if (!isError) {
				var fieldRegExp = new RegExp('-' + fieldName + '-');
				errorsField.value = errorsField.value.replace(fieldRegExp, '');
			}
		} else {
			if (isError) {
				errorsField.value += '-' + fieldName + '-';
			}
		}
	}
	// The comment field
	var commentField = getRefToDiv(commentFieldId);
	if (commentField != null) {
		if (field.value.length == 0)
			setDivContent(commentField, "Maximal " + maxLen + " Zeichen");
		else 
			setDivContent(commentField, field.value.length + " von maximal " + maxLen + " Zeichen");
		if (isError)
			commentField.style.color = "#FF9900";
		else
			commentField.style.color = "";
	}
	// The save image
	setSubmitButton(formName, isError);
	// The result
	return !isError;
} // checkTextField

// Checks a number of an input text field
function checkNumberField(formName, fieldName, commentFieldId, type, min, max) {
	// parsing and checking for errors
	if (document.forms[formName] == null) {
		alert("form \"" + formName + "\" doesn't exist");
		return false;
	}
	var field = document.forms[formName].elements[fieldName];
	var correctedValue = field.value.replace(/[^\d\.\-]/g, '');
	var parsedValue;
	if (type == 'double')
		parsedValue = parseFloat(correctedValue);
	else
		parsedValue = parseInt(correctedValue);
	if (isNaN(parsedValue)) {
		if (field.value != correctedValue)
			field.value = correctedValue;
	} else if (field.value != parsedValue)
		field.value = parsedValue;
	var isError = isNaN(parsedValue) || !isNaN(parseInt(min)) && field.value < parseInt(min) || !isNaN(parseInt(max)) && field.value > parseInt(max);
	// errors field
	var errorsField = document.forms[formName].elements['errors'];
	if (errorsField != null) {
		if (errorsField.value.indexOf('-' + fieldName + '-') != -1) {
			if (!isError) {
				var fieldRegexp = new RegExp('-' + fieldName + '-');
				errorsField.value = errorsField.value.replace(fieldRegexp, '');
			}
		} else {
			if (isError) {
				errorsField.value += '-' + fieldName + '-';
			}
		}
	}
	// The comment field
	var commentField = getRefToDiv(commentFieldId);
	if (commentField != null) {
		if (isError) {	
			if (!isNaN(parseInt(min)) && !isNaN(parseInt(max)))
				setDivContent(commentField, "Zwischen " + tsep(min) + " und " + tsep(max));
			else if (!isNaN(parseInt(min)))
				setDivContent(commentField, "Mindestens " + tsep(min));
			else if (!isNaN(parseInt(max)))
				setDivContent(commentField, "Maximal " + tsep(max));
			commentField.style.color = "#FF9900";
		} else {
			setDivContent(commentField, "&#160;");
			commentField.style.color = "";
		}
	}
	// The save image
	setSubmitButton(formName, isError);
	// The result
	return !isError;
} // checkNumberField

// enables or disables the submit button
function setSubmitButton(formName, isError) {
	var saveButtonImageName = "image-save-" + formName;
	var saveButtonImage = (saveButtonImageName != null && saveButtonImageName != '') ? document.images[saveButtonImageName] : null;
	if (saveButtonImage != null) {
		var errorsField = document.forms[formName].elements['errors'];
		var isFormError = isError || errorsField != null && errorsField.value != '';
		if (isFormError) {
			if (saveButtonImage.src.indexOf('save.gif') != -1)
				saveButtonImage.src = saveButtonImage.src.replace(/save.gif/, 'save_disabled.gif');
		} else {
			if (saveButtonImage.src.indexOf('save_disabled.gif') != -1)
				saveButtonImage.src = saveButtonImage.src.replace(/save_disabled.gif/, 'save.gif');
		}
	}
} // setSubmitButton

/*************************************************************************************************************
*** table functions: different row colors, sorting ***
**************************************************************************************************************/

// Row color style definitions
var CLASSNAME_ROW_ODD = "table1-bg1";
var CLASSNAME_ROW_EVEN = "table1-bg2";
var CLASSNAME_ROW_SELECTED = "table1-selected";

// Resets the row colors of a table after sorting
function setDifferentRowColorsToTable(sorteableTable, classnameRowOdd, classnameRowEven) {
	if (classnameRowOdd == null || classnameRowOdd == '')
		classnameRowOdd = CLASSNAME_ROW_ODD;
	if (classnameRowEven == null || classnameRowEven == '')
		classnameRowEven = CLASSNAME_ROW_EVEN;
	var rows = sorteableTable.tBody.rows;
	var l = rows.length;
	for (var i = 0; i < l; i++) {
		removeClassName(rows[i], i % 2 ? classnameRowOdd : classnameRowEven);
		addClassName(rows[i], i % 2 ? classnameRowEven : classnameRowOdd);
	}
}

// Mark a table row as selected
function setRowSelected(elementId) {
	var element = (elementId != null)?document.getElementById(elementId):this;
	if (element == null)
		return;
	addClassName(element, CLASSNAME_ROW_SELECTED);
} // setOptions

// Mark a table row as unselected
function setRowUnselected(elementId) {
	var element = (elementId != null)?document.getElementById(elementId):this;
	if (element == null)
		return;
	removeClassName(element, CLASSNAME_ROW_SELECTED);
} // setOptions

// adds a class to a specific element. I.e used for sorteable tables with different row colors.
function addClassName(el, sClassName) {
	var s = el.className;
	var p = s.split(" ");
	var l = p.length;
	for (var i = 0; i < l; i++) {
		if (p[i] == sClassName)
			return;
	}
	p[p.length] = sClassName;
	el.className = p.join(" ");
}

// removes a class to a specific element I.e used for sorteable tables with different row colors.
function removeClassName(el, sClassName) {
	var s = el.className;
	var p = s.split(" ");
	var np = [];
	var l = p.length;
	var j = 0;
	for (var i = 0; i < l; i++) {
		if (p[i] != sClassName)
			np[j++] = p[i];
	}
	el.className = np.join(" ");
}

/*************************************************************************************************************
*** Ajax functions ***
**************************************************************************************************************/

//ACCESSIBLE VARIABLES (for use within your callback functions):
//1) ajaxpack.ajaxobj //points to the current ajax object
//2) ajaxpack.filetype //The expected file type of the external file ("txt" or "xml")
//3) ajaxpack.basedomain //The root domain executing this ajax script, taking into account the possible "www" prefix.
//4) ajaxpack.addrandomnumber //Set to 0 or 1. When set to 1, a random number will be added to the end of the query string of GET requests to bust file caching of the external file in IE. See docs for more info.

//ACCESSIBLE FUNCTIONS:
//1) ajaxpack.getAjaxRequest(url, parameters, callbackfunc, filetype)
//2) ajaxpack.postAjaxRequest(url, parameters, callbackfunc, filetype)

function createAjaxObj() {
	var httprequest = false;
	if (window.XMLHttpRequest) { // if Mozilla, Safari etc
		httprequest = new XMLHttpRequest();
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject) { // if IE
		try {
			httprequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				httprequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	return httprequest;
}

var ajaxpack = new Object();
ajaxpack.basedomain = "http://" + window.location.hostname;
ajaxpack.ajaxobj = createAjaxObj();
ajaxpack.filetype = "txt";
ajaxpack.addrandomnumber = 0; //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest = function(url, parameters, callbackfunc, filetype) {
	ajaxpack.ajaxobj = createAjaxObj(); //recreate ajax object to defeat cache problem in IE
	if (ajaxpack.addrandomnumber == 1) //Further defeat caching problem in IE?
		parameters = parameters + "&ajaxcachebust=" + new Date().getTime();
	if (this.ajaxobj) {
		this.filetype = filetype;
		this.ajaxobj.onreadystatechange = callbackfunc;
		this.ajaxobj.open('GET', url + "?" + parameters, true);
		this.ajaxobj.send(null);
	}
}

ajaxpack.postAjaxRequest = function(url, parameters, callbackfunc, filetype) {
	ajaxpack.ajaxobj = createAjaxObj(); //recreate ajax object to defeat cache problem in IE
	if (this.ajaxobj) {
		this.filetype = filetype;
		this.ajaxobj.onreadystatechange = callbackfunc;
		this.ajaxobj.open('POST', url, true);
		this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.ajaxobj.setRequestHeader("Content-length", parameters.length);
		this.ajaxobj.setRequestHeader("Connection", "close");
		this.ajaxobj.send(parameters);
	}
}

/*************************************************************************************************************
*** common functions ***
**************************************************************************************************************/

function getArgsArr() {
	var query = location.search.substring(1);
	var pairs = query.split("&");
	var args  = new Object;
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1)
			continue;
		var argname = pairs[i].substring(0, pos);
		var value = pairs[i].substring(pos + 1);
		value = value.replace(/\+/g, " ");
		args[argname] = unescape(value);
	}
	return args;
}

//  Number formatting, includes thousands and decimal separators
function tsep(n) {
	var ts = "'"; // thousands separator
	var ds = "."; // decimal separator

	var numString = String(n), prefixString = numString, suffixString = "";
	var i = numString.indexOf(".");
	if (i != -1) { // if ".", then split:
		prefixString = numString.substring(0, i);
		suffixString = ds + numString.substring(i + 1);
	}
	return prefixString.replace(/(\d)(?=(\d{3})+([.]|$))/g, "$1" + ts) + suffixString;
}

// end hiding from old browsers -->
