function tryagain(){
	$('.loader').empty();
	$('input:radio').removeAttr("checked");
	$('#quiz').slideDown(1000);
};

$(document).ready(function(){	
	$('input').not('input[type="text"]').focus(function() {this.blur();});
	
	required = ["name", "email", "phone", "address", "city", "state", "zip"];
	radios = ["q1", "q2", "q3", "q4", "q5", "q6", "q7"];
	email = $("#email");
	

	$("#quiz").submit(function(){	
		for (i=0;i<required.length;i++) {
			var input = $('#'+required[i]);
			if (input.val() == "") {
				input.addClass("notanswered").next('span').show();
			} else {
				input.removeClass("notanswered").next('span').hide();
			}
		}
		
		for (k=0;k<radios.length;k++) {
			var radioinput = $('input[name='+radios[k]+']:checked');
			if (!radioinput.val()) {
				$('input[name='+radios[k]+']').addClass("notanswered");
				$('#missing-'+radios[k]).show();
			} else {
				$('input[name='+radios[k]+']').removeClass("notanswered");
				$('#missing-'+radios[k]).hide();
			}
		}
		
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("notanswered");
		}

		if ($(":input").hasClass("notanswered")) {
			$("#error").slideDown(500);
			return false;
		} else {
			score=0;
			if ($('input[name=q1]:checked').val() == "ash") {score++};
			if ($('input[name=q2]:checked').val() == "dynamic") {score++};
			if ($('input[name=q3]:checked').val() == "flemisheye") {score++};
			if ($('input[name=q4]:checked').val() == "x16") {score++};
			if ($('input[name=q5]:checked').val() == "o1306") {score++};
			$('#quiz').add($("#error")).hide();
			if (score == 5) {
				$('.loader').append($('.bar'));
				$('.bar').css({display:'block'});
			
				$.post('/quiz/mail.php',{name:$("#name").val(),
								  phone:$("#phone").val(),
								  cname:$("#cname").val(),
								  address:$("#address").val(),
								  city:$("#city").val(),
								  state:$("#state").val(),
								  zip:$("#zip").val(),
								  country:$("#country").val(),
								  email:email.val(),
								  grade:score,
								  prize:$("input[name=q7]:checked").val(),
								  sticker:$("input[name=q6]:checked").val()},		
				function(){
				  window.location.replace("/contestsuccess?pmc=AC");
				});
				return false;
			}
			else {
				$('.loader').append("<p>OOOOPS!! Maybe you should read the catalog again! <br />You scored a "+score+" out of 5.  Please <a href='javascript:void();' id='tryagain'>try again</a>!</p>");
				$("#tryagain").click(function(){tryagain()});
				return false;
			}
		}
	});
	
});
