

function internalModifySelectElements(sVisibility) {
  if (document.all) {
    var aForm = document.forms;
    var aElem;
    var oElem;
    for (i = 0; i < aForm.length; i++) {
      aElem = document.forms[i].elements;
      for (j = 0; j < aElem.length; j++) {
        oElem = aElem[j];
        if (oElem.type == "select-one") {
          oElem.style.visibility = sVisibility;
        }
      }
    }
  }
}

function scaleTransparentLayer() {
  var layer = document.getElementById("transparent_layer");
  layer.style.top = 0;
  layer.style.left = 0;
  layer.style.width = parseInt(getWindowWidth()) + "px";
  layer.style.height = parseInt(getWindowHeight()) + "px";
}

function shrinkTransparentLayer() {
  var layer = document.getElementById("transparent_layer");
  layer.style.width = 10 + "px";
  layer.style.height = 10 + "px";
}

function showTransparentLayer() {
  internalModifySelectElements("hidden");
  scaleTransparentLayer();
  document.getElementById("transparent_layer").style.visibility = 'visible';
}

function hideTransparentLayer() {
  document.getElementById("transparent_layer").style.visibility = 'hidden';
  shrinkTransparentLayer();
  internalModifySelectElements("visible");
}



function loadPage(sUrl) {
  window.location.href = sUrl;
  return false;
}


function getFilteredHtmlAttribute(oElement, sAttribute) {
  var sResult = new String(oElement.getAttribute(sAttribute));
  if (sResult == "null" || sResult == "undefined") sResult = "";
  return sResult;
}

function transformLinkHrefs() {
  
  var sNewOnMouseDown = "";
  
  try {
    
    var aLink = document.getElementsByTagName("A");
    
    for (var i = 0; i < aLink.length; i++) {
      
      if (aLink[i].hasClassName("skip")) continue;
      
      var oLink = aLink[i];
      var sHref = getFilteredHtmlAttribute(oLink, "href");
      var sRel = getFilteredHtmlAttribute(oLink, "rel");
      var sTarget = getFilteredHtmlAttribute(oLink, "target");
      var sOnClick = getFilteredHtmlAttribute(oLink, "onclick");
      
      if (sOnClick != "" && sOnClick.substring(0, 9) == "function ") sOnClick = "";
      
      if (sRel != "" && (sRel.substring(0, 9).toLowerCase() == "shadowbox" || sRel.substring(0, 8).toLowerCase() == "lightbox")) {
        sHref = "";
        sOnClick = "";
      }
      
      if (sHref != "" && (sHref.substring(0, 7).toLowerCase() == "mailto:" || sHref.substring(0, 16).toLowerCase() == "javascript:cal_f")) {
        sHref = "";
        sOnClick = "";
      }
        
      var sOnMouseDown = getFilteredHtmlAttribute(oLink, "onmousedown");
      var sOnMouseUp = getFilteredHtmlAttribute(oLink, "onmouseup");
      var bOnlyHref = (sHref != "" && sTarget == "" && sOnClick == "" && sOnMouseDown == "" && sOnMouseUp == "");
      var bOnlyOnClick = ((sHref == "" || sHref == "javascript:void(0);") && sTarget == "" && sOnClick != "" && sOnMouseDown == "" && sOnMouseUp == "");
      
      if (bOnlyHref || bOnlyOnClick) {
        
        if (bOnlyHref) {
          
          var sNewOnMouseDown = (bOnlyHref ? sHref : sOnClick);
          
          if (sHref.substring(0, 11) == "javascript:") {
            sNewOnMouseDown = sHref.substring(11, sHref.length);
            if (sNewOnMouseDown.substr(sNewOnMouseDown.length - 1, 1) != ";") {
              sNewOnMouseDown = sNewOnMouseDown + ";";
              sNewOnMouseDown = Url.decode(sNewOnMouseDown);
            }
          } else {
            sNewOnMouseDown = "loadPage('" + sNewOnMouseDown + "');";
          }
          oLink.href = "javascript:void(0);";
          oLink.onmousedown = new Function(sNewOnMouseDown + "return false;");
          //alert(oLink.onmousedown);
          
        } else {
          
          oLink.onmousedown = oLink.onclick;
          
        }
        
      }
    }
    
  } catch (e) {
    //alert("transformLinkHrefs::Error\n\n" + e.message + "\n\n" + sNewOnMouseDown);
  }
  
}



