var current_redirect_url;
var titleBarMouseDown = false;
var xPos;
var yPos;
//positionen merken, falls der nutzer das fenster verschoben hat
var userXPos;
var userYPos;
var docEl = (typeof document.compatMode != "undefined" && document.compatMode != "BackCompat")? 
  	"documentElement" : "body";

  //stack zum speichern der alten inhalte der ajaxframes
  var ajaxFrameStack = document.createElement("x");
  

//document.getElementsByTagName("body").item(0).onmouseup = TitleBarMouseUp();
if (document.body)document.body.onMouseUp = TitleBarMouseUp();
//document.onmouseup = TitleBarMouseUp();
/*
 * Nur IDA!
 * Focus festhalten für Reload
 */
var storedFocusObject = null;

function storeFocus(obj)
{
  storedFocusObject = obj;
}

function looseFocus(obj)
{
  storeFocus(obj);
  var ajaxFrame = document.getElementById("ajaxFrame");
  if(ajaxFrame)
  {
    //Browser bestimmen
    var userAgent = navigator.userAgent;

    inputs = ajaxFrame.getElementsByTagName("button");
    for(i=0;i<inputs.length;i++)
    {
      //Vor-/Zurückbutton suchen, da der erste Button jetzt der Druck ist
      if((inputs[i].name.indexOf('cmd_next') > -1) || (inputs[i].name.indexOf('cmd_prev') > -1) || (inputs[i].name.indexOf('cmd_close') > -1))
      {
	  //nur für Safari-Browser, da Bug im Aufbau des input-Arrays
	  if(userAgent.indexOf('Safari') > -1)
	  {
	      inputs[i].type = 'submit';
	      inputs[i].name='IDABox[action][jump]';
	      inputs[i].click();
	      break;
	  }//alle anderen
	  else if((inputs[i].type == 'submit') || (inputs[i].type == 'button'))
	  {
	      inputs[i].name='IDABox[action][jump]';
	      inputs[i].click();
	      break;
	  }
      }
    }
  }
  else
  {
    inputs = document.getElementsByTagName("input");
    for(i=0;i<inputs.length;i++){
      if((inputs[i].name.indexOf('cmd_next') > -1) || (inputs[i].name.indexOf('cmd_prev') > -1) || (inputs[i].name.indexOf('cmd_close') > -1)){
	if(inputs[i].type == 'submit') {
	  inputs[i].name='IDABox[action][jump]';
	  inputs[i].click();
	}
      }
    }
  }
}

/*
//IDA: Events abfangen
function keyPress(Event)
{
  if (!Event)
    Event = window.event;
  
  //bei [ENTER] weiter
  if(Event.keyCode == 13)
  {
    document.getElementById('cmd_next').click();
  }
}
*/
/*
 * ruft eine seite aus dem framework auf. 
 * box - pfad (url) zur seite, die angezeigt werden soll
 * params - parameter (string), die ueber method(GET/POST) an die box uebermittelt werden sollen
 */
