function testOnlineOffice() {
	alert('OnlineOffice.js exists.');
}

function openWinAsModal() {
	if(navigator.appVersion.indexOf("MSIE") != -1)
		return true;
	else
		return false;

}

function isIEBellowVer7() {
	//Detect IE5.5+
	version = 7.0;
	if(navigator.appVersion.indexOf("MSIE") != -1) {
		temp = navigator.appVersion.split("MSIE");
		version = parseFloat(temp[1]);
	}
	
	if (version < 7.0)
		return true;
	else
		return false;
}

function IEBoxModelCalculate(size, diff) {
	if(isIEBellowVer7() == false) {
		var newSize = "";
		if(size.substr(size.length - 2, size.length) == "px") {
			newSize = size.substr(0, size.length - 2);
			newSize = (newSize - diff) + "px";
		}
		else
			newSize = size - diff;
		size = newSize;	
	}
	
	return size;
}

function changeBackgroundColor(obj, color) {
	obj.style.backgroundColor = color;
}

var dateObj = null;
function selectDate(obj, pageTitle, pageUrl) { 
	
	dateObj = obj;
	openEditAndReturn(pageUrl + '&date=' + document.getElementById(obj).value, pageTitle, '263px', '300px', 'selectDateReturn');
}

function selectDateReturn(val) {
	if(val != null)
		document.getElementById(dateObj).value = val;
}

		
function newSupportQuestion(sendData, helpText) {
	pageWidth = '708px';
	pageHeight = '423px'; //SP2 fix pageHeight = '403px';
	
	pageWidth = IEBoxModelCalculate(pageWidth, 7);
	pageHeight = IEBoxModelCalculate(pageHeight, 52);
	
	
	pageData = Array();
	pageData[0] = "";
	pageData[1] = "";
	pageData[2] = "";
	
	if(sendData) {
		pageUrl = document.location.href;
		pageTitle = document.title;
		pageRawData = "";
		
		for(i = 0; i < document.forms.length; i++) {
			pageRawData += "### Form " + (i+1) + " " + document.forms[i].name + " ###\n";
			for(j = 0; j < document.forms[i].elements.length; j++) {
				var item = document.forms[i].elements[j];
				if(item.name != "")
					pageRawData += item.name + ": " + item.value + "\n";
			}
		}
		
		pageData[0] = pageUrl;
		pageData[1] = pageTitle;
		pageData[2] = pageRawData;
		//alert(pageData);
	}
	
	window.supportData = pageData;
	
	rndNum = Math.random()
	rndNum = parseInt(rndNum * 100000);
	
	openNormal(ADMINPRO_INC + 'open_window.php?page_url=' + NEW_SUPPORT_QUESTION_URL + '&page_title=' + helpText + '&rnd_dummy=' + rndNum, new Array('width=' + pageWidth + ',', 'height=' + pageHeight + ','));
	
	return false;
}


function initOpenedWindows() {
	if(parent.window.openedWindows) {
		for ( keyVar in parent.window.openedWindows ) {
			var win = parent.window.openedWindows[keyVar];
			if(win.closed)
				continue;
			//var page = ;
			//t = "List3: ";
			//for ( k in page ) {
			//	t += k + ", ";
			//}
			if(win.document.getElementById("page").contentDocument.editForm.parentid.value)
				win.document.getElementById("page").contentDocument.editForm.parentid.value = document.editForm.list_id.value;
		}
	}
	else {
		parent.window.openedWindows = Array();
	}
}

