// DREAMWEAVER FUNKTIONEN
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' Bitte eine gueltige Email-Adresse eingeben.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' Bitte eine Nummer eingeben.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' Bitte eine Nummer zwischen '+min+' und '+max+' eingeben.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' wird benoetigt.\n'; }
  } if (errors) alert('Folgender Fehler ist aufgetreten:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//SCROLLING
var srollObject = null;
	
function initPage() {
  var objElement = document.getElementById("content");
  srollObject = new scroll(objElement, 150);
}

function scroll(objElement, intHeight, intWidth) {
  var self = this;
  this._x = 0;
  this._y = 0;
  objElement.style.top = "0px";
  objElement.style.left = "0px";

  this.setPosition = function(strDirection, intPos, x) {
    if (intPos > 0) 
		intPos = 0;
	if (objElement.offsetWidth - document.getElementById("scrollWindowDiv").offsetWidth < 0) {
		intPos = 0;
	} else {
		if (intPos < ((objElement.offsetWidth - document.getElementById("scrollWindowDiv").offsetWidth) * -1))
			intPos = ((objElement.offsetWidth - document.getElementById("scrollWindowDiv").offsetWidth) * -1);
	}
	//if (intPos < objElement.offsetWidth) intPos = objElement.offsetWidth;
	
    switch (strDirection) {
      case 'x':
        if (intPos < intWidth - objElement.offsetWidth)
          intPos = intWidth - objElement.offsetWidth;
        this._x = intPos;
        objElement.style.left  = this._x +"px";

        break;
      case 'y':
        if (intPos < intHeight - objElement.offsetHeight)
          intPos = intHeight - objElement.offsetHeight;
        this._y = intPos;
        objElement.style.top  = this._y +"px";
        break;
    }
  };

  this.scrollX = function(x) { this.setPosition('x', this._x + x, x); };
  this.scrollY = function(y) { this.setPosition('y', this._y + y, y); };

  this.startX = function(x) {
    this.scrollTimer = window.setInterval(
      function() { self.scrollX(x); }, 1 );
  };

  this.startY = function(y) {
    this.scrollTimer = window.setInterval(
      function() { self.scrollY(y); }, 1 );
  };

  this.stop = function() {
    if (this.scrollTimer) window.clearInterval(this.scrollTimer); };
};
//EIN- /AUSBLENDEN
function JSblend(ShowLayer) {
	var oElems = document.getElementsByTagName("div");
	for (var i = 0; i < oElems.length; i++) {
		if (oElems[i].id.indexOf("dot_") > -1) {
			if (oElems[i].id != ShowLayer) 
			{
				oElems[i].style.visibility = "hidden";
				oElems[i].style.position = "absolute";
			}
			else  
			{
				oElems[i].style.visibility = "visible";
				oElems[i].style.position = "static";
			}
		}
	}
}
function ShowNeuheiten(ID) {
    var oElem = document.getElementById(ID);
    if (oElem.style.display == 'none') {
        oElem.style.display = 'block';
    }
    else {
        oElem.style.display = 'none';
    }
}

//ANDERE FUNKTIONEN
function JSddMenu(ShowLayer) {
	var oElems = document.getElementsByTagName("div");
	for (var i = 0; i < oElems.length; i++) {
		if (oElems[i].id.indexOf("div_nav_") > -1) {
			if (oElems[i].id != ShowLayer) 
				oElems[i].style.visibility = "hidden";
			else  
				oElems[i].style.visibility = "visible";
		}
	}
}
function JSddMenuDISPLAY(ShowLayer) {
	var oElems = document.getElementsByTagName("div");
	for (var i = 0; i < oElems.length; i++) {
		if (oElems[i].id.indexOf("div_dis_") > -1) {
			if (oElems[i].id != ShowLayer) 
				oElems[i].style.display = "none";
			else  
				oElems[i].style.display = "block";
		}
	}
}
function MImg(ImgID, Src) {
    document.getElementById(ImgID).src = Src;
}

//Utilities
var helpObject = new Object; //needed for the quick help

var mouseX;
var mouseY;

//saves the mouse position
function RefreshMousePosition(e) {
    /*
    if (!e)
        var e = window.event || window.Event;

    if(!IsIE()) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    } 
    else {
        mouseX = e.clientX + document.body.scrollLeft;       
        mouseY = e.clientY + document.body.scrollTop;
    }
    */
}


//assign the mouse move handler, so whenever we move the mouse the latest position will be saved.
document.onmousemove = RefreshMousePosition;


//gets the x position of the cursor
function GetMouseX() {   
    return mouseX;
}


//gets the y position of the cursor
function GetMouseY() {      
    return mouseY;
}

//for the EworxTabControl
function Tab_Over() { 
    if (!IsIE) {
        img = document.getElementById("divTabHoverIcon"); 
        img.style.display = "block"; 
        img.style.top = GetMouseY()  - 3 + "px"; 
        img.style.left = GetMouseX() - 20 + "px";
   }
   else {
        img = document.getElementById("divTabHoverIcon"); 
        img.style.display = "block";
        img.style.top = window.event.y + document.documentElement.scrollTop + "px";
        img.style.left = window.event.x - 20 + "px";        
    }
} 

//for the EworxTabControl
function Tab_Out() { 
    document.getElementById("divTabHoverIcon").style.display = "none"; 
    //alert("tab out");
};


//used for timeouts in Atlas calls
function Timeout(result) {
    alert("A unexpected timeout has occurred. Please retry.");
}


//used for errors in Atlas calls
function Error(result) {
    alert("An unexpected error has occurred: " + result.get_message());
}

//gets an element on the document
function GetElement(id) {
    if (!document.getElementById(id))
        alert("Object '" + id + "' does not exist on this document.");
    return document.getElementById(id);
}

//opens a popup window
function Popup(url, id, width, height, resizeable) {
    resizeable ? resizeable = "yes" : resizeable = "no";
    var screenLeft = ((screen.width - width) / 2);
    var screenTop = (screen.height - height) / 2;  
    var w = window.open(url, id, "toolbar=no,scrollbars=yes,location=no,status=no,menubar=no,resizable=" + resizeable + ",width=" + width + ",height=" + height + ",top=" + screenTop + ",left=" + screenLeft);
    if (w)
        w.focus();
    else
        alert("Popup blockiert.");
    return w;
}

//displays the 'please wait' window
function Wait() {
    var div = GetElement("divWait");
    if (IsIE()) { 
        div.style.left = ((document.body.offsetWidth - 300) / 2) + "px";
        div.style.top = ((document.body.offsetHeight - 75) / 2) + "px";
    }
    else { 
        div.style.left = ((window.innerWidth - 250) / 2) + "px";
        div.style.top = ((window.innerHeight - 75) / 2) + "px";
    }
    div.style.display = "inline";
    setTimeout("document.getElementById('imgWait').src = document.getElementById('imgWait').src", 10);
    selects = document.getElementsByTagName("select");
    for (i = 0; i < selects.length; i++) 
        if (Hits(div, selects[i]))
            selects[i].style.display = "none";
}

//hides the 'please wait' window
function HideWait() {
    div = document.getElementById("divWait");
    selects = document.getElementsByTagName("select");
    for (i = 0; i < selects.length; i++) 
        if (Hits(div, selects[i]))
            selects[i].style.display = "inline";
    div.style.display = "none";
}

//hides or displays a div
function ToggleOne(div) {
    div = document.getElementById(div);  
      
    if (div.style.display == "none")
        div.style.display = "block";
    else
        div.style.display = "none";  
}

//waits some time before displaying the quick help
function HelpOver(id, x, y) {
    if (helpObject[id] != 1)
        helpObject[id] = 1;
    setTimeout("DisplayHelp('" + id + "'," + x + ", " + y +")", 500);
}

//displays the quick help
function DisplayHelp(id, x, y) {
    if (helpObject[id] == 1) {
        div = GetElement(id);
        if (div.style.display == "none") {
            img = document.getElementById("img" + id);
            if (img) {
                div.style.top = FindPosY(img) + 30;
                div.style.left = FindPosX(img);
            }
            else {
                div.style.top = y;
                div.style.left = x;
            }
            
            GetElement(id).style.display = "block";
            
            if (IsIE()) {
                selects = document.getElementsByTagName("select");
                helpObject[id + "selects"] = selects;
                for (i = 0; i < selects.length; i++) 
                    if (Hits(div, selects[i]))
                        selects[i].style.display = "none";
            }
        }
    }
}

//hides the quick help
function HelpOut(id) {
    helpObject[id] = 0;
   
    div = GetElement(id);
    if (div.style.display != "none") {
        mouseX = GetMouseX();
        mouseY = GetMouseY();

        divX = FindPosX(div);
        divY = FindPosY(div);

        if (mouseX - 2 < divX || mouseX > parseInt(divX + GetWidth(div)) || mouseY - 2 < divY || mouseY > parseInt(divY + GetHeight(div))) {
            GetElement(id).style.display = "none";
            selects = helpObject[id + "selects"];
            for (i = 0; i < selects.length; i++) 
                selects[i].style.display = "inline";
        }
    }
}

//checks if one object hits another one at any place
function Hits(obj1, obj2) {
    obj1X = FindPosX(obj1);
    obj1Y = FindPosY(obj1);
    obj1Width = GetWidth(obj1);
    obj1Height = GetHeight(obj1);
    obj2X = FindPosX(obj2);
    obj2Y = FindPosY(obj2);
    obj2Width = GetWidth(obj2);
    obj2Height = GetHeight(obj2);
    
    for (x = obj2X; x <= obj2X + obj2Width; x += 2)
        if (x >= obj1X && x <= obj1X + obj1Width)
            //we hit it on the X axis
            for (y = obj2Y; y <= obj2Y + obj2Width; y += 2)
                if (y >= obj1Y && y <= obj1Y + obj1Height)
                    return true;

    return false;
}

//hides or displays a div and writes the value whether or not it's displayed to a hidden field
function ToggleOneAndSet(div, hiddenField) {
    div = document.getElementById(div);  
    hiddenField = document.getElementById(hiddenField);
      
    if (div.style.display == "none") {
        div.style.display = "block";
        hiddenField.value = "ON";
    }
    else {
        div.style.display = "none"; 
        hiddenField.value = "OFF";
    }
}

//hides or displays a div and toggles an image
function ToggleOneAndImage(div, image, onImage, offImage) {
    div = GetElement(div);  
    image = GetElement(image);
      
    if (div.style.display == "none") {
        div.style.display = "block";
        image.src = onImage;
    }
    else {
        div.style.display = "none"; 
        image.src = offImage;
    }
}

//hides or displays a div and toggles two images
function ToggleOneAndTwoImages(div, image1, image2, onImage1, onImage2, offImage1, offImage2) {
    if (GetElement(div).style.display == "none")
        GetElement(image2).src = onImage2;
    else
        GetElement(image2).src = offImage2;

    ToggleOneAndImage(div, image1, onImage1, offImage1);
}

//hides and shows two divs
function Toggle(div1, div2) {
    div1 = document.getElementById(div1);  
    div2 = document.getElementById(div2);  
          
    if (div1.style.display == "none") {
        div1.style.display = "block";
        div2.style.display = "none";  
    }
    else {
        div1.style.display = "none";  
        div2.style.display = "block";
    }
}

//hides and shows two divs
function ToggleByValue(div1, div2, value) {
    div1 = document.getElementById(div1);  
    div2 = document.getElementById(div2);  
          
    if (value) {
        div1.style.display = "block";
        div2.style.display = "none";  
    }
    else {
        div1.style.display = "none";  
        div2.style.display = "block";
    }
}

//hides and shows two divs, and sets a hidden field whether or not the first div is displayed
function ToggleAndSet(div1, div2, hiddenField) {
    div1 = document.getElementById(div1);  
    div2 = document.getElementById(div2);      
    hiddenField = document.getElementById(hiddenField);
      
       
    if (div1.style.display == "none") {
        div1.style.display = "block";
        div2.style.display = "none";        
        hiddenField.value = "OFF";
    }
    else {
        div1.style.display = "none"; 
        div2.style.display = "block";        
        hiddenField.value = "ON";
    }
}

//adds a new item to a listbox
function AddToList(list, text, value) {
    list.length ++;
    list.options[list.length - 1].value = value;
    list.options[list.length - 1].text = text;
}

//removes the currently selected item from a listbox
function RemoveSelectedFromList(list) {
    selected = list.selectedIndex;
    if (selected >= 0) {
        replaceTextArray = new Array(list.length - 1);
        replaceValueArray = new Array(list.length - 1);
        for (var i = 0; i < list.length; i++) {
            if ( i < selected) { replaceTextArray[i] = list.options[i].text; }
            if ( i > selected ) { replaceTextArray[i-1] = list.options[i].text; }
            if ( i < selected) { replaceValueArray[i] = list.options[i].value; }
            if ( i > selected ) { replaceValueArray[i-1] = list.options[i].value; }
        }
        list.length = replaceTextArray.length;  
        for (i = 0; i < replaceTextArray.length; i++) { 
            list.options[i].value = replaceValueArray[i];
            list.options[i].text = replaceTextArray[i];
        }
    }
}


//gets the value how much the user has scrolled to the left
function GetScrollLeft() {
    if (self.pageXOffset) // all except Explorer 
	    return self.pageXOffset;
    else if (document.documentElement && document.documentElement.scrollLeft) // Explorer 6 Strict
	    return document.documentElement.scrollLeft;
    else if (document.body) // all other Explorers
	    return document.body.scrollLeft;
}


//gets the value how much the user has scrolled down
function GetScrollTop() {
    if (self.pageYOffset) // all except Explorer 
	    return self.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
	    return document.documentElement.scrollTop;
    else if (document.body) // all other Explorers
	    return document.body.scrollTop;
}


//gets the actual width of an object
function GetWidth(obj) {
    return obj.offsetWidth;
}


//gets the actual height of an object
function GetHeight(obj) {
    return obj.offsetHeight;
}

//returns the x-offset of an object
function FindPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
		
	return curleft;
}