function requestWebbox(box, params , method, secondLoad,callback) {
    if (params==null)    
        params = '';
    
    //Scroll-Offsets ermitteln
    var offsets = getOffsets();
    
    overlay = document.getElementById("overlay");
    objBody = document.getElementsByTagName("body").item(0);
    if(!secondLoad)
    {
      // Wenn bereits ein Ajax-Frame existiert, dann darin das Wartebild anzeigen und am Ende den Inhalt austauschen
      // -> das gibt beim Blättern in Ajax-Listen eine viel bessere Optik als das ständige auf- und zu..
      /*
      if(document.getElementById("ajaxFrame")){
	document.body.removeChild(document.getElementById("overlay"));    		
	document.body.removeChild(document.getElementById("ajaxFrame"));    
      }
      */
      
      ajaxFrame = document.getElementById("ajaxFrame");
      if (!ajaxFrame) {
      
      // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
      objOverlay = document.createElement("div");
      objOverlay.setAttribute("id","overlay");
      //objOverlay.onmousemove=moveAjaxFrame;
      //objOverlay.onmouseup=TitleBarMouseUp;
      objOverlay.style.visibility = "visible";
      objOverlay.style.position = "fixed";
      objOverlay.style.top = "0";
      objOverlay.style.left = "0";
      objOverlay.style.zIndex = "90";
      objOverlay.style.width = "100%";
      objOverlay.style.height = "100%";
      
      objOverlay.onclick = function ()
      {
	  var is_ida = document.getElementById("jump_level_ajax");
	  if(!is_ida)
	      closeFrame();
	  return false;
      }
      
      objBody.insertBefore(objOverlay, objBody.firstChild);

      }
      
      objLoadingImage = document.createElement("img");	
      objLoadingImage.setAttribute("id","loadingImage");
      objLoadingImage.src = "/skins/icons/loading.gif";
      objLoadingImage.setAttribute("id","loadingImage");
      objLoadingImage.style.position = "absolute";
      objLoadingImage.style.top = '50%';
      objLoadingImage.style.left = '50%';
      objLoadingImage.style.marginTop = '-4px';
      objLoadingImage.style.marginLeft = '-4px';
      objLoadingImage.style.zIndex = '150';
      //objLoadingImage.style.visibility = "hidden";

      if (ajaxFrame)
        ajaxFrame.appendChild(objLoadingImage);
      else
        objOverlay.appendChild(objLoadingImage);

      //setFramePosition(objLoadingImage);
      //objLoadingImage.style.visibility = "visible";

      if(params == 'picture') {
        params = '';
        // preload image
        imgPreload = new Image();
        
        imgPreload.onload=function(){
          
          // BildFrame bauen
          objImage = document.createElement("div");
          objImage.setAttribute("id","imageFrame");
          objImage.innerHTML = '<p align="center"><a href="#" onclick="closeFrame(); return false;"><div align="right" style="padding-top:-20px;margin-top:-20px;"><img src="/skins/icons/close.gif"/></div>' 
          + '<img src="' + box + '"/></a></p><p align="center">' 
          + '<a href="#" onclick="closeFrame(); return false;" class="menu_1_link" style="color:#000000;">[schlie&szlig;en]</a></p>';
          objImage.style.position = "absolute";
          objImage.style.left = 150 + "px";
          objImage.style.top = 150 + "px";
          objImage.style.zIndex = "160";
          objImage.onclick = "closeFrame();";
          objImage.style.visibility = "hidden";
          objImage.style.backgroundColor = "#ffffff";
          objImage.style.borderStyle = "solid";
          objImage.style.borderColor = "#777777";
          objImage.style.borderWidth = "2px";
          if (objOverlay.nextSibling)objBody.insertBefore(objImage, objOverlay.nextSibling);
          else objBody.appendChild(objImage);
          setFramePosition(objImage);
          objImage.style.visibility = "visible";
          
          //Ladebalken verstecken
          objLoadingImage = document.getElementById("loadingImage");
          if(objLoadingImage)
            objLoadingImage.style.visibility = "hidden";
        }
        imgPreload.src = box;
        return false;
      }
      else if (!ajaxFrame) {
	ajaxFrame = document.createElement("div");
        ajaxFrame.setAttribute("id", "ajaxFrame");
        //ajaxFrame.onmousemove=moveAjaxFrame;
        ajaxFrame.style.position = "absolute";
        ajaxFrame.style.left = offsets.x+150;
        ajaxFrame.style.top = offsets.y+150;
        ajaxFrame.style.visibility = "hidden";
        ajaxFrame.style.overflow = "visible";
        ajaxFrame.style.zIndex = "100";
        ajaxFrame.style.backgroundColor = "#ffffff";
        /*ajaxFrame.style.borderStyle = "solid";
        ajaxFrame.style.borderColor = "#777777";
        ajaxFrame.style.borderWidth = "2px";
        ajaxFrame.style.backgroundColor = "#ffffff";*/
        if (objOverlay.nextSibling)objBody.insertBefore(ajaxFrame, objOverlay.nextSibling);
	else objBody.appendChild(ajaxFrame);
      } 
	ajaxFrame.setAttribute("box", box);
 
   }
    else
    {
      ajaxFrame2 = document.createElement("div");
      //ajaxFrame2.onmousemove=moveAjaxFrame;
      ajaxFrame2.setAttribute("id", "ajaxFrame2");
      ajaxFrame2.style.position = "absolute";
      ajaxFrame2.style.left = offsets.x+150;
      ajaxFrame2.style.top = offsets.y+150;
      ajaxFrame2.style.visibility = "hidden";
      ajaxFrame2.style.overflow = "visible";
      ajaxFrame2.style.zIndex = "100";
      ajaxFrame2.setAttribute("box", box);
      ajaxFrame2.style.borderStyle = "solid";
      ajaxFrame2.style.borderColor = "#777777";
      ajaxFrame2.style.borderWidth = "2px";
      ajaxFrame2.style.backgroundColor = "#ffffff";
    
      if(objOverlay.nextSibling)objBody.insertBefore(ajaxFrame2, objOverlay.nextSibling);
      else objBody.appendChild(ajaxFrame2);	
      ajaxFrame = ajaxFrame2;
    }
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        var http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try {
            var http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                var http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    
    if (!http_request) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return;
    }
    else {
      if(http_request.overrideMimeType) 
        http_request.overrideMimeType('text/xml');
    }
    
    http_request.onreadystatechange = function() {getWebbox(http_request, secondLoad,callback);};
    
    if(method=="GET"){
      http_request.open("GET", box+params, true);
      http_request.send(null);
    }else{
        http_request.open("POST", box, true);
        http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        if (params) {
                http_request.setRequestHeader("Content-length", params.length);
                http_request.send(params);
        } else
          http_request.send('');
    }
}

