/**
 * A simple lock manager.
 */
function lockmgr()
{
  this.locks = new Array();

  this.lock = function(identifier)
  {
    if (!this.locks[identifier])
      this.locks[identifier] = true;
  }
  
  this.unlock = function(identifier)
  {
    if (this.locks[identifier])
      this.locks[identifier] = false;
  }
  
  this.islocked = function(identifier)
  {
    if (!this.locks[identifier])
      return false;
    return true;
  }
}

/**
 * An array containing objects identified by an unique id.
 * 
 * @see getObject(Class, ID)
 */
var _objects = new Array();

/**
 * Get an object.
 *
 * If the object doesn't exists it will be created and stored in
 * an array.
 *
 * @param The class (as string)
 * @param The id, used as first parameter when creating a new instance
 *
 * @return An object
 *
 * @author Ringo Sasse
 */
function getObject(Class, ID)
{
  if (!ID) ID = 0;

  Index = Class;
  Index += "___" + ID;
	
  if (Class && _objects[Index])
    return _objects[Index];
  
  createStr =	"_objects['"+Index+"'] = new "+Class+"('"+ID+"');";
  eval(createStr);
  return _objects[Index];
}

/**
 * Set a global object, that can't be created within getObject().
 *
 * getObject() can use only one parameter for creating new objects, create
 * object yourself and put it into global object pool to access it later.
 *
 * @param The class (as string)
 * @param The id, used as first parameter when creating a new instance
 * @param An object
 *
 * @author Ringo Sasse
 */
function setObject(Class, ID, Object)
{
  if (!ID) ID = 0;

  Index = Class;
  Index += "___" + ID;
	
  _objects[Index] = Object;
}

/**
 * Let your script sleep.
 *
 * This function is for testing only, most browser will block this.
 *
 * @param Time to sleep in milliseconds
 *
 * @author Ringo Sasse
 */
function sleep(time)
{
  var _now = new Date();
  var _endtime = _now.getTime() + time;
  
  while (true)
  {
    _now = new Date();
    
    if (_now.getTime() > _endtime) return ;
  }
}

/**
 * The missed getElementsByClassName.
 *
 * Use it like getElementsByTagName ...
 *
 * @param A tagname
 * @param A classname
 *
 * @author Ringo Sasse
 *
 * TODO Make it working without setting a tagname
 */
document.getElementsByClassName = function (tagname, classname)
{
  var _elements = document.getElementsByTagName(tagname);
  var _found = new Array();
  
  for (i=0; i<_elements.length; i++)
  {
    if (_elements[i].className==classname) _found[_found.length] = _elements[i];
  }
  
  return _found;
}

/**
 * Browser check
 */
