var dependent_window_reference;

function TreeScroll() {
  this.frame        = null;
  this.focus        = 0;
  this.offset       = 0;
  this.height       = 0;
  this.vector       = null;
  this.setFrame     = setTreeFrame;
  this.setFocus     = setTreeFocus;
  this.getFocus     = getTreeFocus;
  this.setVector    = setTreeVector;
  this.getVector    = getTreeVector;
  this.updateOffset = updateTreeOffset;
  this.scrollTo     = scrollToTree;
}

function setTreeFrame(myFrame) {
  this.frame = myFrame;
  if (this.frame != null){
    if (document.all) {
      this.height = this.frame.document.body.clientHeight;
    }else {
      this.height = this.frame.innerHeight;
    }
  } else {
    this.height = 0;
  }
}

function setTreeFocus(myFocus) {
  this.focus = myFocus * 18;
}
function getTreeFocus() {
  return this.focus;
}
function updateTreeOffset() {
  if (this.frame != null){
    if (document.all) {
      this.offset = this.frame.document.body.scrollTop;
    }else {
      this.offset = this.frame.pageYOffset;
    }
  }
}
function setTreeVector(myVector) {
  this.vector = myVector;
}
function getTreeVector() {
  return this.vector;
}
function scrollToTree(){
  if (this.frame) {
    if (this.focus < this.offset || this.focus > this.offset + this.height){
      this.offset = this.focus;
    }
    this.frame.scrollTo(0,this.offset);
    //alert("Offset:"+this.offset+", focus:"+this.focus+", height:"+this.height);
  }
}

function deCodeHTML(stringName){
  var muster = new Array(/&amp;/g,/&quot;/g,/&lt;/g,/&gt;/g);
  var ersatz = new Array("&","\"","<",">");
    for (i = 0; i < muster.length; i++){
      if (stringName.search(muster[i]) > -1){
        stringName = stringName.replace(muster[i],ersatz[i]);
      }
    }
  return stringName;
}

function Wechseln(navdatei, inhdatei)
{
  // waiting for frame[0], with sleep or timer???
  if (top.frames[0].disable_top != null)
  {
    if ( top.frames[0].initialized )
    {
      top.frames[0].disable_top();
    }
  }
  top.parent.frames[1].document.location = navdatei;
  top.parent.frames[3].document.location = inhdatei;
}

function OffneFenster(dateiname,fenstername,breite,hoehe,sx,sy) {
  var fenster = window.open (dateiname, fenstername, "resizable=yes,toolbar=no,location=no,directories=no,scrollbars=no,status=no,menubar=no,width="+breite+",height="+hoehe+",left="+sx+",top="+sy+"");
  fenster.focus();
}

function OeffneSucheFenster(params)
{
  var fenster = window.open ("/SearchMdsShowWindow.do"+params, "", "resizable=yes,toolbar=no,location=no,directories=no,scrollbars=no,status=no,menubar=no,width=680,height=570,left=1,top=1");
  fenster.focus();
  return fenster;
}

function getOffneFenster(dateiname,fenstername,breite,hoehe,sx,sy)
{
  var fenster = window.open (dateiname, fenstername, "resizable=yes,toolbar=no,location=no,directories=no,scrollbars=no,status=no,menubar=no,width="+breite+",height="+hoehe+",left="+sx+",top="+sy+"");
  fenster.focus();
  return fenster;
}

function close_dependent_window() { // schliesst abhaengige Fenster (z. B. Suche)
  if (dependent_window_reference != null && !dependent_window_reference.closed) {
    dependent_window_reference.close();
    dependent_window_reference = null;
  }
}

