$(function() {
	$("#datepicker").datepicker({ 	dateFormat : 'dd.mm.yy',
									changeMonth : true,
									changeYear : true,
									yearRange : '-60:0'});
});

function checkForm(id)
{
	var status = 1;
	if(id == "#Other")
	{
		if($("#PresenceOnFestival INPUT[type='radio']:checked").length == 0)
		{
			$("#PresenceOnFestival").next("div").addClass('error').fadeIn(1000);
			status = 0;
		}
		else
			$("#PresenceOnFestival").next("div").addClass('error').fadeOut(1000);
	}
	if(!$(id).val()){
		status = 0; 
		$(id).next("div").addClass("error").fadeIn(1000);
	}
	else{		
		$(id).next("div").fadeOut(1000);
	}
	return status;
}

function isValidEmailAddress(emailAddress) {
	var filter=/^.+@.+\..{2,3}$/;
	return filter.test(emailAddress);
}

$(document).ready(function(){
	$("form").submit(function(){
			if((checkForm("#FirstAndLastName") && checkForm("#MediumTitle") && isValidEmailAddress($("#EditorEmail").val()) &&
					checkForm("#MediumFunction") &&	checkForm("#Email") &&
					checkForm("#Telephone")) && checkForm("#Other"))
			{
				return true;
			}
			else
				return false;
	});
	$("#FirstAndLastName").blur(function(){checkForm("#FirstAndLastName")});
	
	$("#EditorEmail").blur(function () 
			{
				
				if(!$(this).val())
					checkForm("#EditorEmail");
				else if(!isValidEmailAddress($(this).val()))
				{
					$("#wrongFormat").addClass("error").fadeIn(1000);
				}
				else
				{
					$("#wrongFormat").fadeOut(1000);
					$(this).next("div").fadeOut(1000);
				}
			});
	
	$("#MediumTitle").blur(function () {checkForm("#MediumTitle")});
    
	$("#MediumFunction").blur(function () {checkForm("#MediumFunction")});
    
	$("#Email").blur(function () {checkForm("#Email")});

	$("#Telephone").blur(function () {checkForm("#Telephone")});

	$("#Other").blur(function () { checkForm("#Other")});

});