var agent	       = window.navigator.userAgent; 
var isMac     	   = ((agent.indexOf("Mac")!=-1)||(agent.indexOf("mac")!=-1)); 
var isIE6 		   = ( agent.indexOf("MSIE 6") != -1 );
var isIE           = (navigator.appVersion.indexOf("MSIE") != -1);    
var isWin          = (navigator.appVersion.indexOf("Windows") != -1); 

//popup functions
function ndPopup(aTag, pWidth, pHeight) {
	//This function is supposed to be called from the a-tag with valid href-attribute
	//If pWidth and pHeight are given, the popup will assume them as width and height
	if (!pWidth) pWidth=600; if (!pHeight) pHeight=500;
	if (top.ndCMSPopup && !top.ndCMSPopup.closed && top.ndCMSPopup.close) top.ndCMSPopup.close();
	top.ndCMSPopup = top.open(aTag.href, "ndCMSPopup", "width="+pWidth+",height="+pHeight+",resizable=1,menubar=1");
	top.ndCMSPopup.focus();
	return false;
}

function gkPopup(aTag, pWidth, pHeight) {
	if (!pWidth) pWidth=600; if (!pHeight) pHeight=500;
	if (top.ndPopup && !top.ndPopup.closed && top.ndPopup.close) top.ndPopup.close();
	top.ndPopup = top.open(aTag.href, "gkPopup", "width="+pWidth+",height="+pHeight+",resizable=1,menubar=1,scrollbars=1");
	top.ndPopup.focus();
	return false;
}


//switches the plus/minus icon
function switchPlusMinus(imageSrc) {
	var newSrc;
	var oldSrc = imageSrc;
	if (oldSrc.indexOf("minus") > -1) {
		newSrc = oldSrc.replace(/minus/gi, "plus");
	}
	if (oldSrc.indexOf("plus") > -1) {
		newSrc = oldSrc.replace(/plus/gi, "minus");
	}
	return newSrc;
}

//sets the plus/minus icon
function setPlusMinus(imageSrc, appearance) {
	var newSrc;
	var oldSrc = imageSrc;
	
	if (appearance == "plus") {
		newSrc = "/images/buttons/showhide_plus.gif";
	} else if (appearance == "minus") {
		newSrc = "/images/buttons/showhide_minus.gif";
	}
	return newSrc;
}

//
function moreDatesRowHover(tr, action) {
	var th = tr.getElementsByTagName("th")[0];
	if (action == "in") {
		tr.style.backgroundColor = "#E4EBF4";
		if (th) th.style.backgroundColor = "#E4EBF4";
	} else {
		tr.style.backgroundColor = "";
		if (th) th.style.backgroundColor = "";
	}	
}

function moreDatesShowHideCourseInfo(aTag, action) {
	var infoDiv = aTag.parentNode.getElementsByTagName("DIV")[0];
	if (infoDiv.style.display == "block") {
		infoDiv.style.display = "none";
	} else {
		infoDiv.style.display = "block";
	}
}


// for multiselects with checkboxes. see advanced course search
function checkSelectAll(objCheck) {
	var tableObj = objCheck.parentNode.parentNode.parentNode;
	var checkArr = tableObj.getElementsByTagName("input");
	for (var i=0; i<checkArr.length; i++) {
		if (objCheck.checked) {
			checkArr[i].prev = checkArr[i].checked;
			checkArr[i].checked = true;
		} else if (checkArr[i] != objCheck) {
			checkArr[i].checked = checkArr[i].prev;
		}
	}
}

// for multiselects with checkboxes. see advanced course search
function checkSelectOne(objCheck) {
	if (! objCheck.checked) {
		var tableObj = objCheck.parentNode.parentNode.parentNode;
		var checkAll = tableObj.getElementsByTagName("input")[0];
		checkAll.checked = false;
	}
}

// render functies
function renderIds() {
	for (var i=0; i<arguments.length; i++) {
		var obj = document.getElementById(arguments[i])
		if (obj && obj.style) {
			obj.style.display = '';
		}
 	}
}
 
function renderNotIds() {
	for (var i=0; i<arguments.length; i++) {
 		var obj = document.getElementById(arguments[i]);
		if (obj && obj.style) {
			obj.style.display = 'none';
		}
	}
}

// Function to write javascript-buttons, that will only appear if 
// javascript is enabled (Conform Drempelsweg)
// Remark: Onsubmits may still be used in the form!!!
// @ btnType 	'txt', 'text', 'btn' or 'button', in most cases 'btn' will be used
// @ tnTxt 		is the text that should appear in the Nice Button
// @ frmObj  	is the form that should be submitted after pressing the Nice Button
function writeButton(btnType, btnTxt, frmObj) {
	var aStart = "<a href=# onclick='" + (document.forms[frmObj].onsubmit ? "if (document.forms[\""+frmObj+"\"].onsubmit()!=false) " : "" ) +"document.forms[\""+frmObj+"\"].submit(); return false;'>"+ btnTxt;
	//document.write('<input type="submit" class="drempelsweg">');
	if (btnType=="button" || btnType=="btn") {
		document.write('' +
		'<table cellpadding="0" cellspacing="0" border="0" class="generic_button">' +
		'<tr>' +
		'	<td><img src="/images/buttons/generic_buttonpart_left.gif" width="10" height="16" alt=""></td>' +
		'	<td class="buttontext">' + aStart + '</a></td>' +
		'	<td><img src="/images/buttons/generic_buttonpart_right.gif" width="19" height="16" alt=""></td>' +
		'</tr>' +
		'</table>');
	} else if (btnType == "text" || btnType == "txt") {
		document.write("&nbsp;" + aStart + "</a>");
	}
}

// same as write button, but a javascript instead of a form is given as handler
function writeButtonWithHandler(btnTxt, href, stringToEval) {
	document.write('' +
	'<table cellpadding="0" cellspacing="0" border="0" class="generic_button">' +
	'<tr>' +
	'	<td><img src="/images/buttons/generic_buttonpart_left.gif" width="10" height="16" alt=""></td>' +
	'	<td class="buttontext"><a href="'+ href + '" target="_blank" onclick="' + stringToEval + ';return false;">' + btnTxt + '</a></td>' +
	'	<td><img src="/images/buttons/generic_buttonpart_right.gif" width="19" height="16" alt=""></td>' +
	'</tr>' +
	'</table>');
}


// select childnodes as an array from root id, tagname and classname (conjunctive)
// id is optional (but then it should be null), tagname and className are not optional and are strings 
function getChildNodes(id, tagName, className) {
	var obj, tagObjArr, classObjArr = [];
	(id) ? obj = document.getElementById(id) : obj = document;
	(obj && tagName) ? tagObjArr = obj.getElementsByTagName(tagName) : [];
	// loop though the elements with tagName and extract the ones with className
	for (var i=0; i<tagObjArr.length; i++) {
		if (tagObjArr[i].className) {
			var classes = tagObjArr[i].className.split(" ");
			for (var j=0; j<classes.length; j++) {
				if (classes[j] == className) {
					classObjArr[classObjArr.length] = tagObjArr[i];
				}
			}
		} 	
	}
	return classObjArr;
}

function trimString(str) {
  var s;
  s = str.replace( /^\s+/g, "" );// strip leading
  s = s.replace( /\s+$/g, "" );// strip trailing
  return s;
}