//returns the y-offset of an object
function FindPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
		
	return curtop;
}

//drag & drop
var g_isDragging = false;
var g_hasDragMoved = false;
var g_dragAndDropArgument = ""; //[type]@[elementId]@[index]
var g_dropArgument = "";        //[type]@[elementId]@[index]
var g_dropFunctions = new Array();

//returns if somenthing is dragging at the moment.
function IsDragging() {
    return (g_isDragging && g_hasDragMoved);
}

//starts dragging
function Drag(argument, e) {
    var button;
    if (!e)
        e = window.event;
    if (e && e.which)
        button = e.which;
    else if (e)
        button = e.button;

    if (IsIE() && button != 1)
        return; //we only allow left clicks to start the drag/drop operation.

    g_isDragging = true;
    g_dragAndDropArgument = argument;
    Move();
    document.onselectstart = new Function ("return false;")
    document.onmousemove = function() { Move(); return false; };
    document.onmouseup = function() { ResetDrag(); Move(); return false; }
}

//stops dragging -> Drop
function Drop(onto) {
    if (onto != g_dragAndDropArgument) {
        g_dropArgument = onto;
        document.onselectstart = new Function ("return true;")
        CallDropFunctions(onto)
    }
    ResetDrag();
    return true;
}

//the Drag-Start-Argument. Which element has been dragged.
function GetDragDropArgument() {
    return g_dragAndDropArgument;
}

