﻿$(document).ready(function(){
	
	$('#reset').click(function() {
	
		$(':input', form).each(function() {
		
		    var type = this.type;
		    
		    var tag = this.tagName.toLowerCase();
		    
		    if (type == 'text' || tag == 'textarea')
		    	this.value = "";
		    	
		});
	});
	
	$("#contact-form").submit(function() {
		
		$("input").css({ backgroundColor: '#111'});
			
		// check if the value for name is not empty or not equal to default value
		var name = $("input#name").val();
		if (name == "" || name =="Name") {
			$("input#name").css({ backgroundColor: '#ffffbb'});
			$("input#name").focus();
			return false;
		}

		// check if the value for email is not empty or not equal to default value
		var email = $("input#email").val();
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;	
		
		if (email == "" || email =="Email") {
			$("input#email").css({ backgroundColor: '#ffffbb'});
			$("input#email").focus();
			return false;
		}
		
		// test if the value for email is a valid email address
		else if(!emailReg.test(email)) {
			$("label#email_error2").fadeIn('slow');
			$("input#email").css({ backgroundColor: '#ffffbb'});
			$("input#email").focus();
    		return false;
		}
		
		// check if the value for subject is not empty or not equal to default value
		var subject = $("input#subject").val();
		if (subject == "" || subject =="Subject") {
		    $("input#subject").css({ backgroundColor: '#ffffbb'});
		    $("input#subject").focus();
			return false;
		}
		
		// check if the value for message is not empty or not equal to default value
		var message = $("textarea#message").val();
		if (message == "" || message =="Message") {
		    $("textarea#message").animate({ backgroundColor: '#ffffbb'}, 400);
			$("textarea#message").focus();
			return false;
		}		
	
		var str = $(this).serialize();

		$.ajax({
			type: "POST",
			url: "php/contact.php",
			data: str,
			
			success: function(msg){
			
				$("#note").ajaxComplete(function(event, request, settings){
					$("#note").show();
					if(msg == 'Your message has been sent. I will be in touch soon.'){
						result = '<span class="notification_ok">Your message has been sent. We will be in touch soon.</span>';
						$("#fields").hide();
					} 
					else {
						result = msg;
					}
					$(this).html(result);
				});
			}
		});
		
		return false;
	
	});
	
});