/*
 * verarbeitet die webbox, die vom server angefordert wurde
 * erzeugt ein div (falls noch nicht vorhanden) mit der erhaltenen box und blendet es an der aktuellen mausposition ein
 */
function getWebbox(http_request, secondLoad,callback){
    if(http_request.readyState == 4){
	xml = http_request.responseXML;
	if (xml!=null) {if (xml.body)response=xml.body;		
	    		 else response=xml.documentElement;
	    if ((response)&&(response.xml)){
		xml = response.xml;
	    } else if (response){
		var s = new XMLSerializer();
		xml =  s.serializeToString(response); 
	    } else {
	    	xml = http_request.responseText;
	    }
	} else
	    xml = http_request.responseText;
    } else {
	return;
    }
   

    if(secondLoad)
      var ajaxFrame = document.getElementById("ajaxFrame2");
    else
    {
      var loadingImage = document.getElementById("loadingImage");
      var ajaxFrame = document.getElementById("ajaxFrame");
    }
    ajaxPanel = document.createElement("div");
    //stephan: wenn ein dialog "closepreview" zurueckgibt, wird die seite im vorschaumodus neu gelanden
    if((xml.indexOf("closepreview") <= 1) && (xml.indexOf("closepreview") > -1)){
      if(window.location.href.indexOf("?preview")>-1 || window.location.href.indexOf("&preview")>-1) 
	window.location.reload(); 
      else if(window.location.href.indexOf("?")==-1) 
	window.location.href=window.location.href+"?preview=yes";
      else 
	window.location.href=window.location.href+"&preview=yes";    

    }else if((xml.match("^close"))){
    	var href=window.location.href;
    	window.location.href=href; 
    	

    }else if((xml.match("<html>close"))){
	if (xml.match("<html>close:")) {
	    xml = xml.split(":");
	    xml = xml[1].split("<");
	    requestWebbox(xml[0]);
	    return;
	} else
		var href=window.location.href;
	    window.location.href=href; 

    }else{

	//document.onmousemove=storeMousePosition;
	ajaxFrame.innerHTML = "";
	ajaxPanel.innerHTML = xml;
	evalScript(xml);
	ajaxFrame.appendChild(ajaxPanel);
	setFramePosition(ajaxFrame, secondLoad);
	formatInputs(ajaxFrame);
	ajaxFrame.style.visibility = "visible";
    }
    
    //Ladebalken ausblenden, sobald Inhalt fertig geladen ist
    if(loadingImage)
      loadingImage.style.visibility = "hidden";

    if(secondLoad)
      setTimeout("removeFrame()", 1);

    //immer zurücksetzen!
    autosubmit = false;
    document.bgColor='#444444';
    /*
    //Tasten abfangen
    var is_ida = document.getElementById("jump_level_ajax");
    if(is_ida)
      document.onkeypress = keyPress;
    */

    var ida_error = document.getElementById("ida_error");
    if(ida_error && ida_error.value) {
      alert(ida_error.value);
      ida_error.value="";
    }

    if(storedFocusObject)
      setFocus();
    
    removeWait();
    if(callback)eval(callback);
}

function removeFrame()
{
  var ajaxFrame = document.getElementById("ajaxFrame2");
  document.body.removeChild(document.getElementById("ajaxFrame"));
  ajaxFrame.setAttribute("id", "ajaxFrame");
}

