



// Fix for bug in IE6.0 float handling (don't ask, don't tell ;)

if (document.all && window.attachEvent) window.attachEvent("onload", fixWinIE);
function fixWinIE() {
       document.all.content.style.display = 'block';
}


// The calendar popup show/hide:

    function showDay(date) {
        document.getElementById('day' + date).style.visibility = 'visible';
        return true;
    }
    
    function hideDay(date) {
        document.getElementById('day' + date).style.visibility = 'hidden';
        return true;
    }

// Focus on error or tabindex=1
if (window.addEventListener) window.addEventListener("load",setFocus,false);
else if (window.attachEvent) window.attachEvent("onload",setFocus);
function setFocus() {
    var xre = new RegExp(/\berror\b/);
    // Search only forms to avoid spending time on regular text
    for (var f = 0; (formnode = document.getElementsByTagName('form').item(f)); f++) {
        // Search for errors first, focus on first error if found
        for (var i = 0; (node = formnode.getElementsByTagName('div').item(i)); i++) {
            if (xre.exec(node.className)) {
                for (var j = 0; (inputnode = node.getElementsByTagName('input').item(j)); j++) {
                    inputnode.focus();
                    return;   
                }
            }
        }
        // If no error, focus on input element with tabindex 1
        for (var i = 0; (node = formnode.getElementsByTagName('input').item(i)); i++) {
            if (node.getAttribute('tabindex') == 1) {
                 node.focus();
                 return;   
            }
        }
    }
}

/********* Table sorter script *************/
// Table sorter script, thanks to Geir Bækholt for this.
// DOM table sorter originally made by Paul Sowden 

function compare(a,b)
{
    au = new String(a);
    bu = new String(b);

    if (au.charAt(4) != '-' && au.charAt(7) != '-')
    {
    var an = parseFloat(au)
    var bn = parseFloat(bu)
    }
    if (isNaN(an) || isNaN(bn))
        {as = au.toLowerCase()
         bs = bu.toLowerCase()
        if (as > bs)
            {return 1;}
        else
            {return -1;}
        }
    else {
    return an - bn;
    }
}



function getConcatenedTextContent(node) {
    var _result = "";
	  if (node == null) {
		    return _result;
	  }
    var childrens = node.childNodes;
    var i = 0;
    while (i < childrens.length) {
        var child = childrens.item(i);
        switch (child.nodeType) {
            case 1: // ELEMENT_NODE
            case 5: // ENTITY_REFERENCE_NODE
                _result += getConcatenedTextContent(child);
                break;
            case 3: // TEXT_NODE
            case 2: // ATTRIBUTE_NODE
            case 4: // CDATA_SECTION_NODE
                _result += child.nodeValue;
                break;
            case 6: // ENTITY_NODE
            case 7: // PROCESSING_INSTRUCTION_NODE
            case 8: // COMMENT_NODE
            case 9: // DOCUMENT_NODE
            case 10: // DOCUMENT_TYPE_NODE
            case 11: // DOCUMENT_FRAGMENT_NODE
            case 12: // NOTATION_NODE
                // skip
                break;
        }
        i ++;
    }
  	return _result;
}



