/*
 * 	Contact Form Validation v1.0
 *	By Simon Bouchard <www.simonbouchard.com>
 *	You need at least PHP v5.2.x with JSON support for the live validation to work.
 */

function submitForm() {
	
	$('#contactForm').submit(function() {
		
		// Disable the submit button
		$('#contactForm input[type=submit]')
			.attr('value', 'отправлено')
			.attr('disabled', 'disabled');

		// AJAX POST request
		$.post(
			$(this).attr('action'),
			{
				name:$('#name').val(),
				email:$('#email').val(),
				message:$('#message').val()
			},
			function(errors) {
				// No errors
				if (errors == null) {
					$('#contactForm').hide().html('<h3>Спасибо.</h3><p>Ваше сообщение отправлено.</p>').show();
					
					Cufon.replace('#content-holder h1, #content-holder h2, #content-holder h3, #content-holder h4, #content-holder h5, #content-holder h6', {
					
						hover: true
					
					});
					
					$('.alert').removeClass('alert');
				}

				// Errors
				else {
					// Re-enable the submit button
					$('#contactForm input[type=submit]')
						.removeAttr('disabled')
						.attr('value', 'Submit');

					// Technical server problem, the email could not be sent
					if (errors.server != null) {
						$('.alert').removeClass('alert');
						alert(errors.server);
						return false;
					}

					// Empty the errorbox and reset the error alerts
					$('.alert').removeClass('alert');

					// Loop over the errors, mark the corresponding input fields,
					// and add the error messages to the errorbox.
					for (field in errors) {
						if (errors[field] != null) {
							$('#' + field).addClass('alert');
						}
					}
				}
			},
			'json'
		);

		// Prevent non-AJAX form submission
		return false;
	});

}
