/*
*  (c) spirito GmbH, Duisburg 2002-2003
*  All rights reserved
*
*  http://www.spirito.de
*  This is part of the DADO Application Server Software
*
*  $Revision: 2443 $
*  $Author: holtwick $
*  $Date: 2007-01-19 12:18:02 +0100 (Fr, 19 Jan 2007) $
*
*/

/**

	Helpers for calculating size relative to screen size.

**/

function dado_CalcWidth(v)
{
	 var rx = /(\d+)(\%)/
	 var c = rx.exec(v)
	 if(c)
	 {	
	 	return Math.round(screen.width / 100.0 * c[1])
	 }
	 return v
}

function dado_CalcHeight(v)
{
	 var rx = /(\d+)(\%)/
	 var c = rx.exec(v)
	 if(c)
	 {	
	 	return Math.round(screen.height / 100.0 * c[1])
	 }
	 return v
}

function dado_CalcSize(w, h)
{
	var s = ""
	var w = dado_CalcWidth(w)
	var h = dado_CalcHeight(h)
	var dx = Math.round((screen.width - w) / 2)
	var dy = Math.round((screen.height - h) / 5)	
	
	s = s + "width=" + w
	s = s + ",height=" + h	
	s = s + ",left=" + dx
	s = s + ",top=" + dy 
	s = s + ",screenX=" + dx
	s = s + ",screenY=" + dy
	
	return s
}

/**

	Open windows by extracting the URL for the window
	out of an a-tag href-attribute and using its target
	name. Tell the width and height by a number describing
	the count of pixels or by percentage values relative
	to screen size, e.g.
	
	<a href="test.html" target="someone" onClick="return dado_OpenWin(this, '80%', 300)">
		Some Window
	</a>

**/

function dado_OpenWin(l, w, h)
{
	var sz = dado_CalcSize(w, h)
    var cWindow = window.open(l.href, l.target, sz + ",resizable=yes,scrollbars=yes")
    if (cWindow.focus != null) cWindow.focus()
    if (!cWindow.opener) cWindow.opener = self
	cWindow.opener2 = self
    return false
}

function dado_OpenWinScroll(l, w, h, s)
{
	var sz = dado_CalcSize(w, h)
    var cWindow = window.open(l.href, l.target, sz + ",resizable=yes,scrollbars=" + s)
    if (cWindow.focus != null) cWindow.focus()
    if (!cWindow.opener) cWindow.opener = self
	cWindow.opener2 = self
    return false
}

function dado_OpenFixWin(l, w, h)
{
	var sz = dado_CalcSize(w, h)
    var cWindow = window.open(l.href, l.target, sz + ",resizable=no,scrollbars=no")
    if (cWindow.focus != null) cWindow.focus()
    if (!cWindow.opener) cWindow.opener = self
	cWindow.opener2 = self
    return false
}

function dado_OpenWinNoScroll(l, w, h)
{
	var sz = dado_CalcSize(w, h)
    var cWindow = window.open(l.href, l.target, sz + ",resizable=yes,scrollbars=no")
    if (cWindow.focus != null) cWindow.focus()
    if (!cWindow.opener) cWindow.opener = self
	cWindow.opener2 = self
    return false
}

function dado_OpenWinArgs(l, w, h, a)
{
	var sz = dado_CalcSize(w, h)
    var cWindow = window.open(l.href, l.target, sz + a)
    if (cWindow.focus != null) cWindow.focus()
    if (!cWindow.opener) cWindow.opener = self
	cWindow.opener2 = self
    return false
}

function dado_OpenFormWin(l, w, h)
{
	var sz = dado_CalcSize(w, h)
    var cWindow = window.open("about:blank", "translation", sz + ",resizable=yes,scrollbars=yes")    
    if (cWindow.focus != null) cWindow.focus()	
	if (!cWindow.opener) cWindow.opener = self
    return true
}

/***************************************************
Listenfunktionen
*****************************************************/

function dado_ListSortByValue(obj)
{
	/*
	Liste nach den Werten sortieren
	
	obj - Die Liste
	*/
	
	function mysort(a, b)
	{	
		if(a.value < b.value) return -1
		if(a.value > b.value) return 1
		return 0
	}
	
	var arr = Array()
	for(var i=0; i<obj.length; i++) {
		arr[i] = obj.options[i]
	}
	arr.sort(mysort)	
	for(var i=0; i<arr.length; i++) {		
		obj.options[i] = new Option(arr[i].text, arr[i].value, false, false)
	}
}

function dado_ListSort(obj)
{
	/*
	Liste nach den sichtbaren Texten sortieren
	
	obj - Die Liste
	*/

	function mysort(a, b)
	{	
		if(a.text < b.text) return -1
		if(a.text > b.text) return 1
		return 0
	}
	
	var arr = Array()
	for(var i=0; i<obj.length; i++) {
		arr[i] = obj.options[i]
	}
	arr.sort(mysort)	
	for(var i=0; i<arr.length; i++) {		
		obj.options[i] = new Option(arr[i].text, arr[i].value, false, false)
	}
}

var dado_ListSortByText = dado_ListSort

function dado_ListDel(obj, value) 
{
	/*
	Einen Wert aus einer Liste löschen.
	*/
	for(var i=0; i<obj.length; i++) {
		if(obj.options[i].value == value) { 
			obj.options[i] = null
			return 
		}
	}
}

function dado_ListAdd(obj, text, value) 
{
	/*
	Wert zu einer Liste hinzufügen. Ein vorher schon
	bestehender Eintrag mit dem gleichen Wert wird ersetzt.
	*/
	dado_ListDel(obj, value)
	el = new Option(text, value, false, false);
 	obj[obj.length] = el;
 	dado_ListSort(obj)
}

function dado_ListHas(obj, value) 
{
	for(var i=0; i<obj.length; i++) {
		if(obj.options[i].value == value) 
			return true
	}
	return false
}

function dado_ListMatch(obj, value, defl) 
{
	/*
	Sucht einen Eintrag in der Liste, dessen Anfang des 
	sichtbaren Textes mit <value> übereinstimmt. Groß-Klein ist
	egal. Der gefundene Eintrag wird dann ausgewählt, sonst
	der an der Position <defl>.
	*/
	if(value)
	{
		for(var i=0; i<obj.length; i++) 
		{			
			if(obj.options[i].text.toLowerCase().indexOf(value.toLowerCase()) == 0) 
			{
				obj.options.selectedIndex = i
				return
			}
		}
	}
	obj.options.selectedIndex = defl
}

function dado_ListSync(obj, text, value, checked)
{
	/*
	Setzt oder löscht einen Eintrag in einer Liste je nachdem,
	ob <checked> wahr oder falsch ist.
	
	obj - Die Liste
	text - Textteil
	value - Wertteil
	checked - Gesetzt oder nicht
	*/
	if(checked)
		dado_ListAdd(obj, text, value)
	else
		dado_ListDel(obj, value)	
}

function dado_CheckboxAll(form, obj, name)
{        
	/*
	Setzt alle Checkboxen oder umgekehrt
	
	form - Das Formular
	obj - Vorgabecheckbox
	name - Name der Checkboxen
	*/
	for (var i=0; i<form.elements.length; i++)
	{
		var el = form.elements[i];
		if(el.name == name) el.checked = obj.checked;
    }
}

/*
function dado_ListObject(obj)
{
	this.listObj = obj
	this.Sort = function _sort() { return dado_Sort(this.listObj) }
}
*/


// Wartet millis Millisekunden
function pause(millis)
{
    var date = new Date();
    var curDate = null;
    do {
        curDate = new Date(); 
    } while(curDate-date < millis);
} 