var Util={};
Util.logHttpStatus = function(responseGot)
{
	if(responseGot != null)
		Util.log("Http Status:" + responseGot.status + " - " + responseGot.statusText);
	else
		Util.log("Response Object is null");
}
Util.log = function(logGot)
{
	strLog=Util.curTimeStamp() + ' [' + window.document.title + '] ' + logGot;
	if (window.console) console.log(strLog); // firebug, safari
	//else if (window.opera) opera.postError(strLog);
	//else if (window.widget) window.alert(strLog); // dashboard
	/*else if (window.IEconsole)
	{
		window.IEconsole.innerHTML+='<div>' + strLog + '</div>' + '<div class="hr"></div>'; // SelfMadeLogger
		window.IEconsole.scrollTop=window.IEconsole.scrollHeight;
	}*/
	//else if(gadget.parent.console) gadget.parent.console.log(strLog);
	//else window.alert(strLog); // IE
}
Util.gel = function(idGot)
{
	return document.getElementById(idGot);
}
Util.toggleScrollbars = function(objectGot)
{
	if(typeof objectGot != 'undefined')
	{
		if(objectGot.offsetHeight<objectGot.scrollHeight)
			objectGot.style.overflowY = 'scroll';
		else
			objectGot.style.overflowY = 'hidden';
		if(objectGot.offsetWidth<objectGot.scrollWidth)
			objectGot.style.overflowX = 'scroll';
		else
			objectGot.style.overflowX = 'hidden';
	}
}
Util.gelClass = function(classNameGot)
{
	var all_obj,ret_obj=new Array(),j=0;
	if(document.all)all_obj=document.all;
	else if(document.getElementsByTagName && !document.all)all_obj=document.getElementsByTagName("*");
	for(var ao=0;ao<all_obj.length;ao++)
	{
		var classes = all_obj[ao].className.split(' ');
		if(classes.length>1)
		{
			for(var i2=0;i2<classes.length;i2++)
			{
				if(classes[i2]==classNameGot)
				{
					ret_obj[j]=all_obj[ao];
					j++
				}
			}
		}
		else
		{
			if(classes[0]==classNameGot)
			{
				ret_obj[j]=all_obj[ao];
				j++
			}
		}
	}
	return ret_obj;
}
Util.getAllPropertiesAsString = function(objectGot,nameGot)
{
	var strProperties=nameGot+'<br />';
	for(property in objectGot)
		strProperties += '<strong>' + property + ':<\/strong> ' + objectGot[property] + '<br />';
	return strProperties;
}
Util.getVars = function()
{
	for(i=0;i<window.prefs;i++)
	{
		gadVar=window.prefs[i];
		Util.log(gadVar.value);
		Util.log(gadVar.name);
	}
}

Util.capitalize = function(strGot) {
	if(typeof strGot != 'undefined' && strGot!='')
	{
		var newVal = '';
		var arWords = strGot.split(' ');
		for(var c=0; c < arWords.length; c++)
		{
			if(c>0)
				 newVal+= ' '
			newVal += arWords[c].substring(0,1).toUpperCase() + arWords[c].substring(1,arWords[c].length);
		}
		return newVal;
	}
	else
		return null;
}
Util.createTag = function(nameGot,attributesGot,contentGot,parentObjGot)
{
	if(nameGot=='a')
		attributesGot+=',onfocus=this.blur()';
	var tmpTag;
	if (parentObjGot != null && parentObjGot.nodeType==9)
	{
		//Util.log('test'+typeof parentObjGot);
		tmpTag = parentObjGot.createElement(nameGot);
	}
	else
		tmpTag = document.createElement(nameGot);
	if(attributesGot!=null && attributesGot!='')
	{
		//TODO JK: was ist wenn ein ',' im atribut vorkommt? - Naja...
		var arAttributes = attributesGot.split(',');
		for(var i3=0;i3<arAttributes.length;i3++)
		{
			var arValues=arAttributes[i3].split('=');
			if(arValues[0]=='type' && arValues[1]=='button' )
			{
				//tmpTag.type=arValues[1];
			}
			else
			{
				var tmp;
				if (parentObjGot != null && parentObjGot.nodeType==9)
				{
					tmp = parentObjGot.createAttribute(arValues[0]);
				}
				else
					tmp = document.createAttribute(arValues[0]);
				tmp.nodeValue=arValues[1];
				tmpTag.setAttributeNode(tmp);
			}
		}
	}
	if(contentGot!=null && contentGot!='')
	{
		var tmpContent;
		if (parentObjGot != null && parentObjGot.nodeType==9)
		{
			//Util.log('test'+typeof parentObjGot);
			tmpContent = parentObjGot.createTextNode(contentGot);
		}
		else
		{
			tmpContent = document.createTextNode(contentGot);
		}
		//tmpContent.innerHTML=contentGot;
		tmpTag.appendChild(tmpContent);
		//tmpTag.innerHTML=contentGot;
	}
	return tmpTag;
}
Util.createTagNS = function(nameGot,attributesGot,contentGot,parentObjGot)
{
	if(typeof document.createElementNS !='undefined')
	{
		if(nameGot=='a')
			attributesGot+=',onfocus=this.blur()';
		var tmpTag = document.createElementNS(null,nameGot);
		if(attributesGot!=null && attributesGot!='')
		{
			var arAttributes = attributesGot.split(',');
			for(var i3=0;i3<arAttributes.length;i3++)
			{
				var arValues=arAttributes[i3].split('=');
				var tmp = document.createAttribute(arValues[0]);
				tmp.nodeValue=arValues[1];
				tmpTag.setAttributeNode(tmp);
			}
		}
		if(contentGot!=null && contentGot!='')
		{
			var tmpContent = document.createTextNode(contentGot);
			tmpTag.appendChild(tmpContent);
		}
		return tmpTag;
	}
	else
	{
		return Util.createTag(nameGot,attributesGot,contentGot,parentObjGot);
	}
}
Util.getTargetNodeValue = function(targetGot)
{
	if(typeof targetGot.textContent!='undefined')
		return targetGot.textContent;
	else
		return targetGot.text;
};
Util.getTargetAttributeValue = function(targetGot,strPropertyNameGot)
{
	var value = '';
	if(targetGot != null && targetGot.attributes[strPropertyNameGot] != null)
		value = targetGot.attributes[strPropertyNameGot].nodeValue;
	return value;
};
Util.findGadget = function(nameGot)
{
	var gadget;
	try
	{
		if(nameGot==parent.gadget.name)//parent.document.getElementById('gadgetname').content)
		{
			gadget = parent.gadget;
		}
	}
	catch(e)
	{
		var curgadget;
		for(var i = 0 ; i < parent.length ;i++)
		{
			curgadget = parent[i];
			/*try
			{
				var pathname=curgadget.location.pathname.split('/');
				var filenamewithex=pathname[pathname.length-1].split('.');
				var filename=filenamewithex[0];
				if(nameGot == filename)
				{
					gadget = curgadget.gadget;
					break;
				}
			}*/
			try
			{
				if(nameGot==curgadget.gadget.name)//document.getElementById('gadgetname').content)
				{
					gadget = curgadget.gadget;
					break;
				}
			}
			catch(e)//WICHTIG falls auf der Seite Gadgets einer anderen Domain sind!
			{
				//Util.log(i+1 + '. Gadget: Zugriff verweigert');
			}
		}
	}
	if(gadget!=null)
		return gadget;
	else
	{
		//throw('Gadget "' + nameGot + '" NOT found!');
		return null;
	}
}

