
$(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: '<br>Please fill in your name.',
				minlength: $.format('At least {0} characters required!')
			},
			applicant_email: {
				required: '<br>Please fill in your email address.',
				minlength: $.format('At least {0} characters required!')
			},
			applicant_telephone: {
				required: '<br>Please fill in your telephone number.'
			}
		}
	});


	/*//set the onclick event for applications
	$('.howModal').click(function (e) {
				e.preventDefault();
				$('#howToModalContent').modal({
											containerId: 'howToContainer'	
				});
	});*/

});


function sendStep1() {
	
	$('#step1FormMessage').html('<img src="/images/global/rcs_loader.gif" title="loading..." align="middle" />');
	$('#step1FormMessage').hide();
	window.status = '';
	
	if ($('#step1Form').valid() == true) {
		
		$('#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('/cards/apply-step2.php');
					
					$('#applicationModalContent').html('<img src="/images/global/rcs_loader.gif" title="loading..." align="middle" />');
					$('#applicationModalContent').load('/cards/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.';
	}
}

function loadHowModal() {
	$('#application-form').html('<img src="/images/global/rcs_loader.gif" title="loading..." align="middle" />');
	$('#application-form').load('/cards/how-to-open.php');
	//$('#howToModalContent').modal({containerId: 'howToContainer'});	

}

function LoadStep1() {
	$('#application-form').html('<img src="/images/global/rcs_loader.gif" title="loading..." align="middle" />');
	
	var browserName=navigator.appName; 

	$('#application-form').load('/cards/apply-step2.php');

	/*
 	if (browserName == "Microsoft Internet Explorer"){
	}
	else{
		// this only works for firefox
		$.modal.close();
		$('#applicationModalContent').modal({containerId: 'applicationContainer'});
	} 
	*/ 

}
