/**
 * STANDARD FUNCTIONS FOR SHB
 */

/* Redirect to specified URL */
function nxcRedirect(url) {
	document.location.href = url;
}

/* Redirect to specified URL upon confirmation from user */
function nxcConfirmRedirect(url, question) {
	if(confirm(question)) {
		document.location.href = url;
	}
}

/* Toggle display css property between 'none' and 'block'.
   Assumes that elements are initially set to 'block' unless
   explicitly set on the element's html-tag */
function nxcToggleDiv(id, storeCookie, forceDisplay) {
	var obj = document.getElementById(id);
	if(obj) {
		var display = obj.style.display;
		if(!display) display = "none";
		if(display == "none") display = "block";
		else display = "none";
		if(forceDisplay) display = "block";
		
		if(storeCookie) {
			var id_array = new NxcPersistentArray();
			id_array.cookieName = "nxctogglediv";
			id_array.nxcReadCookie();
			if(display == "block") id_array.nxcAddId(id);
			else id_array.nxcRemoveId(id);
			id_array.nxcWriteCookie();
		}
		
		obj.style.display = display;
	}
}

function nxcInitToggleDiv() {
	var id_array = new NxcPersistentArray();
	id_array.cookieName = "nxctogglediv";
	id_array.nxcReadCookie();
	id_array_array = id_array.nxcGetArray();
	for(var i=0; i<id_array_array.length; i++) nxcToggleDiv(id_array_array[i], false, true);
}

/* Opens new window and checks for popup-blockers */
function nxcOpenWin(url, width, height, featureless) {
	var features = "";
	if(width && height) features += "width="+width+",height="+height+",";
	features += "resizable=1,scrollbars=1";
	if(featureless) features += ",directories=0,location=0,menubar=0,status=0,toolbar=0";
	
	var win = window.open(url, "nxcwin", features);
	
	if(win) {
		win.focus();
	} else {
		alert("Kunne ikke åpne nytt vindu. Deaktiver alle popup-blokkere.");
	}
}

/* Array prototype with support for cookies */
function NxcPersistentArray() {
    this.delim = "|";
    this.cookieName = null;
    
    this.pArray = new Array();
}

NxcPersistentArray.prototype.nxcGetArray = function() {
    //if(this.pArray == null) this.pArray = new Array();
    return this.pArray;
}

NxcPersistentArray.prototype.nxcReadCookie = function() {
	if(this.cookieName == null) return;

	var c = document.cookie;
	var val = null;

	startPos = c.indexOf( this.cookieName + "=" );
	endPos   = c.indexOf( ";", startPos );
	if(startPos > -1) {
		startPos += this.cookieName.length + 1;
		
		if(endPos == -1) val = c.substr(startPos);
		else val = c.substring(startPos, endPos);
		if(val != null) {
			a = unescape(val).split(this.delim);
            this.pArray = a;
		}
	}
}
NxcPersistentArray.prototype.nxcWriteCookie = function() {
    var array = this.nxcGetArray();
	if(this.cookieName != null && array != null) {
		document.cookie = this.cookieName + "=" + escape(array.join(this.delim)) + "; path=/";
	}
}
NxcPersistentArray.prototype.nxcAddId = function(id) {
	var array = this.nxcGetArray();
	if(array.join().indexOf(id) == -1)
		array.push(id);
	//for(i=0; i<array.length; i++) alert(array[i]);
}
NxcPersistentArray.prototype.nxcRemoveId = function(id) {
	var array = this.nxcGetArray();
	for(var i=0; i<array.length; i++) {
		if(id == array[i]) {
			array.splice(i, 1);
			break;
		}
	}
}

/* Simple cookie */
function NxcCookie(cookieName) {
    this.cookieName = cookieName;
}

NxcCookie.prototype.read = function() {
	if(this.cookieName == null) return null;

	var c = document.cookie;
	var val = null;

	startPos = c.indexOf( this.cookieName + "=" );
	endPos   = c.indexOf( ";", startPos );
	if(startPos > -1) {
		startPos += this.cookieName.length + 1;
		if(endPos == -1) val = c.substr(startPos);
		else val = c.substring(startPos, endPos);
		if(val != null) return unescape(val);
	}
	return null;
}

NxcCookie.prototype.write = function(val) {
    if(this.cookieName != null) {
        document.cookie = this.cookieName + "=" + escape(val) + "; path=/";
    }
}
