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) {		
		var 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 { 		
/*			var year 	= parseInt($('#year').val());
			var month 	= parseInt($('#month').val())-1;
			var day 	= parseInt($('#day').val());
			var hour	= parseInt($('#hour').val());
			var minutes	= parseInt($('#min').val());			
			if(hour < 12) hour += 12;
			
			d1 = new Date(year, month, day, hour, minutes); // creates a new Date for the 1st June 2005			
			d2 = new Date(); // creates a new Date representing today
			num_hours = ((((d1.getTime() - d2.getTime()) / 1000) / 60) / 60);

			if(!d1 || !d2) { 
				alert('Please check your dates and try again');
			}						
			else if(num_hours < 48) {
				alert("Sorry, we could not submit your reservation as it is within 48 hours\nPlease either change the date of your Reservation\nor alternatively call (02) 9259 5656");
			}
			else {*/
				$(form).find('input.submit').val('submitting...').attr('disabled','disabled');
				$.post($(form).attr('action')+'/ajax/', params,
					function(data) {
						$('.boxy-content').html(data);
					}
				);
			//}
		}	
	},
	
	subscribe_mailinglist: function(form) { 
		
		var errors = tools.verify(form);	
		
		if(errors) { 
			alert("Could you 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);
				}
			);
		}
	    	return false;
	},
	
	verify: function(form) { 
		var error = '';
		//$('.verify').removeClass('check');

		$(form).find('.verify').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) { 
		var errors = tools.verify(form);		
		if(errors) { 
			alert("Could you please fill in the following Details?\n"+errors);
			$(form).removeAttr('style');
			return false;
		}
		else { 
			return true;
		}
	}
    
};