function setFramePosition(frame, secondLoad)
{
  var ajax_width = document.getElementById("ajax_width");
  var ajax_height = document.getElementById("ajax_height");
  var ajax_compare_table = document.getElementById("ajax_compare_table");
  var ajax_compare_div = document.getElementById("ajax_compare_div");
  var ajax_level_col = document.getElementById("ajax_level_col");

  //Scroll-Offsets ermitteln
  var offsets = getOffsets();

  //Framegröße bestimmen
  var size = {width:0, height:0};
  //Wenn eine feste Größe vorgegeben ist (hidden-Feld)
  if(ajax_width && ajax_height){
    //Menübreite fest, da offsetWidth die falsche Elementbreite liefert
    //ajax_level_col1.style.width = 250;
    //ajax_level_col1.style.height = (parseInt(ajax_height.value)-50);
    //ajax_level_col2.style.width = 250;
    //ajax_level_col3.style.width = 250;
    size.width = parseInt(ajax_width.value);
    size.height = parseInt(ajax_height.value);
    /*linn: entfernt wegen wei�em rand in ajax-ansicht ohne navigation
      ajax_compare_table.style.height = (parseInt(ajax_height.value)-80);
    ajax_compare_div.style.height = (parseInt(ajax_height.value)-80);
    ajax_compare_table.style.width = (parseInt(ajax_width.value)-265);
    ajax_compare_div.style.width = (parseInt(ajax_width.value)-265);*/
    ajax_compare_table.style.height = (parseInt(ajax_height.value));
    ajax_compare_div.style.height = (parseInt(ajax_height.value));
  
    ajax_compare_table.style.width = (parseInt(ajax_width.value));
    ajax_compare_div.style.width = (parseInt(ajax_width.value));

    //ajax_compare_table.style.width = '100%';
    //ajax_compare_div.style.width = '100%';
  }else{
    if(typeof document.layers != 'undefined'){
      size.width = frame.clip.width;
      size.height = frame.clip.height;
    }else if(frame.offsetWidth){
      size.width = parseInt(frame.offsetWidth);
      size.height = parseInt(frame.offsetHeight);
    }
  }
  
  //Frame neu psositionieren, wenn Werte bekannt
  if((size.width > 0) && (size.height > 0)){
    frame.style.width = size.width;
    frame.style.height = size.height;

    if(userXPos && secondLoad){
      //falls das fentser schonmal verschoben -> Position nicht neu berechnen
      frame.style.top = userYPos+'px';
      frame.style.left = (userXPos-size.width/2)+'px';
      return;
    }else if((!secondLoad) || ajax_width){
      if(size.width < getWindowWidth()){
	frame.style.left = getWindowWidth()/2-size.width/2+'px';
	//frame.style.marginLeft = '-' + (Math.floor(size.width/2)) + 'px';
      }else{
	//wenn Dialog zu breit für das Fenster, dann 20px neben linken Rand und Overlay verlängern
	frame.style.left = '20px';
	overlay = document.getElementById("overlay");
	overlay.style.height = getPageHeight()+"px"; 
      }
      
      if(size.height < getWindowHeight())
	frame.style.top = (Math.floor(getWindowHeight()/2) + offsets.y)-(Math.floor(size.height/2)) + 'px';
      else{
	//wenn Dialog zu lang für das Fenster, dann 20px unter oberen Rand und Overlay verlängern
	frame.style.top = (offsets.y + 20) + 'px';
	overlay = document.getElementById("overlay");
	overlay.style.height = getPageHeight()+"px"; 
      }
    }else{
      var ajaxFrame = document.getElementById("ajaxFrame");
      frame.style.top = ajaxFrame.style.top;
      frame.style.marginLeft = ajaxFrame.style.marginLeft;
      frame.style.left = ajaxFrame.style.left;
    }
  }
}

/*
 * entfernt alle Frames bei Klick auf das Overlay
 */
function closeFrame(restoreId){
    //document.onmousemove='';
    var ajaxFrame = document.getElementById("ajaxFrame");
    var imageFrame = document.getElementById("imageFrame");
    var overlay = document.getElementById("overlay");

    //fallse der alte inhalt des ajaxframes wiederhergestellt werden soll
    if(restoreId){
 	for(i=0; i<ajaxFrameStack.childNodes.length; i++){
	  if(ajaxFrameStack.childNodes[i].getAttribute('id')==restoreId){
	    embeddedFrame = document.getElementById(restoreId);
	    if(embeddedFrame){
	      embeddedFrame.innerHTML = ajaxFrameStack.childNodes[i].innerHTML;
 	      ajaxFrameStack.removeChild(ajaxFrameStack.childNodes[i]);
	      return;
            }
	  }
	}
      return;
    }
    if(ajaxFrame){
        document.body.removeChild(ajaxFrame);
        document.body.removeChild(overlay);
    }    
    else if(imageFrame){
      document.body.removeChild(imageFrame);
      document.body.removeChild(overlay);
    }
    else
      document.body.removeChild(overlay);
}

function pleaseWait(){
  //objBody = document.getElementsByTagName("body").item(0);
  objAjax = document.getElementById("ajaxFrame");
  if(objAjax)
  {
    ovWidth = objAjax.style.width;
    ovHeight = objAjax.style.height;
    ovTop = objAjax.style.top;
    ovLeft = objAjax.style.left;
    
    waitingDiv = document.createElement("div");
    waitingDiv.setAttribute("id","waitingDiv");
    waitingDiv.style.visibility = "visible";
    waitingDiv.style.position = "absolute";
    waitingDiv.innerHTML = '<table width="100%" height="100%" id="idaoverlay"><tr valign="middle"><td align="center"><img src="/skins/icons/loading.gif"/></td></tr></table>';
    waitingDiv.style.zIndex = "290";
    waitingDiv.style.left = '0px';
    waitingDiv.style.top = '0px';
    waitingDiv.style.width = ovWidth;
    waitingDiv.style.height = ovHeight;
    objAjax.insertBefore(waitingDiv, objAjax.firstChild);
  }
}

function removeWait()
{
  waitingDiv = document.getElementById("waitingDiv");
  objAjax = document.getElementById("ajaxFrame");

  if(waitingDiv && objAjax)
    objAjax.removeChild(waitingDiv);
}

