
$(document).ready(function() {

	//validate and submit
	$('#step1Form').validate({
		rules: {
			applicant_name: {
				required: true,
				minlength: 3
			},
			applicant_email: {
				required: true,
				email: true,
				minlength: 5
			},
			applicant_telephone: {
				required: true,
				minlength: 10
			}
		},
		messages: {
			applicant_name: {
				required: 'Please fill in your name.',
				minlength: $.format('At least {0} characters required!')
			},
			applicant_email: {
				required: 'Please fill in your email address.',
				minlength: $.format('At least {0} characters required!')
			},
			applicant_telephone: {
				required: 'Please fill in your telephone number.'
			}
		}
	});


});


function sendStep1() {
	
	$('#step1FormMessage').html('');
	$('#step1FormMessage').hide();
	window.status = '';
	
	if ($('#step1Form').valid()) {
		
		$('#step1FormMessage').html('Processing...');
		$('#step1FormMessage').show();
		
		$('#step1Form').ajaxSubmit({
								   
			url: '/includes/application_post.php',
			type: 'post',
			dataType: 'json',
			
			success: function(json) {
				
				$('#step1FormMessage').html('Reply received');
				var jsonData = eval(json); // IE6 and Safari compatibility
				if (jsonData['success'] == true || jsonData['success'] == 'true') {
					
					$('#step1FormMessage').html('Loading the next step...');
		
					var pageTracker = _gat._getTracker("UA-6722308-1");
					pageTracker._trackPageview('/insurance/apply-step2.php');
					
					$('#applicationModalContent').html('<img src="/images/global/rcs_loader.gif" title="loading..." align="middle" />');
					$('#applicationModalContent').load('/insurance/apply-step2.php'); 
				
				} else {
					$('#step1FormMessage').html(jsonData['error']);
				}
			}
		});
	} else {
		window.status = 'One or more of your fields have a validation error, please check what is filled in.';
	}
}