var intBrowser  = checkBrowser();
var ePopup      = false;
var oNGen       = new NamensGenerator('oNGen','/shadowworld/');

function checkBrowser()
{
  // Testen welcher Browser verwendet wird.

  var strCodeName = navigator.appCodeName.toUpperCase();
  var strName = navigator.appName.toUpperCase();
  var strVersion = navigator.appVersion.toUpperCase();
  var strAgent = navigator.userAgent.toUpperCase();
  var intBrowser = 0;

  if( strName.search(/MICROSOFT INTERNET EXPLORER/)>-1 && strVersion.search(/MSIE/)>-1 && strAgent.search(/MSIE/)>-1) {
      // IE
      intBrowser = 1;
  }
  else if( strName.search(/NETSCAPE/)>-1 && strAgent.search(/MOZILLA/)>-1 && strAgent.search(/FIREBIRD/)>-1 ) {
      // Firebird
      intBrowser = 3;
  }
  else if( strName.search(/NETSCAPE/)>-1 && strAgent.search(/MOZILLA/)>-1 ) {
      // Mozilla
      intBrowser = 2;
  }
  else if( strName.search(/KONQUEROR/)>-1 && strAgent.search(/KONQUEROR/)>-1 ) {
      // Konqueror
      intBrowser = 5;
  }

  if( window.opera ) {
      // Opera
      intBrowser = 4;
  }
  return( intBrowser );
}


function removeOptionSelected( id )
{
  var elSel = document.getElementById( id );
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}


function selectAllConns( selObj ) {

  var i;

  for (i = 0; i < selObj.length; ++i)
    selObj.options[i].selected = true;
}


function insertAtCursor(myField, myValue ) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;

  //MOZILLA/NETSCAPE support
  } else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
}


function insertBlockAtCursor(myField, myStart, myEnd ) {

  // Internet Explorer
  if(document.selection){
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myStart + sel.text + myEnd;

  // Mozilla & Co.
  } else if(typeof(myField.selectionStart) != 'undefined' && typeof(myField.selectionEnd) != 'undefined' ) {
    strSel  = window.getSelection();
    nStart  = myField.selectionStart;
    nEnd    = myField.selectionEnd;
    myField.value = myField.value.substring(0, nStart) + myStart + myField.value.substring(nStart, nEnd) + myEnd + myField.value.substring(nEnd, myField.value.length);

  } else if(document.getSelection) {
    myField.value += myStart+myEnd;

  // put at end, if no better position found
  } else {
    myField.value += myStart+myEnd;
  }
  return;

  neuesarray = myField.value.split(selection);

  if (selection != "" && neuesarray.length == 2) {
    var start = textarea.value.indexOf(selection);
    var end = start+selection.length;
    var davor = textarea.value.substr(0,start);
    var danach = textarea.value.substr(end);
  }
  return;
}


function myClick( strRef, nType ) {
  elName = document.getElementById(strRef);
  switch( nType ) {
    case 1:     // break
      insertAtCursor( elName, '[[br:]]\n' );
      break;
    case 2:     // paragraph
      insertBlockAtCursor( elName, '\n<p>','</p>\n' );
      break;
    case 3:     // emphazise
      insertBlockAtCursor( elName, '<em>','</em>' );
      break;
    case 101:   // picture
      alert( "\nBitte tragt ein:\n1. bei SRC die URL des Bildes\n2. bei ALT einen Text mit Beschreibung\n\nHinweise:\nDas Bild wandert an die rechte Seite. Wenn ihr es links haben wollt, dann müsst ihr das \"right\" durch ein \"left\" ersetzen.\n\nDie URL der Bilder auf diesem Server könnt ihr im Menü->Autor->Bilder finden. Externe Bilder können mit //... eingebunden werden, das \"http:\" am Anfang MUSS weggelassen werden." );
      insertAtCursor( elName, '\n[[image:SRC|right|ALT]]\n' );
      break;
    case 151:     // 6world
      insertBlockAtCursor( elName, '[[6world:',']]' );
      break;
    case 152:     // helix
      insertBlockAtCursor( elName, '[[helix:',']]' );
      break;
    case 201:   // cite
      insertAtCursor( elName, '\n<p><em>Zitate</em>\n* ...\n* ...\n</p>\n' );
      break;
    case 202:   // short data
      insertAtCursor( elName, '\n<p><em>Kurzbeschreibung</em>\n\nRealname: \n* SIN: \n* Hintergrund: \n</p>\n' );
      break;
    case 501:   // security
      insertAtCursor( elName, '\n<p><em>Sicherheit:</em>\n* Türen: <i> Magschloss(?), Metalldetektor(?), Chem-Schniffer(?)</i>\n* Personenkontrolle</b>: <i> SIN/ID-Scanner(?), Aura-Check(???)</i>\n* Wachleute: <i> ... </i>\n* Notrufsystem: <i> ... </i>\n* externe Sicherheitsfirma: <i> ... </i>\n* Matrix: <i> ... </i>\n* Sonstiges/Extras: <i> ... </i>\n</p>\n' );
      break;
    case 601:   // coop details
      insertAtCursor( elName, '\n<p>\n<em>Detailinfos:</em>\n* <b>Präsident/CEO:</b> ... \n* <b>Stammsitz:</b> ... \n* Hauptaktionäre:</b> ... \n* wichtige Persönlichkeiten:</b> ... \n* <b>Geschäftsprofil:</b> ... \n* Militär:</b> ... \n</p>\n' );
      break;
    default:
      alert( 'Fehler: noch nicht implementiert!' );
  }
  return true;
}


