// Date feature 
/////////////////////////////////////////////////////////////////////

function makeArray(len)
{
  for (var i = 0; i < len; i++)
    this[i] = null;
  this.length = len;
}

function WriteDate()
{
  document.write(daynames[day] + "en den " + date + " " + monthnames[month] + " " + year);
}

var daynames = new makeArray(7);
daynames[0] = "Söndag"; daynames[1] = "Måndag"; daynames[2] = "Tisdag"; daynames[3] = "Onsdag";
daynames[4] = "Torsdag"; daynames[5] = "Fredag"; daynames[6] = "Lördag";

var monthnames = new makeArray(12);
monthnames[0] = "januari"; monthnames[1] = "februari"; monthnames[2] = "mars"; monthnames[3] = "april";
monthnames[4] = "maj"; monthnames[5] = "juni"; monthnames[6] = "juli"; monthnames[7] = "augusti";
monthnames[8] = "september"; monthnames[9] = "oktober"; monthnames[10] = "november"; monthnames[11] = "december";

var now = new Date();
var day = now.getDay();
var month = now.getMonth();
var year = now.getYear();
var date = now.getDate();
//var hour = now.getHours();
//var minutes = now.getMinutes();

if (year < 2000)
{
  year += 1900;
}

//if (hour < 10)
//  hour = "0" + hour;

//if (minutes < 10)
//  minutes = "0" + minutes;


function FocusEvent(obj)
{
  if (obj.value == "Personalnummer" || obj.value == "Lösenord")
    obj.value = "";
}

function ConfirmDelete()
{
	if (confirm('Du har valt att ta bort denna artikel.\nTryck OK för att fortsätta.'))
		return confirm('Vill du verkligen ta bort denna artikel?');
	else
		return false;
}

function ConfirmFlag()
{
  return confirm('Aktiveringsmekanismen används när man vill kunna\nvälja vilken eller vilka texter som ska visas.\n\nEtt plustecken = aktiverad.\n\nVill du aktivera/inaktivera denna text?'); 
}

function ForgotMail()
{
  bRet = false;
  
  pid = document.getElementById("pid");
  if (pid.value != "" && pid.value != "Personalnummer")
    bRet = confirm("Nu kommer ett mail med lösenord skickas till den e-postadress som\när kopplad till det angivna personalnumret.\n\nÄr detta ok?");
  else
    alert("Ange ditt personalnummer och klicka på \"Glömt lösenordet?\" igen\nså skickas ditt lösenord till den e-postadress du uppgivit.");
    
  if (bRet == true)
  {
    document.getElementById("action").value = "send_pswd";
    document.login_form.submit();
  }
}

function ShowKorplan()
{
	//TODO: Firefox can not deal with åäö in the filename
	obj = document.getElementById("Box4");
	if (obj && obj.selectedIndex != -1)
	{
		wdw = window.open(obj.options[obj.selectedIndex].value, "", "toolbar=no,width=600,height=700,resizable=yes");
	}
	else
		alert("Alla steg måste väljas!");
}

function ReallyDeleteUser()
{
	return confirm("Vill du verkligen radera denna användare?\nOperationen går inte att ångra.\n\nFortfarande säker?");
	
}

// Functions for changing font size dynamically

FSList = null;

function initFSList()
{
	FSList = listFSNodes('txtSizable', window.document);
}

//window.onload = initFSList;

function testVar(objToTest)
{
	return (objToTest == null || objToTest == undefined || objToTest == false)? false : true;
}

// Change the font-size by steps of delta of all tags that are referring to className
function changeFSByClassName(className, delta)
{
	if (!testVar(FSList))
	{
		FSList = listFSNodes(className, window.document);
	}

	for (var i = 0; i < FSList.length; i++)
	{
		var fs;

		if (testVar(FSList[i].style.fontSize))
			fs = parseInt(FSList[i].style.fontSize);
		else
			fs = parseInt(getFSByClassRef(FSList[i].className));

		if (!isNaN(fs))
		{
			if (testVar(FSList[i].getAttribute('fontSizeLimits')))
			{
				var limits = FSList[i].getAttribute('fontSizeLimits').split(",");
				var minFS = parseInt(limits[0]);
				if (minFS < 0)
					minFS = 0;

				var maxFS = parseInt(limits[1]);

				if (!isNaN(maxFS) && !isNaN(maxFS))
				{
					if (fs + delta > maxFS)
						fs = maxFS;
					else if (fs + delta < minFS)
						fs = minFS;
					else
						fs += delta;

					FSList[i].style.fontSize = fs + 'px';
				}
			}
			else
			{
				fs += delta;

				if (fs < 0)
					fs = 0;

				FSList[i].style.fontSize = fs + 'px';
			}
		}
	}
}

// get the lastly defined and referred-to font-size in the css style sheets
function getFSByClassRef(classRefs)
{
	var sheets = document.styleSheets;
	var cssFS = "";

	if (sheets.length > 0)
	{
		var classRefArr = classRefs.split(" ");

		for (var x = 0; x < sheets.length; x++)
		{
			var rules = document.all ? sheets[x].rules : sheets[x].cssRules;

			if (rules.length > 0)
			{
				for (var y = 0; y < rules.length; y++)
				{
					for (var d = 0; d < classRefArr.length; d++)
					{
						if (rules[y].selectorText.indexOf(classRefArr[d]) >= 0)
						{
							var fs = rules[y].style.fontSize;

							if (testVar(fs))
								cssFS = fs;
						}
					}
				}
			}
		}
	}

	return cssFS;
}

// return next node in document order
function nextFSNode(node)
{
	if (!node) return null;

	if (node.firstChild)
		return node.firstChild;
	else
		return nextFSWide(node);
}

// helper function for nextNode()
function nextFSWide(node)
{
	if (!node) return null;

	if (node.nextSibling)
		return node.nextSibling;
	else
		return nextFSWide(node.parentNode);
}

// return previous node in document order
function prevFSNode(node)
{
	if (!node) return null;

	if (node.previousSibling)
		return previousFSDeep(node.previousSibling);

	return node.parentNode;
}

// helper function for prevNode()
function previousFSDeep(node)
{
	if (!node) return null;

	while (node.childNodes.length)
			node = node.lastChild;

	return node;
}

// return an Array of all nodes, starting at startNode and
// continuing through the rest of the DOM tree
function listFSNodes(className, startNode)
{
	var list = new Array();
	var pattern = new RegExp("(^|\\s)"+className+"(\\s|$)");

	var node = startNode;

	while(node)
	{
			if (pattern.test(node.className))
				list.push(node);

			node = nextFSNode(node);
	}

	return list;
}

function ShowHideSentFormEntries(thisObj, id)
{
	if (thisObj.innerHTML == '(visa)')
	{
		document.getElementById(id).style.display = '';
		thisObj.innerHTML = '(dölj)';
	}
	else
	{
		document.getElementById(id).style.display = 'none';
		thisObj.innerHTML = '(visa)';
	}
}

function ShowHideFormInstructions(thisObj, id)
{
	if (thisObj.innerHTML == '(visa instruktioner)')
	{
		document.getElementById(id).style.display = '';
		thisObj.innerHTML = '(dölj instruktioner)';
	}
	else
	{
		document.getElementById(id).style.display = 'none';
		thisObj.innerHTML = '(visa instruktioner)';
	}
}