function openEditAndReload(pageUrl, pageTitle, pageWidth, pageHeight, sendVal) {
	pageWidth = IEBoxModelCalculate(pageWidth, 7);
	pageHeight = IEBoxModelCalculate(pageHeight, 52);
		
	path = self.location.href.substring(0, self.location.href.lastIndexOf('.php') + 1);
	path = path.substring(path.indexOf('/', 8), path.lastIndexOf('/') + 1);
	
	if(pageUrl.substr(0, 1) == '/')
		path = "";

	rndNum = Math.random()
	rndNum = parseInt(rndNum * 100000);
	
	
	if(openWinAsModal()) {	//IE
		var retVal = null;
		retVal = openModal(ADMINPRO_INC + 'open_window.php?page_url=' + path + pageUrl + '&page_title=' + pageTitle + '&rnd_dummy=' + rndNum + '&closeFunc=0', sendVal, new Array('dialogWidth:' + pageWidth + ';', 'dialogHeight:' + pageHeight + ';'));
		if(retVal) {
			try {
				if(selectedTab && document.refreshForm.selecttab)
					document.refreshForm.selecttab.value = selectedTab;	
				}
			catch(ex) {}
			document.refreshForm.submit();
		}
		return null;
	}
	else {	//Firefox or Safari
		var winId = openNormal(ADMINPRO_INC + 'open_window.php?page_url=' + path + pageUrl + '&page_title=' + pageTitle + '&rnd_dummy=' + rndNum + '&closeFunc=reloadWin', new Array('width=' + pageWidth + ',', 'height=' + pageHeight + ','));
		winId.openerPageTitle = document.title;
		//winId.onunload = reloadWin;
		//winId.addEventListener("unload", reloadWin, false); 
		//winId.addEventListener("unclose", reloadWin, false); 
		if(parent.window.openedWindows)
			parent.window.openedWindows[path] = winId;
	}

}

window.reloadWin = reloadWin;

function reloadWin() {
	//alert(win.returnValue + " && " + win.openerPageTitle + " == " + document.title);
//	if(win.returnValue && win.openerPageTitle == document.title) {
		try {
			if(selectedTab && document.refreshForm.selecttab)
				document.refreshForm.selecttab.value = selectedTab;	
			}
		catch(ex) {}
		document.refreshForm.submit();
//	}
}



function openEditAndReturn(pageUrl, pageTitle, pageWidth, pageHeight, returnFunc, sendVal) {
	return openEditAndReturnSetModal(pageUrl, pageTitle, pageWidth, pageHeight, returnFunc, sendVal, openWinAsModal());
}

function openEditAndReturnSetModal(pageUrl, pageTitle, pageWidth, pageHeight, returnFunc, sendVal, openAsModal) {
	pageWidth = IEBoxModelCalculate(pageWidth, 7);
	pageHeight = IEBoxModelCalculate(pageHeight, 52);
		
	path = self.location.href.substring(0, self.location.href.lastIndexOf('.php') + 1);
	path = path.substring(path.indexOf('/', 8), path.lastIndexOf('/') + 1);
	
	if(pageUrl.substr(0, 1) == '/')
		path = "";

	rndNum = Math.random()
	rndNum = parseInt(rndNum * 100000);

	//Öppna fönsteret
	if(openAsModal) {	//IE
		var retVal = null;
		retVal = openModal(ADMINPRO_INC + 'open_window.php?page_url=' + path + pageUrl + '&page_title=' + pageTitle + '&rnd_dummy=' + rndNum + '&closeFunc=0', sendVal, new Array('dialogWidth:' + pageWidth + ';', 'dialogHeight:' + pageHeight + ';'));
		if(retVal) {
			eval(returnFunc + "(retVal)");
		}
		return;
	}
	else {	//Firefox or Safari
		if(parent.window.openedWindows[path] == null || parent.window.openedWindows[path].closed) {	
			var winId = openNormal(ADMINPRO_INC + 'open_window.php?page_url=' + path + pageUrl + '&page_title=' + pageTitle + '&rnd_dummy=' + rndNum + '&closeFunc=' + returnFunc, new Array('width=' + pageWidth + ',', 'height=' + pageHeight + ','));
			if(winId == null || winId == undefined) {
				alert("Kontrollera att du har godkänt admin.eject.se i din popup-blocker.");
				return;
			}
			winId.openerPageTitle = document.title;
//			winId.returnFunction = returnFunc;
//			winId.onunload = returnWin;
			parent.window.openedWindows[path] = winId;
		}
		else {
				parent.window.openedWindows[path].focus();
		}
	}	
}

function isCorrectWinToReturnTo(win) {
	if(win.returnValue && win.openerPageTitle == document.title) {
		return true;
	}
	else {
		return false;
	}
}

/*
function returnWin(win) {
	if(win.returnValue && win.openerPageTitle == document.title) {
		//alert(this.returnFunction);
		this.top.parent.window.focus();
		this.returnFunction(win.returnValue);
	}
}
*/

