function OnLoad(){

	//alert('Loaded');
	InitCheckboxes();
	InitDropDowns();


	if (window.location.hash)
	{
		//alert(window.location.hash+'\r\n'+location.href);
		if (window.location.hash.substr(1,4) == 'Info')
			showPopUp(unescape(window.location.hash.substr(6)), false);
		else if (window.location.hash.substr(1,10) == 'StaticInfo')
			showPopUp(unescape(window.location.hash.substr(12)), true);
			//alert(window.location.hash.substr(6));
		//location.href = location.href.substr(0, location.href.length-location.hash-length);
	}
}


function showPopUp(text, staticPopUp)
{
	//var staticPopUp = false;
	if (text.substr(0,7) == 'Static:')
	{
		if (text.substr(7,1) == '[' || text.substr(7,1) == '{')
		{
			staticPopUp = true;
			arr = eval('('+text.substr(7)+')');
			text = '';
			for (var i=0;i<arr.length;i++)
				text += arr[i].text + '<br>';
		}
		else
		{
			text = text.substr(7);
		}
	}

	obj = document.getElementById('popupdiv');
	obj.innerHTML = text;
	obj.style.display = (obj.style.display == 'none') ? '' : 'none';
	obj.onclick = function(){showPopUp('');}

	if (obj.style.display != 'none')
		placePopUp(obj, staticPopUp);
}
function placePopUp(obj, staticPopUp)
{
	//obj = document.getElementById(obj);
	/*
	if (document.documentElement)
	{
		theLeft = document.documentElement.scrollLeft;
		theTop = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		theLeft = document.body.scrollLeft;
		theTop = document.body.scrollTop;
	}
	//theLeft += x;
	//theTop += y;
	*/
	//subMenu = document.getElementById('submenu');
	//theLeft = getPageOffsetLeft(subMenu)+((subMenu.offsetWidth / 3) - (obj.offsetWidth / 2));
	topDiv = document.getElementById('top');
	theLeft = getPageOffsetLeft(topDiv)+1;
	theTop = getPageOffsetTop(topDiv)+1;
	
	
	obj.style.left = theLeft + 'px' ;
	obj.style.top = theTop + 'px' ;
	//setTimeout("placeIt('layer1')",500);
	if (!staticPopUp)
		setTimeout("showPopUp('')",4000);
}

function InitCheckboxes(){
	var checkboxes = GetElementsByClass("checkbox");
	
	var testChecked = new RegExp("(^|\\s)checked(\\s|$)")
	var testDisabled = new RegExp("(^|\\s)disabled(\\s|$)")
	var inpt;
	
	for (var i=0;i<checkboxes.length;i++)
	{
		inpt = document.createElement('input');
		inpt.type = 'checkbox';
		inpt.name = checkboxes[i].id;
		inpt.id = 'input'+checkboxes[i].id;
		inpt.style.display = 'none';
		inpt.checked = testChecked.test(checkboxes[i].className);
		checkboxes[i].appendChild(inpt);
		checkboxes[i].oldOnClick = checkboxes[i].onclick;
		if (!testDisabled.test(checkboxes[i].className))
		{
			checkboxes[i].onclick = function(){
									//var inpt = document.getElementById('input'+this.id);
									var inpt = this.getElementsByTagName('INPUT');
									inpt = inpt[0];
									if (inpt.checked == true)
									{
										inpt.checked = false;
										this.className = 'checkbox';
									}
									else
									{
										inpt.checked = true;
										this.className = 'checkbox checked';
									}
									if (this.oldOnClick != null)
										this.oldOnClick();
								}
		}
		checkboxes[i].onselectstart = function(){return false;};
		checkboxes[i].onmousedown = function(){return false;};

	}
		
}

function InitDropDowns(){
	var dropdowns = GetElementsByClass("dropDown");
	var button;
	var drop;
	
	for (var i=0;i<dropdowns.length;i++)
	{
		button = GetElementsByClass("button", dropdowns[i])[0];
		button.onclick = function(){
								var drop = GetElementsByClass("drop", this.parentNode.parentNode)[0];
								if (drop.style.display == 'none')
									drop.style.display = '';
								else
									drop.style.display = 'none';
							}
		button.onselectstart = function(){return false;};
		button.onmousedown = function(){return false;};
		
		drop = GetElementsByClass("drop", dropdowns[i])[0];
		drop.style.display = 'none';
		
	}
}

function GetElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function newsAllDayClick(el){
	var time = document.getElementById('edit_news_time');
	var endTime = document.getElementById('edit_news_endTime');
	
	var otherChecked;
	var thisChecked;
	
	if (el.id == 'mainPage')
		otherChecked = isChecked(document.getElementById('allDay'));
	else
		otherChecked = isChecked(document.getElementById('mainPage'));
	
	thisChecked = isChecked(el);
	
	if (thisChecked == true || otherChecked == true)
	{
		time.style.display = 'none';
		endTime.style.display = 'none';
	}
	else
	{
		time.style.display = '';
		endTime.style.display = '';
	}
}

