window.onload = init;

function init() {
	//initFonts();
	//var detect = navigator.userAgent.toLowerCase();
	//if(detect.indexOf('msie') != -1){
	//	window.attachEvent("onload", correctPNG);
	//}
}

/* enlarge shrink fonts */
var G_FONTNAME = "BBTFontSize";
var G_DEFAULTFONTSIZE = 11;
var G_MEDIUMFONTSIZE = 13;
var G_MAXFONTSIZE = 15;

function popcorn(url,width,height,title)
{
	var params='scrollbars,resizable'+(width?',width='+width:'')+(height?',height='+height:'')
	return !window.open(url,title||'popcorn',params) 
}

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 );
}

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 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 CheckWWW() {
	var email = document.getElementById('cl_email').value;
	var www = document.getElementById('cl_www').value;

	document.getElementById('cl_senderemail').value = email + '-' + www;
}

function Clearbox(fieldname){
	var field = document.getElementById(fieldname);

	if(field.value == 'Svar..'){
		field.value = '';
	}
}

function dimItem (obj)
{
    var reveal = new fx.Opacity(obj, {duration: 100});
    reveal.setOpacity(.6);
}

function unDimItem (obj)
{
    var reveal = new fx.Opacity(obj, {duration: 500 });
    reveal.setOpacity(.6);
    reveal.custom(.6, 1);
}

function preloadImages() { //v3.0
  var d=document;
  if(d.images)
  {
      if(!d.pi) d.pi=new Array();
      var i,j=d.pi.length,a=preloadImages.arguments;
      for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0)
	{
	    d.pi[j]=new Image;
	    d.pi[j++].src=a[i];
	}
    }
}