/*
This is a special version of the normal openModal() function used to open edit Windows in
AdminPRO. It open the page with the correct open_window.php file and the other settings.
The current page can also refreshed after the window as been closed.
*/
function openEditModal(pageUrl, pageTitle, pageWidth, pageHeight, returnVal, sendVal) {
	
	pageWidth = IEBoxModelCalculate(pageWidth, 7);
	pageHeight = IEBoxModelCalculate(pageHeight, 52);
		
	path = self.location.href.substring(0, self.location.href.lastIndexOf('.php') + 1);
	path = path.substring(path.indexOf('/', 8), path.lastIndexOf('/') + 1);
	//path = self.location.href.substring(self.location.href.indexOf('/', 8), self.location.href.lastIndexOf('/') + 1);
	
	if(pageUrl.substr(0, 1) == '/')
		path = "";

	rndNum = Math.random()
	rndNum = parseInt(rndNum * 100000);
	var retVal = null;
	retVal = openModal(ADMINPRO_INC + 'open_window.php?page_url=' + path + pageUrl + '&page_title=' + pageTitle + '&rnd_dummy=' + rndNum, sendVal, new Array('dialogWidth:' + pageWidth + ';', 'dialogHeight:' + pageHeight + ';'));
	if(retVal) {
		if(returnVal)
			return retVal;
		else {
			try{
				if(selectedTab && document.refreshForm.selecttab)
					document.refreshForm.selecttab.value = selectedTab;	
				}
			catch(ex) {}
			document.refreshForm.submit();
		}
	}
	return null;
}

function openModal(url, argumentsList, settingsList) {
//function openModal(url, width, height, handle, extendedSettings) {


/*

Syntax: returnValue openModal(url, argumentsList, settingsList);

url: should be a valid url.

argumentsList: should be in the form of an object-array.
	Every item in the argumentslist can be of any type of object.
	Tip: include a handle to the parent-window: Mostly the value this is used, but null is also ok if you won't use it.

settingsList: should be in the form of an object-array.
	Every item in the extendedSettingslist MUST be on the form; VALIDPROPERTYNAME:VALUE;

Valid measure-types
	cm, mm, in, pt, pc, px, em, ex

dialogHeight:sHeight
	Sets the height of the dialog window (see Remarks for default unit of measure).

dialogLeft:sXPos
	Sets the left position of the dialog window relative to the upper-left corner of the desktop.

dialogTop:sYPos
	Sets the top position of the dialog window relative to the upper-left corner of the desktop.

dialogWidth:sWidth
	Sets the width of the dialog window (see Remarks for default unit of measure).

center:{ yes | no | 1 | 0 | on | off }
	Specifies whether to center the dialog window within the desktop. The default is yes.

dialogHide:{ yes | no | 1 | 0 | on | off }
	Specifies whether the dialog window is hidden when printing or using print preview. This feature is only 
	available when a dialog box is opened from a trusted application. The default is no.

edge:{ sunken | raised }
	Specifies the edge style of the dialog window. The default is raised.

help:{ yes | no | 1 | 0 | on | off }
	Specifies whether the dialog window displays the context-sensitive Help icon. The default is yes.

resizable:{ yes | no | 1 | 0 | on | off }
	Specifies whether the dialog window has fixed dimensions. The default is no.

scroll:{ yes | no | 1 | 0 | on | off }
	Specifies whether the dialog window displays scrollbars. The default is yes.

status:{ yes | no | 1 | 0 | on | off }
	Specifies whether the dialog window displays a status bar. The default is yes for untrusted dialog 
	windows and no for trusted dialog windows.

unadorned:{ yes | no | 1 | 0 | on | off }
	Specifies whether the dialog window displays the border window chrome. This feature is only available 
	when a dialog box is opened from a trusted application. The default is no.
*/

	// Set the default values, whenever the user specifies the same 
	// name as on of the following the default-value is overridden.
	var defaultValues = new Array('scroll:0;', 'center:1;', 'status:1;', 'help:0;', 'edge:raised;', 'unadorned:0;', 'resizable:0;');

	// regular expression on the form 'NAME:VALUE;'
	var reg = /^[a-z]+:[a-z,0-9]+;$/i
	
	for(var i=0; i<settingsList.length; i++) {
		// Is it in a valid format?
		if(reg.test(settingsList[i])) {
			var index = settingsList[i].indexOf(':');
			var settingName = settingsList[i].substring(0, index);

			for(var j=0; j<defaultValues.length; j++) {
				index = defaultValues[j].indexOf(':');
				var defaultValueName = defaultValues[j].substring(0, index);
				
				if(settingName == defaultValueName) {
					defaultValues[j] = ''; // Remove it because we have specified a value and don't need a default-value.
					break;
				}
			}
		} else
			settingsList[i] = ''; // Remove it since it's invalid.
	}
	
	// Build the settings-string.
	var settings = '';
	for(var i=0; i<settingsList.length; i++) {
		if(settingsList[i] != '')
			settings += settingsList[i];
	}
	for(var i=0; i<defaultValues.length; i++) {
		if(defaultValues[i] != '')
			settings += defaultValues[i];
	}

	if(argumentsList == null)
		argumentsList = new Object;
	
	var winObj = null;
	if(window.dialogArguments && window.dialogArguments.window) {
		winObj = window.dialogArguments.window;
	}
	else
		winObj = window;
	
	argumentsList.window = winObj;	
	
	return window.showModalDialog(url, argumentsList, settings);
}