//resets the drag & Drop mechanism
function ResetDrag() {
    g_isDragging = false;
    g_HasDragMoved = false;
}

//checks if it is a drag or if it is a click
function CheckDragging() {
    g_hasDragMoved = g_isDragging;
    Move();
}

//shows a div while dragging something
function Move() {
    div = GetElement("divDragAndDrop");
    if (IsDragging()) {
        if (IsIE()) { //Gecko won't display the dragging div, but at least we won't get javascript errors.
            div.style.visibility = "visible";
            div.style.left = window.event.x + document.documentElement.scrollLeft + 1;
            div.style.top = window.event.y + document.documentElement.scrollTop - 11;
        }
    }
    else {
        div.style.visibility = "hidden";
        document.onmousedown = null;
        document.onmouseup = null;
    }
}

function GetDragDropArgumentIndex() {
    return GetDragDropArgumentValue(g_dragAndDropArgument, 2);
}

function GetDragDropArgumentType() {
    return GetDragDropArgumentValue(g_dragAndDropArgument, 0);
}

function GetDragDropArgumentId() {
    return GetDragDropArgumentValue(g_dragAndDropArgument, 1);
}

//Get one value of a drag argument
function GetDragDropArgumentValue(argument, index) {
    var ret;
    var startIndex = 0;
    var endIndex = 0;
    
    for (var i = 0; i < index; i++) {
        startIndex = argument.indexOf("@") + 1;
        argument = argument.substr(startIndex, argument.length - startIndex);        
    }
    endIndex = argument.substr(startIndex, argument.length - startIndex).indexOf("@") + startIndex;

    if (endIndex <= 0)
        endIndex = argument.length - startIndex;
    
    ret = argument.substring(0, endIndex);
    return ret;
}

