/***
 * window.onload Stack
 */

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} 
	else {
		window.onload = function() {
			if (oldonload) oldonload();
			func();
		}
	}
}

/***
 * IE Objectload Fix
 */ 
 
// replace the contents of object-tags by themselves
function fixIEObjectLoad() {
	var arrObjects = document.getElementsByTagName('object');
	for (var i = 0; i < arrObjects.length; i++) {
		if (arrObjects[i].outerHTML)
			arrObjects[i].outerHTML = arrObjects[i].outerHTML;
	}
}

// run after page has fully loaded	
addLoadEvent(fixIEObjectLoad);

/***
 * this functions are used for the timezones (displayed in the header)
 */
 
// add Zero to number 0-9
function IfZero(num) {
	return ((num <= 9) ? ("0" + num) : num);
}

// get selected date
function getDate(curdate, month, day, hour) {
	return Date.UTC(curdate.getUTCFullYear(), month, day, hour);
}

// DST for selected areas
function getDST(area, dif, utctime) {
	var curdate = new Date();
	var curyear = curdate.getUTCFullYear();

	switch(area) {
		case "EU":
    	var dstDayB = 31 - (Math.floor(5*curyear/4) + 1) % 7;
    	dstDayA = 31 - (Math.floor(5*curyear/4) + 4) % 7;
		  var dstStart = getDate(curdate, 2, dstDayA, 1);   // GMT time
			var dstEnd = getDate(curdate, 9, dstDayB, 1);     // GMT time

			if(utctime >= dstStart && utctime < dstEnd) return 1;

			return 0;
		break;

		case "US":
			var dstDayB = 7 - (Math.floor(5*curyear/4) + 4) % 7;
			dstDayA = 31 - (Math.floor(5*curyear/4) + 1) % 7;
			var dstStart = getDate(curdate, 3, dstDayB, 2);
			var dstEnd = getDate(curdate, 9, dstDayA, 3);

			utctime = utctime-(dif*3600000);

			if(utctime >= dstStart && utctime < dstEnd) return 1;
			return 0;
 		break;
		 
		case "EST":
			var dstDayB = 7 - (Math.floor(5*curyear/4) + 4) % 7;
			dstDayA = 31 - (Math.floor(5*curyear/4) + 1) % 7;
			var dstStart = getDate(curdate, 3, dstDayB, 2);
			var dstEnd = getDate(curdate, 9, dstDayA, 3);

			utctime = utctime-(dif*3600000);

			if(utctime <= dstStart || utctime >= dstEnd) return 1;
			return 0;
 		break;

		default:
 			return 0;
		break;
	}
}

// return full string time
function getInfoTime(dt, text, area, differ, utctime, end){
	return text + toGetHourForPlace(dt, differ,getDST(area, differ, utctime)) + end;
}

// init
function initTime() {
	this.dt = new Date();
	this.dif = this.dt.getTimezoneOffset()/60;
	this.UTCTime = Date.UTC(this.dt.getUTCFullYear(), this.dt.getUTCMonth(), this.dt.getUTCDate(), this.dt.getUTCHours());
	this.end = ":" + IfZero(this.dt.getMinutes());// + ":" +  IfZero(this.dt.getSeconds());
}

// get Hour for selected place
function toGetHourForPlace(date, TimeUTCOffset, TimeZoneOffset) {
  var hour = date.getUTCHours()+TimeUTCOffset+TimeZoneOffset;
  return IfZero((hour>=24)?hour-24:((hour<0)?24+hour:hour));
}

// show all times
function GetTime() {
	var init = new initTime();

	//var localTime = "Local: " + init.dt.getHours() + init.end;
	var danishTime 	= getInfoTime(init.dt, "Hvidovre: ", "EU", 1, init.UTCTime, init.end);
	var baselTime 	= getInfoTime(init.dt, "Basel: ", "EU", 1, init.UTCTime, init.end);
	var nutleyTime 	= getInfoTime(init.dt, "Nutley: ", "US", -5, init.UTCTime, init.end);
	var tokyoTime 	= getInfoTime(init.dt, "Tokyo: ", 0, 9, init.UTCTime, init.end);
	var welwynTime 	= getInfoTime(init.dt, "Welwyn: ", "EU", 0, init.UTCTime, init.end);
	var deewhyTime 	= getInfoTime(init.dt, "Dee Why: ", "EST", 10, init.UTCTime, init.end);
	var sanfranciscoTime = getInfoTime(init.dt, "Palo Alto: ", "US", -8, init.UTCTime, init.end);
	
	var space="&nbsp;&nbsp;&nbsp;";
	if(document.getElementById("timeBar")){
		//document.getElementById("timeBar").innerHTML = sanfranciscoTime + space + nutleyTime + space + welwynTime + space + baselTime + space + tokyoTime + space + deewhyTime;
		document.getElementById("timeBar").innerHTML = danishTime;
		setTimeout("GetTime()", 1000);
	}
	else
		setTimeout("GetTime()", 5000);
}