function setActiveTab(activeTab) {
  if (activeTab > 0) {
    try {
      var frm = document.forms[0];
      if (frm != "" && frm.nodeName.toLowerCase() == "form") {
        var tab = document.getElementById("tab");
        if (tab == null) {
          tab = document.createElement("input");
          tab.type = "hidden";
          tab.name = "tab";
          tab.id = "tab";
        }
        tab.value = activeTab;
        frm.appendChild(tab);
      }
    } catch (e) {
      alert(e.message);
    }
  }
}

function checkActiveTab() {
  var ul = $$("ul.tab-menu")
  for (var u = 0; u < ul.length; u++) {
    var currentListItemNumber = 0;
    for (var i = 0; i < ul[u].childNodes.length; i++) {
      if (ul[u].childNodes[i].nodeName.toLowerCase() == "li") {
        currentListItemNumber++;
        if (ul[u].childNodes[i].hasClassName("active")) {
          setActiveTab(currentListItemNumber);
          break;
        }
      }
    }
    break; // Funktioniert bisher nur für eines Tab-Element pro Seite
  }
}

function onTabClick(e) {
  var activeTab = 0;
  var li = Event.findElement(e, "li");
  var activeListItemNumber = 0;
  var currentListItemNumber = 0;
  var activeListItem = null;
  if (li && li.parentNode.nodeName.toLowerCase() == "ul" && li.parentNode.hasClassName("tab-menu")) {
    for (var i = 0; i < li.parentNode.childNodes.length; i++) {
      if (li.parentNode.childNodes[i].nodeName.toLowerCase() == "li") {
        currentListItemNumber++;
        if (li.parentNode.childNodes[i] == li) {
          activeListItem = li.parentNode.childNodes[i];
          activeListItemNumber = currentListItemNumber;
          activeTab = currentListItemNumber;
        } else {
          li.parentNode.childNodes[i].removeClassName("active");
          $("tab-body-" + currentListItemNumber).style.display = "none";
        }
      }
    }
    if (activeListItem != null) {
      activeListItem.addClassName("active");
      $("tab-body-" + activeListItemNumber).style.display = "block";
    }
  }
  setActiveTab(activeListItemNumber);
}



function closePopup() {
  try {
    window.parent.Shadowbox.close();
  } catch (e) {
    alert("Das Popup-Fenster konnte nicht geschlossen werden:\n\n" + e.message);
  }
}



function printPopup() {
  try {
    window.print();
  } catch (e) {
    alert("Der Druck-Dialog konnte nicht aufgerufen werden:\n\n" + e.message);
  }
}



function refreshItemTitle() {
  var header = document.getElementById("item-title");
  if (header != null) {

    var content = "";
    var shortcut = document.getElementById("fSHORTCUT");
    var title = document.getElementById("fTITLE");
    var login = document.getElementById("fLOGIN");
    var firstname = document.getElementById("fFIRSTNAME");
    var lastname = document.getElementById("fLASTNAME");
    var name = document.getElementById("fNAME");
    
    if (title != null && title.value != "") {
      content = title.value;
    } else if (shortcut != null && shortcut.value != "") {
      content = shortcut.value;
    } else if (login != null && login.value != "")  {
      content = login.value;
    } else if (name != null && name.value != "")  {
      content = name.value;
      if (firstname != null && firstname.value != "")  {
        content = firstname.value + " " + content;
      }
    } else if (lastname != null && lastname.value != "")  {
      content = lastname.value;
      if (firstname != null && firstname.value != "")  {
        content = firstname.value + " " + content;
      }
    } else if (firstname != null && firstname.value != "")  {
      content = firstname.value;
    } 
    
    if (content != "") content = "&rarr; " + content;
    header.innerHTML = content;
    
  }
  
  refreshUrlAlias(true);
  
}



function refreshDefaultLanguageItemTitle() {
  var shortcut = document.getElementById("fSHORTCUT");
  var title = document.getElementById("fTITLE_<% DEFAULT_LANGUAGE_SHORTCUT %>");
  if (shortcut != null && title != null && shortcut.value != "" && title.value == "") {
    var sShortcut = new String(shortcut.value);
    title.value = sShortcut.substring(0,1).toUpperCase() + sShortcut.substring(1, sShortcut.length).toLowerCase();
  }
  refreshItemTitle();
}



function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}



function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}



function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}