function openNormal(url, settingsList) {
	var settings = '';

	if(settingsList) {
		// regular expression on the form 'NAME=VALUE,'
		var reg = /^[a-z]+=[a-z,0-9]+,$/i
		
		
		var numReg = /^.+=(\d*)/;
		//Center it on the parent window
		var width = 300;
	  var height = 300;
	 	for(var i=0; i < settingsList.length; i++) {
			// Is it in a valid format?
			if(reg.test(settingsList[i])) {
				index = settingsList[i].indexOf('=');
				settingName = settingsList[i].substring(0, index);
				if(settingName == 'height') {
					regMatch = numReg.exec(settingsList[i]);
					height = regMatch[1];
				}
				else if(settingName == 'width') {
					regMatch = numReg.exec(settingsList[i]);
					width = regMatch[1];
				}
			}
		}
		
		var browseWidth = 0;
		var browseHeight = 0;
		var browseLeft = 0;
		var browseTop = 0;
		if(document.layers || (document.getElementById && !document.all)) { 
			browseWidth = window.outerWidth;
			browseHeight = window.outerHeight;
			browseLeft = window.screenX;
			browseTop = window.screenY;
		}
		else if(document.all) {
			browseWidth = document.body.clientWidth;
			browseHeight = document.body.clientHeight;
			browseLeft = window.screenLeft;
			browseTop = window.screenTop;
			
		}
		
	  var left = browseLeft + (browseWidth / 2) - (width / 2);
	  var top = browseTop + (browseHeight / 2) - (height / 2);
		//alert(left);
		
		// Set the default values, whenever the user specifies the same 
		// name as on of the following the default-value is overridden.
		var defaultValues = new Array('left=' + left + ',', 'top=' + top + ',', 'scroll=0,', 'center=yes,', 'status=yes,', 'help=0,', 'edge=raised,', 'unadorned=0,', 'resizable=0');
	
		
		for(var i=0; i < settingsList.length; i++) {
			// Is it in a valid format?
			if(reg.test(settingsList[i])) {
				index = settingsList[i].indexOf('=');
				settingName = settingsList[i].substring(0, index);
	
				for(var j=0; j<defaultValues.length; j++) {
					index = defaultValues[j].indexOf('=');
					var defaultValueName = defaultValues[j].substring(0, index);
					
					if(settingName == defaultValueName) {
						defaultValues[j] = ''; // Remove it because we have specified a value and don't need a default-value.
						break;
					}
				}
			} else
				settingsList[i] = ''; // Remove it since it's invalid.
		}
		
		// Build the settings-string.
		for(var i=0; i<settingsList.length; i++) {
			if(settingsList[i] != '')
				settings += settingsList[i];
		}
		for(var i=0; i<defaultValues.length; i++) {
			if(defaultValues[i] != '')
				settings += defaultValues[i];
		}
	}

	//TODO: This makes it NOT possible to return values in IE when openNormal is used!
	//But it fix the session lost bug...
	var winObj = null;
	if(window.dialogArguments && window.dialogArguments.window) {
		winObj = window.dialogArguments.window;
	}
	else
		winObj = window;

	return winObj.open(url, "", settings);	
}