function isChecked(el)
{
	var checkboxInput = GetElementsByClass('', el, 'INPUT');
	if (checkboxInput && checkboxInput.length > 0)
		return (checkboxInput[0].checked == true);
	else
		return false;
}

function dateInputHelp(e)
{
	var code;
	var targ;

	if (!e) var e = window.event;
	
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;


	if (code != 8 && code != 46)
	{
		var text = targ.value.trim();
	
		if (text.length == 4)
		{
			targ.value = text + '-';
		}
		else if (text.length == 2)
		{
			var year = parseInt(text, 10);
			if (year != 19 && year != 20)
				targ.value = '20' + text;
		}
		else if (text.length == 7)
		{
			targ.value = text + '-';
		}
		else if (text.length == 6)
		{
			var nbr = parseInt(text.substr(5, 1), 10);
			if (nbr > 1)
				targ.value = text.substr(0, 5) + '0' + nbr + '-';
		}
	}
}

function timeInputHelp(e)
{
	var code;
	var targ;

	if (!e) var e = window.event;
	
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;


	if (code != 8 && code != 46)
	{
		var text = targ.value.trim();
	
		if (text.length == 2)
		{
			targ.value = text + ':';
		}
		else if (text.length == 1)
		{
			var hour = parseInt(text, 10);
			if (hour > 2)
				targ.value = '0' + text + ':';
		}
	}
}




/* Edit User */

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(theSelFrom, theSelTo)
{

	var selLength = theSelFrom.length;
	var selectedOpt = new Array();
	var selectedCount = 0;

	var i;

	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--)
	{
		if(theSelFrom.options[i].selected)
		{
			selectedOpt[selectedCount] = theSelFrom.options[i];
			deleteOption(theSelFrom, i);
			selectedCount++;
		}
	}

	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--)
	{
		theSelTo.options[theSelTo.options.length] = selectedOpt[i];
		theSelTo.options[theSelTo.options.length-1].selected = false;
	}

	sortList(theSelTo);
  
	return false;
}

function moveAllOptions(theSelFrom, theSelTo)
{

	if (confirm('Flytta alla?'))
	{
		var selLength = theSelFrom.length;
		var selectedOpt = new Array();
		var selectedCount = 0;

		var i;
  
		// Find the selected Options in reverse order
		// and delete them from the 'from' Select.
		for(i=selLength-1; i>=0; i--)
		{
			selectedOpt[selectedCount] = theSelFrom.options[i];
			deleteOption(theSelFrom, i);
			selectedCount++;
		}

		// Add the selected text/values in reverse order.
		// This will add the Options to the 'to' Select
		// in the same order as they were in the 'from' Select.
		for(i=selectedCount-1; i>=0; i--)
		{
			theSelTo.options[theSelTo.options.length] = selectedOpt[i];
		}

		sortList(theSelTo);
		//if(NS4) history.go(0);
	}
	return false;
}

function compareText(opt1, opt2)
{
	// not case sensitive
	return opt1.text.toLowerCase() < opt2.text.toLowerCase() ? -1 :
		   opt1.text.toLowerCase() > opt2.text.toLowerCase() ? 1 : 0;
} 

function sortList(lb)
{
	arrOptions = new Array();

	for(i=0; i<lb.length; i++)
	{
		arrOptions[i] = lb.options[i];
	}

	arrOptions.sort(compareText);

	lb.options.length = 0; 
	for (i=0;i<arrOptions.length;i++)
	{
		lb.options[i] = arrOptions[i];
	}
}

function selectAll(selectBox,selectAll)
{
	// have we been passed an ID
	if (typeof selectBox == "string") {
		selectBox = document.getElementById(selectBox);
	}

	for (var i = 0; i < selectBox.options.length; i++) {
		selectBox.options[i].selected = selectAll;
	}
}



/* SelectImage */

function selectImage(td, imageName, imageInfo)
{
	var tbody = td.parentNode.parentNode;
	
	for (var i=0;i<tbody.childNodes.length;i++)
		for (var t=0;t<tbody.childNodes[i].childNodes.length;t++)
			tbody.childNodes[i].childNodes[t].className = '';
			//if (tbody.childNodes[i].childNodes[t].className == 'selected')
				
	td.className = 'selected';
	var arr = imageInfo.split(',');
	document.getElementById('imageInfo').innerHTML = arr[0]+" x "+arr[1];
	document.getElementById('imageInfo').title = imageName;
}


/* FUNCTIONS */

function getPageOffsetLeft(el){
	if (getStyle(el,'position') != 'absolute' && getStyle(el,'position') != 'relative')
		return el.offsetLeft + (el.offsetParent ? getPageOffsetLeft(el.offsetParent) : 0);
	else
		return 0;
}

function getPageOffsetTop(el){
	if (getStyle(el,'position') != 'absolute' && getStyle(el,'position') != 'relative'){
		return el.offsetTop + (el.offsetParent ? getPageOffsetTop(el.offsetParent) : 0);
	}else{
		return 0;
	}
}

function getStyle(el,styleProp){
	if (el.currentStyle)
		return el.currentStyle[styleProp];
	else if (document.defaultView.getComputedStyle)
		return document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
}
