function update_opener() {
	var url = opener.document.location.href;
	opener.document.location.href = url;
	window.close();
}

function update_opener_setfocus() {
	if (typeof(top.opener.document) == 'object') {
		var url = opener.document.location.href;
		opener.document.location.href = url;
	}
	
	//reload grid row or whole grid
//	if (opener.top.UI_CODE.reloadContents) {
//		opener.top.UI_CODE.reloadContents();
//	}

//	if (opener.top.main && opener.top.main.reloadGrid) {
//		opener.top.main.reloadGrid();
//	}
	opener.focus();
	window.close();
}


function opener_setfocus() {
	opener.focus();
	window.close();
}


function refresh_page() {
	var url = document.location.href;
	document.location.replace(url);
}


function apneVinduScroll(url, navn, b, h) {
        var sFeatures = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";

        if (b != null) {
                sFeatures += ",width=" + b;
        }
        if (h != null) {
                sFeatures += ",height=" + h;
        }
        return window.open(url, navn, sFeatures);
}


function apneFulltVindu(url, navn, b, h) {
        var sFeatures = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=0,left=0";
///	var sFeatures = "fullscreen=yes,status=yes,toolbar=no,menubar=no,resizable=yes";


	var client_window_height=screen.height; 
	var client_window_width=screen.width;

        if (b != null) {
                sFeatures += ",width=" + client_window_width;
        }
        if (h != null) {
                sFeatures += ",height=" + client_window_height;
        }
        return window.open(url, navn, sFeatures);
}



function openModalCentered (url, width, height, windowName, featureString) {
  if (!windowName)
    windowName = '';
  if (!featureString)
    featureString = '';
  else
    featureString = ',' + featureString;
  var x = Math.round((screen.availWidth - width) / 2);
  var y = Math.round((screen.availHeight - height) / 2);
  featureString = 'left=' + x + ',top=' + y + ',width=' + width + ',height=' + height + featureString;
	var feat = "scroll:No; resizable:Yes; status:No; dialogHeight:" + height + "px; dialogWidth: "+width+"px;";
    	
  var hWnd = window.showModalDialog (url, windowName, feat);
  return hWnd;
}



function openCentered (url, width, height, windowName, featureString) {
  if (!windowName)
    windowName = '';
  if (!featureString)
    featureString = '';
  else
    featureString = ',' + featureString;
  var x = Math.round((screen.availWidth - width) / 2);
  var y = Math.round((screen.availHeight - height) / 2);
  featureString = 'left=' + x + ',top=' + y + ',width=' + width + ',height=' + height + featureString;
  var hWnd = window.open (url, windowName, featureString);
//  top.common_code.push_window(hWnd);
  return hWnd;
}


function Hjelp(topic) {
	openHelpDialog(topic);
}

function openHelpDialog(keyword) {
	var url = 'http://docs.easypublish.no/index.php?kw='+keyword;
	var hWnd = openCentered(url, 850, 600, 'help', 'menubar=yes,resizable=yes,scrollbars=yes,location=yes,status=yes');
//	var g_wndReminder = window.showModelessDialog(url, window,"dialogHeight:600px;dialogWidth:800px;status:yes;resizable:yes;help:no;unadorned:yes");
	hWnd.focus();
}

function randomNavn() {
	return new Number(Math.round(Math.random() * 99999999)).toString();
}



// A useful function in PHP that I thought js should have!
/*
Array.prototype.implode = function ( w ) {
    ret = "";
    for ( i = 0; i < this.length; i ++ ) {
        ret += this[i] + w;
    }
    return ret.substring( 0, ret.length - w.length );
}
*/
/*
index.php?page_id=X -> param[page_id] = X
*/
function getParamArrayFromUrl(url) {
	var _idx_start = url.indexOf('?');
	var _params = '';
	if (_idx_start > 0) {
		_params  =  url.substring(_idx_start+1);
	} else {
		_params = url;
	}
	var arr_params_old = new Object;
	arr_params_old = _params.split('&');			

	var arr_params = new Object;
	
	if (arr_params_old.length > 0) {
		var _p;
		var _str;
		for (i=0;i<arr_params_old.length;i++) {
			_str = arr_params_old[i];
			_p = _str.split('=');
			arr_params[_p[0]] = _p[1];			
		}
	}
	return arr_params;
}

/**
 * @param string $url
 */
function getModifiedUrl(url, arr_params_new) {
	url = url.replace('#','');
	var _idx_start = url.indexOf('?');
	var _params = '';
	var arr_params_old = new Object;
	var _before_params = url.substring(0, _idx_start);

	if (_idx_start >= 0) {
		_params = url.substring(_idx_start + 1);
		arr_params_old = _params.split('&');	
		_before_params = url.substring(0, _idx_start);
	} else {
		_before_params = url;
	}

	var new_url = '';
	
	var arr_params = new Object;
	
	
	if (arr_params_old.length > 0) {
		var _p;
		var _str;
		for (i=0;i<arr_params_old.length;i++) {
			_str = arr_params_old[i];
			_p = _str.split('=');
			arr_params[_p[0]] = _p[1];			
		}
	}
	
	if (typeof(arr_params)=='object') {
		for (_p in arr_params_new) {								
			arr_params[_p] = arr_params_new[_p];
		}
		
		var arr_p = new Array;
		var queryPart = '';
		var param;
		var i = 0;
		for (_p in arr_params) {
			
			param = _p + '=' + arr_params[_p];
			if (i== 0) {
				queryPart = param;
			} else {
				queryPart += '&' + param;
			}
			
			i++;
		}
		new_url = _before_params + '?' + queryPart;
		
		return new_url;
	}	
}