/*
 * sammelt alle daten des ajaxFrames und schickt ein submit an den server (durch erneutes aufrufen der box)
 */
function processAjaxFrame(element){
    var is_ida = document.getElementById("jump_level_ajax");

  //if(is_ida)
    pleaseWait();

    //alert("processAjaxFrame_ajax");
    if(element != null)
      name = element.getAttribute("name");
      
    else
      name='';
    //action herausfinden...
    var close_test = name.search('cmd_close');
    if (close_test != -1){
      var href=window.location.href;
    	window.location.href=href; }

    divs = document.getElementsByTagName("div");
    for(i=0; i<divs.length; i++){
	if(divs[i].getAttribute("id")=="ajaxFrame"){
	  ajaxframe = divs[i];
	  box = divs[i].getAttribute("box");
	}
    }
    if (box.indexOf("?")>-1)
        box = box.substring(0,box.indexOf("?"));
    inputs = ajaxframe.getElementsByTagName("input");
    param=name+"=update";
    for (i=0;i<inputs.length;i++){
        if((inputs[i].type == "checkbox") || (inputs[i].type == "radio")){
            if(inputs[i].checked)
                param = param+ "&"+ inputs[i].name+"="+encodeURIComponent(inputs[i].value);
        }
        else
        {
          //Zeichen für die Übertragung/Transformation ersetzen/entfernen
          var saveinput = inputs[i].value;
          var inputvalue = inputs[i].value.replace(/ ¤/, "");
          var inputvalue = inputs[i].value.replace(/¤/, "");
	  //alert("test");
          //inputvalue = inputvalue.replace(/\./, "");
          //inputvalue = inputvalue.replace(/,/, ".");

          //zurückschreiben, falls keine Zahl
          if(isNaN(inputvalue))
            inputvalue = saveinput;
          param = param + "&" + inputs[i].name + "=" + encodeURIComponent(inputvalue);
        }
    }
   
    textareas = ajaxframe.getElementsByTagName("textarea");
    for (i=0;i<textareas.length;i++)
        param = param+ "&"+ textareas[i].name+"="+encodeURIComponent(textareas[i].value) ;
    selects = ajaxframe.getElementsByTagName("select");
    for (i=0;i<selects.length;i++)
        param = param+ "&"+ selects[i].name+"="+encodeURIComponent(selects[i].value) ;
    param = param.replace(/\+/, "%2B");
    requestWebbox(box, param, "POST", true);
}

function setFocus()
{
  if(storedFocusObject.id)
  {
    if(document.getElementById(storedFocusObject.id))
    {
      document.getElementById(storedFocusObject.id).focus();
      if(document.getElementById(storedFocusObject.id).type == 'text')
	document.getElementById(storedFocusObject.id).select();
    }
  }
}

function getWindowWidth() {
    if (window.innerWidth) {
        width = window.innerWidth;
    } else if (document.body && document.body.offsetWidth) {
        width = document.body.offsetWidth;
    } else {
        width = 200;
    }
    return width;
}

function getOffsets() {
  //Framegröße bestimmen
  var offsets = {x:0, y:0};
  if(typeof window.pageXOffset != 'undefined')
  {
    offsets.x = window.pageXOffset;
    offsets.y = window.pageYOffset;
  }
  else
  {
    offsets.x = document.body.scrollLeft;
    offsets.y = document.body.scrollTop;    
  }
  return offsets;
}

function getWindowHeight() {
    if (window.innerHeight) {
        height = window.innerHeight;
    } else if (document.body && document.body.offsetHeight) {
        height = document.body.offsetHeight;
    } else {
        height = 200;
    }
    return height;
}

function getPageWidth() {
  if(getPageScroll()[0] < getWindowWidth()){	
    pageWidth = getWindowWidth();
  } else {
    pageWidth = getPageScroll()[0];
  }
    return pageWidth;
}

function getPageHeight() {
  if(getPageScroll()[1] < getWindowHeight()){
    pageHeight = getWindowHeight();
  } else { 
    pageHeight = getPageScroll()[1];
  }
  return pageHeight;
}

function openPopup(Adresse) {
  Fenster1 = window.open(Adresse, "PDFDownload");
  Fenster1.focus();
}

function formatInputs(frame)
{
  inputs = frame.getElementsByTagName("input");

  for (i=0;i<inputs.length;i++)
  {
    if((inputs[i].getAttribute("moneyfield") == "yes" ) && (inputs[i].type != "checkbox") && (inputs[i].type != "radio") && (inputs[i].type != "hidden"))
      inputs[i].value = formatZahl(inputs[i].value);	
  }
}

