/*
 * contact.js
 * Copyright (C) 2009
 * Author: Vanity Stuurland <vanity.stuurland@lightmaker.com>
 * Created: 2009-11-25
 */

$(document).ready(function()
{
	// Hide the errormessages from the required input fields.	
	$("#nameFieldError").hide();
	$("#emailFieldError").hide();
	$("#phoneFieldError").hide();
	$("#messageFieldError").hide();
	$('.contactMessage').hide();
	
	//Handler Event for clicking submit button.
	$(".contactSubmitBtn").click(function(event)
	{
				
			//For matching email and phone number.
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			var phoneReg = /^\+?[0-9]+\-?\s*\(?[0-9]+\)?[0-9]+\s*[0-9]+\s*[0-9]+\s*[0-9]+\s*[0-9]+\s*[0-9]+$/;

			
			//Making variables of the id's from the input fields.
			var name = $("#nameField").val();
			var email = $("#emailField").val();
			var phone = $("#phoneField").val();
			var website = $("#websiteField").val();
			var message = $("#messageField").val();
			var voorkeur = $("#voorkeurField").val();
			var subscribe = $("#subscribeField").val();
			
			var hasError = false;
			
			
			//Trimming the spaces from the input fields.
			$('input, textarea').each(function(){
				$(this).val(jQuery.trim($(this).val()));
			});

			
			//Checks if someone add their name to the input field.
			if(name == ""){
				$("#nameFieldError").show();
				$("#nameField").focus();
				$("#nameField").animate({marginTop: '4px'}, 500);
				$("#emailField").animate({marginTop: '4px'}, 500);
				$(".contactFillBottom").animate({marginTop: '10px'}, 500);
				hasError = true;
			} else {
				$("#nameFieldError").hide();
			}
			
			
			//Checks if someone add their email the right way.
			if(email == ""){	
				$("#emailFieldError").show();
				$("#emailField").focus();
				$("#nameField").animate({marginTop: '4px'}, 500);
				$("#emailField").animate({marginTop: '4px'}, 500);
				$(".contactFillBottom").animate({marginTop: '10px'}, 500);
				hasError = true;
			} else if (!emailReg.test(email)){
				$("#emailFieldError").show();
				$("#emailField").focus();
				$("#nameField").animate({marginTop: '4px'}, 500);
				$("#emailField").animate({marginTop: '4px'}, 500);
				$(".contactFillBottom").animate({marginTop: '10px'}, 500);
				hasError = true;
			} else {
				$("#emailFieldError").hide();
			}
			
			
			//Checks if someone add their phone number the right way.
			if(phone != ""){	
				if(!phoneReg.test(phone)){
					$("#phoneFieldError").show();
					$("#phoneField").focus();
					$("#websiteField").animate({marginTop: '15px'}, 100);
					hasError = true;
				} else {
					$("#phoneFieldError").hide();
					$("#websiteField").animate({marginTop: '0px'}, 100);
				}
			}
			
			//Checks if someone add their message to the input field.
			if(message == ""){
				$("#messageFieldError").show();
				$("#messageField").focus();
				hasError = true;
			} else {
				$("#messageFieldError").hide();
			}
			
			
			
			if (name != "" && email != "") {
				$("#nameField").animate({marginTop: '0px'}, 500);
				$("#emailField").animate({marginTop: '0px'}, 500);
				$(".contactFillBottom").animate({marginTop: '0px'}, 500);
			}
			
			
			//checks if everyting is filled in the right way and send the data to the php file
			/*if (hasError == false)
			{
				
				$.post(global_stylesheet_directory + "/mail.php",
				{
					name: $("input[name='name']").attr("value"),
					email: $("input[name='email']").attr("value"),
					phone: $("input[name='phone']").attr("value"),
					website: $("input[name='website']").attr("value"),
					message: $("textarea[name='message']").attr("value"),
					voorkeur: $("input:radio[name='voorkeur']").attr("value"),
					subscribe: $("input:radio[name='subscribe']").attr("value")
				}
				
				);


			}*/

			
			return !hasError;
	
	});
});