$(document).ready(function(){
	if($('#forename')) {
		showChildren(false);
		lockDateFields();
		
		jQuery.validator.addMethod("childRequired", function(value, element) {
			var i = parseInt(element.id.substring(element.id.length - 1));
			var checkChildren = parseInt(childrenShown);
			if (i <= checkChildren) {
				return !this.optional(element);
			} else {
				return this.optional(element);
			}
		}, "");
		
		$("#infoPackForm").validate({
			rules: {
				'parent[forename]': "required",
				'parent[surname]': "required",
				'parent[city]': "required",
				'parent[address1]': "required",
				'parent[country]': "required",
				'parent[phone]': "required",
				'parent[more_info]': "required",
				'parent[email]': {
					required: true,
					email: true
				}
			},
			messages: {
				'parent[forename]': "",
				'parent[surname]': "",
				'parent[more_info]': "",
				'parent[email]': {
	                required: "",
	                email: ""
	            },
	            'parent[phone]': "",
	            'parent[country]': "",
	            'parent[city]': "",
	            'parent[address1]': ""
	        }//,
	      //  success: function(label) {
	       //     label.html(" ").addClass("checked");
	      //  }
	        /*errorPlacement: function(error, element) {
	            if ( element.is(":radio") )
	                error.appendTo( element.parent().next().next() );
	            else if ( element.is(":checkbox") )
	                error.appendTo ( element.next() );
	            else
	                error.appendTo(element.parent());
	        }, 
			submitHandler: function() {
	            alert("submitted!");
	        }*/
		});
	}
});

var maxChildren = 5;
var childrenShown = 0;

function showChildren(effects) {
	if (effects) {
		var speed = 'slow';
	} else {
		var speed = '';
	}

	childrenShown = $('#num_children').val();
	var childDivs = $("div.childDetails");

	for (var i = 1; i <= maxChildren; i++) {
		var targetDiv = "#childDetails" + i;

		if (i <= childrenShown) {
			$(targetDiv).show(speed);
		} else {
			$(targetDiv).hide(speed);
		}
	}
	
	//$("form").valid();
}

function lockDateFields() {
	var fields = ['#child_dob1', '#child_dob2', '#child_dob3', '#child_dob4', '#child_dob5',
	              '#child_start1', '#child_start2', '#child_start3', '#child_start4', '#child_start5'];

	for (i in fields) {
		$(fields[i])[0].readOnly = true;
	}
}