//Zahl formatieren
function formatZahl(zahl)
{
  var save = zahl;
  var k = 2; //Anzahl Nachkommastellen
  var fix = true; //feste Anzahl

    if(!k) k = 0;
    var neu = '';
    // Runden
    var f = Math.pow(10, k);
    zahl = '' + parseInt( zahl * f + (.5 * (zahl > 0 ? 1 : -1)) ) / f ;
    
    //falls zahl falscher Typ, dann Original zurück
    if(zahl == 'NaN')
      return save;

    // Komma ermittlen
    var idx = zahl.indexOf('.');
    // fehlende Nullen einfügen
    if(fix)
    {
         zahl += (idx == -1 ? '.' : '' )
         + f.toString().substring(1);
    }
    // Nachkommastellen ermittlen
    idx = zahl.indexOf('.');
    if( idx == -1) idx = zahl.length;
    else neu = ',' + zahl.substr(idx + 1, k);

    // Tausendertrennzeichen
    while(idx > 0)
    {
        if(idx - 3 > 0)
        neu = '.' + zahl.substring( idx - 3, idx) + neu;
        else
        neu = zahl.substring(0, idx) + neu;
        idx -= 3;
    }
    
    //Eurozeichen aufügen
    neu += " €";
    return neu;
}



function getPageScroll(){

  xScroll = 0;
  yScroll = 0;

	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}


/*
 * =========================ab=hier=embedded=========================
 *
 * Neue Methode für mehrere Webboxen auf einer Seite
 */
