$(function() {
		   
	$('.error').hide();
	$('#formContainer').show();

	// Handle Highligh and Outline of Input Fields
	$('input.text-input, input.select-input, textarea.text-input').focus(function(){
		$(this).addClass("inputOutline");
		$(this).parents('.rowContainer').addClass("inputHighlight");
	});
	$('input.text-input, input.select-input, textarea.text-input').blur(function(){
		$(this).removeClass("inputOutline");
		$(this).parents('.rowContainer').removeClass("inputHighlight");
	});

	$(".button").click(function() {

		$('input.text-input, input.select-input, textarea.text-input').removeClass("inputOutline");
		$('input.text-input, input.select-input, textarea.text-input').parents('.rowContainer').removeClass("inputHighlight");

		// validate and process form
		// first hide any error messages
    	$('.tooltipContainer').hide();

		bErrFound = false;
		
		// First Name
		var name = $("input#txtFirstname").val();
		if (name == "") {
			$(".tooltipContainer#txtFirstnameErr").show();
			// Only set focus to first error
			if(!bErrFound) {
				$("input#txtFirstname").focus();
			}
			bErrFound = true;
		}
		
		// Email
		
		//$result = true;
		$name = $("input#txtEmail").val();
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		if(!pattern.test($name) || $name=="") {
			$(".tooltipContainer#txtEmailErr").show();
			// Only set focus to first error
			if(!bErrFound) {
				$("input#txtEmail").focus();
			}
			bErrFound = true;
		};
		
		// Comments
		var name = $("textarea#txtComments").val();
		if (name == "") {
			$(".tooltipContainer#txtCommentsErr").show();
			// Only set focus to first error
			if(!bErrFound) {
				$("textarea#txtComments").focus();
			}
			bErrFound = true;
		}

		// Return false if Errors Found
		if(bErrFound){
			return false;
		}


		//
		// Build a data string to send to email and DB
		//
		
		//alert('before data string');

		var sFirstName = $("input#txtFirstname").val();
		var sLastName = $("input#txtLastname").val();
		var sEmail = $("input#txtEmail").val();
		var sPhone = $("input#txtPhone").val();
		var sLeadSource = $("input#txtLeadSource").val();
		var sComments = $("textarea#txtComments").val();

		var dataString = 'firstname='+ sFirstName + '&lastname=' + sLastName + '&email=' + sEmail + '&phone=' + sPhone + '&leadsource=' + sLeadSource + '&comments=' + sComments;

		$.post( "formcontactemail.asp", {firstname: sFirstName, lastname: sLastName, email: sEmail, phone: sPhone, leadsource: sLeadSource, comments: sComments}, function(){} );

		// Post to the DB

		//alert (dataString);return false;
		
		$.ajax({
			type: "POST",
			url: "formcontactprocess.php",
			data: dataString,
			success: function() {
				$('#contact-form').html();
				$('form').clearForm();
				$('input[id=txtLeadSource]:first').attr("checked","checked");
				$('#complete-message')
					.fadeIn(1500, function() {
						$('#message').append("<img id='checkmark' src='images/check.png' />");
					});
      		}
     	});
		
    	return false;
		
	}); // End .button.click()
	
});


$.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' || type == 'password' || tag == 'textarea')
		this.value = '';
	  else if (type == 'checkbox' || type == 'radio')
		this.checked = false;
	  else if (tag == 'select')
		this.selectedIndex = -1;
	});
};

$(document).ready(function() {
	$('.formContainer').click(function(event) {
		$('#complete-message').hide();
	});
});