function sort(e) {
    var el = window.event ? window.event.srcElement : e.currentTarget;

    // a pretty ugly sort function, but it works nonetheless
    var a = new Array();
    // check if the image or the th is clicked. Proceed to parent id it is the image
    // NOTE THAT nodeName IS UPPERCASE
    if (el.nodeName == 'IMG') el = el.parentNode;
    //var name = el.firstChild.nodeValue;
    // This is not very robust, it assumes there is an image as first node then text
    var name = el.childNodes.item(1).nodeValue;
    var dad = el.parentNode;
    var node;
    
    // kill all arrows
    for (var im = 0; (node = dad.getElementsByTagName("th").item(im)); im++) {
        // NOTE THAT nodeName IS IN UPPERCASE
        if (node.lastChild.nodeName == 'IMG')
        {
            lastindex = node.getElementsByTagName('img').length - 1;
            node.getElementsByTagName('img').item(lastindex).setAttribute('src','http://www.ion.ag/arrowBlank.gif');
        }
    }
    
    for (var i = 0; (node = dad.getElementsByTagName("th").item(i)); i++) {
        var xre = new RegExp(/\bnosort\b/);
        // Make sure we are not messing with nosortable columns, then check second node.
        if (!xre.exec(node.className) && node.childNodes.item(1).nodeValue == name) 
        {
            //window.alert(node.childNodes.item(1).nodeValue;
            lastindex = node.getElementsByTagName('img').length -1;
            node.getElementsByTagName('img').item(lastindex).setAttribute('src','http://www.ion.ag/arrowUp.gif');
            break;
        }
    }

    var tbody = dad.parentNode.parentNode.getElementsByTagName("tbody").item(0);
    for (var j = 0; (node = tbody.getElementsByTagName("tr").item(j)); j++) {

        // crude way to sort by surname and name after first choice
        a[j] = new Array();
        a[j][0] = getConcatenedTextContent(node.getElementsByTagName("td").item(i));
        a[j][1] = getConcatenedTextContent(node.getElementsByTagName("td").item(1));
        a[j][2] = getConcatenedTextContent(node.getElementsByTagName("td").item(0));		
        a[j][3] = node;
    }

    if (a.length > 1) {
	
        a.sort(compare);

        // not a perfect way to check, but hell, it suits me fine
        if (a[0][0] == getConcatenedTextContent(tbody.getElementsByTagName("tr").item(0).getElementsByTagName("td").item(i))
	       && a[1][0] == getConcatenedTextContent(tbody.getElementsByTagName("tr").item(1).getElementsByTagName("td").item(i))) 
        {
            a.reverse();
            lastindex = el.getElementsByTagName('img').length - 1;
            el.getElementsByTagName('img').item(lastindex).setAttribute('src','http://www.ion.ag/arrowDown.gif');
        }

    }
	
    for (var j = 0; j < a.length; j++) {
        tbody.appendChild(a[j][3]);
    }
}
    
function init(e) {
    var tbls = document.getElementsByTagName('table');
    for (var t = 0; t < tbls.length; t++)
        {
        // elements of class="listing" can be sorted
        var re = new RegExp(/\blisting\b/)
        // elements of class="nosort" should not be sorted
        var xre = new RegExp(/\bnosort\b/)
        if (re.exec(tbls[t].className) && !xre.exec(tbls[t].className))
        {
            try {
                var tablename = tbls[t].getAttribute('id');
                var thead = document.getElementById(tablename).getElementsByTagName("thead").item(0);
                var node;
                // set up blank spaceholder gifs
                blankarrow = document.createElement('img');
                blankarrow.setAttribute('src','http://www.ion.ag/arrowBlank.gif');
                blankarrow.setAttribute('height',6);
                blankarrow.setAttribute('width',9);
                // the first sortable column should get an arrow initially.
                initialsort = false;
                for (var i = 0; (node = thead.getElementsByTagName("th").item(i)); i++) {
                    // check that the columns does not have class="nosort"
                    if (!xre.exec(node.className)) {
                        node.insertBefore(blankarrow.cloneNode(1), node.firstChild);
                        if (!initialsort) {
                            initialsort = true;
                            uparrow = document.createElement('img');
                            uparrow.setAttribute('src','http://www.ion.ag/arrowUp.gif');
                            uparrow.setAttribute('height',6);
                            uparrow.setAttribute('width',9);
                            node.appendChild(uparrow);
                        } else {
                            node.appendChild(blankarrow.cloneNode(1));
                        }
    
                        if (node.addEventListener) node.addEventListener("click",sort,false);
                        else if (node.attachEvent) node.attachEvent("onclick",sort);
                    }
                }
            } catch(er) {}
        }
    }
}

// initialize the sorter functions 
// add stuff to secure it from broken DOM-implanetations or missing objects.
   
    
    	
//    p.appendChild(document.createTextNode("Change sorting by clicking on each individual heading."));
//    document.getElementById(tablename).parentNode.insertBefore(p,document.getElementById(tablename));
    

