/*
Author: Njuguna Gathere, Tirus
*/

$(document).ready(function(){
	$('#targetSlider').html('');
	$('#targetContent ul').cycle({
		fx:'fade',
		speed:1000,
		delay:1000,
		pause:1,
		timeout:5000,
		pager:'#targetSlider'
	});

	$(function(){
		$('.error').hide();
		$(".button").click(function() {
			$('.error').hide();

			var name = $("input#name").val();
			if (name == "") {
				$("label#name_error").show();
				$("input#name").focus();
				return false;
			}
			var email = $("input#email").val();
			if (email == "") {
				$("label#email_error").show();
				$("input#email").focus();
				return false;
			}
			var msg = $("textarea#msg").val();
			if (msg == "") {
				$("label#msg_error").show();
				$("input#msg").focus();
				return false;
			}
			var capcha = $("input#captcha").val();
			if (capcha == "") {
				$("label#captcha_error").show();
				$("label#captcha_error").html("This field is required.");
				$("input#captcha").focus();
				return false;	
			}
			if (capcha != capch) {
				$("label#captcha_error").show();
				$("label#captcha_error").html("Human verification code is wrong.");
				$("input#captcha").focus();
				return false;
			}
			
			// $.get('includes/verify.php', { captcha: $('#captcha').val() }, function(data){
			// 	if (data === "ERROR") {
			// 		$("label#captcha_error").show();
			// 		$("label#captcha_error").html("the code you entered is wrong");
			// 		$("input#captcha").focus();
			// 		return false;
			// 	}else{
			// 		return true;
			// 	}
			// });
			

			var dataString = 'name='+ name + '&email=' + email + '&msg=' + msg;
			//alert (dataString);return false;

			$.ajax({
				type: "POST",
				url: "includes/process.php",
				data: dataString,
				success: function() {
					$('#contact_form').html("<div id='message'></div>");
					$('#message').html("<h2>Your message was successfully submitted!</h2>")
					.append("<p>Thank you and I will be in touch with you soon.</p>")
					.hide()
					.fadeIn(1500, function() {
						$('#message').append("");
					});
				}
			});
			return false;
		});
	});
});