Util.logAllFrames = function()
{
	var temp;
	//parent[0].prefs[0]="Erste Variable";
	for(var i = 0 ; i < parent.length ;i++)
	{
		temp = parent[i];
		try
		{
			var tmpfile=temp.location.pathname.split('/');
			var file=tmpfile[tmpfile.length-1];
			Util.log(i+1 + '. Gadget: ' + temp.document.title + '\n' + temp.document.location.href);
		}
		catch(e)
		{
			Util.log(i+1 + '. Gadget: Zugriff verweigert');
		}
	}
}
Util.curTimeStamp = function()
{
	var Datum = new (Date);
	var Stunde = Datum.getHours();
	var Minute = Datum.getMinutes();
	var Sekunde = Datum.getSeconds();
	if(Sekunde < 10)
	    Sekunde = "0" + Sekunde;
	if(Minute < 10)
	    Minute = "0" + Minute;
	if(Stunde < 10)
		Stunde = "0" + Stunde;
	return Stunde+":"+Minute+":"+Sekunde;
}
Util.serializeXML = function(xmlDocumentGot)
{
	var strXML = '';
	if(xmlDocumentGot.xml)
	{
		strXML = xmlDocumentGot.xml;
	}
	else if(typeof XMLSerializer != 'undefined')
	{
		var s = new XMLSerializer();
		strXML = s.serializeToString(xmlDocumentGot);
	}
	else if(xmlDocumentGot.outerHTML!=null && xmlDocumentGot.outerHTML!='')
		strXML = xmlDocumentGot.outerHTML;
	return strXML;
};

Util.createQueryString = function(strClassNameGot,arFieldsGot,arAgregationGot,strOrderByFieldGot,bAscGot,strQueryGot)
{
	var strQuery;
	if(strQueryGot === null || strQueryGot === '')
		{strQuery = "select o from " + strClassNameGot + " o" ;}
	else
		{strQuery = strQueryGot ;}
	if(arFieldsGot.length == arFieldsGot.length && arFieldsGot.length > 0)
	{
		var bIncludesWhere = false;
		if(strQuery.indexOf("where")==-1)
			{strQuery += " where ";}
		else
			{bIncludesWhere = true;}
		for(var i = 0; i < arFieldsGot.length;i++ )
		{
			var fieldMap = arFieldsGot[i];
			var strFieldName =fieldMap.name;
			var strFieldValue = fieldMap.value;

			if(i > 0 || bIncludesWhere===true)
				{strQuery += " and ";}
			strQuery += "o." + strFieldName + " like \'" + "%25" + strFieldValue + "%25" + "\'";
			//srtQuery=escape(strQuery);
		}/*
		if(strOrderByFieldGot != null && strOrderByFieldGot != "")
		{
			strQuery += ' order by o.' + strOrderByFieldGot ;
			if(bAscGot)
				strQuery += ' asc';
			else
			 strQuery += ' desc';
		}*/
	}
	return strQuery;
};

