$(function() {
		
		$("#submit1").click(function() {
			var type = 'draw';
			var name = $("input#name1").val();
			var email = $("input#email1").val();
			var newsletter = $("input#newslettercheck").is(':checked');
			if (newsletter == true) {
				newsletter = 1;
			} else {
				newsletter = 0;
			}
			var draw = 1;
			process_form(name, email, newsletter, draw, type)
		});
		
		$("#submit2").click(function() {
			var type = 'newsletter';
			var name = $("input#name2").val();
			var email = $("input#email2").val();
			var newsletter = 1;
			var draw = 0;
			process_form(name, email, newsletter, draw, type)
		});
});

function process_form(name, email, newsletter, draw, type) {
			
		if (name == "") {
			alert ("Please fill in your name.");
			return false;
 }
		
		if ( (email == "") || (email.indexOf(".") > 2) == false || (email.indexOf("@") > 0) == false ) {
			alert ("Please fill in your email Address.");
			return false;
 }
		
		// only do this if not return false from above tests
		var dataString = 'name=' + name + '&email=' + email + '&newsletter=' + newsletter + '&draw=' + draw;
		
		// stop the form from reloading the page
		$('.submit').removeAttr('disabled');
		
		if (type == 'draw') {
			$('#doing1').html('<img src=\"http://www.guidetoukcasinos.com/images/loading3.gif\" style=\"margin:15px 0 4px 10px;\" />');
		} else if (type == 'newsletter') {
			$('#doing2').html('<img src=\"http://www.guidetoukcasinos.com/images/loading3.gif\" style=\"margin:6px 0 0 10px;\" /> ');
		}
		
		// write the details to the database
		
		$.ajax({
 		type: "POST",
 			url: "http://www.guidetoukcasinos.com/includes/add-names-to-db.php",
				data: dataString
			});
			
			if (type == 'draw') {
				$('#doing1').html('<img src=\"http://www.guidetoukcasinos.com/images/check.png\" style=\"margin:8px 0 0 10px;\" />');
			} else if (type == 'newsletter') {
				$('#doing2').html('<img src=\"http://www.guidetoukcasinos.com/images/check.png\" style=\"margin:0px 0 0 10px;\" />');
			}
 return false;
}
runOnLoad(function(){
});
