﻿

	/* ------------------------------------------------	*
	 *  Common JavaScriptfile for ProjectX website		*
	 * ------------------------------------------------ *
	 *  Created: 2007-10-20								*
	 *  Author: Thomas Atterberg [thomas@version1.se]	*
	 * ------------------------------------------------ *
	 *  The use of this file or it's functions is		*
	 *  restricted. Pleas contact the author befor		*
	 *  using any of them in out own website			*
	 * ------------------------------------------------ */

function OpenCenterdPopup(aLink, windowWidth, windowHeight, aPageName)
	 {
		var left = GetWindowPositionLeft();
		var top = GetWindowPositionTop();

		left = (left > 0 ? left : 0) + (GetViewportWidth() / 2) - (windowWidth / 2);
		top = (top > 0 ? top : 0) + (GetViewportHeight() / 2) - (windowHeight / 2);

		window.open(aLink, aPageName, 'width=' + windowWidth + ',height=' + windowHeight + ',left=' + left + ',top=' + top);
	}

	function GetWindowPositionLeft() {
		if (document.all)
			return window.screenLeft;
		else
			return window.screenX;
	}

	function GetWindowPositionTop() {
		if (document.all)
			return window.screenTop;
		else
			return window.screenY;
	}

	function GetViewportWidth() {

		var viewportwidth;

		if (typeof window.innerWidth != 'undefined')
			viewportwidth = window.innerWidth;
		else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
			viewportwidth = document.documentElement.clientWidth;
		else
			viewportwidth = document.getElementsByTagName('body')[0].clientWidth;

		return viewportwidth;
	}

	function GetViewportHeight() {

		var viewportheight;

		if (typeof window.innerWidth != 'undefined')
			viewportheight = window.innerHeight;
		else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight != 'undefined' && document.documentElement.clientHeight != 0)
			viewportheight = document.documentElement.clientHeight;
		else
			viewportheight = document.getElementsByTagName('body')[0].clientHeight;

		return viewportheight;
	}
	
	function ShowEditPanel(aUrl, aWidth, aHeight, aUpdate)
	{
		var blocker		 = document.getElementById('blocker');
		var iframe		 = document.getElementById('popupeditor');
			
		var windowWidth;
		var windowHeight;

		if(navigator.userAgent.indexOf("MSIE") > 0)
		{
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		else
		{
			windowWidth = window.innerWidth;
			windowHeight = window.innerHeight;
		}
		
		if(windowWidth+10 > aWidth && windowHeight+10 > aHeight)
		{
			var left = (windowWidth - aWidth) / 2;
			var top  = ((windowHeight - aHeight) / 2) + 10;
		
			blocker.style.display = "";
		
			iframe.style.display  = "";
			iframe.style.width	  = aWidth;
			iframe.style.height	  = aHeight;
			iframe.style.left	  = left + "px";
			iframe.style.top	  = top + "px";
			iframe.src			  = aUrl;
		}
		else
		{
			if(aUrl.lastIndexOf('?') == -1)
				aUrl += "?open=popup";
			else
				aUrl += "&open=popup";
			
			OpenCenterdPopup(aUrl,aWidth,aHeight,'popup');
		}
	}
	
	function CloseEditWindow(content)
	{
		var popupeditor = document.getElementById('popupeditor');
		
	//	document.getElementById(document.getElementById('contentDiv').value).style.display = '';
	
		document.getElementById('blocker').style.display = 'none';
		
		popupeditor.style.display = 'none';
		popupeditor.src = 'about:blank';
		
	//	var editName = document.getElementById('contentDiv').value;
	//	var editor = document.getElementById(editName);
		
	//	if(content)
	//		editor.innerHTML = content;
	
		//PageMethods.UpdateList(AlertSuccess);
	}
	
	function AlertSuccess()
	{
		alert('Success');
	}
	
	function SetHeight(height)
	{
		var iframe		 = document.getElementById('popupeditor');
		iframe.style.height	  = height + "px";
	}
	
	
	function loadXMLDoc(url)
	{
		// branch for native XMLHttpRequest object
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send(null);
			// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				req.onreadystatechange = processReqChange;
				req.open("GET", url, true);
				req.send();
			}
		}
	}
	
	function processReqChange()
	{
		// only if req shows "complete"
		if (req.readyState == 4) {
			// only if "OK"
			if (req.status == 200) {
				document.getElementById("content").innerHTML = req.responseText;
			} else {
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
			}
		}
	}

	function moveBetweenList(listFrom, listTo, remove, hiddenValues, selectList) {

		var listFromSelected = document.getElementById(listFrom).selectedIndex;

		if (listFromSelected > -1) {
			var listToLength = document.getElementById(listTo).length;

			document.getElementById(listTo).options[listToLength] = new Option(document.getElementById(listFrom).options[listFromSelected].text, document.getElementById(listFrom).options[listFromSelected].value);
			document.getElementById(listFrom).options[listFromSelected] = null;

			listToString(document.getElementById(selectList), document.getElementById(hiddenValues))
		}
	}

	function listToString(fromList, toString) {
		var objString = '';

		for (var i = 0; i < fromList.length; i++) {
			if (i == 0)
				objString += fromList.options[i].value;
			else
				objString += '|' + fromList.options[i].value;
		}

		toString.value = objString;
	}

	var needToConfirm = false;
	var customMessage = 'Du har inte sparat dina ändringar.';

	window.onbeforeunload = function(evt) {

		var message = customMessage;
		
		if (needToConfirm) {
			if (typeof evt == 'undefined') {
				evt = window.event;
			}
			if (evt) {
				evt.returnValue = message;
			}
			return message;
		}
	}
