jQuery(document).ready(function() {
	
$("#submitted").hide().attr("src", "http://www.sageportfoliogroup.com/img/global/green-loader.gif");
$("#thank-you-msg").hide();

var emailPat = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

// convert text area multiline into single line separated by "; "
function killNewline(string) {
	while (string.indexOf("\n") > 0) {
		string = string.replace(/\n/, "; ");
	}
	return string;
}

$("#contact-submit").click(function() {
	
	var req = $("#required-text").val();
	if (req == "") {
		
		$("#submitted").show();
	
		// getting field values
		var contact_fname = $("#contact-first-name").val();
		var contact_lname = $("#contact-last-name").val();
		var contact_email = $("#contact-email").val();
		var contact_message = $("#contact-message").val();
		var contact_phone = $("#contact-phone").val();
		var contact_message2 = killNewline(contact_message);
		
		// checkboxes will be passed as a string separated by commas
		var interests = "";
		if($("#interest-executive-coaching").attr("checked") == true) {
			interests = interests + "Executive Coaching,";
		}
		if($("#interest-sales-coaching").attr("checked") == true) {
			interests = interests + "Sales Coaching,";
		}
		if($("#interest-leadership-training").attr("checked") == true) {
			interests = interests + "Leadership Training,";
		}
		if($("#interest-sales-training").attr("checked") == true) {
			interests = interests + "Sales Training,";
		}
		if($("#interest-hiring-support").attr("checked") == true) {
			interests = interests + "Hiring Support,";
		}
		if($("#interest-team-building").attr("checked") == true) {
			interests = interests + "Team Building,";
		}
		if($("#interest-strategic-planning").attr("checked") == true) {
			interests = interests + "Strategic Planning,";
		}
		if(interests=="") {
			interests = "none";
		} else {
			// delete the last comma
			interests = interests.substring(0, (interests.length -1) );
		}
	
		// the email address is NOT valid
		if(!emailPat.test(contact_email)) {
			$("#submitted").hide();
			alert("The email address is not valid");
			$("#contact-first-name").focus();
			return false;
			
		// either field is empty	
		} else if (contact_fname == "" || contact_lname == "" || contact_message == "" || contact_phone == "") {
			$("#submitted").hide();
			$("#contact-first-name").focus();
			alert("All the fields are required");
			return false;
		
		// else inputs are valid
		} else {
			
			$.ajax({
				type: 	"POST",
				url: 	"/php/spgMail.php",
				data: 	"fname=" + contact_fname + 
						"&lname=" + contact_lname + 
						"&email=" + contact_email +
						"&phone=" + contact_phone +
						"&interests=" + interests +
						"&message=" + contact_message2,
				success: function () {
					$("#submitted").hide();
					$("#thank-you-msg").show("slow");
					
					// clear form and hide message
					setTimeout(function() {
						$("#thank-you-msg").hide();
						$("#contact-first-name").val("");
						$("#contact-last-name").val("");
						$("#contact-email").val("");
						$("#contact-phone").val("");
						$("#contact-message").val("");
						$("#interest-executive-coaching").attr("checked", false);
						$("#interest-sales-coaching").attr("checked", false);
						$("#interest-leadership-training").attr("checked", false);
						$("#interest-sales-training").attr("checked", false);
						$("#interest-hiring-support").attr("checked", false);
						$("#interest-team-building").attr("checked", false);
						$("#interest-strategic-planning").attr("checked", false);
					}, 10000);
					
				},
				async: false
			});
			
		}
		
	} // end if "required"
		
}); // end contact submit function
	
}); //end $ function