if (window.addEventListener) window.addEventListener("load",init,false);
else if (window.attachEvent) window.attachEvent("onload",init);

       
// **** End table sort script ***



// Actions used in the folder_contents view

function submitFolderAction(folderAction) {
    document.folderContentsForm.action = document.folderContentsForm.action+'/'+folderAction;
    document.folderContentsForm.submit();
}

function submitFilterAction() {
    document.folderContentsForm.action = document.folderContentsForm.action+'/folder_contents';
    filter_selection=document.getElementById('filter_selection');
    for (var i =0; i < filter_selection.length; i++){
        if (filter_selection.options[i].selected) {
            if (filter_selection.options[i].value=='#') {
                document.folderContentsForm.filter_state.value='clear_view_filter';
            }
            else {
                document.folderContentsForm.filter_state.value='set_view_filter'
            }
        }						
    }
    document.folderContentsForm.submit();
}
    

// Functions for selecting all checkboxes in folder_contents view

isSelected = false;

function selectAll() {
  checkboxes = document.getElementsByName('ids:list');
  for (i = 0; i < checkboxes.length; i++)
    checkboxes[i].checked = true ;
  isSelected = true;
  return isSelected;
}

function deselectAll() {
  checkboxes = document.getElementsByName('ids:list');
  for (i = 0; i < checkboxes.length; i++)
    checkboxes[i].checked = false ;
  isSelected = false;
  return isSelected;
}

function toggleSelect(selectbutton) {
  if (isSelected == false) {
    selectbutton.setAttribute('src','http://www.ion.ag/select_none_icon.gif');
    return selectAll();
  }
  else {
    selectbutton.setAttribute('src','http://www.ion.ag/select_all_icon.gif');
    return deselectAll();
  }
}

function mouseoverTabs(obj)
{
  var tabsp_arr=MM_findObj('tab_spalten');
  var tabli_arr=MM_findObj('tab_links');
  
  if(tabsp_arr && tabli_arr)
  {
    if(!tabsp_arr[0])
      tabsp_arr = new Array(tabsp_arr);
    if(!tabli_arr[0])
      tabli_arr = new Array(tabli_arr);
    

    for(j=0;j<tabli_arr.length;j++)
    {
      if(tabli_arr[j].parentNode==obj)
      {
        for(i=0;i<tabsp_arr.length;i++) 
        {
          if(tabli_arr[j].parentNode==tabsp_arr[i])
          {
            if(tabsp_arr[i].background.search(/tab_sel_/)>-1)
            {
              tabsp_arr[i-1].background=tabsp_arr[i-1].background.replace(/tab_sel_/,'tab_');
              tabsp_arr[i].background=tabsp_arr[i].background.replace(/tab_sel_/,'tab_');
              tabsp_arr[i+1].background=tabsp_arr[i+1].background.replace(/tab_sel_/,'tab_');
            }
            else
            {
              tabsp_arr[i-1].background=tabsp_arr[i-1].background.replace(/tab_/,'tab_sel_');
              tabsp_arr[i].background=tabsp_arr[i].background.replace(/tab_/,'tab_sel_');
              tabsp_arr[i+1].background=tabsp_arr[i+1].background.replace(/tab_/,'tab_sel_');
            }
          }
        }
        
        if(tabli_arr[j].className=='selected')
          tabli_arr[j].className='plain';
        else
          tabli_arr[j].className='selected';      
      }
    }
  } 
  return true;
}


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;
}

	//-----------------------------------------------------------------------------
	//	Ändert die Hintergrundfarbe des übergebenen Objektes und ändert vom 
	//	nächsten Objekt des Typs IMG den src 
	//  Autor: MN
	//
	//	@param	obj				das Objekte von dem der Hintergrund geändert werden soll				
	//	@param	over			bei mouseover = 1, sonst 0
	//-----------------------------------------------------------------------------
	
