$(document).ready(function() { 
	var options = { /*target: '#alert',*/
					beforeSubmit:  validateContactForm,  // pre-submit callback 
					success:       showResponse,  // post-submit callback
					error:         showError,     // error callback
			 		url:           'mailman.php?action=send&method=ajax',
					clearForm:     false
	}; 
	$('#contactForm').ajaxForm(options); 
}); 

$.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;
  });
};

function showResponse(text){
	if(text == 'error'){
		$("#alerts").html('<div class="error">There was a problem sending the message. Please try again.</div>').stop().animate({ height: '37px'}, 'slow', 'easeOutQuad');
	}else if(text == 'success'){
		$("#alerts").html('<div class="success">Your message was sent successfully!</div>').stop().animate({ height: '37px'}, 'slow', 'easeOutQuad');
	}
}
			
function validateContactForm(){
				
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var colorRed = '#FFC6C6';
	
	if($("#txtName").val() == ''){
		resetFormCSS();
		$("#txtName").css('background-color', colorRed);
		$("#txtName").focus();
		return false;
	}else if($("#txtEmail").val() == ''){
		resetFormCSS();
		$("#txtEmail").css('background-color', colorRed);
		$("#txtEmail").focus();
		return false;
	}else if($("#txtMessage").val() == ''){
		resetFormCSS();
		$("#txtMessage").css('background-color', colorRed);
		$("#txtMessage").focus();
		return false;
	}

	return true;
}

function resetFormCSS(){
	$("#txtName").css('background-color', '');
	$("#txtEmail").css('background-color', '');
	$("#txtMessage").css('background-color', '');
}

function showError(){
	$("#alerts").html('<div class="error">There was a problem sending the message. Please try again.</div>');
}

