// DHTML menu script

function toggle(menuID, depth) {
  if (!document.getElementById) return true;
  var myLevel = level[depth];
  if (myLevel != null) {
    for (var i = myLevel.length-1; i >= 0; i-- ) {
      if (myLevel[i] != menuID) {
        var anObj = document.getElementById(myLevel[i]);
        if (anObj != null) anObj.style.display = 'none';
      }
    }
  }
  var theMenu = document.getElementById(menuID);
  if (theMenu != null) { theMenu.style.display = (theMenu.style.display=='block') ? 'none' : 'block'; }
  return false;
}


// Gereric Popup window script

function open_popup(url,target,width,height) {
  menuWindow=window.open(url,target,"toolbar=0,scrollbars=auto,location=0,status=0,menubar=0,width=" + width + ",height=" + height);
  setTimeout("menuWindow.focus();", 100);
}


// Form validation functions

function searchValid(theForm)
{
  if (document.layers == null)
  {
    var error = "";
    error += (theForm.SearchFor.value == "") ? "Vinsamlega slįšu inn leitarorš.\n" : "";
    if (error != "") {
      alert(error);
      return false;
    }
    return true;
  }
}


function loginValid(theForm)
{
  if (document.layers == null)
  {
    var error = "";
    error += (theForm.Login.value == "")    ? "Vinsamlega slįšu inn notandanafn.\n" : "";
    error += (theForm.Password.value == "") ? "Vinsamlega slįšu inn lykilorš.\n" : "";
    if (error != "") {
      alert(error);
      return false;
    }
    return true;
  }
}


function subscrValid(theForm)
{
  if (document.layers == null)
  {
    var error = "";
    error += checkEmail(theForm.VIS_Email.value);
    if (error != "") {
      alert(error);
      return false;
    }
    return true;
  }
}


function contactValid(theForm)
{
  if (document.layers == null)
  {
    var error = "";
    error += (theForm.name.value == '') ? "Vinsamlega slįšu inn nafn.\n" : "";
    error += checkEmail(theForm.email.value);
    error += (theForm.question.value == '') ? "Žś gleymdir fyrirspurninni.\n" : "";
    if (error != "") {
      alert(error);
      return false;
    }
    return true;
  }
}


function quizValid(theForm)
{
  if (document.layers == null)
  {
    var error = "";
    error += checkRadios(theForm, "QuizAnswer_2");
    if (error != "") {
      alert(error);
      return false;
    }
    return true;
  }
}




// Single Form-field validation functions (only used by the form-validate functions above)
function checkEmail( strng )
{
  var error = "";
  var emailFilter=/^.+@.+\..{2,3}$/;
  if (!(emailFilter.test(strng))) { 
    error += "Vinsamlega slįšu inn löglegt netfang.\n";
  }
  var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
  if (strng.match(illegalChars)) {
    error += "Netfangiš inniheldur ólögleg tįkn.\n";
  }
  return error;
}


function checkRadios(theForm, radioName) {
  var error = "";
  var checkvalue = false;
  for(i = 0; i < theForm.elements.length; i++) {
    if(theForm.elements[i].name == radioName) {
      if(theForm.elements[i].checked) {
        checkvalue = true; 
        break;
      }
    }
  }
  if(!(checkvalue)) {
    error = "Vinsamlegast hakašu viš valkost.\n";
  }
  return error;
} 

function checkKennitala(Kt) {
  var error = "";
  if (Kt == '') {
    error += "Vinsamlega slįšu inn kennitölu.\n";
  }
  else {
    str_Kt = new String(Kt);
    str_Ktclean = new String;
    for(var i=0; i<str_Kt.length; i++) {
      var t = parseInt(str_Kt.charAt(i),10);
      if(!isNaN(t)) {
        str_Ktclean += t;
      }
    }
    var margf= new Array(3,2,7,6,5,4,3,2,1);
    var summa = 0;
    for (i=0;i!=9;i++) {
      summa = summa + (margf[i] * str_Ktclean.charAt(i));
    }
    if ((summa % 11) || (str_Ktclean.length != 10)) {
      error += "Athugašu aš kennitala sé rétt slegin inn\n";
    }
  }
  return error;
}


// Make sure that the pagebody block is at least equal height to 
// each of the side columns
var isChanged = false;
function fixPagebodyHeight()
{
  if (document.layers == null)
  {
    var hBody = document.getElementById('pagebody').offsetHeight;
    var hExtra1 = (document.getElementById('pageextra1') != null) ? document.getElementById('pageextra1').offsetHeight : 0;
    var hExtra2 = (document.getElementById('pageextra2') != null) ? document.getElementById('pageextra2').offsetHeight : 0;
    var hExtra3 = (document.getElementById('pageextra3') != null) ? document.getElementById('pageextra3').offsetHeight : 0;
    var hExtra4 = (document.getElementById('pageextra4') != null) ? document.getElementById('pageextra4').offsetHeight : 0;
    if (hExtra1 > hBody) { hBody = hExtra1; isChanged = true; }
    if (hExtra2 > hBody) { hBody = hExtra2; isChanged = true; }
    if (hExtra3 > hBody) { hBody = hExtra3; isChanged = true; }
    if (hExtra4 > hBody) { hBody = hExtra4; isChanged = true; }
    if (isChanged == true)
    {
      document.getElementById('pagebody').style.height = hBody + "px";
    }
  }
  disableCSSinStupidBrowsers()
}


// Set explicit height value to the containing blocks of images 
// (Fixes various float bugs in IE5.5 and IE6 for Windows)
if (document.all && window.attachEvent && navigator.appName == "Microsoft Internet Explorer")
{
  window.attachEvent("onload", doIEStuff);
}
function doIEStuff()
{
  fixFloatsInWinIE();
}
function fixFloatsInWinIE()
{
  for (i = 0; i < document.images.length; i++ )
  {
    myNode = document.images[i].parentNode;
    if (myNode.tagName == "div")
    {
      h = myNode.offsetHeight;
      myNode.style.height = h + "px";
      dh = myNode.offsetHeight - h;
      myNode.style.height = h - dh + "px";
    }
  }
  if (isChanged == false)
  {
    h = pagebody.offsetHeight;
    pagebody.style.height = h + "px";
    dh = pagebody.offsetHeight - h;
    pagebody.style.height = h - dh + "px";
  }
}


/*
 Browser sniffing code borrowed from: http://www.webreference.com/tools/browser/javascript.html
 The following bit of javascript code is Free Software[tm]. Licence: GNU/GPL v2.0 or later.
*/
var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();

// *** BROWSER VERSION ***
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);

var iePos  = appVer.indexOf('msie');
if (iePos !=-1) {
  is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
  is_major = parseInt(is_minor);
}

var is_opera = agt.indexOf('opera') != -1;

var is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;
var is_konq = agt.indexOf('konqueror') != -1;
var is_khtml  = (is_safari || is_konq);

var is_ie   = ((iePos!=-1) && (!is_opera) && (!is_khtml));
var is_ie5dn = (is_ie && is_minor < 5.0);

// *** PLATFORM ***
var is_mac = (agt.indexOf("mac")!=-1);


/* Disable all CSS in stupid browsers */
function disableCSSinStupidBrowsers()
{
  if (is_ie5dn) 
  {
    var x = null;
    if (document.getElementsByTagName) 
    {
      x = document.getElementsByTagName('link');
    }
    else if (document.all) 
    {
      x = document.all.tags('link');
    }
    if (x != null)
    {
      x[0].disabled = true;
      x[1].disabled = true;
    }
  }
}


