/* Standardfunktionen für Fachhochschule Dortmund */

////////// neues Fenster öffnen mit Parameterübergabe ///////////////

function openWindow (url,windowName,opt) {
  // opt im Format "r,st,m,l=36,h=56"
  if (!opt) var opt = "";

  // array: searchString,parameter,default,...
  var optionParam = new Array(
    "m","menubar","no",
    "lo","locationbar","no",
    "st","status","no",
    "r","resizable","no",
    "sc","scrollbars","yes",
    "l=","left","150",
    "t=","top","100",
    "w=","width","500",
    "h=","height","600");

  var negate = new Array();
  negate["yes"] = "no";
  negate["no"] = "yes";

  var options = "";
  for (var i=0; i < optionParam.length; i+=3) {
    options += optionParam[i+1]+"=";

    pos = opt.indexOf(optionParam[i]);
    if (pos!=-1) {
      if (i<15) options += negate[optionParam[i+2]]; // m lo st r sc
        else { // l t w h
          eval("var regexp = /"+optionParam[i]+"(\\d+)/.exec(opt);");
          options += RegExp.$1;
        }
    } else options += optionParam[i+2];
    options += ",";
  }

  win = window.open(url,windowName,options.substr(1));
  win.focus();
}

////////// Eingabe eines Formular-Textfeldes prüfen /////////////////

function checkElement(element, pattern, message, ignoreNoInput, focusElement, defaultValue) {
  // focusElement = 1, falls ggf. Element focussiert werden soll
  // ignoreNoInput = 1, falls leeres Feld keinen Fehler anzeigen soll
  // defaultValue = 1, falls Element mit default-Wert belegt ist

  eval("var regexp = "+pattern+";"); // reg. Ausdruck
  var ok = regexp.test(element.value);

  if (!ok || defaultValue) {
    if (!element.value || defaultValue) { // Feld leer
      if (!ignoreNoInput && (message || defaultValue)) alert(message);
      } else alert("Ihre Eingabe ist nicht zulässig.");
    if (focusElement) {
      element.select();
      element.focus();
    }
    return false;
    } else return true;
}

////////// Element auf- oder zuklappen //////////////////////////////

function popupOrHide(elementId) {
  with(document.getElementById(elementId).style) {
   if (display == 'block') display = 'none';
     else  display = 'block';
  }
}

////////// Stringfunktion trim realisieren //////////////////////////  

function trim(value) {
  var s = value;
  while (s.charAt(0) == " ") {
    s = s.substr(1);
  };
  while (s.charAt(s.length - 1) == " ") {
    s = s.substr(0, s.length - 1);
  };
  return s;
};

////////// Link aus select-Box //////////////////////////////////////

function linkToValue(fSelect) {
  var linkData = fSelect.options[fSelect.selectedIndex].value.split("|");
  var url = linkData[1];
  if (url) {
    if (linkData[0] == "1") {
      win = window.open(url);
      win.focus();
    } else top.location.href = url;
  }
}

////////// Link zu JTrade ///////////////////////////////////////////

function callUpJTrade(id) {
  location.href = 'http://app.jtrade.de/?' + id;
}

/////////// default-Eintrag in Eingabefeld realisieren //////////////

var defaultText = true;

function handleDefaultText(fElement, defaultTextName) {
  if (!defaultTextName) { // onFocus
    if (defaultText) fElement.value = "";
    } else { // onBlur
    fElement.value = trim(fElement.value);
    if (fElement.value == "") {
      fElement.value = defaultTextName;
      defaultText = true;
    } else defaultText = false;
  }
}