

$(document).ready(function(){
	
if($.browser.version !="6.0"){
  //  Initialize Backgound Stretcher	   
		$(document).bgStretcher({
			images: ['/images/bg.jpg'],
			imageWidth: 950, imageHeight: 650,slideShow:false
		});
	}
				$('#toggleAnimation').click(function(){
	       if($(this).val() == "Pause Animation"){
	           $(this).val("Resume Animation");
	           $(document).bgStretcher.pause();
	       } else {
	           $(this).val("Pause Animation");
	           $(document).bgStretcher.play();
	       }
		});


	$("#homecontact_form").validate({
		errorLabelContainer: $("#homecontact_form div.hideerror"),
			onkeyup: false
	});

	$("#enquiry_form").validate({
		onkeyup: false,
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
            if (errors) {
            	var message = errors == 1
                	? 'You missed 1 field. It has been highlighted'
                    : 'You missed ' + errors + ' fields. They have been highlighted';
                    alert(message);
			}
		}
	});
	
	
	$("#subscriber-rego-form").validate({
		rules: {
			firstname: "required",
			surname: "required",
			postcode: { required: true, minlength: 4},
			email_address: { required: true, email: true},
			
			privacy: "required"
		},
		errorContainer: "#errorContainer",
		submitHandler: function(form) {
			if($("#subscriber-rego-form").valid()) 
				form.submit();
		}		
	});	

	
	$("#volunteer-rego-form").validate({
		rules: {
			firstname: "required",
			surname: "required",
			contact_number: { required: true, minlength: 10},
			email_address: { required: true, email: true},
			skill_other: { skillEntered: true },
			rollhidden: { rollEntered: true},
			timehidden: { timeEntered: true},

			privacy: "required"
		},
		errorContainer: "#errorContainer",
		submitHandler: function(form) {
			if($("#volunteer-rego-form").valid()) 
				
				form.submit();
		}		
	});	
	
	
		
	//custom validation rule for skills
	$.validator.addMethod("rollEntered", 
		function(value, element) {
			var valid = false;
			$(".rollcheckbox").each(function() {
				if(this.checked == 1){valid = true;}
				});			
			return valid;

		}, "Please tick or enter a role"
	);
  	
	
	//custom validation rule for skills
	$.validator.addMethod("timeEntered", 
		function(value, element) {
			var valid = false;
			$(".timecheckbox").each(function() {
				if(this.checked == 1){valid = true;}
				});			
			return valid;

		}, "Please select your preferred time"
	);

	
	
	//custom validation rule for skills
	$.validator.addMethod("skillEntered", 
		function(value, element) {
			var valid = false;
			$(".skill-checkbox").each(function() {
				if(this.checked == 1)
					valid = true;
			});
			if(valid == false) {
				var spacefix = / /gi;
				var otherBox = $("#skill_other").val()+"";
				if(otherBox.replace(spacefix, "") != "" && $("#skill_other").val() != "Other (Please Specify)")
					valid = true;
				else {
					$("#skill_other").val("Other (Please Specify)");
				}
			}
			return valid;
		}, "Please tick or enter a skill"
	);
  	
	// Select all text in box ready to override
	$("#skill_other").focus(function() { 
		if($(this).val() == "Other (Please Specify)")
			$(this).val("");
	});
	$("#skill_other").keypress(function() {	$(this).removeClass("error"); });
});