/**
 * GLOBAL FUNCTIONS FOR SHB
 */

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

/* Redirect to specified URL upon confirmation from user */
function nxcConfirmRedirect(redirecturl, question) {
	if(confirm(question)) {
		window.location.replace(redirecturl);
	}
}

function nxcRefresh(params) {
    if(!params) params = new Object();
    if(params.asNewThread) {
        setTimeout("nxcRefresh();", 1);
        return;
    }
    window.location.reload(true);
}

/* 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 == "") 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) {
		if(width && height) {
		    win.moveTo( (screen.availWidth/2 - width/2), (screen.availHeight/2 - height/2) );
		}
		win.focus();
	} else {
		alert("Kunne ikke åpne nytt vindu. Deaktiver alle popup-blokkere.");
	}
}

/* Sets classId and posts 'CreateForm'-form */
function CreateObject(classId) {
	document.forms['CreateForm'].ClassID.value = classId;
	document.forms['CreateForm'].submit();
}

/* Array prototype with support for cookies */
function NxcPersistentArray() {
    var delim = "|";
    var cookieName = null;

    var 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.pArray = a;
		}
	}
}
NxcPersistentArray.prototype.nxcWriteCookie = function() {
    var array = this.nxcGetArray();
	if(this.cookieName != null && array != null) {
		document.cookie = this.cookieName + "=" + escape(array.join("|")) + "; path=/";
	}
}
NxcPersistentArray.prototype.nxcAddId = function(id) {
	var array = this.nxcGetArray();
	if(array.join().indexOf(id) == -1)
		array.push(id);
}
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;
		}
	}
}

/* Adds function to window.onload event chain */
function nxcAddLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function toggleActionPane(paneId, toggler) {
    nxcToggleDiv(paneId, true);
    if(toggler) {
        if(toggler.src.indexOf("_up") != -1) toggler.src = toggler.src.replace("_up", "_down");
        else toggler.src = toggler.src.replace("_down", "_up");
        if(toggler.blur) toggler.blur();
    }
}

function clickthebutton(buttonId) {
  var button;
  if(document.all) {
    button = document.all[buttonId];
  } else {
    button = document.getElementById(buttonId);
  }
  if(button) {
    button.click();
  }
}

function click(buttonId) {
  var button;
  if(document.all) {
    button = document.all[buttonId];
  } else {
    button = document.getElementById(buttonId);
  }
  if(button) {
    button.click();
  }
}
