// ------- Row selection ------- //
var selectedrow = -1;

function disableSelection(target){
  if (typeof target.onselectstart!="undefined") //IE route
  	target.onselectstart=function(){return false}
  else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
  	target.style.MozUserSelect="none"
  else //All other route (ie: Opera)
  	target.onmousedown=function(){return false}
  target.style.cursor = "default"
}

function getParentOfType(elem, type) {
	var res;
	res = elem;
	while (res.tagName.toLowerCase() != type.toLowerCase())
		res = res.parentNode;
	return res;
}

function selectrow(nrow) {
	//Fjern eventuelle tidligere markeringer
	var trs = document.getElementsByTagName('tr'); 
	for (var i = 0; i < trs.length; i++)
	    if (trs[i].className == 'selected') trs[i].className = 'selectable';
	
	//Vælg ny række
	nrow.className = 'selected';
	selectedrow = nrow.rowIndex;
	var form = getParentOfType(nrow, 'form');
	form.selid.value = nrow.id;
}

function checksel(errstr) {
	if ($('selid').value == '') {
		alert(errstr);
		return false;
	}
	return true;
}

// ------- Form extensions ------- //

function addhidden(formid, name, value) {
	var form = $(formid);
	if (!form) return false;

	var ins = form.getElementsByTagName('input'); 
	for (var i = 0; i < ins.length; i++)
    if (ins[i].name == name) {
      ins[i].value = value;
      return true;
    }

  var inp = document.createElement("input");
  inp.setAttribute("type", "hidden");
  inp.setAttribute("name", name);
  inp.setAttribute("value", value);
  form.appendChild(inp);
  return true;
}

function remove(div) {
  Effect.SwitchOff(div, {afterFinish: function (elem) { top.document.body.removeChild(elem.element); }});
}

function ajloader(elemid, msg) {
  if (!msg) msg = 'Indlæser...';
  top.document.getElementById(elemid).innerHTML = '<div style="text-align: center"><img src="images/ajax-loader.gif" style="margin-left: auto; margin-right: auto;" /><br />'+msg+'</div>';
}

function sysmsg(msg) {
  if (msg) {
    ajloader('sysmsg', msg);
  } else {
    top.document.getElementById('sysmsg').innerHTML = '';
  }
}

function focusfirstinput(elem) {
  var inputs = elem.getElementsByTagName('input');
  if (inputs)
    if (inputs[0]) inputs[0].focus();
}

// ------- Custom alert, prompt og confirm metoder ------- //

function alert(text, title, timeout) {
  var div = top.document.createElement("div");
  
  div.className = 'msgbox';
  if (!title) title = 'Systembesked';
  div.innerHTML = '<b>'+title+'</b><div style="position: absolute; left: 0px; top: 30px; height: 94px; width: 296px; padding: 3px; overflow: auto;">'+text+'</div>';
  div.style.cursor = 'hand';
  div.style.display = 'none';
  div.onclick = function () { remove(div) };
  top.document.body.appendChild(div);
  Effect.Appear(div, {afterFinish: function(elem) { focusfirstinput(elem.element); }});
  if (timeout != null) setTimeout(function () { remove(div); }, timeout);
  return div;
}

function prompt(title, text, opt) {
  var div = alert(text, title);
  div.onclick = null;
  div.style.cursor = 'arrow';

  var iok = top.document.createElement('input');
  iok.className = 'button';
  iok.style.position = 'absolute';
  iok.type = 'button';
  iok.value = 'OK';
  iok.style.bottom = '30px';
  iok.style.right = '3px';
  iok.onclick = function () { remove(div); opt.callback(inp.value); };

  var ica = top.document.createElement('input');
  ica.className = 'button';
  ica.style.position = 'absolute';
  ica.type = 'button';
  ica.value = 'Anuller';
  ica.style.bottom = '30px';
  ica.style.left = '3px';
  ica.onclick = function () { remove(div); opt.callback(null); };

  var inp = top.document.createElement('input');
  inp.onkeyup = function (e) {
    var keynum;
    if(window.event) {
      keynum = window.event.keyCode
    } else if(e.which) {
      keynum = e.which
    }
    if (keynum == 27) ica.onclick(); //Escape
    if (keynum == 13) iok.onclick(); //Enter
  }
  
  inp.type = 'text';
  inp.style.position = 'absolute';
  inp.style.top = '75px';
  inp.style.left = '3px';
  inp.style.width = '300px';

  div.appendChild(inp);
  div.appendChild(iok);
  div.appendChild(ica);
}

function confirm(title, text, opt, btnok, btncancel) {
  var div = alert(text, title);
  div.onclick = null;
  div.style.cursor = 'arrow';

  var iok = top.document.createElement('input');
  iok.className = 'button';
  iok.style.position = 'absolute';
  iok.type = 'button';
  iok.value = btnok;
  iok.style.bottom = '30px';
  iok.style.right = '3px';
  iok.onclick = function () { remove(div); opt.callback(true); };

  var ica = top.document.createElement('input');
  ica.className = 'button';
  ica.style.position = 'absolute';
  ica.type = 'button';
  ica.value = btncancel;
  ica.style.bottom = '30px';
  ica.style.left = '3px';
  ica.onclick = function () { remove(div); opt.callback(false); };

  div.appendChild(iok);
  div.appendChild(ica);
}

// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this notice.

// SETUPS:
// ===============================

// Set the horizontal and vertical position for the popup

PositionX = 100;
PositionY = 100;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)

defaultWidth  = 500;
defaultHeight = 500;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var AutoClose = false;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(300,300);');
writeln('width=300-(document.body.clientWidth-document.images[0].width);');
writeln('height=300-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" onclick="window.close();" src='+imageURL+' style="display:block"></body></html>');
close();		
}}