Util.OrderComparator = function(elA,elB)
{
	var elGUIA = elA.getElementsByTagName('gui')[0];
	var orderA = Number(elGUIA.getAttribute('order'));
	var elGUIB = elB.getElementsByTagName('gui')[0];
	var orderB = Number(elGUIB.getAttribute('order'));

	if(isNaN(orderA))
		{orderA = 0;}
	if(isNaN(orderB))
		{orderB = 0;}
	return orderA - orderB;
};
Util.calculateOffsetWidth=function(objectGot)
{
	if (objectGot.style.display != 'none') {
		var marginLeft = $(objectGot).getStyle('margin-left');
		if (marginLeft == null)
			marginLeft = 0;
		var marginRight = $(objectGot).getStyle('margin-right');
		if (marginRight == null)
			marginRight = 0;
		var paddingLeft = $(objectGot).getStyle('padding-left');
		if (paddingLeft == null)
			paddingLeft = 0;
		var paddingRight = $(objectGot).getStyle('padding-right');
		if (paddingRight == null)
			paddingRight = 0;
		var position = $(objectGot).getStyle('left');
		if (position == null)
			position = 0;
		var value = parseInt(marginLeft) + parseInt(marginRight) + parseInt(paddingLeft) + parseInt(paddingRight) + parseInt(position);
		return document.body.clientWidth - value;
	}
	else
		return null;
}
Util.calculateOffsetHeight=function(objectGot)
{
	var padding=parseInt(objectGot.getStyle('padding-top'))+parseInt(objectGot.getStyle('padding-bottom'));
	var margin=parseInt(objectGot.getStyle('margin-bottom'))+parseInt(objectGot.getStyle('margin-top'));
	var position=parseInt(objectGot.getStyle('top'))+parseInt(objectGot.getStyle('bottom'));
	return document.body.clientWidth-margin-padding-position;
}
Util.request = function(urlGot,parameterGot)
{
	if (typeof(UWA) != "undefined" )
		{UWA.Data.request(urlGot,parameterGot);}
	else if(typeof(Ajax) != "undefined" && typeof(Ajax.Request) != "undefined") // if Prototype framework
	{
		var contentType = '';
		if(typeof parameterGot.contentType != 'undefined')
		{
			contentType = parameterGot.contentType
		}
		else
		{
			contentType = 'text/xml';
		}
		if(typeof(parameterGot.method) == 'undefined')
			parameterGot.method = 'post';
		if(typeof(parameterGot.onError) == 'undefined')
			parameterGot.onError=parameterGot.onComplete;
		/*new Ajax.Request
		(
			urlGot,
			{
				method: "post",
				onComplete: parameterGot.onComplete,
				onError: parameterGot.onComplete,
				postBody: parameterGot.postBody,
				requestHeaders:{'Content-Type':contentType},
				contentType: contentType
			}
		);*/
		new Ajax.Request
		(
			urlGot,
			{
				method: parameterGot.method,
				onComplete: parameterGot.onComplete,
				onError: parameterGot.onError,
				postBody: parameterGot.postBody,
				requestHeaders:parameterGot.requestHeaders,
				contentType: contentType
			}
		);
	}
	else
	{
		//TODO: response is not bind to the callback method (error)
		var request = null;
		if (window.XMLHttpRequest)//if IE7, Mozilla, Safari, etc: Use native object
			{request = new XMLHttpRequest();}
		else if (window.ActiveXObject)//otherwise, use the ActiveX control for IE5.x and IE6
		{
			try
			{
				request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) { }
			if (request === null)
			{
				try
				{
					request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) { }
			}
		}
		request.open("POST", urlGot, (parameterGot.onComplete !== null));
		if(parameterGot.action !== null)
			{request.setRequestHeader("SOAPAction", parameterGot.action);}
		request.setRequestHeader("Content-Type", "text/xml");
		//alert(soap);
		request.onreadystatechange = parameterGot.onComplete;

		request.send(parameterGot.postBody);
	}
};

Util.getXML = function(urlGot,callbackGot,parameterGot)
{
	Util.request(
	urlGot,
	{
		'method': 'post',
		'type': 'xml',
		'onComplete': callbackGot,
		'parameters': parameterGot
	});
};

Util.newXMLDocument = function(xmlChildGot){
	var xmlDoc = null;
	if (document.implementation && document.implementation.createDocument)
	{
		if (typeof(xmlChildGot) == 'string')
		{
			xmlChildGot = Util.createTagNS(xmlChildGot);
		}
		return xmlChildGot;//xmlDoc=document.implementation.createDocument('','',null);//Gecko/Mozilla...
	}
	else if(typeof ActiveXObject != 'undefined')
	{
		var msXmlAx=null;
		try
		{
			msXmlAx=new ActiveXObject('Msxml2.DOMDocument');//Newer IE
		}
		catch (e)
		{
			msXmlAx=new ActiveXObject('Msxml.DOMDocument');//Older IE
		}
		xmlDoc=msXmlAx;
		if (typeof(xmlChildGot) == 'string')
		{
			xmlChildGot = xmlDoc.createElement(xmlChildGot);//Util.createTagNS(xmlChildGot);
		}
		xmlDoc.appendChild(xmlChildGot);
		return xmlDoc;
	}

}

