window.onload = init;

function init() {
	var search = document.getElementById('SearchString1');
	if (search != null) {
		search.onclick = search_onclick;
		search.onblur = search_onblur;
	}
	
	var fontSize = "11";
	if ((getCookie("fontsize") != null) &&  (getCookie("fontsize") != '')) {
		fontSize =  getCookie("fontsize");
	}
	if (fontSize != 11) {
		document.body.style.fontSize = getPercentSize(fontSize) + "%";
	}	

	FixContentBottom();
	setElementHeight('newsitem1', 'newsitem2');
}

function FixContentBottom()
{
	var detect = navigator.userAgent.toLowerCase();
	var rightpanel = document.getElementById('rightpanel');
	var contentbottom = document.getElementById('contentbottom');

	var topbox1 = document.getElementById('topbox1');
	var topbox2 = document.getElementById('topbox2');

	if(rightpanel != null && contentbottom != null && topbox1 != null)
	{
		var xheight = rightpanel.offsetHeight;
		var contentarea = contentbottom.offsetHeight;

		if(detect.indexOf('msie') != -1){
			contentarea = contentarea + topbox1.offsetHeight + 26;		
		}
		else
		{
			contentarea = contentarea + topbox1.offsetHeight + 14;
		}
	
		//alert(contenttop.offsetHeight + " " + contentbottom.offsetHeight + " " + testbox.offsetHeight);
 
		//alert(contentarea + " " + xheight);
		if(contentarea > xheight)
		{
			rightpanel.style.height = contentarea + "px";
		}
		else
		{
			var difference = xheight - contentarea;
			topbox1.style.height = difference + topbox1.offsetHeight;
			topbox2.style.height = difference + topbox2.offsetHeight;
		}
	
		xheight = rightpanel.offsetHeight;

		if(detect.indexOf('msie') != -1){
			xheight = xheight + 36;		
		}
		else
		{
			xheight = xheight + 24;
		}
	
		contentbottom.style.top = xheight + "px";
	}
}

function search_onclick() {
	var search = document.getElementById('SearchString1');
	if (search.value == 'Sláðu inn leitarorð') {
		search.value = '';
	}
}

function search_onblur() {
	var search = document.getElementById('SearchString1');
	if (search.value == '') {
		search.value = 'Sláðu inn leitarorð';
	}
}

function enlargeFont() {
	var size = getCookie("fontsize");

	if (size == null) {
		size = 11;
	}
	size = parseInt(size) + 2;
	
	if (size > 13) {size = 13}
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie("fontsize",size);
}

function shrinkFont() {
	var size = getCookie("fontsize");
	if (size == null) {
		size = 11;
	}
	size = parseInt(size) - 2;
	if (size < 9) {size = 9}
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie("fontsize",size);
}

function restoreSize() {
	size = "11";
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie("fontsize",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) + "; path=/; expires="+expire.toGMTString();	
	//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));
}
function popcorn(x,w,h,y)
{
	var f='scrollbars,resizable'+(w?',width='+w:'')+(h?',height='+h:'')
	return !window.open(x,y||'popcorn',f) 
}

/* 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') > 0) {
			
			/* 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 sortNum(a,b) { return b-a }

function setElementHeight(one,two) {
	if (document.getElementById(one)) {
		var obj=new Array(3);
		var option=[one,two];
		for(var i=0; i<option.length; i++) {
			document.getElementById(option[i]).style.height="auto";
			obj[i]=document.getElementById(option[i]).offsetHeight;
			nh=obj.sort(sortNum);
		}
		nh1=nh.splice(1,2);
		var newHeight = parseInt(nh) + parseInt(10);
		for(var i=0; i<option.length; i++) {
			document.getElementById(option[i]).style.height=newHeight+"px";
		}
	}
}