function myOnOver() {
  return true;
}


function showText( elName, strText ) {
  var elCode  = document.getElementById('CodeHolder');
  var elmText = document.createTextNode(strText);

  if( elCode.firstChild )
    elCode.replaceChild(elmText, elCode.firstChild);
  else
    elCode.appendChild(elmText);

  elCode.className = 'code';
  scroll(0,0);
}


/** --------------------------------------------------------------------------------
 * Class: Namensgenerator
 * ---------------------------------------------------------------------------------
 */
function NamensGenerator(strObj, baseURL) {
  this.strObj = strObj;
  this.baseURL = baseURL;
  this.eWindow = new myWindow('Namensgenerator', this.baseURL );
  this.eName = 0;
  this.eSex = 0;

  this.open = open;
  this.close = close;
  this.maximize = maximize;
  this.setFields = setFields;

  function open(strBox, strFieldName, strFieldSex) {

    var eRow, eCol, eTmp;

    if( this.eWindow.open(strBox, this.strObj) ) {
      strURL = this.baseURL + 'rnd_name.php?mode=search';
      if( strFieldName.length > 0 ) {
        strURL = strURL + '&field_name=' + strFieldName;
        this.eName = document.getElementById(strFieldName);
      }
      if( strFieldSex.length > 0 ) {
        strURL = strURL + '&field_sex=' + strFieldSex;
        this.eSex = document.getElementById(strFieldSex);
      }
      eTmp = createElement( 'iframe', '', new Array('class','src'), new Array('popupiframe',strURL), null);
      this.eWindow.setContent( eTmp );
    }

    return;
  }


  function close() {
    this.eWindow.close();
    return;
  }


  function maximize() {
    this.eWindow.maximize();
    return;
  }


  function setFields( strName, strSex ) {

    this.eName.value = strName;
    this.eSex.value = strSex;
    return;
  }

}


/** --------------------------------------------------------------------------------
 * Class: myWindow
 * ---------------------------------------------------------------------------------
 */
