function statusText(text) {
  self.status = text; 
  return true;
}

/******************************************************************************
 ** Class HTML																						*
 ** Constructor: window.document to handle												*
 ** Written by Arnulf Heimsbakk																*
 ******************************************************************************/

// Constructor for the class
function HTML(doc) {
	this.doc = doc
	this.search = doc.location.search
	this.cookie = doc.cookie
	this.newSearch = ""
	
	//functions
	this.getCookie = GetCookie
	this.setCookie = SetCookie
	this.getParam = GetParam
	this.updateParam = UpdateParam
	this.reload = Reload
}

// Gets url parameter with spesific name
function GetParam(param) {
	re = new RegExp("" + param + "+[=]([A-Za-z0-9_]*)", "g")
	q = re.exec(this.search)
	if (!q)
		return false
	else
		return q[1]
}

// Sets a memory resident cookie for this document
function SetCookie(name, val) {
	this.doc.cookie = name + "=" + escape(val)
}

// Gets a cookie for this document
function GetCookie(name) {
	this.cookie = this.doc.cookie
	label = name + "="
	labelLen = label.length
	i = 0
	for (i = 0; i < this.cookie.length; i++) {
		j = i + labelLen
		if (this.cookie.substring(i,j) == label) {
		if ((cEnd = this.cookie.indexOf(";",j)) == -1) 
			cEnd = this.cookie.length
		return unescape(this.cookie.substring(j,cEnd))
		}
	} 
	return ""
}

function UpdateParam(param, value) {
   if (this.getParam(param) != value) {
	  	re = new RegExp(param + "+[=]" + this.getParam(param) + "*")
	  	if (re.test(this.search)) {
  		  	this.newSearch = this.search.replace(re, param + "=" + value) 
		}
		else if (this.search.charAt(0) == '?') {
			this.newSearch = this.search + "&" + param + "=" + value
		}
		else {
			this.newSearch = this.search + "?" + param + "=" + value
		}
	} 
	else
	  this.newSearch = this.search;
	this.search = this.newSearch;
}

function Reload() {
//	if (this.newSearch != "") {
   	l = this.doc.location;
    	l.replace(l.protocol + "//" + l.hostname + l.pathname + this.newSearch)
//    	this.doc.location.href = l.protocol + "//" + l.hostname + l.pathname + this.newSearch
//	} 
}


/******************************************************************************
 ** Class DT																						*
 ** Constructor: window.document to handle												*
 ** Written by Arnulf Heimsbakk																*
 ** klasse brukt til a samle en del funksjoner som er kjekke aa bruke gjennom *	
 ** alle sidene i det Digitale Laereverket												*
 ******************************************************************************/

function DT ( doc ) {
	this.doc = doc

	this.status = Status
	this.wiLookup = WordIndexLookup
	this.cLookup = ChapterLookup
}

// skriver en statusbeskjed paa statuslinjen til browser
function Status(text) {
  window.status = text
  return true
}

// kjorer indeksfilen for aa oppdatere rammen med ord
function WordIndexLookup(word_id) {
//	d = new HTML(parent.frames.cmd.document)
//	d.updateParam('wi', word_id)
//	d.updateParam('r', 'wi')
//	d.reload()
//	document.frames.right.location.href = "/EBok/Word/?wi=" + word_id + "&b=" + book_id
	parent.frames.right.location.href = '/EBok/Word/?wi=' + word_id + '&b=' + book_id;
}

// kjorer indeksfilen for aa oppdatere et kapittel
function ChapterLookup(number, chapter_id) {
   d = new HTML(parent.frames.cmd.document)
	d.updateParam('c', number)
	d.updateParam('a', chapter_id)
	d.updateParam('r', 'c')
	d.reload()
}
