function Fensterweite ()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
  	}
  	else if (document.body && document.body.offsetWidth)
  	{
    	return document.body.offsetWidth;
  	}
  	else
  	{
    	return 0;
  	}
}

function Fensterhoehe ()
{
  	if (window.innerHeight)
  	{
    	return window.innerHeight;
  	}
  	else if (document.body && document.body.offsetHeight)
  	{
    	return document.body.offsetHeight;
  	}
  	else
  	{
    	return 0;
  	}
}

function neuAufbau(bGo)
{
	if(typeof bGo == 'undefined') bGo = false;
	
	if (Weite != Fensterweite() || Hoehe != Fensterhoehe() || bGo)
  	{
		if (document.getElementById('adminrightalignedwrapper'))
		{
			var adminrightalignedwrapper = document.getElementById('adminrightalignedwrapper');
			adminrightalignedwrapper.style.right = "2";
			adminrightalignedwrapper.style.right = "1";
		}
		if (document.getElementById('userrightalignedwrapper'))
		{
			var adminrightalignedwrapper = document.getElementById('userrightalignedwrapper');
			adminrightalignedwrapper.style.right = "2";
			adminrightalignedwrapper.style.right = "1";
		}
 	}
}

function correctWidthByContent()
{
	var nOverallWidth = document.getElementById('maintable').offsetWidth;
	if (nOverallWidth > 5000)
	{
		var nCenterWidth = nOverallWidth - 2002;
		document.getElementById('adminheadstart').style.width = "999px";
		document.getElementById('adminheadcenter').style.width = nCenterWidth.toString() + "px";
		document.getElementById('adminheadend').style.width = "999px";
	}
	document.getElementById('adminheadmenu').style.width = nOverallWidth;
	
}

function disableButton(btn, title, target, bSubmit)
{
	if(typeof bSubmit == 'undefined') bSubmit = true;
	
	if (title)
	{
		btn.value = title;
		if (!btn.style.width)
		{
			btn.style.width = "100px";
		}
	}
	btn.disabled = true;
	btn.style.cursor = "wait";
	
	if (bSubmit)
	{
		if(target)
		{
			location.href = target;
		}
		else
		{
			btn.form.submit();
		}
	}
}

/*
* Calls an URL via AJAX with GET
*/
function ajaxCall(sUrl, sHandleAs, nTimeOut, sLayer, sAlertOnFail, bSync)
{
	if(typeof sUrl == 'undefined') return;
	if(typeof sHandleAs == 'undefined') sHandleAs = "text";
	if(typeof nTimeOut == 'undefined') nTimeOut = 5000;
	if(typeof sLayer == 'undefined') sLayer = '';
	if(typeof sAlertOnFail == 'undefined') sAlertOnFail = '';
	if(typeof bSync == 'undefined') bSync = false;

	var bSuccess = 1;
	var sResponse = dojo.xhrGet({
		url: sUrl,
		handleAs: sHandleAs,
		timeout: nTimeOut,
		sync: bSync,
		load: function(response, args) {
			if (sLayer != '')
			{
				dojo.byId(sLayer).innerHTML = response;
			}
			return response;
		},
		error: function(response, args) {
			console.log("Failed xhrGet", response, args);
			bSuccess = 0;
			return response;
		}
	});

	if (bSuccess == 0 && sAlertOnFail != '')
	{
		alert(sAlertOnFail);
	}

	return sResponse;
}

function ajaxCallPost(sUrl, oPostData, sHandleAs, nTimeOut, bSync, sLayer, sAlertOnFail)
{
	if(typeof sUrl == 'undefined') return;
	if(typeof oPostData == 'undefined') oPostData = "";
	if(typeof sHandleAs == 'undefined') sHandleAs = "text";
	if(typeof nTimeOut == 'undefined') nTimeOut = 5000;
	if(typeof bSync == 'undefined') bSync = false;
	if(typeof sLayer == 'undefined') sLayer = '';
	if(typeof sAlertOnFail == 'undefined') sAlertOnFail = '';

	var bSuccess = 1;
	var sResponse = dojo.xhrPost({
		url: sUrl,
		content: oPostData,
		handleAs: sHandleAs,
		timeout: nTimeOut,
		sync: bSync,
		load: function(response, args) {
			if (sLayer != '')
			{
				dojo.byId(sLayer).innerHTML = response;
			}
			return response;
		},
		error: function(response, args) {
			console.log("Failed xhrPost", response, args);
			bSuccess = 0;
			return response;
		}
	});

		return bSuccess;
	}
	
	/**
	 * getFormData() gathers form element data into an array of objects that can
	 * be passed to ajaxCallPost()
	 */
	function getFormData(theform)
	{
		var oPostData = {};
		
		for(n=0; n < theform.elements.length; n++)
		{
			if (!theform.elements[n].name)
			{
				continue;
			}
			
			formName  = theform.elements[n].name; 
			formValue = fieldValue(theform.elements[n]);
			
			if (formValue)
			{
				if (oPostData[formName] != null)
				{
					if (oPostData[formName].constructor != Array)
					{
						var sTmp = oPostData[formName];
						oPostData[formName] = new Array(sTmp);
					}
					oPostData[formName].push(formValue);
				}
				else
				{
					oPostData[formName] = formValue;
				}
			}
		}
	
		return oPostData;
	}
		
	/**
	 * Returns the value of the field element.
	 */
	var fieldValue = function(el) {
		var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
	
		if (!n || el.disabled || t == 'reset' || t == 'button' ||
			(t == 'checkbox' || t == 'radio') && !el.checked ||
			(t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
			tag == 'select' && el.selectedIndex == -1) {
				return null;
		}
		
		if (tag == 'select') {
			var index = el.selectedIndex;
			if (index < 0) {
				return null;
			}
			var a = [], ops = el.options;
			var one = (t == 'select-one');
			var max = (one ? index + 1 : ops.length);
			for (var i = (one ? index : 0); i < max; i++) {
				var op = ops[i];
				if (op.selected) {
					var v = op.value;
					if (!v) { // extra pain for IE...
						v = (op.attributes && op.attributes['value'] &&
								!(op.attributes['value'].specified)) ?
								op.text : op.value;
				}
					if (one) {
						return v;
					}
					a.push(v);
				}
			}
			return a;
		}
		return el.value;
	};