Util.callWebService = function(urlGot,callbackGot,postBodyGot,offsetGot,limitGot, sortFieldGot, wsReturnGot, wsReturnParameterGot)
{
	if(typeof offsetGot == 'undefined')offsetGot=0;
	if(typeof limitGot == 'undefined')limitGot=15;
	if(typeof sortFieldGot == 'undefined')sortFieldGot=0;
		//'requestHeaders':{'Content-Type':'text/xml','Authorization':'Basic admin:admin'},
	Util.request(
	urlGot,
	{
		'method': 'post',
		'type': 'xml',
		'onComplete': callbackGot,
		'postBody': postBodyGot,
		//'requestHeaders' : {'Content-Type':'text/xml;charset=ISO-8859-1','offset':offsetGot,'limit':limitGot,'sortfields':sortFieldGot,'ws-return':wsReturnGot,'ws-return-parameter':wsReturnParameterGot},
		'requestHeaders' : {'offset':offsetGot,'limit':limitGot,'sortfields':sortFieldGot,'ws-return':wsReturnGot,'ws-return-parameter':wsReturnParameterGot},
		'contentType': 'text/xml'
	});
};

/**
 *
 * @param {String} urlGot the url to the ressource
 * @param {Element} elementGot - the element that is to update
 * @param {Function} callbackGot - the callback that is to call after all with the elementGot as paramenter
 */
Util.updateElementByURL = function(urlGot,elementGot,callbackGot)
{
	var temp = function(responseGot)
	{
		this.appendChild(responseGot.responseXML);
		callbackGot(this);
	};

	Util.request(
	urlGot,
	{
		'method': 'get',
		'type': 'xml',
		'onComplete': temp.bind(elementGot)
	});
};

Util.getFeed = function(urlGot,callbackGot)
{
	Util.request(
	urlGot,
	{
		'method': 'post',
		'type': 'xml',
		'proxy': 'feed',
		'onComplete': callbackGot,
		'parameters': parameterGot
	});
};

Util.alert = function(messageKeyGot)
{
	gadget.coverAll();
	if(typeof gadget.language[messageKeyGot]!='undefined')
		alert(gadget.language[messageKeyGot]);
	else
	{
		Util.log('Keine Übersetzung für "'+messageKeyGot+'" im Wörterbuch "'+ gadget.language.languageName +'" vorhanden.')
		alert(messageKeyGot);
	}
		gadget.uncoverAll();
};
Util.confirm = function(messageKeyGot)
{
		gadget.coverAll();
		var answer;
	if (typeof gadget.language[messageKeyGot] != 'undefined')
	{
		answer= confirm(gadget.language[messageKeyGot]);
	}
	else
	{
		Util.log('Keine Übersetzung für "' + messageKeyGot + '" im Wörterbuch "' + gadget.language.languageName + '" vorhanden.')
		answer= confirm(messageKeyGot);
	}
		gadget.uncoverAll();
		return answer;
};

Util.setVarsByCookie = function ()
{
	var allCookies=document.cookie;
	var cookieArr=allCookies.split("; ");
	var cookie='';
	for(var i=0;i<cookieArr.length;i++)
	{
		cookie=cookieArr[i].split("=");
		//Util.log('Found Cookie '+cookie[0] + ' with Value ' + cookie[1]);
		gadget.setValue(cookie[0],unescape(cookie[1]),false);
	}
};
Util.getCookieValue = function (cookieNameGot)
{
	var allCookies=document.cookie;
	var cookieArr=allCookies.split("; ");
	var cookie='';
	for(var i=0;i<cookieArr.length;i++)
	{
		cookie=cookieArr[i].split("=");
		if(cookie[0] == cookieNameGot)
		{
			cookieValue=unescape(cookie[1]);
			Util.log('getCookie '+cookieNameGot + ' with Value ' + cookieValue);
			return cookieValue;
		}
	}
	return false;
};

Util.setCookieValue = function (nameGot, valueGot, expDaysGot)
{
	var expDays = 1000 * 60 * 60 * 24 * expDaysGot;
	var now = new Date();
	var expiration = new Date(now.getTime() + expDays);
	document.cookie = nameGot + "=" + valueGot + ";expires=" + expiration.toGMTString() + ";";
	Util.log('setCookie '+nameGot + ' to ' + valueGot);
}
Util.translateElement = function(gadgetGot,strKeyGot)
{
	var value = strKeyGot;
	if(gadgetGot==null)
		gadgetGot=gadget;
	var curLanguage=gadgetGot.getLanguage();
	if (typeof(curLanguage[strKeyGot]) != 'undefined')
	{
		value = curLanguage[strKeyGot];
	}
	else
		Util.log('no entry found in dict '+ curLanguage.languageName +' for ' + strKeyGot);
	return value;
}