//Get the index of the drop argument
function GetDropArgumentValue() {
    return GetDragDropArgumentValue(g_dropArgument, 2);
}

//Get the html type on which is dropped to
function GetDropArgumentType() {
    return GetDragDropArgumentValue(g_dropArgument, 0);
}

//Get the name of the element on which is dropped to
function GetDropArgumentName() {
    return GetDragDropArgumentValue(g_dropArgument, 1);
}

//Register a function, which should be called after dropping
function RegisterDrop(func) {
    g_dropFunctions.push(func);
}

//Calls the drop functions
function CallDropFunctions(argument) {
    for (var i = 0; i < g_dropFunctions.length; i++) {
        var func = g_dropFunctions[i];
        f = new Function(func);
        f();
    }
}

function DragDragDropDiv() {
    document.write("<div id='divDragAndDrop' style='position: absolute; visibility: hidden; background-color:#aaaaaa; width:200px; height:22px;'></div>");
}
DragDragDropDiv();

//Master Functions
var masterResizeFunctions = new Array();

function MasterResize(fncs) {
    var fncs = "";
    for (var i = 0; i < masterResizeFunctions.length; i++) {
        fncs += masterResizeFunctions[i];
    }
    if (fncs != "") 
        RegisterEvent(window, "onresize", new Function(fncs));
}