// run after page has fully loaded
addLoadEvent(GetTime);

/***
 * general functions
 */

// opens a popup window
function showPopup(popupUrl, popupName, popupHeight, popupWidth, popupScroll){
    var winWidth = (screen.width - popupWidth)/2;
    var winHeight = (screen.height - popupHeight)/2;
    parameter = 'height='+popupHeight+',width='+popupWidth+',top='+winHeight+',left='+winWidth+',scrollbars='+popupScroll;
    popup = window.open(popupUrl, popupName, parameter);
    if (parseInt(navigator.appVersion) >= 4){
        popup.window.focus();
    }
}

// opens the printer dialog and closes the window after printing
function printWindow() {
    if (window.print) {
        window.print();
        window.setTimeout("window.close()",3000);
	}
}

/***
 * Form validation functions
 */
 
function checkTEDOInput (myForm)
{
	if ((myForm.uid.value == "")  || (myForm.pwd.value == ""))
	{
		alert("Username or/and Password are empty");
		return false;
	}
	else
	{
		return true;
	}
} 
 
function checkSearchCollegueInput(myForm)
{
	var re= new RegExp("\\w{2}");
	
	if(re.test(myForm.sName.value)==false)
	{
		alert("Please enter at least two characters.");
		myForm.sName.focus();
		return false;
	}
}

function checkSearchCollegueInputAdvanced(myForm)
{
	var re= new RegExp("\\w{2}");
	
	if(re.test(myForm.sName.value)==false && re.test(myForm.gName.value)==false && re.test(myForm.dept.value)==false && re.test(myForm.l.value)==false)
	{
		alert("Please fill in at least one field with at least two characters.");
		myForm.sName.focus();
		return false;
	}
	else
	{
		myForm.submit();
		return true;
	}
}

function checkSearchLearningsInput(myForm)
{
	var retval = true;
	
	// check if search string is composed of multiple words separated by a space, a comma, a semicolon or a plus.
	if ((myForm.FreeText.value.indexOf(" ") !=  -1) || (myForm.FreeText.value.indexOf(",") !=  -1) || (myForm.FreeText.value.indexOf(";") !=  -1)  || (myForm.FreeText.value.indexOf("+") !=  -1))
		alert("Single word searches are most reliable; if you do not find what you are seeking, search again with one term only.");

	// check if search string is provied
	if (!myForm.FreeText.value)
	{
		alert("Please insert a search term");
		retval = false;
	}

	return retval;
}

function limitText(limitField, limitCount, limitNum)
{
	if (limitField.value.length > limitNum)
		limitField.value = limitField.value.substring(0, limitNum);
	else
		if(limitCount != false)
			limitCount.innerHTML = limitNum - limitField.value.length;
}

/***
 * Dropdown Functions
 */

function showMenu(obj)
{
	document.getElementById(obj).style.visibility='visible';
}

function hideMenu(obj)
{
	document.getElementById(obj).style.visibility='hidden';
}

/***
 * Remote Calendar
 */
 
/**
 * Module:	Remote Calendar
 * Creator:	Andreas Nebiker (andreas.nebiker@netarchitects.com)
 * Version:	1.07
 *			-> See RemoteCalendarControl for Details
 */

function getCoords (element) {
	var objCoords = new Object();
	objCoords.x = element.offsetLeft;
	objCoords.y = element.offsetTop;
	
	while (element = element.offsetParent) {
		objCoords.x += element.offsetLeft;
		objCoords.y += element.offsetTop;
	}
	
	return objCoords;
}

function getWindowSizes () {
	var objSizes = new Object();

	// all except ie
	if (self.innerHeight) { 														
		objSizes.x = self.innerWidth;
		objSizes.y = self.innerHeight;
	}
	// ie 6 strict mode
	else if (document.documentElement && document.documentElement.clientHeight) {	
		objSizes.x = document.documentElement.clientWidth;
		objSizes.y = document.documentElement.clientHeight;
	}
	// other ies
	else if (document.body) { 														
		objSizes.x = document.body.clientWidth;
		objSizes.y = document.body.clientHeight;
	}
	
	return objSizes;
}
 