function requestNewWebbox(box, params , method, id, frame, save_content,callback) {
    if (params==null)    
        params = '';

    // Zielframe holen und leeren
    var mainframe = document.getElementById(frame);
    if(mainframe){
      //inhalt fuer spaeter speichern
      if(save_content==true){
        ajaxFrameStack.appendChild(mainframe.cloneNode(true));
      }
      mainframe.innerHTML = "";
    }

    //stephan: falls kein embeddes javaframe da ist, wird der normale requestwebbox genutzt (zb bei tabellen im ajaxframe)
    if(!mainframe)
      return requestWebbox(box, params, method, false);

    // Ajax Frame erzeugen
    var ajaxFrame = '';
    ajaxFrame = document.createElement("div");
    ajaxFrame.setAttribute("id", id);
    //ajaxFrame.onmousemove=moveAjaxFrame;
    ajaxFrame.style.width = "100%";
    ajaxFrame.style.visibility = "hidden";
    ajaxFrame.style.overflow = "visible";
    ajaxFrame.style.zIndex = "3";
    ajaxFrame.setAttribute("box", box);

    // Ajax Frame zu Zielframe hinzufügen
    mainframe.appendChild(ajaxFrame);
	
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        var http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) 
            http_request.overrideMimeType('text/xml');
    } else if (window.ActiveXObject) { // IE
        try {
            var http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
               var http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    
    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return;
    }

    //ajaxFrame sauber holen!
    var ajaxFrame = document.getElementById(id);
    ajaxFrame.innerHTML="<img src='/skins/icons/ajax_loading.gif'/> Bitte warten, die Daten werden geladen ...";
    ajaxFrame.style.visibility = "visible";

    http_request.onreadystatechange = function() {getNewWebbox(http_request, box, id,callback);};
    if(method=="GET"){
        http_request.open("GET", box+params, true);
        http_request.send(null);
    }else{
        http_request.open("POST", box, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
        http_request.setRequestHeader('Content-Length', params.length);
        http_request.send(params);
    }
}

//bildschirmgerechtes Anzeigen eines längeren Textes in alert
function splitAndShow(text)
{
  i = 0;
  alert(text.length/2000 + " Seiten");
  while(i < text.length)
    {
      str = text.substr(i, 2000);
      i += 2000;
      alert(str);
    }
}


/*
 * erzeugt eine neue webbox
 */


function getNewWebbox(http_request, box, id,callback){
    if(http_request.readyState == 4) {
	xml = http_request.responseXML;
	if (xml!=null) {if (xml.body)response=xml.body;		
	    else response=xml.documentElement;
	    if ((response)&&(response.xml)){
		xml = response.xml;
	    } else if (response){
		var s = new XMLSerializer();
		xml =  s.serializeToString(response); 
	    } else
	    xml = http_request.responseText;
	} else
	    xml = http_request.responseText;
    } else {
	return;
    }
    if(xml.match("^close")){
    	var href=window.location.href;
	    window.location.href=href; 
    } else {
	//document.onmousemove=storeMousePosition;   	
        var ajaxFrame = document.getElementById(id);
        var ajaxPanel = document.createElement("div");
        ajaxFrame.innerHTML = "";
	    ajaxPanel.innerHTML = xml;
        ajaxFrame.appendChild(ajaxPanel);

        //CloseButton ausblenden
        var closeFrameButton = document.getElementById("closeFrameButton");
        if(closeFrameButton)
          closeFrameButton.innerHTML = "&nbsp;";
    }
    if(callback)eval(callback);
}

function getLoadingImage(item)
{
    var objLoadingImage = document.createElement("img");	
    objLoadingImage.id= "loadingImage_"+item;
    objLoadingImage.src = "/skins/icons/ajax_loading.gif";
    objLoadingImage.setAttribute("class","loadingImage");


    return objLoadingImage;

}

function removeLoadingImage(control)
{

    var par = control.parentNode.parentNode;
    var par_children = par.childNodes;
    for (var i=0;i<par_children.length;i++) {
        if (par_children[i].className == 'loadingImage') {
            par.removeChild(par.childNodes[i]);
        }
    }

}

function prepare_request() {

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        var http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try {
            var http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                var http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    
    if (!http_request) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    } else {
      if (http_request.overrideMimeType) {
        http_request.overrideMimeType('text/xml');
      }
      return http_request;
    }

}

function requestListValues() {

    http_request = prepare_request();

    params = 'boxname='+encodeURIComponent(document.getElementsByTagName('form')[0].getAttribute('name'))+
             '&supplier='+encodeURIComponent(selected_values[0]['filter_supplier'])+
             '&category='+encodeURIComponent(selected_values[0]['filter_category'])+
             '&class='+encodeURIComponent(selected_values[0]['filter_class'])+
             '&product='+encodeURIComponent(selected_values[0]['filter_product']);

    var url = '/ima.vertragswerke.scripts.control_options.script';
    http_request.onreadystatechange = setControlOptions;
    http_request.open("POST", url, true);
    http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    http_request.send(params);

}

function getElementsByClass(tagname,classname)
{

    var matches = new Array();

    elements = document.getElementsByTagName(tagname);
    for (var i=0;i<elements.length;i++) {
        if ( (elements[i].getAttribute('class') != null) && (elements[i].getAttribute('class') == classname) ) {
            matches.push(elements[i]);
        }
    }

    return matches;
}

function updateWebboxMenu()
{

    var parms = '';
    
    if (selected_values[0]['filter_product'] != '') {
        parms+= '/product='+encodeURIComponent(selected_values[0]['filter_product']);
    }
    if (selected_values[0]['filter_class'] != '') {
        parms+= '/class='+encodeURIComponent(selected_values[0]['filter_class']);
    }
    if (selected_values[0]['filter_supplier'] != '') {
        parms+= '/supplier='+encodeURIComponent(selected_values[0]['filter_supplier']);
    }
    if (selected_values[0]['filter_category'] != '') {
        parms+= '/category='+encodeURIComponent(selected_values[0]['filter_category']);
    }

    var anchors = getElementsByClass('a','webbox_menu_link');

    for (var i=0;i<anchors.length;i++) {

        var href = '';
        var onclick = ''
        var ar_href = new Array();
        var ar_onclick_quotes = new Array();
        var ar_onclick_slashes = new Array();

        href = anchors[i].getAttribute('href');
        ar_href = href.split('/');
        anchors[i].setAttribute('href',parms+'/'+ar_href[ar_href.length-1]);

        onclick = anchors[i].getAttribute('onclick');
        ar_onclick_quotes = onclick.split('"'); //"
        ar_onclick_slashes = ar_onclick_quotes[1].split('/');

        anchors[i].setAttribute('onclick',ar_onclick_quotes[0]+'"'+
                                parms+'/'+
                                ar_onclick_slashes[ar_onclick_slashes.length-2]+'/'+
                                ar_onclick_slashes[ar_onclick_slashes.length-1]+'"'+
                                ar_onclick_quotes[ar_onclick_quotes.length-1]);

    }
}

function prependAllOption(select) {
    var option = document.createElement('option');
    var dash = document.createTextNode('-');
    option.appendChild(dash);
    option.value = '';
    select.appendChild(option);
}

function setControlOptions() {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {

            var xmldoc = http_request.responseXML;

            var root_node = xmldoc.getElementsByTagName('Controls').item(0);
            var req_controls = root_node.childNodes;

            // FIREFOX-ONLY!!!
            // var req_controls = xmldoc.firstChild.childNodes;


            // jede Liste aus dem Request
            for (var i=0;i<req_controls.length;i++) {
                // filter_supplier
                var current_control = 'filter_'+req_controls[i].nodeName.toLowerCase();

                // jede Liste aus der Seite
                for (var j=0;j<control_cells.length;j++) {

                // FIREFOX-ONLY!!!
                // if (control_cells[j].getElementsByTagName('label')[0].getAttribute('for') == current_control) {

                    if (control_cells[j].getElementsByTagName('label')[0].attributes['for'].value == current_control) {
                        // passendes Seitencontrol zum aktuellen RequestControl wählen
                        var control = control_cells[j].getElementsByTagName('select')[0];

                        // control erstmal leeren
                        removeOptions(control);
                    }

                }

                // "-" voranstellen
                prependAllOption(control);

                // DataSet-Einträge des aktuellen RequestControls holen
                var datasets = req_controls[i].firstChild.childNodes[1].childNodes;

                for (var j=0;j<datasets.length;j++) {
                    var option = document.createElement('option');

                    if (datasets[j].getAttribute('name') == selected_values[0][current_control]) {

                        var attr = document.createAttribute('selected');
                        attr.nodeValue = 'selected';
                        option.setAttributeNode(attr);

                        var opt_text = document.createTextNode('['+datasets[j].getAttribute('name')+']');
                        option.appendChild(opt_text);

                    } else {

                        var opt_text = document.createTextNode(datasets[j].getAttribute('name'));
                        option.appendChild(opt_text);

                    }

                    control.appendChild(option);

                }


                //removeLoadingImage(control);
                //control.style.display = 'block';
            }

            new Function(document.getElementById('mainframe').firstChild.text)();

        } else {

            return false;

        }
    }
}

