﻿function getParm(string,parm) {
var startPos = string.indexOf(parm + "=");
  if (startPos > -1) {
  startPos = startPos + parm.length + 1;
  var endPos = string.indexOf("&",startPos);
    if (endPos == -1) {
    endPos = string.length;
    }
    return unescape(string.substring(startPos,endPos));
  }
  return '';
}

function isURL(string) {
  var iniPos = string.indexOf(".");
  var endPos = string.lastIndexOf(".");
  if(iniPos==-1) {				//punktum er obligatorisk
  return false;
  }
  if(iniPos==endPos) {				//mindst 2 punktummer...
  return false;
  }
  if(string.indexOf("..")!=-1) {		//...dog aldrig efter hinanden
  return false;
  }
  if(iniPos==7) {				//punktum må ikke være første tegn
  return false;
  }
  if(string.slice(string.length-1)==".") {	//punktum må ikke være sidste tegn
  return false;
  }
  var strDom = string.slice(endPos+1);
  if(strDom.length<2) {				//Mindst 2 tegn efter sidste punktum
  return false;
  }
  return true;
}

function getCategory() {
  var index = document.getElementById("section").selectedIndex;
  var ident = document.getElementById("section").options[index].value;
  if(showident!='all') {
    if(ident!='') {
    location.href = 'news.aspx?cat=' + ident;
    }
    else {
    location.href = 'news.aspx';
    }
  }
  else {
    if(ident!='') {
    location.href = 'news.aspx?cat=' + ident + '&show=all';
    }
    else {
    location.href = 'news.aspx?show=all';
    }
  }
}

function getNews() {
  var index = document.getElementById("view").selectedIndex;
  if(index==0) {
    if(catident=='') {
    location.href = 'news.aspx?show=all';
    }
    else {
    location.href = 'news.aspx?cat=' + catident + '&show=all';
    }
  }
  else {
    if(catident=='') {
    location.href = 'news.aspx';
    }
    else {
      if(document.getElementById("section")) {
        if(document.getElementById("section").selectedIndex==0) {
        location.href = 'news.aspx';
        }
        else {
        location.href = 'news.aspx?cat=' + catident;
        }
      }
      else {
      location.href = 'news.aspx?cat=' + catident;
      }
    }
  }
}

function showItem(newsident) {
location.href = 'news.aspx?id=' + newsident;
}

function setNews() {
  if(rowColor!='') {
    var list = document.getElementById("listall");
    var divs = list.getElementsByTagName("div");
    for(var i=0; i<divs.length; i++) {
    if(i%2==0) divs[i].style.backgroundColor = rowColor;
    }
    list.style.visibility = 'visible';
  }
  else {
  var passed = location.search.substring(1);
  var newsID = getParm(passed,'id');
  if(newsID!='') setLinks();
  }
}

function setLinks() {
  var iHTML = document.getElementById("bodytext").innerHTML;
  var matches = iHTML.match(/http:\/\//g);
  if(matches!=null) {
    iHTML = ' ' + iHTML + ' ';
    iHTML = iHTML.replace(new RegExp('<','g'), ' <');
    var total = matches.length;
    var index = 0;
    var startPos, spacePos, ini, mid, end;
    for(var i=0; i<total; i++) {
      startPos = iHTML.indexOf(matches[i], index);
      spacePos = iHTML.indexOf(' ', startPos);
      mid = iHTML.slice(startPos, spacePos);
      if(isURL(mid)) {
      ini = iHTML.slice(0, startPos);
      end = iHTML.slice(spacePos);
      mid = '<a href="' + mid + '">' + mid + '</a>';
      iHTML = ini + mid + end;
      index = spacePos+16;
      }
      else {
      index = spacePos+1;
      }
    }
    iHTML = iHTML.slice(1, iHTML.length-1);
    document.getElementById("bodytext").innerHTML = iHTML;
  }
}