//function openModal(url, width, height) {
//	var settings = "dialogWidth:" + width + "px; dialogHeight:" + height + "px; center:yes; status:no; help:no; edge:raised";
//	var retVal = window.showModalDialog(url, null, settings);
//	return retVal;
//}

function oldOpenWindow(url, id, width, height, focus, parentWindow, allowDuplicates, extendedSettings) {
	if(focus == null)
		focus = true;

	var settings = "height=" + height + ",width=" + width;
	if(extendedSettings != null && extendedSettings.length > 0)
		settings += "," + extendedSettings;

	if(eval(id) == null || eval(id + ".closed;")) {
		// There are no window with the specified id open at the moment - so just create a new one and set it in focus!	
		eval(id + "=window.open('" + url + "','" + id + "','" + settings + "')");
		eval(id + ".focus()");
	} else if(focus) {
		eval(id + ".focus()");
	} else {
		eval(id + "=window.open('" + url + "','" + id + "','" + settings + "')");
		eval(id + ".focus()");
	}
	eval(id + ".parentWindow=" + parentWindow);
}

//
// Opens a window and returns the variable from the window in 'id'.
//
function openWindow(url, id, width, height, focus, parent, allowDuplicates, extendedSettings) {
	if(focus == null)
		focus = true;

//	var settings = "height=" + height + ",width=" + width + ",status=no,toolbar=no,menubar=no,location=no,titlebar=no,resizable=no,fullscreen=no,directories=no,channelmode=no";
	var settings = "height=" + height + ",width=" + width;
	if(extendedSettings != null && extendedSettings.length > 0)
		settings += "," + extendedSettings;

	if(id != null)
		alert('ID=' + id);
	else
		alert('id is null!');

	alert('1');
	eval(id);
	alert('2');

	if(eval(id + ".closed;"))
		alert('tjoho!!!');
	else
		alert('froho!!!');

	if(eval(id) == null || eval(id + ".closed;")) {
		// There are no window with the specified id open at the moment - so just create a new one and set it in focus!	
		eval(id + "=window.open('" + url + "','" + id + "','" + settings + "')");
		eval(id + ".focus()");
	} else if(focus) {
		eval(id + ".focus()");
	} else {
		eval(id + "=window.open('" + url + "','" + id + "','" + settings + "')");
		eval(id + ".focus()");
	}
	eval(id + ".parent=" + parent);
	
	//var window_dim = "width:" + width + "px; height:" + height + "px; center:yes; status:no; help:no; edge:raised";
//	var retWindow = window.open(url, null, settings);
//	return retWindow;
}

//
// Checks all checkboxes on the given form with the value
// specified.
//
// Can be used to either check all checkboxes or uncheck them.
//
function checkAllOnForm(formObj, checkValue) {
	for(var i=0; i<formObj.length; i++) {
		fieldObj = formObj.elements[i];
		if(fieldObj.type == 'checkbox')
			fieldObj.checked = checkValue;
	}
}

//
// Checks all checkboxes on the given form with the specified id 
// with the value specified.
//
// Can be used to either check all checkboxes or uncheck them.
//
function checkAllWithName(formObj, name, checkValue) {
	for(var i=0; i<formObj.length; i++) {
		fieldObj = formObj.elements[i];
		if(fieldObj.type == 'checkbox') {
			if(fieldObj.name.lastIndexOf(name) > -1 && fieldObj.name.lastIndexOf(name) == (fieldObj.name.length - name.length))
				fieldObj.checked = checkValue;
		}
	}
}

function getElementEndingWithName(findName) {
	var form = document.forms[0];
	for(var i=0; i<form.length; i++) {
		var fieldObj = form.elements[i];
		if(fieldObj.name.lastIndexOf(findName) > -1 && fieldObj.name.lastIndexOf(findName) == (fieldObj.name.length - findName.length)) {
			return fieldObj;
		}
	}
	
	return null;
}

// START
// AddTask-specific.
//

