$(document).ready(function(){
	
	jQuery.validator.addClassRules("requiredIfJoint", {
		required: function(element) {
			return $("#jointapp").attr('checked');
		}
	});
	
	jQuery.validator.addClassRules("requiredIfAnyDateSelected", {
		required: function(element) {
			var req = false;
			$("select option:selected", $(element).parent()).each(function(i,elm){
				if ($(elm).val()!='') {
					req = true;
					console.log('here');
					return false;
				}
			});
			return req;
		}
	});
	
	$('#indapp').click(function(){
		$('.coapplicant').hide();
	});
	
	$('#jointapp').click(function(){
		$('.coapplicant').show();
	})
	
	$("#indapp").attr('checked', 'checked');
	$('.coapplicant').hide();
	
	$("#applyonline").validate({
		rules: {
			'other[bankruptcydate][Date_Day]': {
				required: function(element) {
					return $("#bankruptcy option:selected").val() == 'Yes';
				}
			},
			'other[bankruptcydate][Date_Month]': {
				required: function(element) {
					return $("#bankruptcy option:selected").val() == 'Yes';
				}
			},
			'other[bankruptcydate][Date_Year]': {
				required: function(element) {
					return $("#bankruptcy option:selected").val() == 'Yes';
				}
			},
			'other[othercreditname]': {
				required: function(element) {
					return $("#othercredit option:selected").val() == 'Yes';
				}
			},
			'applicant[0][confirmemail]': {
				equalTo: "#app0email"
			},
			'applicant[1][confirmemail]': {
				equalTo: "#app1email"
			}
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function (response) {
					$('#applyonline').fadeOut('slow', function(){$('#applyonlineContainer').html(response)});
				},
				type: 'post'
			});
		}
	});
});
