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();
}

function email(address) {
	location.href = 'mailto:'+address+'@capacent.is'
}

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 == 'Search string') {
		search.tempvalue = search.value;
		search.value = '';
	}
}

function search_onblur() {
	var search = document.getElementById('SearchString1');
	if (search.value == '') {
		search.value = search.tempvalue;
	}
}

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;
	//debugger;
	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(".") > 0) && (value.indexOf("@") > 0);
}

/****************/
/***  AJAX *****/
/***************/

var xmlHttp;
function ChangeVisibility(fileid)
{
	xmlHttp=GetXmlHttpObject()

	var visibility = document.getElementById('ckfile_' + fileid).checked;	

	if(visibility)
	{
		visibility = 1;
	}else{
		visibility = 0;
	}

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
 
	var url = "/controls/UpdateFileVisibility.aspx";
	url = url + "?fileid=" + fileid;
	url = url + "&visibility=" + visibility;
	url = url + "&sid=" + Math.random();

	xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)

	return false;
} 

function stateChanged() 
{ 
	if (xmlHttp.readyState == 4) 
	{
		if (xmlHttp.status == 200) 
		{
			xmlHttp.responseText;
		}
	}
}
 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function SetCustomervalues(id, name)
{
	document.getElementById('customername').value = name;
	document.getElementById('customerid').value = id;
}

function CheckDates()
{
	var datefrom = new Date(document.getElementById('datefrom').value.replace(/\./ig,'/'));
	var dateto = new Date(document.getElementById('dateto').value.replace(/\./ig,'/'));


	if (datefrom  > dateto)
	{
		alert ("Frá dagsetning getur ekki verið seinna en til dagsetning.");
		return false;
	}

	return true;
}

function lisa_flash(s,w,h)
{
	var a=arguments
	with(document)
	{
		writeln('<object codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" height="'+h+'" width="'+w+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">')
		writeln('<param name="Movie" value="'+s+'">')
		for(var i=3;i<a.length;i+=2)writeln('<param name="'+a[i]+'" value="'+a[i+1]+'">')
		write('<embed src="'+s+'" quality="high" height="'+h+'" width="'+w+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" ')
		for(i=3;i<a.length;i+=2)write(a[i]+'="'+a[i+1]+'" ')
		writeln('/>')
		write('</object>')
	}
}