jQuery(function($) {
	
	// Clear Form on Submit
	$.fn.clearForm = function() {
	  return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
		  return $(':input',this).clearForm();
		if (type == 'text' || tag == 'textarea')
		  this.value = '';
	  });
	};

	// Contact Form 
  $('#contactForm').submit(function() {
  	// Form Variables
		var errors = 0;
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;		
  	// Remove Error Class
  	$('input, textarea').removeClass('validation-failed');
  	
  	// Validate Name
  	if ($("#cname").val() == "") {
	  	$("#cname").addClass("validation-failed");
	  	errors++;
	  }			
		// Validate Email
		if(!reg.test($("#cemail").val())) {
      $("#cemail").addClass("validation-failed"); 
      errors++;
    }
    // Validate Comments
    if ($("#ccomments").val() == "") {
	  	$("#ccomments").addClass("validation-failed");
	  	errors++;
	  }
	  if (errors !== 0) {
	   $('#thank_you').html('There was an error with your message. Please try again.').slideDown(500);
			return false;
    }
    // Ajax Submit
    jQuery.ajax({
      data: $(this).serialize(),
      url: this.action,
      timeout: 2000,
      error: function() {
        console.log("Failed to submit");
      },
      success: function() {
        $('#thank_you').html('<strong>Thank You!</strong><br />I\'ll be in touch soon.').slideDown(500);
        $('#contactForm').clearForm();
      }
    })
    return false;
  })
  // End Contact Form
})