function refreshUrlAlias(always) {
  
  var aliasValue = "";
  var alias = document.getElementById("fURL_ALIAS");
  var title = document.getElementById("fTITLE");
  var itemNumber = document.getElementById("fITEM_NUMBER");
  
  if (alias == null || (alias.value != "" && !always)) return false;
  
  if (title != null && title.value != "") {
    aliasValue = title.value;
  } else if (itemNumber != null && itemNumber.value != "") {
    aliasValue = itemNumber.value;
  }
  
  if (aliasValue != "") {
    
    aliasValue = trim(aliasValue);
    
    var replaceSrc = new Array(" ", "/", "ä", "ö", "ü", "ß", "--", "--", "--", "--", "--", "--", "--", "--", "--", "--");
    var replaceDst = new Array("-", "-und-", "ae", "oe", "ue", "ss", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-");
    
    for (var i = 0; i < replaceSrc.length; i++) {
      aliasValue = aliasValue.replace(new RegExp(replaceSrc[i], "g"), replaceDst[i]);
    }
    
  }
  
  alias.value = aliasValue.toLowerCase();
  
  return true;
  
}






/**
*
*  Javascript sprintf
*  http://www.webtoolkit.info/
*
*
**/
 
sprintfWrapper = {
 
  init : function () {
 
    if (typeof arguments == "undefined") { return null; }
    if (arguments.length < 1) { return null; }
    if (typeof arguments[0] != "string") { return null; }
    if (typeof RegExp == "undefined") { return null; }
 
    var string = arguments[0];
    var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
    var matches = new Array();
    var strings = new Array();
    var convCount = 0;
    var stringPosStart = 0;
    var stringPosEnd = 0;
    var matchPosEnd = 0;
    var newString = '';
    var match = null;
 
    while (match = exp.exec(string)) {
      if (match[9]) { convCount += 1; }
 
      stringPosStart = matchPosEnd;
      stringPosEnd = exp.lastIndex - match[0].length;
      strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
 
      matchPosEnd = exp.lastIndex;
      matches[matches.length] = {
        match: match[0],
        left: match[3] ? true : false,
        sign: match[4] || '',
        pad: match[5] || ' ',
        min: match[6] || 0,
        precision: match[8],
        code: match[9] || '%',
        negative: parseInt(arguments[convCount]) < 0 ? true : false,
        argument: String(arguments[convCount])
      };
    }
    strings[strings.length] = string.substring(matchPosEnd);
 
    if (matches.length == 0) { return string; }
    if ((arguments.length - 1) < convCount) { return null; }
 
    var code = null;
    var match = null;
    var i = null;
 
    for (i=0; i<matches.length; i++) {
 
      if (matches[i].code == '%') { substitution = '%' }
      else if (matches[i].code == 'b') {
        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
        substitution = sprintfWrapper.convert(matches[i], true);
      }
      else if (matches[i].code == 'c') {
        matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
        substitution = sprintfWrapper.convert(matches[i], true);
      }
      else if (matches[i].code == 'd') {
        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
        substitution = sprintfWrapper.convert(matches[i]);
      }
      else if (matches[i].code == 'f') {
        matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
        substitution = sprintfWrapper.convert(matches[i]);
      }
      else if (matches[i].code == 'o') {
        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
        substitution = sprintfWrapper.convert(matches[i]);
      }
      else if (matches[i].code == 's') {
        matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
        substitution = sprintfWrapper.convert(matches[i], true);
      }
      else if (matches[i].code == 'x') {
        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
        substitution = sprintfWrapper.convert(matches[i]);
      }
      else if (matches[i].code == 'X') {
        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
        substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
      }
      else {
        substitution = matches[i].match;
      }
 
      newString += strings[i];
      newString += substitution;
 
    }
    newString += strings[i];
 
    return newString;
 
  },
 
  convert : function(match, nosign){
    if (nosign) {
      match.sign = '';
    } else {
      match.sign = match.negative ? '-' : match.sign;
    }
    var l = match.min - match.argument.length + 1 - match.sign.length;
    var pad = new Array(l < 0 ? 0 : l).join(match.pad);
    if (!match.left) {
      if (match.pad == "0" || nosign) {
        return match.sign + pad + match.argument;
      } else {
        return pad + match.sign + match.argument;
      }
    } else {
      if (match.pad == "0" || nosign) {
        return match.sign + match.argument + pad.replace(/0/g, ' ');
      } else {
        return match.sign + match.argument + pad;
      }
    }
  }
}
 
sprintf = sprintfWrapper.init;

function getWindowWidth() {
  var windowWidth = 0;
  if (typeof(window.innerWidth) == "number") {
    windowWidth=window.innerWidth;
  } else {
    if (document.documentElement && document.documentElement.clientWidth) {
      windowWidth = document.documentElement.clientWidth;
    } else {
      if (document.body && document.body.clientWidth) {
        windowWidth=document.body.clientWidth;
      }
    }
  }
  return windowWidth;
}

function getWindowHeight() {
  var windowHeight = 0;
  if (typeof(window.innerHeight) == "number") {
    windowHeight=window.innerHeight;
  } else {
    if (document.documentElement && document.documentElement.clientHeight) {
      windowHeight = document.documentElement.clientHeight;
    } else {
      if (document.body && document.body.clientHeight) {
        windowHeight=document.body.clientHeight;
      }
    }
  }
  return windowHeight;
}

function getScrollWidth() {
  var scrollWidth = 0;
  if (parseInt(document.body.scrollWidth)) scrollWidth = document.body.scrollWidth;
  if (scrollWidth < getWindowWidth()) scrollWidth = getWindowWidth();
  return scrollWidth;
}

function getScrollHeight() {
  var scrollHeight = 0;
  if (parseInt(document.body.scrollHeight)) scrollHeight = document.body.scrollHeight;
  if (scrollHeight < getWindowHeight()) scrollHeight = getWindowHeight();
  return scrollHeight;
}

function getPosition(element) {
  
  var elem=element,tagname="",x=0,y=0;
    
  while ((typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined")) {
    y+=elem.offsetTop;
    x+=elem.offsetLeft;
    tagname=elem.tagName.toUpperCase();

    if (tagname=="BODY") elem=0;

    if (typeof(elem)=="object" && typeof(elem.offsetParent)=="object") elem=elem.offsetParent;
  }
  
  position=new Object();
  position.x=x;
  position.y=y;
  return position;
} 

function openCentered(win, name, url, w, h, l, t) {
  if ((!l || (l == -1)) && (l != 0)) {
    var l = Math.round(0.5 * (screen.width - w));
  }
  if ((!t || (t == -1)) && (t != 0)) {
    var t = Math.round(screen.height * (1.0 - 0.618034) - 0.5 * h);
    if (t < 0.0) t = 0.0;
  }
  if (win && !win.closed) win.close();
  win = window.open(url, name,
              "left=" + l + ",top=" + t + ",width=" + w + ",height=" + h + ",toolbar=no,scrollbars=yes,resizable=yes"
  );
  return win;
}

function openWindow(sName, sUrl, iWidth, iHeight, bToolbar, bResizable, bScrollbars) {
  
  // Calculate window's left and top position
  var iLeft   = parseInt((screen.width / 2) - (iWidth / 2));
  var iTop    = parseInt((screen.height / 2) - (iHeight / 2) - 50);
  if (iTop < 0) iTop = 0;
  
  // Check if window should have a toolbar, be resizable or have scrollbars
  var sToolbar    = (bToolbar    ? "yes" : "no");
  var sResizable  = (bResizable  ? "yes" : "no");
  var sScrollbars = (bScrollbars ? "yes" : "no");
  
  // Open window and focus it
  var oWindow = null;
  
  oWindow = window.open(sUrl, sName, "width=" + iWidth + ",height=" + iHeight + ",top=" + iTop + ",left=" + iLeft + ",toolbar=" + sToolbar + ",resizable=" + sResizable + ",scrollbars=" + sScrollbars);
  
  try {
    oWindow.focus();
  } catch (e) {
    alert("Das Öffnen des Fensters wurde von Ihrem Browser blockiert.\nBitte erlauben Sie für diese Seite Popups und rufen Sie das Fenster erneut auf.");
    oWindow = null;
  }
  
  return oWindow;
  
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
  // public method for url encoding
  encode : function (string) {
    return escape(this._utf8_encode(string));
  },
 
  // public method for url decoding
  decode : function (string) {
    return this._utf8_decode(unescape(string));
  },
 
  // private method for UTF-8 encoding
  _utf8_encode : function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
 
    for (var n = 0; n < string.length; n++) {
 
      var c = string.charCodeAt(n);
 
      if (c < 128) {
        utftext += String.fromCharCode(c);
      }
      else if((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      }
      else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
 
    }
 
    return utftext;
  },
 
  // private method for UTF-8 decoding
  _utf8_decode : function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;
 
    while ( i < utftext.length ) {
 
      c = utftext.charCodeAt(i);
 
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      }
      else if((c > 191) && (c < 224)) {
        c2 = utftext.charCodeAt(i+1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      }
      else {
        c2 = utftext.charCodeAt(i+1);
        c3 = utftext.charCodeAt(i+2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
 
    }
 
    return string;
  }
 
}
function loadPage(sUrl) {
  window.location.href = sUrl;
  return false;
}

