
function checkMail(name){
	var x = name;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (filter.test(x)){
		return true;
	}else{
		return false;
	}
}

function contactSubmitValidate(){
		check1 = false;
		check2 = true;
		check3 = false;
		check4 = false;
		$('#email').focus();

		$('.req').each(function(){
			$(this).blur(function(){
			var errorMsg = $(this).parent().find('.error');
			var Id = $(this).attr('id');
			var value = $(this).val();
			
			if(Id == "email" && value == ""){
				errorMsg.fadeIn();
				check1 = false;
			}else if(Id == "email" && value != ""){
				errorMsg.fadeOut();
				check1 = true;
			}else if(Id == "telephone" && value != "" && isNaN(value)){
				errorMsg.fadeIn();
				check2 = false;
			}else if(Id == "telephone" &&  value == "" || Id == "telephone" &&  value != "" && !isNaN(value)){
				errorMsg.fadeOut();
				check2 = true;
			}else if(Id == "message" && value == ""){
				errorMsg.fadeIn();
				check3 = false;
			}else if(Id == "message" && value != ""){
				errorMsg.fadeOut();
				check3 = true;
			}else if(Id == "name" && value == "" && checkMail(value)==false){
				errorMsg.fadeIn();
				check4 = false;
			}else if(Id == "name" && value != "" && checkMail(value)==true){
				errorMsg.fadeOut();
				check4 = true;
			}
	
			if (check1 == true && check2 == true && check3 == true && check4 == true){
				status = true;
			}else{
				status = false;
			}
		});		
	});
	
}

function contactSubmit() {
	if (status == true){
		var str = $('#contactForm').serialize();
		$('#error-message').fadeOut();
		$.ajax({
			type: "POST",
			url: '../cmd.php?action=contactSubmit',
			data: str,
			success: function(){
				$("#contactForm").ajaxComplete(function(event, request, settings){
					$('.sub-content-med, #contactForm').fadeOut(500, function(){
					$('.page-title').html('Thanks, I\'ve received your message.');
					result = '<p>I\'ll respond to you as soon as possible.<br/>Also just so you know I\'ve sent you a confirmation email to the email address you gave me.</p>';
					$('.sub-content-med').html(result);
					});
					$('.sub-content-med').removeClass('contact-content');
					$('.sub-content-med').slideDown();
				});
				
			}
		});
	}else{
		$('#error-message').fadeIn();
	}
}


