window.onload = init;

function oeffneEmpfehlung(link){
        var theRefererForm = link + "?referer="+ location.href;
 	window.open(theRefererForm,'weiterempfehlen','width=560px,height=670px,resizable=yes,menuebar=no,scrollbars=yes, left=0, top=0'); 
}

function submitNotEmptyOrDefault(field){
   if (field.value == '' || field.value == field.defaultValue){
      field.focus();
      return false;
   }
   return true;

}

function clearOnFocus(field){
   if (field.value == field.defaultValue)
      field.value = '';
}
function resetOnBlur(field){
   if (field.value == '')
      field.value = field.defaultValue;
}

function init(){
    initTooltips();
    initTabs();
    initHighlight();
    initAnchor();
}
function initTabs(){
   initTabsForTags("h2", "toggle");
   initTabsForTags("h3", "toggle");
}
function initTabsForTags(inTagName, inClassName){
    var toggles = getElementsByTagAndClassName(inTagName, inClassName);
    for(i = toggles.length - 1; i >= 0; i--){
        var attr       = document.createAttribute("class");
        attr.nodeValue = "toggleCollapsed";
        toggles[i].parentNode.setAttributeNode(attr);
        toggles[i].parentNode.onclick = tabHandler;
    }
}
function initTooltips(){
    var tips       = getElementsByTagAndClassName("span", "tooltip");
    var element    = document.createElement("span");
    var attr       = document.createAttribute("class");
    attr.nodeValue = "marker";
    element.setAttributeNode(attr);
    var textNode   = document.createTextNode("[ i ] ");
    element.appendChild(textNode);

    for(i = tips.length - 1; i >= 0; i--){
        var aNode = element.cloneNode(true);
        tips[i].insertBefore(element.cloneNode(true), tips[i].firstChild)
        aNode.onmouseover = tooltipHandler;
        aNode.onmouseout  = tooltipHandler;
        tips[i].parentNode.insertBefore(aNode , tips[i]);
    }
}

function getElementsByTagAndClassName(inTagName, inClassName){
    var theTags = document.getElementsByTagName(inTagName);
    var theMatches = new Array();
    for(i = 0; i < theTags.length; i++){
        if(theTags[i].className != null && theTags[i].className == inClassName)
            theMatches.push(theTags[i]);
    }
    return theMatches;
}
function getSourceElement(e){
    //IE
    if( typeof( e ) == "undefined" && typeof( window.event ) != "undefined" ){
        return window.event.srcElement;
    }
    //W3C
    return e.target;
}

function tabHandler(e){
    var aSource = getSourceElement(e);
    if ((aSource.tagName.toLowerCase() == "h2" || aSource.tagName.toLowerCase() == "h3") && 
        (aSource.className == "toggle"))
        toggleTabClass(aSource.parentNode);
}
function tooltipHandler(e){
    toggleDisplay(getSourceElement(e).nextSibling);
}
function toggleDisplay(inNode){
    if (inNode.style.display == '' || inNode.style.display == 'none')
        inNode.style.display = 'block';
    else
        inNode.style.display = 'none';
}
function toggleTabClass(node){
      if (node.className == null || node.className == '' || node.className == 'toggleCollapsed')
         node.className = 'toggleExpanded';
      else
         node.className = 'toggleCollapsed';
}


/* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * +- [ZMI] Highlight Text
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

String.prototype.removeWhiteSpaces = function() {return(this.replace(/\s+/g,""));};
String.prototype.leftTrim = function() {return(this.replace(/^\s+/,""));};
String.prototype.rightTrim = function() {return(this.replace(/\s+$/,""));};
String.prototype.basicTrim = function() {return(this.replace(/\s+$/,"").replace(/^\s+/,""));};
String.prototype.superTrim = function() {return(this.replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,""));};

function capitalize(Text) {
  c=Text.split(""); c[0]=c[0].toUpperCase(); return c.join("");
}

function getUrlParam(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

  function capitalize(Text) {
    c=Text.split(""); c[0]=c[0].toUpperCase(); return c.join("");
  }

  // @see http://radio.javaranch.com/pascarello/2005/01/09/1105293729000.html
  function ScrollToElement(theElement) {
    var selectedPosX = 0;
    var selectedPosY = 0;
    while(theElement != null){
      selectedPosX += theElement.offsetLeft;
      selectedPosY += theElement.offsetTop;
      theElement = theElement.offsetParent;
    }
    window.scrollTo(selectedPosX,selectedPosY);
  }

  function highlight_text(text) {
     var exp = "";
     for (var i = 0; i < text.length; i++) {
       var ch = text.charAt(i);
       if ( ch != '*') {
         exp += "(" + ch + "|" + text.toUpperCase().charAt(i) + "|" + text.toLowerCase().charAt(i) + ")";
       }
     }
     var regexp = new RegExp( "(" + exp + ")", "g");
     var el = $('#MainCol');
     var content= el.html().split("<");
     for(var mb=0; mb < content.length; mb++) {
        var cell = content[mb].split(">");
        if(cell[1] && cell[1]!="" && cell[1].basicTrim()!="") { 
           cell[1] = cell[1].replace( regexp, "<span class=\"highlight\">$1</span>");
           content[mb]=cell.join(">");
        }
     }
     content=content.join("<"); 
     el.html(content);
  }

function ensure_visible( ) {
  // Get anchor-link from URL.
  var anchor_link = null;
  if (self.location.href.indexOf("#") > 0) {
    anchor_link = self.location.href.substring(self.location.href.indexOf("#")+1);
  }
  var divs_collapsed = $("div.toggleCollapsed");
  for ( var i = 0; i < divs_collapsed.length; i++) {
    // Expand toggles for highlighted-spans.
    var spans_highlight = $("span.highlight",divs_collapsed[i]);
    if (spans_highlight.length > 0) {
      divs_collapsed[i].className = 'toggleExpanded';
    }
    // Expand toggles for anchor-links.
    if ( anchor_link != null) {
      var els = $("a[@name="+anchor_link+"]",divs_collapsed[i]);
      if (els.length > 0) {
        divs_collapsed[i].className = 'toggleExpanded';
      }
    }
  }
}

function initHighlight() {
  var raw = getUrlParam("raw").split(" ");
  var text1st = null;
  for (var i = 0; i < raw.length; i++) {
    var text = raw[i].basicTrim();
    if (text.length > 0) {
      if (text1st == null) {
        text1st = text;
      }
      highlight_text( text);
    }
  }
  ensure_visible();
  if (text1st!=null) {
    var spans_highlight = $('span.highlight');
    if ( spans_highlight.length>0) {
      ScrollToElement(spans_highlight[0]);
    }
  }
}

/* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * +- [SCR008694]: Expand collapsed toggles for local anchor-links.
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
function initAnchor() {
  $("a[href*=#]").click( function() { clickAnchor(this);});
}

function clickAnchor( el) {
  // Get anchor-link from URL.
  var anchor_link = el.href.substring(el.href.indexOf("#")+1);
  var divs_collapsed = $("div.toggleCollapsed");
  for ( var i = 0; i < divs_collapsed.length; i++) {
    var els = $("a[@name="+anchor_link+"]",divs_collapsed[i]);
    if (els.length > 0) {
      divs_collapsed[i].className = 'toggleExpanded';
    }
  }
}

/* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * +- [ZMI] Open function
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
function open_function(url,width,height,options)
  {
    height += 16;
    width += 16;
    if ( height > screen.availHeight || width > screen.availWidth) {
      if ( options.indexOf( "scrollbars=") < 0) {
        if ( height > screen.availHeight)
          height = screen.availHeight;
        if ( width > screen.availWidth)
          width = screen.availWidth;
        options += ",scrollbars=yes";
      }
    }
    var name = url;
    var i = name.lastIndexOf( "/");
    if ( i > 0) {
      name = name.substring(i+1);
    }
    else {
      name = url;
    }
    i = name.indexOf("?");
    if ( i > 0) {
      name = name.substring(0,i);
    }
    i = name.indexOf("-");
    if ( i > 0) {
      name = name.substring(0,i);
    }
    i = name.indexOf(".");
    if ( i > 0) {
      name = name.substring(0,i);
    }
    var msgWindow = open(url, name, "width=" + width + ",height=" + height
      + ",screenX=" + (screen.width-width)/2
      + ",screenY=" + (screen.height-height)/2
      + ",dependent=yes"
      + ",left=" + (screen.width-width)/2
      + ",top=" + (screen.height-height)/2
      + options
      );
    if ( msgWindow) {
      msgWindow.focus();
      if ( msgWindow.opener == null) {
        msgWindow.opener = self;
      }
    }
  }

/* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * +- Teilnehmerdienste
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
function openTeilnehmerdienste( uri, name)
{
  window.open( uri, name, 'width=900px,height=670px,resizable=yes,menuebar=no,scrollbars=yes, left=0, top=0');
}

