function showElementById(id, link, showText, hideText) {
  document.getElementById(id).style.display = "";
  if (link != null) {
	  document.getElementById(link).href = "javascript:hideElementById('" + id + "', '" + link + "', '" + showText + "', '" + hideText + "')";
	  document.getElementById(link).innerHTML = hideText;
  }
}

function hideElementById(id, link, showText, hideText) {
  document.getElementById(id).style.display = "none";
  if (link != null) {
	  document.getElementById(link).href = "javascript:showElementById('" + id + "', '" + link + "', '" + showText + "', '" + hideText + "')";
	  document.getElementById(link).innerHTML = showText;
  }
}

function showElementByIdIvw(id,code){
	showElementById(id);
	trackPageview("/",code);
}

function showInfos(bodyid, linkid)
{
  document.getElementById(bodyid).style.display = "";
  document.getElementById(linkid).href = "javascript:hideInfos('" + bodyid + "', '" + linkid + "')";
  document.getElementById(linkid).childNodes[0].nodeValue = "Ausblenden";
}

function hideInfos(bodyid, linkid)
{
  document.getElementById(bodyid).style.display = "none";
  document.getElementById(linkid).href = "javascript:showInfos('" + bodyid + "', '" + linkid + "')";
  document.getElementById(linkid).childNodes[0].nodeValue = "Alles anzeigen";
}

function showInfos1(bodyid, linkid, linkvalue, imgid)
{
  var oldlinkvalue = document.getElementById(linkid).childNodes[0].nodeValue;
  document.getElementById(bodyid).style.display = "";
  document.getElementById(linkid).href = "javascript:hideInfos1('" + bodyid + "', '" + linkid + "', '" + oldlinkvalue + "', '" + imgid + "')";
  document.getElementById(linkid).childNodes[0].nodeValue = linkvalue;
  var img = document.getElementById(imgid);
  if (img) {
    img.src = "/img/arrow_down.gif";
  }
  storeSessionSetting("display_" + bodyid, true);
}

function hideInfos1(bodyid, linkid, linkvalue, imgid)
{
  var oldlinkvalue = document.getElementById(linkid).childNodes[0].nodeValue;
  document.getElementById(bodyid).style.display = "none";
  document.getElementById(linkid).href = "javascript:showInfos1('" + bodyid + "', '" + linkid + "', '" + oldlinkvalue + "', '" + imgid + "')";
  document.getElementById(linkid).childNodes[0].nodeValue = linkvalue;
  var img = document.getElementById(imgid);
  if (img) {
    img.src = "/img/trail.gif";
  }
  storeSessionSetting("display_" + bodyid, false);
}

function showInfos(bodyid, linkid, href)
{
  document.getElementById(bodyid).style.display = "";
  document.getElementById(linkid).href = href;
  document.getElementById(linkid).childNodes[0].nodeValue = "Ausblenden";
}

function hideInfos(bodyid, linkid, href)
{
  document.getElementById(bodyid).style.display = "none";
  document.getElementById(linkid).href = href;
  document.getElementById(linkid).childNodes[0].nodeValue = "Alles anzeigen";
}

function storeSessionSetting(attribute, value)
{
  callAjaxRequest('/sessionsettings/storeSessionSetting.action?attribute=' + attribute + '&value=' + value, true, null);
}

/* return new XmlHttpRequest  */
function initXmlHttp() {
  var xmlHttp = null;
  if (typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
  }
  if (!xmlHttp) {
    // Internet Explorer 6 und älter
    try {
      xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xmlHttp  = null;
      }
    }
  }
  return xmlHttp;
}

function trackPageview(url, code) {
	if (pageTracker != null) pageTracker._trackPageview(url);

	if (code != null && code != "") {
		var pixel = document.getElementById("ivwpixel");
		if (pixel != null) {
			pixel.innerHTML = "<img src='http://netzathl.ivwbox.de/cgi-bin/ivw/CP/"+code+";?r="+escape(document.URL)+"&d="+(Math.random()*100000)+"' width='1' height='1' alt='szmtag' />";
		}
	}
}

/* execute a Ajax call with 'GET'  */
function callAjaxRequest(url, assync, resulthandler, xmlHttp, code) {
  if (!xmlHttp) {
    xmlHttp = initXmlHttp();
  }

  if (xmlHttp) {
    xmlHttp.open('GET', url, assync);
    xmlHttp.onreadystatechange = function () {
      if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
        	trackPageview(url, code);
        	if (resulthandler) {
            resulthandler(xmlHttp);
          }
        }
      };
    }
    xmlHttp.send(null);
  }
}

/* execute a Ajax call with 'GET'  */
function callAjaxParam(url, assync, resulthandler, rh_opt, code)
{
  var xmlHttp = initXmlHttp();
  if (xmlHttp) {
    xmlHttp.open('GET', url, assync);
    xmlHttp.onreadystatechange = function () {
    	if (xmlHttp.readyState == 4) {
    		if (xmlHttp.status == 200) {
        	trackPageview(url, code);
    			if (resulthandler) {
            resulthandler(rh_opt, xmlHttp);
          }
        }
      };
    }
    xmlHttp.send(null);
  }
}

/* execute Ajax call with 'POST'  */
function callAjaxPostRequest(url, assync, resulthandler, postString, code)
{
  var xmlHttp = initXmlHttp();
  if (xmlHttp) {
    xmlHttp.open('POST', url, assync);
    if (postString) {
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", postString.length);
      xmlHttp.setRequestHeader("Accept-Charset","UTF-8");
      xmlHttp.setRequestHeader("Connection", "close");
    }
    xmlHttp.onreadystatechange = function () {
    	if (xmlHttp.readyState == 4) {
    		if (xmlHttp.status == 200) {
    			trackPageview(url, code);
    			if (resulthandler) {
            resulthandler(xmlHttp);
          }
        }
      };
    }
    if (postString) {
      xmlHttp.send(postString);
    } else {
      xmlHttp.send(null);
    }
  }
}

function trackLink(id, url) {
	callAjaxRequest("/cms/tracking/click.action?view=" + id, true);
	document.location = url;
};

// Removes leading whitespaces
function ltrim( value ) {
  var re = /\s*((\S+\s*)*)/;
  return value.replace(re, "$1");
}

// Removes ending whitespaces
function rtrim( value ) {
  var re = /((\s*\S+)*)\s*/;
  return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
  return ltrim(rtrim(value));
}
