$(function(){
	//if (elem == document) elem = document.body;
  
	//$(".sessionMajors").hide();
	//$(".sessionDescription").hide();

	$("input:radio[name^=tour]").click(function(){
		var t = $(this).attr("name").substring(4);
		var p = "input:checkbox[name=programs" + t + "]";
		$(this).parent().siblings().children("label").children("input:checked").removeAttr("checked");
		$(this).parent().siblings(".sessionMajors").slideUp();
		$(this).parent().siblings().children(".sessionDescription").slideUp();
		$(this).parent().children(".sessionDescription").slideDown();
		$(this).parent().next(".sessionMajors").slideDown();
		if (t<3) {
			$("input[name=tour3]").removeAttr("checked");
			$("input[name=programs3]").removeAttr("checked");
			$("input[name=programs3]").parents(".sessionMajors").slideUp();
		}
		if (t==3) {
			for (i=1; i<=2; i++) {
			$("input[name=tour" +i+ "]").removeAttr("checked");
			$("input[name=programs" +i+ "]").removeAttr("checked")
			$("input[name=programs" +i+ "]").parents(".sessionMajors").slideUp();
			$("input[name=tour" +i+ "]").siblings(".sessionDescription").slideUp();
			}
		}
	});

	/*$("input:checkbox[name^=programs]").click(function(){
		var pname = $(this).attr("name");
		var t = $(this).parent().parent().prev().children("input.tour");

		if (t.val().substring(0,5) == "Round") {
			if ($("input[name="+pname+"]:checked").length > 2) {
				alert ("You may only select two round table sessions.");
				return false;
			}
		}
	});*/

	$("select.titleSelect").change(function(){
		if ($(this).val() == "Other") {
			//if ($(".titleSelectOther").len > 0) {
			$(".titleOther").show();
		} else {
			$(".titleOther").hide();
			$(".titleSelectOther").val("");
		}
	});


	$("#tours").validate({
		rules: {
			Event: "required",
			FirstName: "required",
			LastName: "required",
			SchoolDistrict: "required",
			Address: "required",
			City: "required",
			County: "required",
			State: "required",
			Phone: {
				required: true,
				phoneUS: true
			},
			Zip: {
				required: true,
				number: true,
				minlength: 5,
				maxlength: 5
			},
			Email: {
				email: true,
				required: true
			},
			Email2: {
				email: true,
				required: true,
				equalTo: "#email"
			},

			tour1: {
				required: function(element) {return ($('input[name=tour3]:checked').length == 0);}
			},
			tour2: {
				required: function(element) {return ($('input[name=tour3]:checked').length == 0);}
			},

			tour3: {
				required: function(element) {return ( ($('input[name=tour1]:checked').length + $('input[name=tour2]:checked').length) == 0);}
			},

			tour4: "required"

		},
		errorPlacement: function(error, element) {
			if (element.attr("name") == "tour1" || element.attr("name") == "tour2" || element.attr("name") == "tour1" )
				error.insertAfter(element.parent().prev());
			else if (element.attr("name") == "Email" || element.attr("name") == "Email2" )
				error.insertAfter("#email2");
			else
			   error.insertAfter(element);
		},

		highlight: function(element, errorClass) {
			$(element).css({'backgroundColor':'#ffc'});
		},
		unhighlight: function(element, errorClass) {
			$(element).css({'backgroundColor':'#fff'});
		}
	});

	//$("input:radio[name^=tour]:checked").parent().next(".sessionMajors").slideDown(); // for page reload or edit, show programs for already selected tours


});