function myWindow( strTitle, baseURL ) {
  this.strTitle = strTitle;
  this.baseURL  = baseURL;

  this.eTable     = null;
  this.eTitle     = null;
  this.eContent   = null;
  this.eMaxi      = null;
  this.eMaxiImg   = null;
  this.maximized  = false;

  this.open = open;
  this.close = close;
  this.maximize = maximize;
  this.setContent = setContent;


  function open(strBox, strObj) {

    var eRow, eCol, eTmp;

    if( (this.eTable = document.getElementById(strBox)) ) {

      if( this.eTable.style.visibility  == 'visible' ) {
        alert( 'Das Fenster ist schon geöffnet.\n\n Bitte schliessen sie das alte Fenster bevor sie ein neues öffnen.' );
        return false;
      }

      // eTmp = createElement('caption', this.strTitle, new Array(), new Array(), this.eTable);
      // eTmp = createElement('summary', 'Die ist ein Test', new Array(), new Array(), this.eTable);

      // header
      eRow              = this.eTable.tHead.insertRow(this.eTable.tHead.rows.length);
      eRow.className    = 'popupheader';
      eRow.id           = 'popupheader';
      eRow.style.Height = '1em';
      eCol = createElement('th', ' ', new Array(), new Array(), eRow);
      eCol.style.width = "5em";
      this.eTitle = createElement('th', this.strTitle, new Array('align'), new Array('center'), eRow);
      eCol = createElement('th', '', new Array('align'), new Array('right'), eRow);
      eCol.style.width = "5em";
      this.eMaxi = createElement('a', '', new Array('class','href'), new Array('','javascript:'+strObj+'.maximize();'), eCol);
      this.eMaxiImg = createElement('img', '', new Array('class','src','alt'), new Array('w_button',this.baseURL + 'images/max.gif','M'), this.eMaxi);
      eTmp = createElement('a', '', new Array('class','href','title'), new Array('w_button','javascript:'+strObj+'.close();', 'Fenster schliessen'), eCol);
      eTmp = createElement('img', '', new Array('class','src'), new Array('w_button',this.baseURL + 'images/exit.gif'), eTmp);

      // footer
      eRow = this.eTable.tFoot.insertRow(this.eTable.tFoot.rows.length);
      eRow.className    = 'popupfooter';
      eRow.id           = 'popupfooter';
      eRow.style.Height = '1em';
      eCol=createElement('td', '', new Array('class','colspan'), new Array('popupfooter','3'), eRow);
      eTmp=createElement('a', 'Fenster schliessen', new Array('class','href','title'), new Array('','javascript:'+strObj+'.close();', 'Fenster schliessen'), eCol);

      // body
      eRow = this.eTable.tBodies[0].insertRow(this.eTable.tBodies[0].rows.length);
      eRow.className    = 'popupbody';
      eRow.id           = 'popupbody';
      eCol=createElement('td', '', new Array('class', 'colspan', 'height', 'align'), new Array('popupbody', '3', '100%', 'center'), eRow);
      this.eContent = eCol;


      // set styling
      this.eTable.style.border      = '2px solid #cccccc';
/*
      this.eTable.style.background  = '#B90000';
*/

      //this.do_restore();
      this.eMaxiImg.src         = this.baseURL + 'images/max.gif';
      this.eMaxiImg.alt         = 'M';
      this.eMaxiImg.title       = 'Fenster maximieren';
      this.eMaxi.title          = 'Fenster maximieren';
      this.maximized            = false;
      this.eTable.style.left    = Math.round((myWidth()  * 0.1)+myXOffset())+'px';
      this.eTable.style.top     = Math.round((myHeight() * 0.1)+myYOffset())+'px';
      this.eTable.style.width   = Math.round((myWidth()  * 0.8))+'px';
      this.eTable.style.height  = Math.round((myHeight() * 0.8))+'px';

      this.eTable.style.visibility  = 'visible';

    } else {
      alert( 'Fehler: invalid popup box' );
      return false;
    }

    return true;
  }

  function close() {
    this.eTable.style.visibility  = 'hidden';
    this.eTable.style.width       = '0px';
    this.eTable.style.height      = '0px';
    while(this.eTable.rows.length > 0){
      this.eTable.deleteRow(0);
    }
    return;
  }

  function maximize() {
    if( this.eTable.style.visibility  == 'visible' ) {
      if( this.maximized ) {
        // do_restore();
        this.eMaxiImg.src         = this.baseURL + 'images/max.gif';
        this.eMaxiImg.alt         = 'M';
        this.eMaxiImg.title       = 'Fenster maximieren';
        this.eMaxi.title          = 'Fenster maximieren';
        this.maximized            = false;
        this.eTable.style.left    = Math.round((myWidth()  * 0.1)+myXOffset())+'px';
        this.eTable.style.top     = Math.round((myHeight() * 0.1)+myYOffset())+'px';
        this.eTable.style.width   = Math.round((myWidth()  * 0.8))+'px';
        this.eTable.style.height  = Math.round((myHeight() * 0.8))+'px';
      } else {
        //do_max();
        this.eMaxiImg.src         = this.baseURL + 'images/restore.gif';
        this.eMaxiImg.alt         = 'R';
        this.eMaxiImg.title       = 'Fenster wiederherstellen';
        this.eMaxi.title          = 'Fenster wiederherstellen';
        this.maximized            = true;
        this.eTable.style.left    = Math.round((myWidth()  * 0.003)+myXOffset())+'px';
        this.eTable.style.top     = Math.round((myHeight() * 0.003)+myYOffset())+'px';
        this.eTable.style.width   = Math.round((myWidth()  * 0.994))+'px';
        this.eTable.style.height  = Math.round((myHeight() * 0.994)-20)+'px';   // footer counts extra
      }
    }

    return;
  }

  function do_restore() {
    this.eMaxiImg.src         = this.baseURL + 'images/max.gif';
    this.eMaxiImg.alt         = 'M';
    this.eMaxiImg.title       = 'Fenster maximieren';
    this.eMaxi.title          = 'Fenster maximieren';
    this.maximized            = false;
    this.eTable.style.left    = Math.round((myWidth()  * 0.1)+myXOffset())+'px';
    this.eTable.style.top     = Math.round((myHeight() * 0.1)+myYOffset())+'px';
    this.eTable.style.width   = Math.round((myWidth()  * 0.8))+'px';
    this.eTable.style.height  = Math.round((myHeight() * 0.8))+'px';
    return;
  }

  function do_max() {
    this.eMaxiImg.src         = this.baseURL + 'images/restore.gif';
    this.eMaxiImg.alt         = 'R';
    this.eMaxiImg.title       = 'Fenster wiederherstellen';
    this.eMaxi.title          = 'Fenster wiederherstellen';
    this.maximized            = true;
    this.eTable.style.left    = Math.round((myWidth()  * 0.003)+myXOffset())+'px';
    this.eTable.style.top     = Math.round((myHeight() * 0.003)+myYOffset())+'px';
    this.eTable.style.width   = Math.round((myWidth()  * 0.994))+'px';
    this.eTable.style.height  = Math.round((myHeight() * 0.994)-20)+'px';   // footer counts extra
    return;
  }

  function setContent( eContent ) {
    this.eContent.appendChild(eContent);
    return;
  }

}

