// Javascriptfunktionen fuer Dokumentenverwaltung 
// 
// (c) 2004 by Sven Stefani, ss@stefani.biz
//
//


function areyousure(msg)
{
    if (msg == "") msg="Sind Sie sicher?";
    return confirm(msg);
}



function showlayer(layername, show)
{
	if(!document.getElementById) return;
	
	var obj = document.getElementById(layername);
	if(!obj || !obj.style) return alert('Fehler!' + layername + ' existiert nicht.');
	
	obj.style.visibility = show ? "visible" : "hidden";
} 


function login_checkselection(layername,cnt){
	if (document.flogin.f_other1.options[document.flogin.f_other1.selectedIndex].value.substr(0,1) == "-") {
		for (var i=1; i<(cnt+1); i++)
		{
			showlayer(layername+i, true); 
			showlayer(layername+i, true);
		}
	}
	else
	{
		for (i=1; i<(cnt+1); i++)
		{
			showlayer(layername+i, false); 
			showlayer(layername+i, false);
		}
	}
	
}


// removes single newlines \r, \n, or \r\n
function removesinglenewline (oldtext)
{
	// replace \n\r with #~-n-~#
	oldtext = oldtext.replace(/\r\n/g, "#~-n-~#");	

	// replace \r\n with #~-n-~#
	oldtext = oldtext.replace(/\n\r/g, "#~-n-~#");	

	// replace \r with #~-n-~#
	oldtext = oldtext.replace(/\r/g, "#~-n-~#");	

	// replace \n with #~-n-~#
	oldtext = oldtext.replace(/\n/g, "#~-n-~#");	

	// replace #~-r-~#{2,} with \n
	oldtext = oldtext.replace(/(#~-n-~#){2,}/g, "\n\n");	

	// remove single #~-n-~#
	oldtext = oldtext.replace(/(#~-n-~#)/g, " ");	

	return oldtext;
}



// the function inserttag (original name was insert) was programmed by Torsten Anacker (torsten -at- anaboe.net)
// http://aktuell.de.selfhtml.org/tippstricks/javascript/bbcode/
// modified by Sven Stefani, 16.03.2005
function inserttag(myform, aTag, eTag) {
	var input = myform;
	input.focus();
	/* für Internet Explorer */
	if(typeof document.selection != 'undefined') {
		/* Einfügen des Formatierungscodes */
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = aTag + insText + eTag;
		/* Anpassen der Cursorposition */
		range = document.selection.createRange();
		if (insText.length == 0) {
			range.move('character', -eTag.length);
		} else {
			range.moveStart('character', aTag.length + insText.length + eTag.length);      
		}
		range.select();
	}
	/* für neuere auf Gecko basierende Browser */
	else if(typeof input.selectionStart != 'undefined')
	{
		/* Einfügen des Formatierungscodes */
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
		/* Anpassen der Cursorposition */
		var pos;
		if (insText.length == 0) {
			pos = start + aTag.length;
		} else {
			pos = start + aTag.length + insText.length + eTag.length;
		}
		input.selectionStart = pos;
		input.selectionEnd = pos;
	}
	/* für die übrigen Browser */
	else
	{
		/* Abfrage der Einfügeposition */
		var pos;
		var re = new RegExp('^[0-9]{0,3}$');
		while(!re.test(pos)) {
			pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
		}
		if(pos > input.value.length) {
			pos = input.value.length;
		}
		/* Einfügen des Formatierungscodes */
		var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
		input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
	}
}

/*
 * When writing generic JavaScript functions (which we do as much as possible), you need to account 
 * for developers of all levels of experience using your generic function. This means preventing errors 
 * from happening if the developer sends in something unexpected as a parameter.
 * 
 * The most recent example of this is working with an array as a parameter. That led us to wonder how to 
 * check a variable to know if it's an array in JavaScript. There's no built-in "isArray" function like 
 * there is with other languages. And, to make matters worse, strings are really arrays of characters.
 * So checking value[0] can give you a valid value even if value is a string instead of an array.
 * 
 * The key to checking to see if a variable is an array is checking to see how the variable was initially 
 * created. This is done with the constructor property of the variable. This returns the internal function
 * used to create the variable. Check it out sometime using the statement alert(myVariable.constructor).
 * Using this knowledge, here is our isArray function:
 * 
 * http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C720080D723
 */
function isArray(obj) {
	   if (obj.constructor.toString().indexOf("Array") == -1)
	      return false;
	   else
	      return true;
}


// cbselect(cb_name, typ)
// created by Sven Stefani, 11.04.2005
// checks, unchecks, or inverts value of checkboxes
// cb_name is the name of checkbox elements (name-attribute)
// typ is what to do: 0 deselect, 1 select, 2 invert value
function cbselect(cb_name, typ)
{
	// get all elements where name-attribute is same as cb_name
	// a_cb is an array of all elements, even if there is onle one element
	a_cb = document.getElementsByName(cb_name);
	
	if (a_cb.length == 0) alert ( 'Keine Elemente mit Attribut  name="'+cb_name+'"  gefunden');
	
	// loop over all elements
	for (var i = 0; i < a_cb.length; i++)
	{
		switch (typ)
		{
			case false:
			case 0:
			case "0":
				a_cb[i].checked = false;
			break;

			case true:
			case 1:
			case "1":
				a_cb[i].checked = true;
			break;

			case "2":
			case 2:
			default:
				a_cb[i].checked = !a_cb[i].checked;
			break;
		}
	}
}


//cbselect(cb_name, typ)
//created by Sven Stefani, 11.04.2005
//checks, unchecks, or inverts value of checkboxes
//cb_ids is an array with ids of checkbox elements (id-attribute)
//typ is what to do: 0 deselect, 1 select, 2 invert value
function cbselectids(cb_ids, typ)
{

	if (isArray(cb_ids))
	{
		// loop over all elements
		for (var i = 0; i < cb_ids.length; i++)
		{
		
    		cbelement = document.getElementById(cb_ids[i]);
    
        if (cbelement != null)
        {
    			switch (typ)
    			{
    				case false:
    				case 0:
    				case "0":
    					cbelement.checked = false;
    				break;
    	
    				case true:
    				case 1:
    				case "1":
    					cbelement.checked = true;
    				break;
    	
    				case 2:
    				case "2":
    				default:
          		alert("x:"+document.getElementById(cb_ids[i]).id);
    					cbelement.checked = !cbelement.checked;
    				break;
    			}
    		}	
		}
	}
	else
	{
		alert("cb_ids is not an array! " + cb_ids );
	}
	
}

/************************************************************************************************************
	(C) www.dhtmlgoodies.com, November 2005
	
	modified by Sven Stefani 12/2007
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/
		
	var tableWidget_okToSort = true;
	var tableWidget_arraySort = new Array();
	tableWidget_tableCounter = 1;
	var activeColumn = new Array();
	var currentColumn = false;
	
	function sortNumeric(a,b){
		
		a = a.replace(/,/,'.');
		b = b.replace(/,/,'.');
		a = a.replace(/[^\d\.\/]/g,'');
		b = b.replace(/[^\d\.\/]/g,'');
		if(a.indexOf('/')>=0)a = eval(a);
		if(b.indexOf('/')>=0)b = eval(b);
		return a/1 - b/1;
	}
	
	function sortString(a, b) {

	  if ( a.toUpperCase() < b.toUpperCase() ) return -1;
	  if ( a.toUpperCase() > b.toUpperCase() ) return 1;
	  return 0;
	}
		
	function sortInfo(show)
	{
	    // kurz warten
		var mydiv = document.getElementById('sortInfo');
		if (show)
		{
			mydiv.style.visibility = "visible";
		}		
	   	else
	   	{
			mydiv.style.visibility = "hidden";	
		}
	}	
		
	function wrapperSortInfo(show)
	{
		mythis = this;
		sortInfo(1);
		window.setTimeout("sortTable(mythis)", 50);
	}

	function sortTable(mythis)
	{
		if(!tableWidget_okToSort)return;
		tableWidget_okToSort = false;
		/* Getting index of current column */
		var obj = mythis;
		var indexThis = 0;
		while(obj.previousSibling){
			obj = obj.previousSibling;
			if(obj.tagName=='TD')indexThis++;		
		}
		
		if(mythis.getAttribute('direction') || mythis.direction){
			direction = mythis.getAttribute('direction');
			if(navigator.userAgent.indexOf('Opera')>=0)direction = mythis.direction;
			if(direction=='ascending'){
				direction = 'descending';
				mythis.setAttribute('direction','descending');
				mythis.direction = 'descending';	
			}else{
				direction = 'ascending';
				mythis.setAttribute('direction','ascending');		
				mythis.direction = 'ascending';		
			}
		}else{
			direction = 'ascending';
			mythis.setAttribute('direction','ascending');
			mythis.direction = 'ascending';
		}
		
		var tableObj = mythis.parentNode.parentNode.parentNode;
		var tBody = tableObj.getElementsByTagName('TBODY')[0];
		
		var widgetIndex = tableObj.getAttribute('tableIndex');
		if(!widgetIndex)widgetIndex = tableObj.tableIndex;
		
		if(currentColumn)currentColumn.className='';
		document.getElementById('col' + widgetIndex + '_' + (indexThis+1)).className='highlightedColumn';
		currentColumn = document.getElementById('col' + widgetIndex + '_' + (indexThis+1));

			
		var sortMethod = tableWidget_arraySort[widgetIndex][indexThis]; // N = numeric, S = String
		if(activeColumn[widgetIndex] && activeColumn[widgetIndex]!=mythis){
			if(activeColumn[widgetIndex])activeColumn[widgetIndex].removeAttribute('direction');			
		}

		activeColumn[widgetIndex] = mythis;
		
		var cellArray = new Array();
		var cellObjArray = new Array();
		for(var no=1;no<tableObj.rows.length;no++){
			var content= tableObj.rows[no].cells[indexThis].innerHTML+'';
			cellArray.push(content);
			cellObjArray.push(tableObj.rows[no].cells[indexThis]);
		}
		
		if(sortMethod=='N'){
			cellArray = cellArray.sort(sortNumeric);
		}else{
			cellArray = cellArray.sort(sortString);
		}
		
		if(direction=='descending'){
			for(var no=cellArray.length;no>=0;no--){
				for(var no2=0;no2<cellObjArray.length;no2++){
					if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
						cellObjArray[no2].setAttribute('allreadySorted','1');	
						tBody.appendChild(cellObjArray[no2].parentNode);				
					}				
				}			
			}
		}else{
			for(var no=0;no<cellArray.length;no++){
				for(var no2=0;no2<cellObjArray.length;no2++){
					if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
						cellObjArray[no2].setAttribute('allreadySorted','1');	
						tBody.appendChild(cellObjArray[no2].parentNode);				
					}				
				}			
			}				
		}
		
		for(var no2=0;no2<cellObjArray.length;no2++){
			cellObjArray[no2].removeAttribute('allreadySorted');		
		}

		tableWidget_okToSort = true;
		sortInfo(0);
	}
	function initSortTable(objId,sortArray)
	{
		var obj = document.getElementById(objId);
		obj.setAttribute('tableIndex',tableWidget_tableCounter);
		obj.tableIndex = tableWidget_tableCounter;
		tableWidget_arraySort[tableWidget_tableCounter] = sortArray;
		var tHead = obj.getElementsByTagName('THEAD')[0];
		var cells = tHead.getElementsByTagName('TD');
		for(var no=0;no<cells.length;no++){
			if(sortArray[no]){
				cells[no].onclick = wrapperSortInfo;	
				cells[no].style.cursor = 'pointer';	
			}else{
				cells[no].style.cursor = 'default';	
			}
		}		
		for(var no2=0;no2<sortArray.length;no2++){	/* Right align numeric cells */
			if(sortArray[no2] && sortArray[no2]=='N')obj.rows[0].cells[no2].style.textAlign='right';
		}		
		
		tableWidget_tableCounter++;
	}
		
	
