//wir sind mal optimistisch. der returnwert ist std. mäßig true
//dH. wenn zB. ein onSubmit-Event keinen returnwert hat, wird das submit ausgef�hrt.
//die Variable ist global, da sonst das evalScripts von Prototype keine wirkung auf diese Variable hat.
var blorRequestReturnValue = true;
var blorFormularFocusedElementId;
var blorFormularLastKey_Tab = false;

document.onkeydown = blor_onKeyDownEvent;
document.onmousedown = blor_onMouseDown;

function blor_GetElementById(id)
{
	return $(id);
}

function blor_replaceElement(id, value)
{
	try {
		Element.replace(id, value);
	} catch(error) { }
}

function blor_insertHtml(parentId, html)
{
	try
	{
		Element.insert(parentId, html);
	} catch(error) {
		alert("insert error: can't find element with id '" + parentId + "'" + error);
	}
}

function blor_removeElement(id)
{
	$(id).remove();
}

function blor_writeInLayer(Layer, Text)
{
	$(Layer).update(Text);
}

function blor_getOffset(element)
{
	return Element.positionedOffset(element);
}

function blor_callCallbackForPageRequest(url)
{
	if (typeof blorBeforePageRequest=="function")
	{
		blorBeforePageRequest(url);
	}
}
function blor_callCallbackForAfterPageRequest(url)
{
	if (typeof blorAfterPageRequest=="function")
	{
		blorAfterPageRequest(url);
	}
}

function blor_template_call(url, isAsynchronus, optionalParameters)
{
	if (typeof optionalParameters == "undefined")
	{
		optionalParameters = "";
	}
	if (typeof isAsynchronus == "undefined")
	{
		isAsynchronus = true;
	}
	var myAjax = new Ajax.Request(url, 
		{
			method: 'post',
			evalScripts: false,
			asynchronous: isAsynchronus,
			parameters: optionalParameters
		}
	);
}

function blor_form_call(url, formular)
{
	document.body.style.cursor = "wait";
	var returnValue = true;
	var myParameters = Form.serialize(formular);
	var myParameters = myParameters + '&' + formular.id + '_focusId=' + blorFormularFocusedElementId;
	var myAjax = new Ajax.Request(url, 
		{
			onSuccess: function () {
				document.body.style.cursor = "auto";
			}, 
			method: 'post',
			parameters: myParameters,
			evalScripts: true,
			asynchronous: false
		}
	);
	
	returnValue = blorRequestReturnValue;
	//default-wert wiederherstellen
	blorRequestReturnValue = true;
	//nun noch die fokuse wiederherstellen..
	return returnValue;
}

function blor_form_ajaxCall(url, formular)
{
	document.body.style.cursor = "wait";
	var returnValue = true;
	var myParameters = Form.serialize(formular);
	var myParameters = myParameters + '&' + formular.id + '_focusId=' + blorFormularFocusedElementId;
	var myAjax = new Ajax.Request(url, 
		{
			onSuccess: function () {
				document.body.style.cursor = "auto";
			}, 
			method: 'post',
			parameters: myParameters,
			evalScripts: true,
			asynchronous: false
		}
	);
	blorRequestReturnValue = true;
	return false;
}

function blor_formular_setFocusedElement(formularElement)
{
	blorFormularFocusedElementId = formularElement.id;
	//alert(blorFormularFocusedElementId);
}

function blor_formular_setFocusOnElementById(elementId)
{
	if(blorFormularLastKey_Tab)
	{
		blor_GetElementById(elementId).activate();
	} else {
		blor_GetElementById(elementId).focus();
	}
}

function blor_onKeyDownEvent(event)
{
	if (!event)
    	event = window.event;
	if (event.which) {
		keyCode = event.which;
	} else if (event.keyCode) {
		keyCode = event.keyCode;
	}
	
	if(keyCode == 9)
	{
		blorFormularLastKey_Tab = true;
	} else {
		blorFormularLastKey_Tab = false;
	}
}
function blor_onMouseDown(event)
{
	blorFormularLastKey_Tab = false;
}

/**
 * Ajax-Request in Layer laden
 */
function blor_loadLayer(Layer,URL, optionalParameters, optionalMethode)
{
	if (typeof optionalParameters == "undefined")
	{
		optionalParameters = "";
	}
	if (typeof optionalMethode == "undefined")
	{
		optionalMethode = "post"
	}
	
	blor_callCallbackForPageRequest(URL);
	
	var BlorAjax = new Ajax.Updater
	(
		Layer,URL,
		{
		 on404: function()
		 {
			blor_callCallbackForAfterPageRequest(URL);
		 	return false;
		 },
		 on401: function () {blor_callCallbackForAfterPageRequest(URL);},
		 onFailure:  function()
		 {
			blor_callCallbackForAfterPageRequest(URL);
			alert ("Blor - Ajax fehler");
		 },
		 onSuccess: function () {blor_callCallbackForAfterPageRequest(URL);}
		 ,evalScripts: 'true'
		 ,asynchronous: 'true'
         ,parameters: optionalParameters
		 ,method: optionalMethode
	 	}
	);	
}
function blor_loadLayerSync(Layer,URL, optionalParameters, optionalMethode)
{
	if (typeof optionalParameters == "undefined")
	{
		optionalParameters = "";
	}
	if (typeof optionalMethode == "undefined")
	{
		optionalMethode = "post"
	}
	
	blor_callCallbackForPageRequest(URL);
	
	var BlorAjax = new Ajax.Updater
	(
		Layer,URL,
		{
		 on404: function()
		 {
			blor_callCallbackForAfterPageRequest(URL);
		 	return false;
		 },
		 on401: function () {blor_callCallbackForAfterPageRequest(URL);},
		 onFailure:  function()
		 {
			blor_callCallbackForAfterPageRequest(URL);
			alert ("Blor - Ajax fehler");
		 },
		 onSuccess: function () {blor_callCallbackForAfterPageRequest(URL);}
		 ,evalScripts: true
		 ,asynchronous: false
         ,parameters: optionalParameters
		 ,method: optionalMethode
	 	}
	);	
}
function blor_setPeriodicalUpdate(layer, url, iFrequency, iDecay, optionalParameters)
{
	if (typeof optionalParameters == "undefined")
	{
		optionalParameters = "";
	}
	new Ajax.PeriodicalUpdater(layer, url, {
		on401: function (periodicalUpdater) {periodicalUpdater.stop()}, method: 'post', frequency: iFrequency, decay: iDecay, parameters: optionalParameters, asynchronous: true, evalScripts: true
	});
}