function OnLoadResize() {
    var fncs = "";
    for (var i = 0; i < masterResizeFunctions.length; i++) {
        fncs += masterResizeFunctions[i];
    }
    f = new Function(fncs);
    f();
    return true;
}

function MasterRegisterResizeFunction(functionName) {
    masterResizeFunctions.push(functionName);
}

function MasterUnRegisterResizeFunction(functionName) {
    masterResizeFunctions.pop(functionName);
}

function MasterGetHeight(withScroll) {
    var height = document.documentElement.clientHeight;
    if (withScroll)
        height = document.documentElement.scrollHeight;
    return height;
}

//browser
function IsIE() {
    return (document.all);
}

function IsOpera() {
    return (navigator.userAgent.indexOf("Opera") != -1 && document.getElementById);
}

function IsNS() {
    return (document.layers);
}

function IsMozilla() {
    return (!document.all && document.getElementById);
}

function RegisterEvent(obj, method, functionPointer) {
    if (IsIE())
        obj.attachEvent(method, functionPointer);
    else 
        obj.addEventListener(GetEventMethodForGecko(method), functionPointer, false);
}

function UnregisterEvent(obj, method, functionPointer) {
    if (IsIE())
        obj.detachEvent(method, functionPointer);
    else
        obj.removeEventListener(GetEventMethodForGecko(method), functionPointer, false);
}

function GetEventMethodForGecko(method) {
    method = method.toLowerCase();
    if (method == "onmousedown")
        method = "mousedown";
    else if (method == "onmouseup")
        method = "mouseup";    
    else if (method == "onclick")
        method = "click";   
    else if (method == "onmousemove")
        method = "mousemove";   
    else if (method == "onmouseover")
        method = "mouseover";   
    else if (method == "onmouseout")
        method = "mouseout";                                           
    else if (method == "onresize")
        method = "resize"; 
    return method;   
}

//gets a value from the querystring
function GetQuerystring(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable)
            return pair[1];
    } 
    alert('Querystring variable ' + variable + ' not found.');
}

//checks or unchecks all checkboxes
function CheckAllCheckboxes(checked, id){
    var i = 0;
    var checkBox = document.getElementById(id + "_" + i);
    while (checkBox) {
        if (!checkBox.disabled)
            checkBox.checked = checked;
        i++;
        checkBox = document.getElementById(id + "_" + i);
    }
}

//for the navigation
function ToggleNavigation(divName) {
    div = GetElement("div" + divName);
    if (div.style.display == "none") {
        div.style.display = "block";
        GetElement("img" + divName).src = icon_opened;
    }
    else {
        div.style.display = "none";
        GetElement("img" + divName).src = icon_closed;      
    }
}

//fires a postback when the user presses the enter key
//usage: onkeypress = PostbackOnEnter(event.keyCode, event.which, 'control');
function PostbackOnEnter(ieChar, geckoChar, control) {
    if ((geckoChar && geckoChar == 13) || ieChar == 13)
        __doPostBack(control, "");
    return true;
}