window.onload = init;


function init() 
{
	initParse();
	initFonts();
}


function CheckWindowSize()
{
	var x,y;

	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}

	if(x < 994)
	{
		ToggleCSS('narrow', true);	
	}
	else
	{
		ToggleCSS('narrow', false);	
	}

}

function ToggleCSS(title, active)
{
	var stylesheets = document.getElementsByTagName('LINK');
	for (var i = 0; i < stylesheets.length; i++)
	{
		if (stylesheets[i].title == title)
		{
			stylesheets[i].disabled = (!active);
		}
	}
}

function GetCSSStatus(title)
{
	var stylesheets = document.getElementsByTagName('LINK');
	for (var i = 0; i < stylesheets.length; i++)
	{
		if (stylesheets[i].title == title)
		{
			return (!stylesheets[i].disabled);
		}
	}
}

function changebackground()
{
	if (GetCSSStatus('alternate'))
	{
		ToggleCSS('alternate', false);	
		setCookie('ContrastCookie', 'NORMAL' );
	}
	else
	{
		ToggleCSS('alternate', true);
		setCookie('ContrastCookie', 'ALTERNATE' );
	}
}

/* enlarge shrink fonts */
var G_FONTNAME = "FSRFontSize";
var G_DEFAULTFONTSIZE = 11;
var G_MEDIUMFONTSIZE = 13;
var G_MAXFONTSIZE = 13;
var G_MINFONTSIZE = 10;

function initFonts() 
{
	//alert( G_FONTNAME )
	var fontSize = G_DEFAULTFONTSIZE;
	
	if ((getCookie(G_FONTNAME) != null) &&  (getCookie(G_FONTNAME) != ''))
	{
		fontSize =  getCookie(G_FONTNAME);
		//setCookie(G_FONTNAME,'');
	}	
	
	if (fontSize != G_DEFAULTFONTSIZE)
	{
		document.body.style.fontSize = fontSize+"px";
	}
	
	setCookie( G_FONTNAME, fontSize );

	var currstyle = "NORMAL";
	
	if ((getCookie('ContrastCookie') != null) &&  (getCookie('ContrastCookie') != ''))
	{
		currstyle =  getCookie('ContrastCookie');
	}

	if (currstyle == "NORMAL" && !GetCSSStatus('alternate'))
	{
		ToggleCSS('alternate', false);	
	}
	else
	{
		ToggleCSS('alternate', true);
	}
}

function CheckIEbackground()
{
	var detect = navigator.userAgent.toLowerCase();
	if(detect.indexOf('msie') != -1){
  		var myWidth = 0, myHeight = 0;

		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			    
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			    
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		
		if(myWidth % 2 == 1){
			document.body.style.background = '#688396 url(/lisalib/getfile.aspx?itemid=176) repeat-y top center';
		}

		//window.alert( 'Width = ' + myWidth );
		//window.alert( 'Height = ' + myHeight );
	}
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}

function normalFont() {
	var size = getCookie(G_FONTNAME);

	if (size == null) {
		size = G_DEFAULTFONTSIZE;
	}

	document.body.style.fontSize = G_DEFAULTFONTSIZE+"px";
	setCookie(G_FONTNAME,G_DEFAULTFONTSIZE);
}

function mediumFont() {
	var size = getCookie(G_FONTNAME);
	if (size == null) {
		size = G_MEDIUMFONTSIZE;
	}

	document.body.style.fontSize = G_MEDIUMFONTSIZE+"px";
	setCookie(G_FONTNAME,G_MEDIUMFONTSIZE);
}

function largeFont() {
	var size = getCookie(G_FONTNAME);
	if (size == null) {
		size = G_MAXFONTSIZE;
	}
	
	document.body.style.fontSize = G_MAXFONTSIZE+"px";
	setCookie(G_FONTNAME,G_MAXFONTSIZE);
}

function enlargeFont() {
	var size = getCookie(G_FONTNAME);
	if (size == null) {
		size = G_DEFAULTFONTSIZE;
	}
	size++;
	if (size > G_MAXFONTSIZE) {size = G_MAXFONTSIZE}
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie(G_FONTNAME,size);
}

function shrinkFont() {
	var size = getCookie(G_FONTNAME);
	if (size == null) {
		size = G_DEFAULTFONTSIZE;
	}
	size--;
	if (size < G_MINFONTSIZE) {size = G_MINFONTSIZE}
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie(G_FONTNAME,size);
}


function restoreSize() {
	size = G_DEFAULTFONTSIZE;
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie(G_FONTNAME,size);	
}

function getPercentSize(size) {
	return (size/16)*100;
}

function setCookie(cookieName,cookieValue) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000*24*1000);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires="+expire.toGMTString();
}

function getCookie(cookieName) {
	oCookie = document.cookie;
	var index = oCookie.indexOf(cookieName + "=");
	if (index == -1) return null;
	index = oCookie.indexOf("=", index) + 1;
	var endstr = oCookie.indexOf(";", index);
	if (endstr == -1) endstr = oCookie.length;
	return unescape(oCookie.substring(index, endstr));
}

/* General form-validation */

function validate(form) {
	var isValid = true;
	for (var i = 0; i < form.elements.length; i++) {
		var elem = form.elements[i];
		if (elem.className.indexOf('reqd') != -1) {
			/* input, select og textarea er höndlað á sama hátt .... */
			if ((elem.tagName == "INPUT") || (elem.tagName == "TEXTAREA") || (elem.tagName == "SELECT")) {			
				if (elem.className.indexOf('emailval') > 0) {
					isValid = isValidEmail(elem.value);
				} else {
					isValid = (elem.value != '');
				}
				
				if (!isValid) {
					alert(elem.title + ' er ekki rétt útfyllt!');
					elem.focus();
					elem.style.borderColor = '#FF4A4A';
					elem.style.backgroundColor = '#FDFAD0';
					return false;
				} else {
					elem.style.borderColor = '';
					elem.style.backgroundColor = '';
				}
			}			
		}
	}
	return true;
}

function isValidEmail(value) {
	return (value.indexOf(".") > 2) && (value.indexOf("@") > 0);
}

function openwindow(url,w,h,sbars,resize)
{
	window.open(url,'ferilskra','toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars='+sbars+', resizable="+resize+, width='+w+', height='+h).focus();
	
}

function toggle(element)
{
	var element = document.getElementById(element);
	if ((element.style.display == 'none') || (element.style.display == ''))
	{
		element.style.display = 'block';
	}
	else
	{
		element.style.display = 'none';
	}
}

function lisa_flash(movie,width,height)
{
	var flash = '<object codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" height="' + height + '" width="' + width + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">';
	flash += '<param name="wmode" value="transparent" />';
	flash += '<param name="bgcolor" value="#ffffff" />';
	flash += '<param name="Movie" value="' + movie + '">';
	flash += '<embed src="' + movie + '" wmode="transparent" quality="high" bgcolor="" height="' + height + '" width="' + width + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">';
	flash += '</embed>';
	flash += '</object>';
	document.write(flash);
}


// Makes, Reads, Deletes cookies
function cookie(name,value,days)
{
	if(value)
	{
		var expires = ""; 
		if (days) 
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*86400000));
			expires = "expires="+date.toGMTString();
		}
		document.cookie = name + "=" + value + "; " + expires + "; path=/";
	}
	else
	{
		var re = new RegExp('(^|.*; )'+name+'=','i');
		if( !re.test(document.cookie) ) return '';
		return document.cookie.replace(re,'').replace(/;.*/i,'');
	}
}