function isEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if ((str.indexOf(at)==-1) || (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		|| (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)  || (str.indexOf(at,(lat+1))!=-1)
		|| (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) || (str.indexOf(dot,(lat+2))==-1) || 
		(str.indexOf(" ")!=-1)) {
		return false;
	}
	return true;
}


var tools = {
    
	media_registration: function (form) {		
		errors = tools.verify(form);	
		if(errors) { 
			alert("There are several items not filled out correctly.\n\nPlease check the following fields:\n\n"+errors);
			$(form).removeAttr('style');
			return false;
		}
		
		return true;
	},	
	
	make_booking: function(form) { 
	
		var params = $(form).serialize();
	
		var errors = tools.verify(form);	
		if(errors) { 
			alert("Please fill in the following details:\n\n"+errors);
			$(form).removeAttr('style');
			return false;
		}
		else {
				$(form).find('input.submit').attr('disabled','disabled').hide('slow').end().fadeTo('slow', 0.2);
		    		$('h2.processing').show();
				$.post($(form).attr('action'), params,
					function(data) {
					        $(form).hide();
					        $('h2.processing').hide();
					    	$('h2.thanks').show();
					}, 'json'
				);
		    		return false;
			//}
		}	
	},
	
	join_team: function(form) { 
	
		var params = $(form).serialize();
	
		var errors = tools.verify(form);	
		if(errors) { 
			alert("Please fill in the following details:\n\n"+errors);
			$(form).removeAttr('style');
			return false;
		}
		else {
		    		return true;
		}	
	},
	
	subscribe_mailinglist: function(form) { 
		
		errors = tools.verify(form);	
		
		if(errors) { 
			alert("Please fill in the following details:\n\n"+errors);
			$(form).removeAttr('style');
			return false;
		}
		else { 
			$(form).find('input.submit').val('submitting...').attr('disabled','disabled')
			$.post($(form).attr('action')+'/ajax/', $(form).serialize(),
				function(data) { 
					$('.boxy-content').html(data.message);
				}
			);
		}	
	},
	
	verify: function(form) { 
		var error = '';
		//$('.verify').removeClass('check');

		$(form).find('input.required, textarea.required').each(function() {
			if($(this).val() == '' || ($(this).hasClass('email') && !isEmail($(this).val())) || ($(this).attr('type') == 'checkbox' && $(this).attr('checked') == false)) {
				//$(this).addClass('check');
				error += '   - '+$(this).attr('title')+'\n';
			}
		});
		return error;
	},
		
	submit_form: function(form) { 
		errors = tools.verify(form);		
		if(errors) { 
			alert("Please fill in the following details:\n"+errors);
			$(form).removeAttr('style');
			return false;
		}
		else { 
			return true;
		}
	}
    
};