function calendarDropdownDisplay (element) {
	var elemDropdown;
	if (element && element.getElementsByTagName('ul')[0]) {
		elemDropdown = element.getElementsByTagName('ul')[0];
		elemDropdown.style.display = 'block';
	}
}

function calendarDropdownHide (element) {
	var elemDropdown;
	if (element && element.getElementsByTagName('ul')[0]) {
		elemDropdown = element.getElementsByTagName('ul')[0];
		elemDropdown.style.display = 'none';
	}
}

function initCalendar () {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
		
	var arrCalendars = new Array();
	var arrDropdowns = new Array('selectmonth', 'selectquarter', 'selectyear');
	var arrDivs = document.getElementsByTagName('div');
	var arrEvents = new Array();
	var arrErrors = new Array();
	var a,e,i;
	
	// navigation
	for (var i = 0; i < arrDropdowns.length; i++) {
		if (document.getElementById(arrDropdowns[i])) {
			arrDropdowns[i] = document.getElementById(arrDropdowns[i]);
			arrDropdowns[i].onmouseover	= function() {
				calendarDropdownDisplay(this);
			}
			arrDropdowns[i].onmouseout = function() {
				calendarDropdownHide(this)
			}
		}
	}
	
	// calendars, entries and errors
	if (arrDivs.length > 0) {
		for (var i = 0; i < arrDivs.length; i++) {
		
			// get errors
			if (arrDivs[i].className && arrDivs[i].className.indexOf('calerror') != -1) {
				arrErrors.push(arrDivs[i]);
			}
		
			// get calendars
			if ( (arrDivs[i].className && arrDivs[i].className.indexOf('calendar') != -1) || (arrDivs[i].className && arrDivs[i].className.indexOf('catlist') != -1) ) {
				arrCalendars.push(arrDivs[i]);
			}
			
			// get & set events			
			if (arrDivs[i].className && arrDivs[i].className.indexOf('calevent') != -1) {
				if (arrDivs[i].getElementsByTagName('dl')[0]) {
					arrDivs[i].info = arrDivs[i].getElementsByTagName('dl')[0];
					
					arrDivs[i].onmouseover = function () {
						var entry 		= (this.info)? this: this.parentNode;
						var info 		= entry.info;	
						var overflowY	= 0;
						var overflowX	= 0;
						var scrollX 	= (document.documentElement.scrollTop == 0)?	document.body.scrollTop: document.documentElement.scrollTop;
						var scrollY 	= (document.documentElement.scrollLeft == 0)?	document.body.scrollLeft: document.documentElement.scrollLeft;
							
						// display infobox
						entry.style.zIndex = '1000';
					 	info.style.display = 'block';						
					
						// move infobox up if it would not be visible in its original spot
						overflowY = getCoords(info).y + info.offsetHeight - (scrollX + getWindowSizes().y);	
						if (overflowY > 0) {
							info.style.top = parseInt(info.style.top - overflowY) - 1 + 'px';
						}
						
						// move infobox left if it would not be visible in its original spot
						overflowX = getCoords(info).x + info.offsetWidth - (scrollY + getWindowSizes().x);	
						if (overflowX > 0) {
							info.style.left = parseInt(info.style.left - overflowX) - 1 + 'px';
						}
					}
					
					arrDivs[i].onmouseout = function () {
						var entry	= (this.info)? this: this.parentNode;
						var info	= entry.info;
						
						// hide infobox and reset temporary changes
						info.style.display	= 'none';
						entry.style.zIndex	= '0';
						info.style.top 		= null;
						info.style.left 	= null;
					}
					
					arrDivs[i].info.onmouseover	= arrDivs[i].onmouseover;
					arrDivs[i].info.onmouseout	= arrDivs[i].onmouseout;
				}
			}
		}
		
		// set errors
		if (arrErrors.length > 0) {
			for (e = 0; e < arrCalendars.length; e++) {
				for (var a = 0; a < arrCalendars[e].childNodes.length; a++) {
					if (arrCalendars[e].childNodes[a].style) arrCalendars[e].childNodes[a].style.opacity = '.7';
				}
			}
			for (e = 0; e < arrErrors.length; e++) {
				arrErrors[e].style.display = 'block';
				arrErrors[e].style.opacity = '1';				
			}
		}
	}
}

addLoadEvent(initCalendar);