$(function() {
	$.info('jquery.modeco-elais-comp09.js loaded');

	$('#btn_submit_modeco_form').click(function() {
		$('#' + this.rel).submit();
		return false;
	});

	$('#modeco-elais-comp09-form').submit(function() {
		var $this = $(this),
			errors = 0;

		$this
			.find('.error').removeClass('error').end()
			.find('.mandatory')
				.each(function() {
					var $t = $(this),
						val = $t.val();
					if (!(val) || val == '') {
						$t.parent().addClass('error');
						errors++;
					}
				});

		if (errors > 0) {
			alert('Δεν έχετε συμπληρώσει σωστά την φόρμα συμμετοχής!');
			return false;
		}
		//else
		$.post($this.attr('action'), $this.serialize(), 
			function(data) { //on ajax complete
				$.info('Opening the ajax response to a boxy window!');
				new Boxy(data, {modal:true, title: 'Παιχνίδι'});
			});
		
		return false; //We made an ajax request and so we must not post the data
	});

	if (window.location.toString().indexOf('form__withgame') > 0) {
		$.get(window.location.toString().replace('form__withgame', '__game'), 
			function(data) { //on ajax complete
				new Boxy(data, {modal:true, title: 'Παιχνίδι'});
			});
	}
});