function changeDivVisibilityByListValues(listName) {
	var listObj = document.getElementById(listName);

	// Hide all layers.
	for(var i=0; i<listObj.options.length; i++) {
		var id = listObj.options[i].value;
		if(id != 0)
			document.getElementById(id).style.display = "none";
	}

	// Show the selected layer.
	var id = listObj.options[listObj.selectedIndex].value;
	if(id != 0)
		document.getElementById(id).style.display = "block";
}

//
// This function toggles the radiobutton choices on | off.
//
function toggleRangeFirstTime() {
	var form = document.forms[0];

	form.day_repeatNumberOfTimesTextbox.disabled = true;
	form.day_stopDateTextBox.disabled = true;
	form.week_repeatNumberOfTimesTextbox.disabled = true;
	form.week_stopDateTextBox.disabled = true;
	form.month_repeatNumberOfTimesTextbox.disabled = true;
	form.month_stopDateTextBox.disabled = true;
	form.year_repeatNumberOfTimesTextbox.disabled = true;
	form.year_stopDateTextBox.disabled = true;
}

function toggleRange() {
	var form = document.forms[0];
	
	if(form.recurrenceTypeList.selectedIndex == 1) {

		form.day_repeatNumberOfTimesTextbox.disabled = false;
		form.day_stopDateTextBox.disabled = false;
		if(form.day_noStopDateRadio.checked) {
			form.day_repeatNumberOfTimesTextbox.disabled = true;
			form.day_stopDateTextBox.disabled = true;	
		} else if(form.day_repeatNumberOfTimesRadio.checked)
			form.day_stopDateTextBox.disabled = true;	
		else if(form.day_stopDateRadio.checked)
			form.day_repeatNumberOfTimesTextbox.disabled = true;

	} else if(form.recurrenceTypeList.selectedIndex == 2) {

		form.week_repeatNumberOfTimesTextbox.disabled = false;
		form.week_stopDateTextBox.disabled = false;
		if(form.week_noStopDateRadio.checked) {
			form.week_repeatNumberOfTimesTextbox.disabled = true;
			form.week_stopDateTextBox.disabled = true;	
		} else if(form.week_repeatNumberOfTimesRadio.checked)
			form.week_stopDateTextBox.disabled = true;	
		else if(form.week_stopDateRadio.checked)
			form.week_repeatNumberOfTimesTextbox.disabled = true;

	} else if(form.recurrenceTypeList.selectedIndex == 3) {

		form.month_repeatNumberOfTimesTextbox.disabled = false;
		form.month_stopDateTextBox.disabled = false;
		if(form.month_noStopDateRadio.checked) {
			form.month_repeatNumberOfTimesTextbox.disabled = true;
			form.month_stopDateTextBox.disabled = true;	
		} else if(form.month_repeatNumberOfTimesRadio.checked)
			form.month_stopDateTextBox.disabled = true;	
		else if(form.month_stopDateRadio.checked)
			form.month_repeatNumberOfTimesTextbox.disabled = true;

	} else if(form.recurrenceTypeList.selectedIndex == 4) {

		form.year_repeatNumberOfTimesTextbox.disabled = false;
		form.year_stopDateTextBox.disabled = false;
		if(form.year_noStopDateRadio.checked) {
			form.year_repeatNumberOfTimesTextbox.disabled = true;
			form.year_stopDateTextBox.disabled = true;	
		} else if(form.year_repeatNumberOfTimesRadio.checked)
			form.year_stopDateTextBox.disabled = true;
		else if(form.year_stopDateRadio.checked)
			form.year_repeatNumberOfTimesTextbox.disabled = true;

	}
}
// STOP
// AddTask-specific.
//


/** Round a floating point number to the specified number
*  of decimal places and return new rounded value.
*/
function roundFloat (f, places) {
	var shift = Math.pow(10, places);
	return Math.round(f * shift)/shift;
}

/** Convert a float to string rounded to the specified number
*  of decimal places.
*/
function formatFloat (f, places) {
	var val = roundFloat(f, places).toString();
	var dot = val.indexOf(".");
	if (dot < 0) {
		val += ".";
		dot = val.length-1;
	}
	var pad = places - ((val.length-1) - dot);
	for (var i=0; i < pad; ++i) {
		val += "0";
	}
	return val;
}

