/*
 * bottom3.com custom js
 *
 * http://www.bottom3.com
 *
 * Copyright (c) 2009 aharris
 *
 */

$(document).ready(function() {

	// validate signup form on keyup and submit
	var regValidator = $("#regForm").validate({
		rules: {
			first: {
				required: true
			},
			last: {
				required: true
			},
			username: {
				required: true,
				minlength: 2,
				remote: "index.php?section=usercheck"
			},
			password: {
				required: true,
				minlength: 5
			},
			password_confirm: {
				required: true,
				minlength: 5,
				equalTo: "#password"
			},
			email: {
				//required: true,
				email: true
			}
		},

		messages: {
			first: {
				required: "Please enter a first name"
			},
			last: {
				required: "Please enter a last name"
			},
			username: {
				required: "Enter a username",
				minlength: jQuery.format("Enter at least {0} characters"),
				remote: jQuery.format("{0} is already in use")
			},
			password: {
				required: "Provide a password",
				rangelength: jQuery.format("Enter at least {0} characters")
			},
			password_confirm: {
				required: "Repeat your password",
				equalTo: "Sorry, your passwords do not match",
				minlength: jQuery.format("Enter at least {0} characters")
			},
			email: {
				//required: "Please enter a valid email address",
				minlength: "Please enter a valid email address"
			}
		},


		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});
	

/*
	function countChecked() {
		var count = 0;
		var n = $("input:checked");
		for (var i = 0; i < n.length; i++) {
			// Do something with a[i]
			if (n[i].type == "checkbox") {
				count += 1;
			}
		}
		return count;
	}


	$(":checkbox").click( function() {
		var submitCount = countChecked();
		$(this).parent('td').next().children(':radio').toggleClass('away');
		if (submitCount > 3) {
			$(this).attr('checked', false);
			$(this).parent('td').next().children(':radio').attr('checked', false);
			$(this).parent('td').next().children(':radio').toggleClass('away');
			$.prompt('The game is called bottom 3...not FOUR');
		}
	} );

	$("#submitBtn").click( function() {
		var submitCount = countChecked();
		if (submitCount < 3) {
			$.prompt('Almost, you have to pick at least 3');
		}
		if (submitCount < 2) {
			$.prompt('Almost, you have to pick 2 losers');
		}
	} );

	$("#finalVoteBtn").click( function() {
		if ($("input[name='runnerup']:checked").val() == $("input[name='winner']:checked").val()) {
			$.prompt('Sorry, the runner-up and the winner must be different');
		}

	} );

	// validate vote form on keyup and submit
	var submitVoteForm = $("#voteForm").validate({
		rules: {
			bottom3: {
				required: true
			},
			loser: {
				required: true
			}
		},
		messages: {
			bottom3: {
				required: function() {
					$.prompt('Almost, you have to choose 2 losers');
				}
			},
			loser: {
				required: function() {
					$.prompt('Oops, you forgot to vote for a loser');
				}
			}
		}
	});
	
	// validate finalvote form on keyup and submit
	var submitVoteForm = $("#finalVoteForm").validate({
		rules: {
			runnerup: {
				required: true
			},
			winner: {
				required: true
			}
		},
		messages: {
			runnerup: {
				required: ""
			},
			winner: {
				required: ""
			}
		},
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				$.prompt('Not quite, you must choose both a runner-up AND a winner!');
			}
		}
	});
*/
	
	var regValidator = $("#feedbackForm").validate({
		rules: {
			name: {
				required: true,
				minlength: 2
			},
			email: {
				required: true,
				email: true
			},
			subject: {
				required: true
			},
			feedback: {
				required: true
			}
		},
		messages: {
			name: {
				required: "Please enter your name",
				minlength: "Your name must be at least 2 characters long"
			},
			email: {
				required: "Please enter a valid email address",
				minlength: "Please enter a valid email address"
			},
			subject: {
				required: "Please enter a subject"
			},
			feedback: {
				required: "Please enter your feedback"
			}
		},


		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});
	

//end of document ready
});