function browsercheck()
{
  this.ver = navigator.appVersion
  this.agent = navigator.userAgent
  this.dom = document.getElementById?1:0
  
  this.opera5 = this.agent.indexOf("Opera 5")>-1;
  
  this.ie5 = (this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
  this.ie6 = (this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  this.ie4 = (document.all && !this.dom && !this.opera5)?1:0;
  this.ie = this.ie4 || this.ie5 || this.ie6
  
  this.mac = this.agent.indexOf("Mac")>-1;
  
  this.ns6 = (this.dom && parseInt(this.ver) >= 5) ?1:0; 
  this.ns4 = (document.layers && !this.dom)?1:0;
  
  this.checked = (this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
}

var bc = new browsercheck()

 
/* Set the opacity of an element */
function set_opacity(element, opacity)
{
  if (element.style)
  {
    var _style = element.style;
    
    /* CSS 3 */
    _style.opacity = (opacity / 100);
    
    /* Mozilla */
    _style.MozOpacity = (opacity / 100);
    
    /* K-HTML (Konqueror/Safari) */
    _style.KhtmlOpacity = (opacity / 100);
    
    /* MS IE */
    _style.filter = "alpha(opacity=" + opacity + ")";
  }
}

document.linktable = new Array();

/* Disable links */
function disable_links(tdocument)
{
  if (navigator.userAgent.match(/MSIE/))
  {
    for (var i = 0; i < tdocument.links.length; ++i)
    {
      tdocument.linktable[i] = new Array();
    
      tdocument.linktable[i]["html"] = tdocument.links[i].outerHTML;
    
      tdocument.links[i].href = "javascript: void 0;";
      tdocument.links[i].onclick = "void 0;";
    }
  }
  else
  {
    for (var i = 0; i < tdocument.links.length; ++i)
    {
      tdocument.linktable[i] = new Array();
    
      tdocument.linktable[i]["href"] = tdocument.links[i].href;
      tdocument.linktable[i]["onclick"] = tdocument.links[i].onclick;
    
      tdocument.links[i].href = "javascript: void 0;";
      tdocument.links[i].onclick = "void 0;";
    }
  }
}

/* Enable links */
function enable_links(tdocument)
{
  if (! tdocument.linktable)
    return;

  if (navigator.userAgent.match(/MSIE/))
  {
    for (var i = 0; i < tdocument.links.length; ++i)
    {
      tdocument.links[i].outerHTML = tdocument.linktable[i]["html"];
    }
  }
  else
  {
    for (var i = 0; i < tdocument.links.length; ++i)
    {
      tdocument.links[i].href = tdocument.linktable[i]["href"];
      tdocument.links[i].onclick = tdocument.linktable[i]["onclick"];
    }
  }
}

function hide_bytagname(tdocument, tagname)
{
  var _applets = document.getElementsByTagName(tagname);
  
  if (_applets.length == 0) return;
  
  for (i=0; i<_applets.length; i++)
    _applets[i].style.visibility = "hidden";
}

function show_bytagname(tdocument, tagname)
{
  var _applets = document.getElementsByTagName(tagname);
  
  if (_applets.length == 0) return;
  
  for (i=0; i<_applets.length; i++)
    _applets[i].style.visibility = "visible";
}

/* Set all children of the given element opaque */
function hide_content(parent)
{
  disable_links(parent.document);
  hide_bytagname(parent.document, "applet");
  if (navigator.userAgent.match(/MSIE/)) hide_bytagname(parent.document, "select");
  
  _child = parent.document.body.firstChild;
  
  while (_child)
  {
    _next = _child.nextSibling;
    
    set_opacity(_child, 30);
    
    _child = _next;
  }
}

/*  */
function show_content(parent)
{
  enable_links(parent.document);
  show_bytagname(parent.document, "applet");
  if (navigator.userAgent.match(/MSIE/)) show_bytagname(parent.document, "select");
  
  _child = parent.document.body.firstChild;
  
  while (_child)
  {
    _next = _child.nextSibling;
    
    set_opacity(_child, 100);
    
    _child = _next;
  }
}

function create_iframe(id, url, width, height)
{
  Pointer = getObject("MousePointer");

  _cw = screen_width();
  if (parseInt(Pointer.getLeft())+parseInt(width)>parseInt(_cw)
      && (parseInt(Pointer.getLeft()) - parseInt(width)) > 0)
  {
    _left = (parseInt(Pointer.getLeft()) - parseInt(width)) + "px";
  }
  else if (parseInt(Pointer.getLeft())+parseInt(width)>parseInt(_cw))
  {
    // Can't position iframe on left side, but it's to large ... resize.
    _left = Pointer.getLeft();
    width = (parseInt(_cw) - parseInt(Pointer.getLeft()) - 4) + "px";
  }
  else
  {
    _left = Pointer.getLeft();
  }
  
  _ch = screen_height();
  if (parseInt(Pointer.getTop())>parseInt(height) && parseInt(Pointer.getTop())+parseInt(height)>parseInt(_ch))
  {
    _top = (parseInt(Pointer.getTop()) - parseInt(height)) + "px";
  }
  else
  {
    _top = Pointer.getTop();
  }
  
  create_abspos_iframe(id, url, width, height, _left, _top);
}

function create_abspos_iframe(id, url, width, height, left, ttop)
{
  if (!document.body.appendChild) 
  	return;
  
  hide_content(window);
  
  if (document.body && document.body.scrollTop)
    ttop += document.body.scrollTop;
  else if (document.documentElement && document.documentElement.scrollTop)
    ttop += document.documentElement.scrollTop;
  else if (window.pageYOffset)
    ttop += window.pageYOffset;
  else
    window.scrollTo(0, 0);
  
  if (navigator.userAgent.match(/MSIE/))
  {
    // This is for m$
    if (!document.getElementById(id))
    {
      ttop = parseInt(ttop) + "px";
      left = parseInt(left) + "px";
      width = parseInt(width) + "px";
      height = parseInt(height) + "px";
    
      iframeHTML = '\<iframe src="' + url + '" id="' + id + '" frameborder="0" style="border: 1px outset #FFFFFF; position: absolute; top: ' + ttop +'; left: ' + left + '; width:' + width + '; height:' + height + '; " scrolling="auto"><\/iframe>';
      document.body.innerHTML += iframeHTML;
    }
    else
    {
      document.getElementById(id).style.left = left;
      document.getElementById(id).style.top = ttop;
    }
    
/*    IFrameObj = new Object();
    IFrameObj.document = new Object();
    IFrameObj.document.location = new Object();
    IFrameObj.document.location.iframe = document.getElementById(id);
    IFrameObj.document.location.replace = function (location) { this.iframe.src = location; }
    IFrameObj.document.location.replace(url);
*/
    return;
  }
  else
  {
    // And this for the rest of the world
    if (document.getElementById(id))
    {
      _frame = document.getElementById(id);
    }
    else
    {  
      _frame = document.createElement("IFRAME");
    }
  
    _frame.id = id;
    
    _frame.src = url;
  
    _frame.style.position = "absolute";
    _frame.style.left = parseInt(left) + "px";
    _frame.style.top = parseInt(ttop) + "px";
    _frame.style.border = "1px outset gray";
    
    _frame.height = height;
    _frame.width = width;
  
    document.body.appendChild(_frame);
  }
}

function close_iframe(id)
{
  if (parent)
  {
    if (parent.document.getElementById(id))
    {
      _frame = parent.document.getElementById(id);
      
      show_content(parent.window);
      
      parent.document.body.removeChild(_frame);
    }
  }
}

function resize_textarea(formid, textid)
{
  var _addrest = 20;
  var _textarea = document.forms[formid].elements[textid];
  
  if (_textarea.clientHeight)
  {
    var _ch = _textarea.clientHeight;
    var _cbody = document.body.clientHeight;
    
    var _rest = parseInt(_cbody) - parseInt(_ch);
    var _height = parseInt(screen_height()) - _rest - _addrest;
    
    _textarea.style.height = _height + "px";

    if(_height < 150)
      _textarea.style.height = "150px";
  }
  
  /* Applet IDs defined in EISStandardLayoutImpl#writeTextareaStyledEnd() */
  var _applet = document.getElementById(textid + "_applet");
    
  if (_applet && _applet.clientHeight)
  {
    var _ch = _applet.clientHeight;
    var _cbody = document.body.clientHeight;
    
    var _rest = parseInt(_cbody) - parseInt(_ch);
    
    _applet.style.height = (parseInt(screen_height()) - _rest - _addrest) + "px";
  }
}

function resize_textarea_new (formid, textid) 
{
	var _textarea = document.forms[formid].elements[textid];

	var _centerTextarea = function() 
	{
		if (_textarea.scrollIntoView) 
		{
			_textarea.scrollIntoView(false);
		} 
		else 
		{
			_textarea.wrappedJSObject.scrollIntoView(false);
		}
	};
				    
	var _textareaKeydown = function(e) 
	{
		if (e.shiftKey && e.ctrlKey && e.keyCode == 13) 
		{
			// shift-ctrl-enter
			_textarea.rows -= 1;
			_centerTextarea();
		}
		else if (e.shiftKey && e.ctrlKey && e.keyCode == 32) 
		{
			// shift-ctrl-space
			_textarea.cols -= 1;
			_centerTextarea();
		}
		else if (e.ctrlKey && e.keyCode == 13) 
		{
			// ctrl-enter
			if (_textarea.offsetHeight < window.innerHeight - 40) 
			{
				_textarea.rows += 1;
			}
			_centerTextarea();
		}
		else if (e.ctrlKey && e.keyCode == 32) 
		{
			// ctrl-space
			if (_textarea.offsetWidth < window.innerWidth - 40) 
			{
				_textarea.cols += 1;
			}
			_centerTextarea();
		}
    };

	addEvent(_textarea, 'keydown', _textareaKeydown, false);
					
	function addEvent(elm, evType, fn, useCapture)
	{
		if (elm.addEventListener) 
		{
			elm.addEventListener(evType, fn, useCapture);  
			return true;  
		} 
		else if (elm.attachEvent) 
		{
		   var _r = elm.attachEvent('on' + evType, fn);  
		   return _r;  
		} 
		else 
		{
			elm['on' + evType] = fn;
		}
	}
}

function resize_calendarbody()
{
  var _calendarbody = document.getElementById("calendarbody");
  var _calendarcontainer = document.getElementById("calendarcontainer");
  
  if (navigator.userAgent.match(/ecko/)
      && _calendarbody.clientHeight
      && _calendarcontainer.clientHeight)
  {
    // FIXME Special handling for mozilla disabled ... calculation of height was completely buggy ...
    // _calendarbody.style.height = _height + "px";
    _calendarbody.style.height = "auto";
    _calendarbody.style.overflow = "auto";
  }
  else
  {
    _calendarbody.style.height = "auto";
    _calendarbody.style.overflow = "auto";
  }
}


function screen_width()
{
  _p = window;
	  
	// Going up to top ...
	// if (_p.frameElement)
	//  while (_p.frameElement.nodeName.toLowerCase()=="iframe")
	//    _p = _p.parent;
	  
  // displayAttributes(_p);
  
  var _w = 0;

  if (navigator.userAgent.match(/ecko/))
  {
    _w = _p.innerWidth;
  }
  else if (_p.document.body.clientWidth)
  {
    _w = _p.document.body.clientWidth;
  }
  else
  {
    _w = 800;
  }
  if (parseInt(_w)<0) 
    return 800;

  return parseInt(_w);
}

function screen_height()
{
  var _h = 0;

  if (navigator.userAgent.match(/ecko/))
  {
    _h = window.innerHeight;
  }
  else if (document.body.clientHeight)
  {
    var _cbh = document.body.style.height;
    document.body.style.height = "100%";
    _h = document.body.clientHeight - 24;
    document.body.style.height = _cbh;
  }
  else
  {
    _h = 600;
  }

  if (parseInt(_h)<0) 
    return 600;
  return parseInt(_h);
}

function displayAttributes(obj, ignoreattr)
{
  if (!ignoreattr) ignoreattr = "";
  
  _text = "";
  _html = "<html><body style=\"margin: 0px; padding: 0px; border: none;\"><table cellspacing=\"1\" cellpadding=\"3\" width=\"100%\">";
  
  _line = 0;
  
  _idx = 0;
  
  for (_key in obj)
  {
    _idx++;
  
    if (ignoreattr.indexOf(_key)<0)
    {
      if (_line % 2==0)
        _bgcolor = "#FFF7F7";
      else
        _bgcolor = "#FFFFFF";
    
      if (typeof obj[_key] != "function" && !_key.match(/^\_/) && !_key.match(/innerHTML/))
      {
        _text += _key + "= " + obj[_key] + " / ";
        if (_idx % 10 == 0) _text += "\n";
        
        _html += "<tr valign=\"top\"><td bgcolor=\"" + _bgcolor + "\" width=\"100\"><b>" + _key + ":</b></td><td bgcolor=\"" + _bgcolor + "\">" + obj[_key] + "</td></tr>";
      
        _line++;
      }
    }
  }
  
  _html += "</table></body></html>";
  
  if (true)
  {
    alert(_text);
  }
  else
  {
    _win = window.open("", "", "width=480,height=300");
    _win.document.open();

    _win.document.write(_html)
  
    _win.document.close();
  }
}


function selectAll()
{
  var _elements = document.getElementsByTagName("input");
  var _i=0;
  for (_i=0;_i<_elements.length;_i++)
  {
    if (_elements[_i].getAttribute("type")!=null && _elements[_i].getAttribute("type")=='checkbox' && _elements[_i].getAttribute("onclick")!=null  && _elements[_i].getAttribute("onclick")!="")
    {
      if (_elements[_i].getAttribute("id")==null || _elements[_i].getAttribute("id").indexOf("check")<0)
      {
        if (_elements[_i].getAttribute("name")==null || _elements[_i].getAttribute("name").indexOf("check")<0)
         _elements[_i].click();
      }
    }
  }
}

/**
 * Equal to document.getElementByID - but better for testing.
 */
function findElementByID(anID, aParentElement)
{
  if (! aParentElement)
    aParentElement = document.body;
    
  for (var _i = 0; _i < aParentElement.childNodes.length; _i++)
  {
    _child = aParentElement.childNodes[_i];
    
    if (_child.id
        && _child.id == anID)
      return _child;
      
    if (_child.innerHTML
        && _child.innerHTML.length > 0
        && _child.innerHTML.match(/anID/))
      return _child;
      
    var _childchild = findElementByID(anID, _child);
    
    if (_childchild)
      return _childchild;
  }
  
  return null;
}

/**
 * Try to find a number within given html element,
 * decrease the found and put it into element.
 */
function decreaseNumber(elementID)
{
  var _element = document.getElementById(elementID);

  if (_element)
  {
    var _str = _element.innerHTML;
    var _int = parseInt(_str, 10);
    
    if (_int && _int > 0)
    {
      var _dint = _int - 1;
      
      _element.innerHTML = "" + _dint;
    }
  }
}


/**
 * toggle the display-stayle of the array of element-ids.
 */
function toggleElements(elements)
{
  var elementIDs=elements.split(",");
  for (_i=0;_i<elementIDs.length;_i++)
  {
    var _element = document.getElementById(elementIDs[_i]);
    if (_element.style.display=='none')
      _element.style.display='block';
    else
      _element.style.display='none';
    
  }
}


/**
 * toggle the display-stayle of the array of element-ids.
 */
function toggleImage(image, off, on)
{
  if (image.src.indexOf(off)<0)
    image.src=off;
  else 
    image.src=on;
}