Util.translateGadget = function(gadgetGot, elementGot)
{
	var languageGot = gadgetGot.getLanguage();
	//Util.log('Translate Gadget! ' + gadgetGot.name + ' ' +elementGot + ' ' + languageGot.languageName);
	if(elementGot == null)
		elementGot = gadgetGot.document.body;
	if (typeof(elementGot) != 'string')
	{
		if (typeof(languageGot) != 'undefined' && languageGot!=null)
		{
			var elmnts = document.getElementsByClassName('translate', elementGot);
			var curElement = null;
			for (var i = 0; i < elmnts.length; i++)
			{
				curElement = elmnts[i];
				if (typeof curElement.name != 'undefined')
				{
					if (typeof languageGot[curElement.name] != 'undefined')
					{
						if (typeof curElement.value != 'undefined')
							curElement.value = languageGot[curElement.name];
						else
							if (typeof curElement.firstChild.nodeValue != 'undefined')
								curElement.firstChild.nodeValue = languageGot[curElement.name];
					}
					else
						Util.log('Keine Übersetzung für "' + curElement.name + '" im Wörterbuch "' + languageGot.languageName + '" vorhanden.');
				}
				else
				{
					var nameAttribute = null;
					for (var n = 0; n < curElement.attributes.length; n++)
					{
						if (curElement.attributes[n].nodeName == 'name')
						{
							nameAttribute = curElement.attributes[n];
						}
					}
					if (nameAttribute != null)
					{
						if (typeof languageGot[nameAttribute.nodeValue] != 'undefined')
						{
							if (typeof curElement.value != 'undefined')
								curElement.value = languageGot[nameAttribute.nodeValue];
							else
								if (typeof curElement.firstChild.nodeValue != 'undefined')
									curElement.firstChild.nodeValue = languageGot[nameAttribute.nodeValue];
						}
						else
						{
							Util.log('Keine Übersetzung für "' + nameAttribute.nodeValue + '" im Wörterbuch "' + languageGot.languageName + '" vorhanden.');
						//window.console.log('gadget.languages.de.'+curElement.name);
						}
					}
					else
						Util.log('Kein Attribut "Name" gefunden!');
				}
			}
			var labelElements = document.getElementsByTagName('label',elementGot);
			for (var i = 0; i < labelElements.length; i++)
			{
				curElement = labelElements[i];
				if (typeof languageGot[curElement.htmlFor] != 'undefined')
				{
					if (typeof curElement.firstChild.nodeValue != 'undefined')
						curElement.firstChild.nodeValue = languageGot[curElement.htmlFor];
				}
				else
				{
					Util.log('Keine Übersetzung für "' + curElement.htmlFor + '" im Wörterbuch "' + languageGot.languageName + '" vorhanden.')
				//window.console.log('gadget.languages.de.'+curElement.htmlFor);
				}
			}
			var buttonElements = document.getElementsByTagName('button',elementGot);
			for (var i = 0; i < buttonElements.length; i++)
			{
				curElement = buttonElements[i];
				if (typeof languageGot[curElement.name] != 'undefined')
				{
					if (typeof curElement.firstChild.nodeValue != 'undefined')
						curElement.firstChild.nodeValue = languageGot[curElement.name];
				}
				else
				{
					Util.log('Keine Übersetzung für "' + curElement.name + '" im Wörterbuch "' + languageGot.languageName + '" vorhanden.')
				//window.console.log('gadget.languages.de.'+curElement.name);
				}
			}
		}
	}
}

/*Util.translate = function(languageGot)
{
	Util.log('Translate all!');
	if(typeof languageGot=='undefined')
		languageGot=gadget.getLanguage();
	else if(typeof languageGot=='string')
		languageGot=gadget.languages[languageGot];
	if(typeof languageGot!='undefined')
	{
		var elmnts = Util.gelClass('translate');
		var curElement=null;
		for(var i=0;i<elmnts.length;i++)
		{
			curElement=elmnts[i];
			if(typeof curElement.name!='undefined')
			{
				if(typeof languageGot[curElement.name] != 'undefined')
				{
					if(typeof curElement.value != 'undefined')
						curElement.value=languageGot[curElement.name];
					else if(typeof curElement.firstChild.nodeValue != 'undefined')
						curElement.firstChild.nodeValue=languageGot[curElement.name];
				}
				else
					Util.log('Keine Übersetzung für "'+curElement.name+'" im Wörterbuch "'+ languageGot.languageName +'" vorhanden.');
			}
			else
			{
				var nameAttribute=null;
				for(var n=0;n<curElement.attributes.length;n++)
				{
					if(curElement.attributes[n].nodeName=='name')
					{
						nameAttribute=curElement.attributes[n];
					}
				}
				if(nameAttribute!=null)
				{
					if(typeof languageGot[nameAttribute.nodeValue]!='undefined')
					{
						if(typeof curElement.value != 'undefined')
							curElement.value=languageGot[nameAttribute.nodeValue];
						else if(typeof curElement.firstChild.nodeValue != 'undefined')
							curElement.firstChild.nodeValue=languageGot[nameAttribute.nodeValue];
					}
					else
					{
						Util.log('Keine Übersetzung für "'+nameAttribute.nodeValue+'" im Wörterbuch "'+ languageGot.languageName +'" vorhanden.');
						//window.console.log('gadget.languages.de.'+curElement.name);
					}
				}
				else
					Util.log('Kein Attribut "Name" gefunden!');
			}
		}
		var labelElements = document.getElementsByTagName('label');
		for(var i=0;i<labelElements.length;i++)
		{
			curElement=labelElements[i];
			if(typeof languageGot[curElement.htmlFor] != 'undefined')
			{
				if(typeof curElement.firstChild.nodeValue != 'undefined')
					curElement.firstChild.nodeValue=languageGot[curElement.htmlFor];
			}
			else
			{
				Util.log('Keine Übersetzung für "'+curElement.htmlFor+'" im Wörterbuch "'+ languageGot.languageName +'" vorhanden.')
				//window.console.log('gadget.languages.de.'+curElement.htmlFor);
			}
		}
		var buttonElements = document.getElementsByTagName('button');
		for(var i=0;i<buttonElements.length;i++)
		{
			curElement=buttonElements[i];
			if(typeof languageGot[curElement.name] != 'undefined')
			{
				if(typeof curElement.firstChild.nodeValue != 'undefined')
					curElement.firstChild.nodeValue=languageGot[curElement.name];
			}
			else
			{
				Util.log('Keine Übersetzung für "'+curElement.name+'" im Wörterbuch "'+ languageGot.languageName +'" vorhanden.')
				//window.console.log('gadget.languages.de.'+curElement.name);
			}
		}
	}
}*/
/**
 * converts from:
 * <return>
 *  <properties>
 *   <name>theName</name>
 *   <value>theValue</value>
 *  </properties>
 * </return>
 * to:
 * <return>
 *  <theName>theValue</theName>
 * </return>
 *
 * @param {Object} xmlDocument
 */
