// JavaScript Document
/*$.validator.setDefaults({
	submitHandler: function() { 
		//alert("submitted!"); 
		$(form).ajaxSubmit({
				target: "#result"
		});
			
		$('#result').show('slow');
	}
});*/
$.metadata.setType("attr", "validate");

$().ready(function() {
				   
		// show a simple loading indicator
		var loader = $('<div id="loader"><img src="images/loading.gif" border="0" alt="loading..." /></div>')
			/*.css({position: "relative", top: "1em", left: "25em"})*/
			.appendTo("#result")
			.hide();
		$().ajaxStart(function() {
			loader.show();
		}).ajaxStop(function() {
			loader.hide();
		}).ajaxError(function(a, b, e) {
			throw e;
		});
	
	$("#frmVolunteer").validate({
		rules: {
			txtFirstName: "required",
			txtLastName: "required",
			rdGender: "required",
			txtStreetAddress: "required",
			txtCity: "required",
			txtState: "required",
			txtZip: {
				required: true,
				number:true
			},
			txtPhone: {
				required: true
				
			},
			txtEmail: {
				required: true,
				email: true
			},
			rdHealthNeeds: "required",
			txtHNDescription: "required"
		},

		
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				target: "#result"
			});
			
			$('#result').show('slow');
			$('#frmVolunteer').resetForm();
		}
		
	});

$("#txtPhone").mask("(999)-999-9999");
	
	$("#result").click(function() {
			$('#frmVolunteer').resetForm();
			$('#result').hide('slow');
	});
	
	$("#btnSubmit").click(function() {
			$('#result').hide('slow');
	});						   
	
	var healthneedsY = $("#rdHealthNeeds_yes");
	var healthneedsN = $("#rdHealthNeeds_no");
	var inital = healthneedsY.is(":checked");
	var HNDescriptionInputs = $("#txtHNDescription").attr("disabled", !inital);
	
	//show when checked
	healthneedsY.click(function() {
		HNDescriptionInputs.removeAttr('disabled');
	});
	healthneedsN.click(function() {
		HNDescriptionInputs.attr('disabled', 'disabled');
	});
	
	function check () {
		var cnt=0;
		for(i=1;i<=5;i++) {
			
			if($("#chkShift"+i).is(":checked")) {
				cnt = cnt+1;
			}
			
		}
		return cnt;
	}
	
	$("#chkShift1,#chkShift2,#chkShift3,#chkShift4,#chkShift5").click(function() {
			
			var n = check();
			if (n >= 2) {
				for(i=1;i<=5;i++) {
					if(!$("#chkShift"+i).is(":checked")) {
						$("#shift_"+i).addClass('gray');
						$("#shift_"+i).find("input").attr("disabled","disabled");
						$("#shift_"+i+" label.error").hide();
						
					}
				}
			
			} else {
				for(i=1;i<=5;i++) {
					if(!$("#chkShift"+i).is(":checked")) {
						$("#shift_"+i).removeClass('gray');
						$("#shift_"+i).find("input").removeAttr('disabled');
					}
				}

			}
	});
});