// set_dependent_window schliesst das alte und speichert Referenz auf das neue abhaengige Fenster.
// Aus NS-Gruenden: Schliessen-Funktion close_dependent_window() explizit vor dem Aufruf dieser Funktion ausfuehren,
//                  set_dependent_window mit setTimeout("set_dependent_window(new_window)",1) aufrufen.
//                  Ansonsten kann NS die Referenz des neuen Fensters mit dem alten ueberschreiben.
function set_dependent_window(new_window) {
  close_dependent_window();
  var millis = new Date().getTime();
  while (dependent_window_reference != null && dependent_window_reference.closed == false) { // sonst gibt's Synchronisationsprobleme
    if (new Date().getTime() > millis + 5000) {
      dependent_window_reference = null;
      //alert("Timeout !!!");
      break;
    }
  }
  dependent_window_reference = new_window;
}

function div(divname){
  this.divname  = divname;
}

// Instance a div-object-constructor
obj_div = new div('');

// Navigation set in highlightet condition
function DivHighlight(divname){
  if (obj_div.divname != divname){

    // It is for the Browser IE, NS6
    if (this.document.all){
      self.document.all[divname].style.backgroundColor ="#FFCEA0"; // hell-orange
    }
    else if (this.document.getElementById){
      self.document.getElementById(divname).style.background="#FFCEA0";
    }
  }
}

// Navigation set in normal condition
function DivNormal(divname){
  DivNormal_1(divname,0);
}

function DivNormal_1(divname, resetFix){
  // Reset if highlightet layer not equals Paramenter 'divname'
  // or reset is forced from DoMark
  if (obj_div.divname != divname || resetFix== 1){

    // It is for the Browser IE, NS6
    if (this.document.all){
      self.document.all[divname].style.backgroundColor ="#E4E7E6"; // Grau
    }
    else if (this.document.getElementById) {
      self.document.getElementById(divname).style.background = "#E4E7E6";
    }
  }
}

// Navigation permanently mark
function DoMark(divname){
  if (obj_div.divname != divname && obj_div.divname !=''){
    DivNormal_1(obj_div.divname, 1);
  }

  obj_div.divname = divname;

  // It is for the Browser IE, NS6
  if (this.document.all){
    self.document.all[divname].style.backgroundColor ="#FFCEA0"; // hell-orange
  }
  else if (this.document.getElementById){
    self.document.getElementById(divname).style.background="#FFCEA0";
  }
}

function callMDSystem(language) {
  systemLocation = "/accessSystem.do?forceRefresh=" + new Date().getTime() +"&lang="+ language;
  OffneFenster(systemLocation,"Hauptfenster",800,600,0,0);
}

// get the window object that contains the default_loc.jsp.
// usually the top object, this function is helpful for JSP that starts within a separate window
//
function getTopWindow() {
  var myWin = top;

  while (!myWin.frames[0] || myWin.frames[0].name != 'iconbar') {
    if (!myWin.opener) return null;
    myWin = myWin.opener.top;
  }
  return myWin;
}


// notify that a page is loaded, actions like disableWaitDiv can start here.
// To use this feature, this function MUST call from the init function (onLoad="javascript:init()")
// of the JSP
//
function pageLoaded( thePage ) {
  var winTop = getTopWindow();
  if ( winTop.thePageWaitFor == thePage || winTop.thePageWaitFor == "") {
    winTop.iconbar.disableWaitDiv();
    winTop.thePageWaitFor = "";
  }

}

// specify the page that the called action is waiting for. The WaitDiv is activated until the "waitFor"
// page is loaded.
//
function waitFor( thePage ) {
  var winTop;

  winTop = getTopWindow();
  winTop.thePageWaitFor = thePage;
  winTop.iconbar.enableWaitDiv();
}

function buttonClicked( ) {

}

function genericEncodeURIComponent(text) {
  if (typeof encodeURIComponent != "undefined") { // since JS 1.5
    return encodeURIComponent(text);
  }
  else { // for older browsers (IE <= 5.5, NS < 6)
    return escape(text).replace(/\//g, "%2F").replace(/\+/g, "%2B").replace(/@/g, "%40"); // replace "/", "+" and "@" manually
  }
}

function disableCopyPasteIcons(iconBarWindow){
  iconBarWindow.disablePaste();
  iconBarWindow.disableCopy();
}