/** -----------------------------------------------------------------------------------
 * TOOLS
 * ------------------------------------------------------------------------------------
 */
function myHeight() {

  if (self.innerHeight) {
    return self.innerHeight - 20;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    return document.documentElement.clientHeight - 60;
  } else {
    return window.innerHeight - 16;
  }
}


function myWidth() {

  if (self.innerWidth) {
    return self.innerWidth - 20;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    return document.documentElement.clientWidth - 20;
  } else {
    return window.innerWidth - 16;
  }
}


function myXOffset() {
  if( intBrowser == 1 ) {
    return document.documentElement.scrollLeft;
  } else {
    return window.pageXOffset;
  }
}


function myYOffset() {
  if( intBrowser == 1 ) {
    return document.documentElement.scrollTop;
  } else {
    return window.pageYOffset;
  }
}


function createElement(strType, strText, aAttributes, aValues, eTarget) {

  var elm=document.createElement(strType);

  for(var i=0; i < aAttributes.length; i++) {
    switch(aAttributes[i].toLowerCase()) {
      case "colspan":
        elm.colSpan=aValues[i];
        break;
      case "rowspan":
        elm.rowSpan=aValues[i];
        break;
      case "class":
      case "classname":
        elm.className=aValues[i];
        break;
      case "width":
        elm.width=aValues[i];
        break;
      case "height":
        elm.height=aValues[i];
        break;
      default:
        var att=document.createAttribute(aAttributes[i]);
        att.nodeValue=aValues[i];
        elm.setAttributeNode(att);
        break;
    }
  }

  if(strText !=''){
    var elmText=document.createTextNode(strText);
    elm.appendChild(elmText);
  }

  if(eTarget !=null){
    eTarget.appendChild(elm);
  }

  return elm;
}


/** -----------------------------------------------------------------------------------
 * obsolete?
 * ------------------------------------------------------------------------------------
 */
function hidebox( strObjName ) {

  if( eIFrame = document.getElementById(strObjName) ) {
    eIFrame.style.visibility = "hidden";
  } else {
    alert( 'Fehler: no popup box' );
  }
  return;
}


function myTest() {
  alert('myTest');
}