/**
 * Parse URL and return parameter-array
 * index.php?foo=bar -> arr[foo] = bar
 *
 * @param string $url
 */
function getParamsAsArray(url) {
	url = url.replace('&amp;','&');
	var _idx_start = url.indexOf('?');
	var _params = '';
	var arr_params_old = new Object;
	var arr_params = new Array;
	var _before_params = url.substring(0, _idx_start);

	if (_idx_start >= 0) {
		_params = url.substring(_idx_start + 1);
		arr_params_old = _params.split('&');	
		_before_params = url.substring(0, _idx_start);
	} else {
		_before_params = url;
	}

	if (arr_params_old.length > 0) {
		var _p;
		var _str;
		for (i=0;i<arr_params_old.length;i++) {
			_str = arr_params_old[i];
			_p = _str.split('=');
			arr_params[_p[0]] = _p[1];			
		}
	}
	return arr_params;
}

function getExpiryDate( nodays){
	var UTCstring;
	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);
	UTCstring = Today.toUTCString();
	return UTCstring;
}




function setCookie(name, value, duration) {
	var cookieString = name + "=" + escape(value)+";EXPIRES="+getExpiryDate(duration);
	cookieString += ';DOMAIN=localhost;PATH=/localhost/ep-4.1.7.2/www/';
	document.cookie = cookieString;
}

function getCookie(name) {
	if (document.cookie) {
    var cookies=document.cookie.split(";");
    for (var i=0; i<cookies.length; i++)
    {
      var varName=(cookies[i].split("=")[0]);
      var varValue=(cookies[i].split("=")[1]);

      while (varName.charAt(0)==" ")
        varName=varName.substr(1,varName.length);
				
      if (varName==name)
        return varValue;
    }
  }
  return false;
}


function deleteCookie( name ) {
	if (getCookie(name)) document.cookie = name + "=" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


var tid, time, action;
var message1 = 'You are getting lazy. No activity for ';
var message2 = ' seconds.';

function setInactivityTimer (time, action, repeat) {
  window.time = time;
  window.action = action;
  window.repeat = repeat;

  if (tid) clearTimeout(tid);

  document.onmousemove = document.onkeyup =
    function (evt) {
      setInactivityTimer(window.time, window.action, window.repeat);
      return true;
    };
  if (repeat)
    action += '; setInactivityTimer(' 
              + time + ', "' + action + '", ' + repeat + ');';
  else 
    action += '; clearEvents();';
  tid = setTimeout(action, time);
}

function clearEvents() {
  document.onmousemove = document.onkeyup = null;
}

/* global functions */
/* from: http://www.uptodata.com/javascripts/css/code.js */

function debug(obj)
{
    var props = "";
    for (var property in obj)
    {
      props += property + ": " + obj[property] + "\n";
    }
    props = "Properties of object: \n\t" + props;
    //alert(props);
    var opts = "width=250,height=450,resizable,scrollbars=auto";
    var win = window.open("", "x", opts, true);
    win.document.writeln("<pre>"+props+"</pre>");
}

function getElementsByClassName(objRef, className)
{
    var matches = new Array();
    var alltags = objRef.getElementsByTagName( "*" );
    for (var i=0; i<alltags.length; i++)
    {
      if (alltags[i].className.toLowerCase() == className.toLowerCase())
      {
        matches[matches.length] = alltags[i];
      }
    }
    return matches;
}

function getEvtTarget(evt)
{
  if (document.all) return window.event.srcElement; // for M$IE
  if (evt.target.nodeType == Node.TEXT_NODE) return evt.target.parentNode; // for Safari
  return evt.target;
}

function deriveName(from, prefix)
{
  var num = from.substring(from.lastIndexOf("-")+1);
  return prefix + "-" + num;
}

function deselectExisting(parentNode, correspondingPrefix)
{
  var existingList = getElementsByClassName(parentNode, "selected");
  if (!existingList || existingList.length == 0) return;  // done
  var existing = existingList[0];
  existing.className = "";
  var existingCorrespondingId = deriveName(existing.id, correspondingPrefix);
  document.getElementById(existingCorrespondingId).className = "";
}

function selectNew(newNode, correspondingPrefix)
{
  newNode.className = "selected";
  var correspondingIdName = deriveName(newNode.id, correspondingPrefix);
  document.getElementById(correspondingIdName).className = "selected";
}

function hasClassName(node, className)
{
  var ra = node.className.split(" ");
  for (var i=0; i<ra.length; i++)
  {
    if (ra[i] == className)
    {
      return true;
    }
  }
  return false;
}

function toggleVisibility(tagid)
{
  var node = document.getElementById(tagid);
  var vis = node.style.visibility;
  if (vis == "" || vis == "visible")
  {
    node.style.visibility = "hidden";
  }
  else
  {
    node.style.visibility = "visible";
  }
}

//ref http://www.webdeveloper.com/forum/showthread.php?t=120202#2
//IE and FF count in different ways the childNodes. 
//FF (DOM compliant mode) counts even the possible textNodes (the gaps between tags) while IE counts only some of them.
var notWhitespace = /\S/;
function cleanWhitespace(node) {
	for (var x = 0; x < node.childNodes.length; x++) {
	 var childNode = node.childNodes[x]
	 if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
	//that is, if it's a whitespace text node
	   node.removeChild(node.childNodes[x])
	   x--
	 }
	 if (childNode.nodeType == 1) {
	//elements can have text child nodes of their own
	   cleanWhitespace(childNode)
	 }
	}
}
