/* CONTACT PAGE FUNCTIONS */

function sendcontact(){

	var formID = "contactform";
	
	$(".error").remove();
	var error = false;
	
	$(".status").html("").removeClass("statuserror");

	// check required fields
	$("#"+formID+" .required").each(function(){
		
		var id = $(this).attr("id");
		var label = $("label[for='"+id+"']").html();
		label = label.replace(":", "");

		var tval = $.trim($(this).val());
		
		if (tval == ""){
			$(this).addClass("fielderror");
			error = true;
		} else {
			$(this).removeClass("fielderror");
		}
	
	});
	
	var email = $("#"+formID+" #inemail").val();
	//console.log(email);
	
	if (!validemail(email)){
		$("#"+formID+" #inemail").addClass("fielderror");
		$(".status").addClass("statuserror").html("Please enter a valid email address!");
		return;
	}
	

	if (error === false){

		var qurl = "/send_contact.php";
		var querystring = $("#contactform").serialize();
		
		$(".status").html("Sending message...");

		$.ajax({ type:"POST", url:qurl, data:querystring, complete: function(e){
			var result = $.trim(e.responseText);
			//console.log(e.responseText);
			
			if (result != ""){
				//console.log(result);
				$(".status").addClass("statuserror").html("There was an error sending your message. Please try again in a few minutes.");
				return;
			} else {
				$("#contactform").fadeOut("500",function(){
					$(this).html("Your message has been sent!").fadeIn("500");
				});

			}
		}});
		
	} else {
		$(".status").addClass("statuserror").html("Please fill in all of the required fields!");
		return;
	}

}

function validemail(email){
	var str = email;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str)){
		return true;
	} else {
		return false;
	}
}