$(document).ready(function () {

	// Slideshow
    $('.slideshow').cycle({
		timeout:  3000, 
		speed:  1500
	});
	
	/*	Validate Quickforms with jQuery validate
	--------------------------------------------------*/
	
	// Remove the inline event handler from the QuickForm's submit button
	$(".submit-button").removeAttr("onclick"); 
	
	// Initialize validation on the entire ASP.NET form.
	$("#aspnetForm").validate({
		// This prevents validation from running on every
		//  form submission by default.
		onsubmit: false
	});

	// Search for controls marked with the causesValidation flag 
	//  that are contained anywhere within elements marked as 
	//  .form, and wire their click event up.
	$('.form .submit-button').click(ValidateAndSubmit);

	// Select any input[type=text] elements within a validation group
	//  and attach keydown handlers to all of them.
	$('.form :text').keydown(function (evt) {
		// Only execute validation if the key pressed was enter.
		if (evt.keyCode == 13) {
			ValidateAndSubmit(evt);
		}
	});
});
 
function ValidateAndSubmit(evt) {
	// Ascend from the button that triggered this click event 
	//  until we find a container element flagged with 
	//  .form and store a reference to that element.
	var $group = $(evt.currentTarget).parents('.form');

	var isValid = true;

	// Descending from that .form element, find any input
	//  elements within it, iterate over them, and run validation on 
	//  each of them.
	// Edit: changed the script to only have required fields validate due to a bug:
	// Read here http://encosia.com/2009/11/24/asp-net-webforms-validation-groups-with-jquery-validation#comment-37741
	// and here http://plugins.jquery.com/node/11803
	
	$group.find(':input.required').each(function (i, item) {
	if (!$(item).valid())
		isValid = false;
	});

	// If any fields failed validation, prevent the button's click 
	//  event from triggering form submission.
	if (!isValid)
	evt.preventDefault();
}

