/*
postForm(<form element>[,<callback function>,<clear>])

Form element: 
The originating form. Needed for disabling and
resetting the form. If the form has a button
with <form id>_submit as id, a loading effect 
will be added.

Callback function (optional): 
Function to call after completion. 
postForm() has a built in error-reporter.
Example: callback(form,success,response);

Clear (optional):
If true (standard) the form element will
be restored to its original state using
form.reset();.
*/
function postForm(form,callback,clear) {
	if(clear!=false) clear=true;
	if(!form) {
		alert("Please specify the source form element!");
		return false;
	}
	if(!callback) {
		var callback = function(form,success,response) {
			if(success) {
				alert("Failed to post your request from "+form.id+" ("+response.status+")! Sorry!");
				return false;
			}
		}
	}
	if($(form.id+"_submit")) {
		var submit = $(form.id+"_submit");
		var tmp_className = submit.className;
		var tmp_innerHTML = submit.innerHTML;
		submit.innerHTML = "V&auml;nta...";
		submit.className = "loading";
	}
	var url;
	if($$(".mceEditor").length>0) tinyMCE.triggerSave();
	var params = form.serialize();
	Form.disable(form);
	if(form.action.indexOf("?")!=-1) url = form.action+"&ajax=1";
	else url = form.action+"?ajax=1";
	new Ajax.Request(url,{
		method:"post",
		asynchronous:true,
		postBody:params,
		onSuccess:function(response) {
			callback(form,true,response);
		},
		onFailure:function(response) {
			callback(form,false,response);
		}
	});
	if(submit) {
		submit.className = tmp_className;
		submit.innerHTML = tmp_innerHTML;
	}
	if(clear) Form.reset(form);
	Form.enable(form);
	Form.focusFirstElement(form);
	return false;
}
/*
trim(str)

Trims a string.

str: String to be trimed.
*/
function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
}

/*
function(url[,stule])

Opens a modal floated div.

url: Content to be putted in the box
style: the style-parameter of the div
*/
function openFloater(url,style) {
	if(!url) {
		alert("Please specify an URL!");
		return false;
	}
	Event.observe(document,"keyup",floater_esc = function(e) {
		if(e.keyCode==27) destroyFloater();
	});
	var params;
	if(url.indexOf("?")!=-1 && url.split("?").length==2) {
		params = url.split("?")[1];
		url = url.split("?")[0];
	}
	new Ajax.Updater("modal_window",url,{
		method:'get',
		parameters:params,
		evalScripts:true,
		onComplete: function(t) {
			if(!style) {
				scrollarr = getScrollOffset();
				$("modal_window").style.top = (scrollarr[1]+100)+"px";
				$("modal_window").style.left = (scrollarr[0]+((document.documentElement.clientWidth/2))-($("modal_window").getWidth()/2))+'px';
			} else if($("modal_window").style.indexOf("display")==-1) $("modal_window").style = style+" display: none;";
			else $("modal_window").style = style;
			$("modal_wrapper").setStyle({top:scrollarr[1]+"px",left:scrollarr[0]+"px"});
			new Effect.Appear($("modal_wrapper"),{duration: 0.2,from: 0, to: 1});
			new Effect.Appear($("modal_window"),{duration: 0.2});
		},
		onFailure: function(t) { alert("Failed to load floater! Ajax status: "+t.status); }
	});
}

/*
destroyFloater()

Closes an open floater.
*/
function destroyFloater() {
	new Effect.Fade($("modal_wrapper"),{duration: 0.2});
	new Effect.Fade($("modal_window"),{duration: 0.2,afterFinish: function() { $("modal_window").innerHTML=null; }});	
	Event.stopObserving(document,"keyup",floater_esc);
}

/*
getScrollOffset()

Returns an array with the scroll offset in px.
*/
function getScrollOffset() {
	var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;
	var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;
	var scrollarr = new Array(scrollx,scrolly)
	return scrollarr
}
