﻿/*
 *	Contact Form Validation
 *	This script is used for the contact form.
*/

$(document).ready(function(){  

	$('#cForm').submit(function() {
	
		// Disable the submit button
		$('#cForm input[type=submit]')
			.attr('value', 'Mesajınız Gönderiliyor...')
			.attr('disabled', 'disabled');
	
		// AJAX POST request
		$.post(
			$(this).attr('action'),
			{
				name:$('#posName').val(),
				email:$('#posEmail').val(),
				message:$('#posText').val()
			},
			function(errors) {
				// No errors
				if (errors == null) {
					$('#cForm')
						.hide()
						.html('<h3>Teşekkürler</h3><p>En kısa zamanda cevap verilecektir</p>')
						.show();
				}
	
				// Errors
				else {
					// Re-enable the submit button
					$('#cForm input[type=submit]')
						.removeAttr('disabled')
						.attr('value', 'Gönder');
	
					// Technical server problem, the email could not be sent
					if (errors.server != null) {
						alert(errors.server);
						return false;
					}
	
					// Empty the errorbox and reset the error alerts
					$('#cForm .errorbox').html('<ul></ul>').show();
					$('#cForm input').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) {
							$('#cForm .errorbox ul').append('<li>' + errors[field] + '</li>');
						}
					}
				}
			},
			'json'
		);
	
		// Prevent non-AJAX form submission
		return false;
	});

});