Util.convertDaoXML = function(xmlDocument)
{
	var xmlProperties = xmlDocument.childNodes;
	for(var i = 0 ; i < xmlProperties.length; i++)
	{
		var nodePropertie = xmlProperties[i];
		var name = nodePropertie.getElementsByTagName('name')[0];
		var value = nodePropertie.getElementsByTagName('value')[0];
		var nodeConverted = Util.createTag(name,null,value);
		xmlProperties.replaceChild(nodeConverted,nodePropertie);
	}
	return xmlDocument;
}

Util.decode_utf8X =  function (utftext)
{
	var temp = typeof(utftext);
	temp2 = temp.substr(0,3);
	if(temp2 == 'str')
	{
		var plaintext = "";
		var i=0;
		var c=0;
		var c1=0;
		var c2=0;
		// while-Schleife, weil einige Zeichen uebersprungen werden
		while(i<utftext.length)
		{
			c = utftext.charCodeAt(i);
			if (c<128)
			{
				plaintext += String.fromCharCode(c);
				i++;
			}
			else if((c>191) && (c<224))
			{
				c2 = utftext.charCodeAt(i+1);
				plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
				i+=2;
			}
			else
			{
				c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
				plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
				i+=3;
			}
		}
		return plaintext;
	}
	else
		return utftext

}
Util.encode_utf8=function(rohtext)
	{
	// dient der Normalisierung des Zeilenumbruchs
	rohtext = rohtext.replace(/\r\n/g,"\n");
	var utftext = "";
	for(var n=0; n<rohtext.length; n++)
		{
		// ermitteln des Unicodes des  aktuellen Zeichens
		var c=rohtext.charCodeAt(n);
		// alle Zeichen von 0-127 => 1byte
		if (c<128)
			utftext += String.fromCharCode(c);
		// alle Zeichen von 127 bis 2047 => 2byte
		else if((c>127) && (c<2048)) {
			utftext += String.fromCharCode((c>>6)|192);
			utftext += String.fromCharCode((c&63)|128);}
		// alle Zeichen von 2048 bis 66536 => 3byte
		else {
			utftext += String.fromCharCode((c>>12)|224);
			utftext += String.fromCharCode(((c>>6)&63)|128);
			utftext += String.fromCharCode((c&63)|128);}
		}
	return utftext;
	}

Util.decode_utf8=function(utftext)
	{
	var plaintext = ""; var i=0; var c=c1=c2=0;
	// while-Schleife, weil einige Zeichen uebersprungen werden
	while(i<utftext.length)
		{
		c = utftext.charCodeAt(i);
		if (c<128) {
			plaintext += String.fromCharCode(c); i++;}
		else if((c>191) && (c<224)) {
			c2 = utftext.charCodeAt(i+1);
			plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
			i+=2;}
		else {
			c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
			plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
			i+=3;}
		}
	return plaintext;
	}
Util.encode_utf8X = function(rohtext)
{
	var temp = typeof(rohtext);
	var temp2 = temp.substr(0,3);
	if(temp2 == 'str')
	{
		// dient der Normalisierung des Zeilenumbruchs
		rohtext = rohtext.replace(/\r\n/g,"\n");
		var utftext = "";
		for(var n=0; n<rohtext.length; n++)
		{
			// ermitteln des Unicodes des  aktuellen Zeichens
			var c=rohtext.charCodeAt(n);
			// alle Zeichen von 0-127 => 1byte
			if (c<128)
				utftext += String.fromCharCode(c);
			// alle Zeichen von 127 bis 2047 => 2byte
			else if((c>127) && (c<2048))
			{
				utftext += String.fromCharCode((c>>6)|192);
				utftext += String.fromCharCode((c&63)|128);}
				// alle Zeichen von 2048 bis 66536 => 3byte
			else
			{
				utftext += String.fromCharCode((c>>12)|224);
				utftext += String.fromCharCode(((c>>6)&63)|128);
				utftext += String.fromCharCode((c&63)|128);
			}
		}
		return utftext;
	}
	else
		return rohtext;

}

/**
 * this method is usually binded on the select element by GUI.Form.createForm
 * @param {Object} entitiesGot
 */