function getSelectedValue(control) {

    var retval = '';

    if (control.selectedIndex != -1) {
        retval = control.getElementsByTagName('option')[control.selectedIndex].firstChild.nodeValue;
        if ( (retval.charAt(0) == '[') && (retval.charAt(retval.length-1) == ']') ) {
            retval = retval.slice(1, retval.length-1);
        }
    }

    if ( (retval == '-') || (retval == 'undefined') ) {
        return '';
    } else {
        return retval;
    }

}

function removeOptions(control) {

    var options = control.getElementsByTagName('option');

    while (options.length) {
        control.removeChild(options[0]);
    }

}

function getControlValues(table_id,control_name)
{

    selected_values = new Array();
    selected_values[0] = new Object();


    control_cells = new Array();

    var table_cells = document.getElementById(table_id).firstChild.firstChild.childNodes;

    for (var i=0;i<table_cells.length;i++) {

        if (table_cells[i].firstChild.firstChild != null) {

            var filter_name = table_cells[i].getElementsByTagName('label')[0].attributes['for'].value;

            if (filter_name != 'filter_'+control_name) {
                //table_cells[i].getElementsByTagName('select')[0].style.display = 'none';
                //table_cells[i].appendChild(getLoadingImage(i));
            }

            control_cells.push(table_cells[i]);

            control = table_cells[i].getElementsByTagName('select')[0];

            selected_values[0][filter_name] = getSelectedValue(control);

        }

    }

    requestListValues();
    updateWebboxMenu();

}



     /*
	  EXPERIMENTELL(stephan): script zum bewegen der ajax-fenster
     */

     function storeMousePosition(e) {
       xPos    =  e? e.pageX : window.event.x;
       yPos    =  e? e.pageY : window.event.y;
      
       if (document.all && !document.captureEvents) {
         xPos    += document[docEl].scrollLeft;
         yPos    += document[docEl].scrollTop;      
       }
        
       if (document.layers) routeEvent(e);
    }
    
    function TitleBarMouseUp(){
      titleBarMouseDown = false;
    }

    function moveAjaxFrame(){
      id='ajaxFrame';
      if(titleBarMouseDown==true){
	document.getElementById(id).style.left = (xPos-parseInt(document.getElementById(id).style.width)/2)+'px';
        document.getElementById(id).style.top = (yPos-20)+'px';
	userXPos = xPos;
	userYPos = yPos-20;
      }
    }

function evalScript(loadjs) {	
 try
  {	
   if(loadjs != '')	
    {	
     var script = "";
     loadjs = loadjs.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, 
     function() {
      if (loadjs !== null) script += arguments[1] + '\n';
      return '';
     });
     if(script) (window.execScript) ? window.execScript(script) : window.setTimeout(script, 0);
    }
   return false;
  }
   catch(e)
    { 
      alert("fehler");
     alert(e);
     alert(e.name);
     alert(e.message);
    }

}



function showDialog(id, url){
    document.getElementById(id).style.left=(xPos+12)+"px";
    document.getElementById(id).style.top=(yPos-10)+"px";
    document.getElementById(id).style.visibility="visible";
    current_redirect_url = url;
}

function submitDialog(param){
if(param)
  window.location.href=current_redirect_url+'&'+param;
else
  window.location.href=current_redirect_url;
}

function closeDialog(id){
 current_delete_url = "";
 document.getElementById(id).style.visibility="hidden"; 
}


function storeMousePosition(e) {
  xPos    =  e? e.pageX : window.event.x;
  yPos    =  e? e.pageY : window.event.y;
 
  if (document.all && !document.captureEvents) {
    xPos    += document[docEl].scrollLeft;
    yPos    += document[docEl].scrollTop;      
  }
   
  if (document.layers) routeEvent(e);
}

function moveWindow(id){
 if(titleBarMouseDown==true){
   document.getElementById(id).style.left=xPos-100;
   document.getElementById(id).style.top=yPos-25;
 }
}

// nicht schön, aber für showDialog leider benötigt
document.onmousemove=storeMousePosition;

