window.onload = init;

function init() {

	if ((getCookie("isWide") != null) && (getCookie("isWide") != ''))
	{
		document.getElementById('wrapper').className = 'wide';
	}

	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) + "%";
	}	
}

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++;
	if (size > 15) {size = 15}
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie("fontsize",size);
}

function shrinkFont() {
	var size = getCookie("fontsize");
	if (size == null) {
		size = 11;
	}
	size--;
	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) + ";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') > 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 setActiveStyleSheet(title) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) { 
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

function widen()
{
	var wrapper = document.getElementById('wrapper');
	if (wrapper.className == '')
	{
		wrapper.className = 'wide';
	}
	else
	{
		wrapper.className = '';
	}
	
	setCookie("isWide", wrapper.className);
}
