
// Fenster
function winopen(url, breite, hoehe, scrolling) {
  window.open(url, "Info", "width=" + breite + ",height=" + hoehe + ",scrollbars=" + scrolling + ",resizable=yes");
}


// Formularverwaltung
function mbbCode (container) {
  this.container = container;
  this.fenster = new Array('mbbURL', 'mbbImage', 'mbbColor', 'mbbDef', 'mbbSmiley');
  this.eingaben = new Array('mbbURL1', 'mbbURL2', 'mbbImage1', 'mbbDef1', 'mbbDef2');

  this.einfuegenURL = function (url, beschreibung) {
    if (url != '' && beschreibung != '') {
      this.einfuegen('[' + url.replace(/ /, '%20') + ' ' + beschreibung + ']', '');

    } else if (url != '') {
      this.einfuegen('[' + url.replace(/ /, '%20') + ']', '');
    }
  }

  this.einfuegenImage = function (url, ausrichtung) {
    if (url != '' && ausrichtung != '') {
      this.einfuegen('[[Image:' + url.replace(/ /, '%20') + '|' + ausrichtung + ']]', '');

    } else if (url != '') {
      this.einfuegen('[[Image:' + url.replace(/ /, '%20') + ']]', '');
    }
  }

  this.einfuegenColor = function (farbe) {
    if (farbe != '') {
      this.einfuegen('##' + farbe + '|', '##');
    }
  }

  this.einfuegenDef = function (begriff, definition) {
    if (begriff != '' && definition != '') {
      this.einfuegen(': ' + begriff + ' : ' + definition + '\n', '');
    }
  }

  this.positionSpeichern = function () {
    // IEs Extrawurst
    if (typeof document.selection != 'undefined') {
      this.position = document.selection.createRange();
    }
  }

  this.einfuegen = function (aTag, eTag) {
    // Fügt aTag und eTag vor und hinter dem ausgewählten Text ein
    //   Quelle: http://aktuell.de.selfhtml.org/tippstricks/javascript/bbcode/index.htm

    this.container.focus();

    // für IE
    if (typeof document.selection != 'undefined' && this.position) {
      // Einfügen des Formatierungscodes
      var range = this.position;
      var insText = range.text;
      range.text = aTag + insText + eTag;

      // Anpassen der Cursorposition
      range = document.selection.createRange();
      if (insText.length == 0) {
        range.move('character', -eTag.length);
      } else {
        // Nicht mehr benötigt
        // range.moveStart('character', aTag.length + insText.length + eTag.length);
      }
      range.select();
      this.positionSpeichern();

    // für neuere auf Gecko basierende Browser
    } else if (typeof this.container.selectionStart != 'undefined') {
      // Einfügen des Formatierungscodes
      var start = this.container.selectionStart;
      var end = this.container.selectionEnd;
      var insText = this.container.value.substring(start, end);
      this.container.value = this.container.value.substr(0, start) + aTag + insText + eTag + this.container.value.substr(end);

      // Anpassen der Cursorposition
      var pos;
      if (insText.length == 0) {
        pos = start + aTag.length;
      } else {
        pos = start + aTag.length + insText.length + eTag.length;
      }
      this.container.selectionStart = pos;
      this.container.selectionEnd = pos;
    } 
  }

  this.fensterEinblenden = function (id) {
    this.fensterAusblenden();
    document.getElementById(id).style.display = 'block';

    if (document.getElementsByName(id + '1')[0]) {
      document.getElementsByName(id + '1')[0].focus();
    }
  }

  this.fensterAusblenden = function () {
    for (i = 0; i < this.fenster.length; i++) {
      document.getElementById(this.fenster[i]).style.display = 'none';
    }

    for (i = 0; i < this.eingaben.length; i++) {
      document.getElementById(this.eingaben[i]).value = '';
    }
  }
}


function submitDisable() {
  i=0;
  while (document.getElementsByTagName('input')[i]) {
    if (document.getElementsByTagName('input')[i].type == 'submit') {
      document.getElementsByTagName('input')[i].disabled = true;
    }
    i++;
  }
}

// Shortcuts
function shortcut(link) {
  if (document.all) {
    link.blur();
    window.location = link.href;
  }
}