$(document).ready(function() {
	
	// Use the email address field to check if they've already entered the sweepstakes.
	$('#email').blur(function() {
		$("#output").load("email.php?check=email&email="+$(this).val(), null, processNum);
	});
	
	// Validate form on submit.
	$('#form').submit(function(e) {
		
		try {
			
			if (!$('#email').val())
				throw "Email is empty!";
			
			if (!$('#firstname').val())
				throw "First name is empty!";
			
			if (!$('#lastname').val())
				throw "Last name is empty!";
			
			if (!$('#dealership').val())
				throw "Dealer name field is empty!";
		}
		
		catch (error) {
			alert(error);
			e.preventDefault();
		}
	});
	
});

// Callback for email AJAX check.
function processNum() {
    if ($("#output").text() > 0 ) {
        alert("This email address has already been entered into this sweepstakes.");
        document.form1.email.value = "";
    }
}

// Don't know what this is for.
/*function popUp(URL){
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=1,height=1');");
}*/

