window.onload = init;

function init() {
	
	var fontSize = "11";
	if ((getCookie("articFontsize") != null) &&  (getCookie("articFontsize") != '')) {
		fontSize =  getCookie("articFontsize");
	}
	if (fontSize != 11) {
		document.body.style.fontSize = getPercentSize(fontSize) + "%";
	}	
}

/*
Font-sizing functions
*/

function enlargeFont() {
	var size = getCookie("fontsize");
	if (size == null) {
		size = 11;
	}
	size++;
	if (size > 14) {size = 14}
	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 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));
}

function getPercentSize(size) {
	return (size/16)*100;
}


/* 
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 + ' is not properly filled out!');
					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);
}