/*function highlight(obj, over) {
	if (over == 1) {
		obj.setAttribute('bgColor', '#9299AB');
		var img = obj.getElementsByTagName('img').item(0);
		if (img != null)
			img.setAttribute('src', 'images/button_img_left2.gif');
	}
	else {
		obj.setAttribute('bgColor', '#BCBFCD');
		var img = obj.getElementsByTagName('img').item(0);
		if (img != null)
			img.setAttribute('src', 'images/button_img_left1.gif');
	}
}*/

	//-----------------------------------------------------------------------------
	//	Ändert die Hintergrundfarbe des übergebenen Objektes und ändert vom 
	//	nächsten Objekt des Typs IMG den src 
	//  Autor: MN+MK
	//
	//	@param	obj				          das Objekte von dem der Hintergrund geändert werden soll				
	//	@param	farbe_standard			standardfarbe
  //	@param	farbe_highlight			highlightfarbe
  //	@param	img_standard			  standardbild
  //	@param	img_highlight			  bild bei highlight
	//-----------------------------------------------------------------------------

function highlight(obj,farbe_standard,farbe_highlight,img_standard,img_highlight) {
	
  var akt_farbe=obj.getAttribute('bgColor').toUpperCase();
  
  if(akt_farbe=="" || akt_farbe==farbe_standard.toUpperCase())
  {    
    obj.setAttribute('bgColor', farbe_highlight.toUpperCase());
		var img = obj.getElementsByTagName('img').item(0);
		if (img != null)
			img.setAttribute('src', img_highlight);
  }
  else
  {
    obj.setAttribute('bgColor', farbe_standard.toUpperCase());
		var img = obj.getElementsByTagName('img').item(0);
		if (img != null)
			img.setAttribute('src', img_standard);
  }
}

//-----------------------------------------------------------------------------
//	Die Funktion öffnet ein neues Fenster mit einer neuen URL
//	Das Array PopupWindow enthält die Objekte der geöffneten Fenster
//
//	Index = 0 ist das Fenster für die großen Bilder
//-----------------------------------------------------------------------------
var PopupWindow = new Array();
function openWindow(index, WindowName, URL, Param, Windowclose) {
  var newWindow = PopupWindow[index];

  if (newWindow == null)
    newWindow = "";
  else
   if (Windowclose == true) newWindow.close();

  //Überprüfen ob noch keine Instanz des Fensters vorhanden ist oder diese schon geschlossen wurde
  if(newWindow == "" || newWindow.closed){
    //Neues Fenster öffnen mit einer neuen Instanz
    newWindow = window.open(URL, WindowName, Param);
    newWindow.focus();
  }
  else {
    //Alte Instanz wieder aktivieren
    newWindow.focus();
  }

  PopupWindow[index] = newWindow;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max;
  var errors = '';
  var 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=args[i+1]; 
    	//if (val.type == 'checkbox' && !val.checked) errors += '- ' + nm + ' ist noch nicht abgehakt.\n';
    	if (val.type == 'checkbox' && !val.checked) errors += '- ' + nm + '\n';
    	else if ((val = val.value) != "") {
      	if (test.indexOf('isEmail') != -1) { 
      		p=val.indexOf('@');
        	if (p < 1 || p == (val.length-1)) errors += '- '+nm+' muss eine e-mail Addresse sein.\n';
      	} 
      	else if (test != 'R') {
        	if (isNaN(val)) errors += '- '+nm+' muss eine Zahl sein.\n';
        	if (test.indexOf('inRange') != -1) { 
        		p = test.indexOf(':');
          	min = test.substring(8,p); 
          	max = test.substring(p+1);
          	if (val < min || max < val) errors += '- ' + nm + ' muss eine Zahl zwischen ' + min + ' und ' + max + ' sein.\n';
    			} 
    		} 
    	}
    	else if (test.charAt(0) == 'R') errors += '- ' + nm + ' fehlt.\n'; 
  	}
  } 
  if (errors) alert('Folgende Fehler traten auf:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function setDisplay(objNameId, value) {
  obj = MM_findObj(objNameId);
  if (obj)
  	obj.style.display=value;
}

function setAttr(objNameId, attr, value) {
  obj = MM_findObj(objNameId);
  if (obj)
    obj.setAttribute(attr, value);
}