$(document).ready(function(){
	$("#submit").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var emailToVal = $("#email").val();
		var phoneVal = $("#phone").val();
		
		if(phoneVal == '') {
			$("#phoneerror").replaceWith('<div class="error2" id="phoneerror">Please enter your phone number.</div>');
			hasError = true;
		}

		var nameVal = $("#name").val();
		if(nameVal == '') {
			$("#nameerror").replaceWith('<div class="error2" id="nameerror">Please enter your name.<br /></div>');
			hasError = true;
		}

		if(phoneVal != '') {
			$("#phoneerror").replaceWith('<div class="error2" id="phoneerror"></div>');
		}

		if(nameVal != '') {
			$("#nameerror").replaceWith('<div class="error2" id="nameerror"></div>');
		}
		
		var messageVal = $("#message").val();
		if(messageVal == '') {
			$("#messageerror").replaceWith('<div class="error" id="messageerror">You forgot to enter the message.</div>');
			hasError = true;
		}
		
		var cityVal = $("#city").val();
		var provinceVal = $("#province").val();
		var worktypeVal = $("#worktype").val();
		var subjectVal = $("#subject").val();
		var recipientsVal = $("#recipients").val();

		if(worktypeVal == '-1') {

			$("#worktypeerror").replaceWith('<div class="error" id="worktypeerror">Please choose an area of inquiry.</div>');
		

			hasError = true;
		}

		if(hasError == false) {
			$(this).hide();
			$("#worktypeerror").replaceWith('<div class="error" id="worktypeerror"></div>');
			$("#messageerror").replaceWith('<div class="error" id="messageerror"></div>');
			$("#nameerror").replaceWith('<div class="error" id="nameerror"></div>');
			$("#phoneerror").replaceWith('<div class="error" id="phoneerror"></div>');
			$("#sendEmail li.buttons").append('<img src="images/ajax-loader.gif" alt="Loading" id="loading" />');
			
			$.post("formmail.php",
   				{ recipients: recipientsVal, subject: subjectVal, Name: nameVal, Email: emailToVal, PhoneNumber: phoneVal, City: cityVal, Province: provinceVal, WorkType: worktypeVal, message: messageVal },
   					function(data){
						$("#sendEmail").slideUp("normal", function() {				   
							
							$("#successful").replaceWith('<div class="notes"><h4>Thank You</h4><p class="last">Your request was received successfully. We will reply as soon as possible.</p></div>');

	$("#loading").replaceWith('<div id="loading"></div>');
																	
						});
   					}
				 );
		}
		
		return false;
	});						   
});
