var TweenWait = 300,
	popinFadeOutMS = 5000;

function doAjax(UrlResource, Data) {
	new Request({
		"url": UrlResource,
		"data": Data,
		"headers": {'X-Request': 'JSON'},
		"onSuccess": function(req) {
			var r = JSON.decode(req);
			if(r.status > 0) {
				return r;
			} else {
				switch(r.status) {
					case -2:
						alert("Internal error");						
					break;                
				}                                                                                                                                                                                                    
			}
			return null;
		}
	}).send();
}

function doAjaxCallback(UrlResource, Data, Callback, CallbackArgs) {
	new Request.JSON({
		"url": UrlResource,
		"data": Data,
		/* "headers": {'X-Request': 'JSON'}, */
		"onSuccess": function(req) {
			var r = req;
			if(r.status > 0) {
				if (Callback)
					Callback(r, CallbackArgs);
			} else {
				switch(r.status) {
					case -2:
						alert("Internal error");
					break;                
				}                                                                                                                                                                                                                                   
			}
		}
	}).send();
}

function doPopin(headline, textmass, buttons) {
	var popin = $("popin");
	
	if (popin != null)
		return;
	
	$(document.body).adopt(
		popin = new Element("div", {"id": "popin", "opacity": 0}).adopt(
			new Element("div", {"class": "content"}).adopt($$(
				new Element("h3", {"html": headline}),
				new Element("p", {"html": textmass}),
				new Element("div",{"html": buttons})
			))
		)
	);
	popin.setStyle("top", window.getSize().y/2 + window.getScroll().y - popin.getSize().y/2);
	popin.setStyle("left", window.getSize().x/2 - popin.getStyle("width").toInt()/2);
	new Fx.Tween(popin, {duration: TweenWait, onComplete: function() {
		(function() {
			new Fx.Tween(popin, {duration: TweenWait, onComplete: function() {
				popin.dispose();
			}}).start("opacity", 0);
		});//.delay(popinFadeOutMS);
	}}).start("opacity", 1);
}


function limitText(limitField, limitNum) {

	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

window.addEvent("domready", function(){
	$$('a.submitform').addEvent('click', function(e){
		e.preventDefault();
		$(e.target).getParent('form').submit();	
	});
});

function validateEmail( email ) {
	apos = email.value.indexOf("@");
	dotpos = email.value.lastIndexOf(".");
	if (apos<1||dotpos-apos<2) {
		return false;
	}
	return true;
}