/**
 * Do simply nothing (sometimes required as action).
 *
 * @author Ringo Sasse
 */
noop = function()
{
  // Nothing to do
}

// can be assigned to tell the open_url method, that we are in an iframe
var _iframe_def = '';

/**
 * Open an url
 *
 * @param The url to open
 * @param The (optional) target inside the current window, can be another frame
 * @param close_iframe (optional) if true, close the current iframe
 * @author Ringo Sasse
 */
open_url = function(url, target, close_iframe)
{
  if (close_iframe && (_iframe_def != ''))
  {
    parent.location.replace(url);
  }
  else
  {
    if (target)
    {
      if (target == '_top')
        top.location.replace(url);
      else if ( top[target]!=null)
        top[target].location.replace(url);
      else
        window.open(url,target);
    }
    else
      location.replace(url);
  }
}

/**
 * Open an url in a new window
 *
 * @param The window name
 * @param The url to open
 *
 * @author Ringo Sasse
 */
open_window = function(windowname, url, width, height, parameter)
{
  if (!windowname)
  {
    var _now = new Date();

    windowname = _now.getMilliseconds();
  }

  if (! parameter
      || parameter.length == 0)
  {
    var _w = window.open(url, windowname);
    _w.resizeTo(width, height);
  }
  else
  {
    parameter += ",";

    var _w = window.open(url, windowname, parameter + "width=" + width + ",height=" + height);
  }
}

/**
 * Reload the current document
 *
 * @param The (optional) target inside the current window, can be another frame
 *
 * @author Ringo Sasse
 */
reload_document = function(target)
{
  if (target)
  {
    if (target == '_top')
      top.location.reload();
    else if (top[target]!=null && top[target].location!=null)
      top[target].location.reload();
  }
  else
    location.reload();
}

/**
 * Reload the current document
 *
 * @param The (optional) target inside the current window, can be another frame
 *
 * @author Ringo Sasse
 */
reload_document_with_params = function(target, params)
{
  var _tf;

  if (target == '_top')
    _tf = top;
  else if (top[target]!=null && top[target].location!=null)
    _tf = top[target];

  var _href = _tf.location.href;
  
  if (params)
    _href += "&" + encodeURI(params);

  _tf.location.replace(_href);
}

/**
 * Reload the parent
 *
 */
reload_parent = function()
{
  parent.location.reload();
}

/**
 * Submit a form
 *
 * @param formid Id/name of the form that should be submitted
 * @param Additional parameters for form action
 *
 * @author Ringo Sasse
 *
 * TODO Split "addtoaction" and check if this is a form field and overwrite
 */
submit_form = function(formid, addtoaction)
{
  var _form = document.forms[formid];

  if (addtoaction)
  {
	  if (_form.action.indexOf('?')>0)
	    _form.action = _form.action + "&" + addtoaction;
	  else
	    _form.action = _form.action + "?" + addtoaction;

  }

  try
  {
     on_submit();
  }
  catch (e)
  {
    ;
  }

  _form.submit();
}

/**
 * Reset a form
 *
 * @param formid Id/name of the form that should be submitted
 *
 * @author Ringo Sasse
 */
reset_form = function(formid)
{
  var _form = document.forms[formid];

  _form.reset();
}

/**
 * Reset a form and submit.
 *
 * @param formid Id/name of the form that should be submitted
 *
 * @author Ringo Sasse
 */
resetandsubmit_form = function(formid)
{
  reset_form(formid);
  submit_form(formid);
}

/**
 * Set a value at a form element
 *
 * @param formid Id/name of the form that should be submitted
 * @param The element id
 * @param The new value
 *
 * @author Ringo Sasse
 *
 * TODO Create new hidden formelement if the element doesn't exists
 */
set_formelement = function(form, element, value)
{
  var f = document.forms[form];
  if (! f )
    return;

  var e = f.elements[element];
  if (! e )
  {
	  var newe = document.createElement('input');
	  newe.type='hidden'
	  newe.setAttribute('id',element);
	  newe.setAttribute('name',element);
	  newe.value=value;
	  f.appendChild(newe);
  }
  else
	  e.value = value;
}

/**
 * Set the text in statusbar.
 *
 * @param aText The text to display in statusbar
 *
 * @author Ringo Sasse
 */
setstatusbartext = function(aText)
{
  if (window.status)
    window.status = aText;
}

/**
 * Toggle the state of an element (display, if invisible and hide, if visible)
 *
 * @param anElementID The element identifier
 *
 * @author Ringo Sasse
 */
toggledisplay = function(anElementID)
{
  obj = document.getElementById(anElementID);

  if (obj != null)
  {
    cdispl = obj.style.display;

    if (cdispl == "none")
    {
      obj.style.display = "block";
    }
    else
    {
      obj.style.display = "none";
    }
  }
}