Util.createSELECT = function(entitiesGot)
{
	var element = this;
	var fieldName = "name";
	var fieldID = "id";

	if(this.attributes['field'] != null)
		fieldName = this.attributes['field'].nodeValue;
	if(this.attributes['fieldID'] != null)
	{
		fieldID = this.attributes['fieldID'].nodeValue;
	}
	if (element.type == 'select-one')
	{
		var tagOPTION = Util.createTag('option', '', '');
		element.appendChild(tagOPTION);
	}
	var valueArray = this.entity.getFieldIdValuesArray(element.name);
	for(var i=0;i<entitiesGot.length;i++)
	{
		var entity = entitiesGot[i];
		var id = entity.getID();
		if(fieldID != null)
			id = entity.getFieldStringValue(fieldID);
		var value = entity.getFieldStringValue(fieldName);
		var selected = '';
		var strMatchString = '('+ id + ')';
		var strEntityFieldValue = this.entity.getFieldStringValue(element.name);
		if(strEntityFieldValue.match(strMatchString) && element.type=='select-one')
		{
			selected = ',selected=selected';
			element.value = value;
		}
		else
		{
			var tagOPTION = new Option(value, id, false, false);//Util.createTag('option','value=' + id + selected,value);
			if(element.type=='select-multiple')
			{
				for (var j = 0; j < valueArray.length; j++)
				{
					var entityValue = valueArray[j];
					if(entityValue == id)
						tagOPTION.selected = true;
				}
			}
			element.options[element.options.length] = tagOPTION;
		}
	}

}

/**
 * this method should be bind to a Entity!!
 * @param {String} strConditionGot e.g. getFieldStringValue(name)=bla would return true or false
 */
Util.calculateExpression = function( strConditionGot, entityGot)
{
	var value = null;
	if(strConditionGot != null)
	{
		var condEqual = strConditionGot.indexOf('=');
		var condGreater = strConditionGot.indexOf('>');
		var condSmaller = strConditionGot.indexOf('<');
		var condAND = strConditionGot.indexOf('&&');
		var condOR = strConditionGot.indexOf('||');


		if (condAND > -1)
		{
			var arExpressions = strConditionGot.split('&&');
			//TODO
		}
		else if (condOR > -1)
		{
			var arExpressions = strConditionGot.split('&&');
			//TODO
		}
		else if(condEqual > -1 && condGreater > -1)
		{
			var arExpressions = strConditionGot.split('>=');
			if(arExpressions.length > 2)
			{
				throw 'ExpressionException: ' + strConditionGot;
			}
			var expLeft = Util.calculateExpression(arExpressions[0], entityGot);
			var expRight = Util.calculateExpression(arExpressions[1], entityGot);
			value = new Boolean(expLeft >= expRight);
		}
		else if(condEqual > -1 && condSmaller > -1)
		{
			var arExpressions = strConditionGot.split('<=');
			if(arExpressions.length > 2)
			{
				throw 'ExpressionException: ' + strConditionGot;
			}
			var expLeft = Util.calculateExpression(arExpressions[0], entityGot);
			var expRight = Util.calculateExpression(arExpressions[1], entityGot);
			value = new Boolean(expLeft <= expRight);
		}
		else if(condSmaller > -1)
		{
			var arExpressions = strConditionGot.split('<');
			if(arExpressions.length > 2)
			{
				throw 'ExpressionException: ' + strConditionGot;
			}
			var expLeft = Util.calculateExpression(arExpressions[0], entityGot);
			var expRight = Util.calculateExpression(arExpressions[1], entityGot);
			value = new Boolean(expLeft < expRight);
		}
		else if(condGreater > -1)
		{
			var arExpressions = strConditionGot.split('>');
			if(arExpressions.length > 2)
			{
				throw 'ExpressionException: ' + strConditionGot;
			}
			var expLeft = Util.calculateExpression(arExpressions[0], entityGot);
			var expRight = Util.calculateExpression(arExpressions[1], entityGot);
			value = new Boolean(expLeft > expRight);

		}
		else if(condEqual > -1)
		{
			var arExpressions = strConditionGot.split('=');
			if(arExpressions.length > 2)
			{
				throw 'ExpressionException: ' + strConditionGot;
			}
			var expLeft = Util.calculateExpression(arExpressions[0], entityGot);
			var expRight = Util.calculateExpression(arExpressions[1], entityGot);
			value = new Boolean(expLeft == expRight);
		}
		else
		{
			//expression no condition
			var posBracketOpen = strConditionGot.indexOf('(');
			var posBracketClose = strConditionGot.indexOf(')');
			var call = strConditionGot.substring(0,posBracketOpen);
			var arParameter = strConditionGot.substring(posBracketOpen+1,posBracketClose).split(',');
			if(call == 'getFieldStringValue')
			{
				value = entityGot.getFieldStringValue(arParameter);
			}
			else if(call == 'setFieldStringValue')
			{
				value = entityGot.getFieldStringValue(arParameter);
			}
			else
			{
				value = strConditionGot;
			}
		}
	}
	return value;

}
Util.opacity=function(id, opacStart, opacEnd, millisec) {
    //speed for each frame
	var obj=Util.gel(id);

    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("Util.changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("Util.changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}
Util.changeOpac=function(opacity, idGot) {
	var object;
	if(typeof idGot=='string')
		object = document.getElementById(idGot);
	if (object != null)
	{
		object = object.style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
	else
		Util.log('No Object found');
}
Util.shiftOpacity=function(id, millisec) {
    if(document.getElementById(id).style.opacity == 0) {
        Util.opacity(id, 0, 70, millisec);
    } else {
        Util.opacity(id, 100, 0, millisec);
    }
}

Util.getFunctionByName=function(functionNameGot)
{
	if (functionNameGot != null && functionNameGot != '')
	{
		var tmpFunc=null;
		if(typeof gadget.functions[functionNameGot] != 'undefined')
		{
			tmpFunc = gadget.functions[functionNameGot];
		}
		else if(typeof gadget.parent.functions[functionNameGot] != 'undefined')
		{
			tmpFunc = gadget.parent.functions[functionNameGot];
		}
		if(tmpFunc==null)
		{
			tmpFunc = function()
			{
				throw Util.curTimeStamp() + " The OnClick Method '" + functionNameGot + "' should be set by hand! Try gadget.functions." + functionNameGot + "()";
			};
		}
		tmpFunc.fname=functionNameGot;
		return tmpFunc;
	}
	else
		return null;
}

Util.submitUpload=function(formGot,resultContainerGot)
{
	var n = 'tmpIframe';
	var divCont = Util.createTag('div');
	divCont.innerHTML='<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="Util.wrapIframeResponse(\''+n+'\')"></iframe>';
	document.body.appendChild(divCont);
	var iframeTag=Util.gel(n);

	var resCont=Util.gel(resultContainerGot);
	if(resCont==null)
	{
		resCont=Util.createTag('div','class=uploadresponse',null);
		formGot.appendChild(resCont);
	}
	var tmpFunc2=function(responseGot)
	{
		if(responseGot.indexOf('[OK]')>-1)
			formGot.reset();
		resCont.innerHTML=responseGot;
		 Util.log('UPLOAD DONE');
	}
	iframeTag.onComplete =tmpFunc2;
	formGot.setAttribute('target', n);
	return true;
}
Util.getChildNodesRecursive=function(objGot)
{
	var curObj=null;
	var retArray=new Array();
	for(var c=0;c<objGot.childNodes.length;c++)
	{
		curObj=objGot.childNodes[c];
		if(curObj.nodeType==1)
			retArray[retArray.length]=curObj;
		if(curObj.hasChildNodes())
		{
			var subArray=Util.getChildNodesRecursive(curObj);
			for(var s=0;s<subArray.length;s++)
			{
				retArray[retArray.length]=subArray[s];
			}
		}

	}
	return retArray;
}
Util.addContextMenuToObjectsLable=function(objsGot,functionNameGot)
{
	var curEl;
	if (typeof objsGot.length != 'undefined')
	{
		for (var t = 0; t < objsGot.length; t++)
		{
			curEl = objsGot[t];
			if (typeof curEl.contextMenu == 'undefined')
				curEl.contextMenu = new ContextMenu(curEl.lable);
			curEl.contextMenu.addMenuButton(functionNameGot, Util.getFunctionByName(functionNameGot).bind(curEl));
		}
	}
	else
	{
		for (strEl in objsGot)
		{
			curEl = objsGot[strEl];
			if (typeof curEl.contextMenu == 'undefined')
				curEl.contextMenu = new ContextMenu(curEl.lable);
			curEl.contextMenu.addMenuButton(functionNameGot, Util.getFunctionByName(functionNameGot).bind(curEl));
		}
	}
}
Util.trim=function(strGot)
{
  return strGot.replace (/^\s+/, '').replace (/\s+$/, '');
}
Util.wrapIframeResponse=function(iframeNameGot)
{
	var iframeTag=Util.gel(iframeNameGot);
	if (iframeTag.contentDocument)
	{
	    var d = iframeTag.contentDocument;
	}
	else
	    if (iframeTag.contentWindow)
	    {
	        var d = iframeTag.contentWindow.document;
	    }
	    else
	    {
	        var d = window.frames[iframeTag.id].document;
	    }
	if (d.location.href == "about:blank")
	{
	    return;
	}

	if (typeof(iframeTag.onComplete) == 'function')
	{
	    iframeTag.onComplete(d.body.innerHTML);
	}
}
Array.prototype.copy = function()
{
	return ((new Array()).concat(this));
};
/*Object.prototype.copy = function()
{
     var tmp = this.constructor();
     if (this.length)
	 {
	 	for (var i = 0; i < this.length; i++)
	 		tmp[i] = this[i];
	 }
	 else
	 	for (var a in this)
	 		tmp[a] = this[a];
     return tmp;
}*/
Util.select = {};
Util.select.setSelectedOption = function(elSelectGot, strOptionValueGot)
{
    for (var i = 0; i < elSelectGot.options.length; i++)
    {
    	var option = elSelectGot.options[i];
		if (option.value == strOptionValueGot)
		{
			elSelectGot.selectedIndex = i;
			//option.selected = true;
		}
    }
}
Util.dom = {};

Util.dom.getElementsByClassName = function(classNameGot)
{
	var all_obj,ret_obj=new Array(),j=0;
	for(var ao=0;ao<this.childNodes.length;ao++)
	{
		var classes = this.childNodes[ao].className.split(' ');
		if(classes.length>1)
		{
			for(var i2=0;i2<classes.length;i2++)
			{
				if(classes[i2]==classNameGot)
				{
					ret_obj[j]=all_obj[ao];
					j++
				}
			}
		}
		else
		{
			if(classes[0]==classNameGot)
			{
				ret_obj[j]=all_obj[ao];
				j++
			}
		}
	}
	return ret_obj;
}

//Element.prototype.getElementsByClassName = Util.dom.getElementsByClassName;
//Object.prototype.returnElementsByClassName =''

