$(document).ready(function(){ 
	
	$("#psm_form").submit(function() {
			// create json object of form variables
			var formval = { talkgroup:$("#talkgroup").val(), fname:$("#fname").val(), lname:$("#lname").val(), email:$("#email").val() };

			// validate server side
			$.ajax({
				type: "POST",
				url: "talk_action.cfm",
				dataType: "json",
				data: formval,
				success: handleSuccess,
				error: handleError
			});				
		return false;
	});
 });


//Handle a SUCCESS on ajax call
var handleSuccess = function(response){
	if ((response.fname == "true") || (response.lname == "true"))
		{ $("#msg_name").text("First and Last Name Required").show().fadeOut(4000); }
	if (response.email == "true")
		{ $("#msg_email").text("Valid Email Address Required").show().fadeOut(4000); }	
		
	if ((response.fname == "false") && (response.lname == "false") && (response.email == "false"))
		{						
		// html for thank you message
		var HTMLstring = '<h1>Thank you</h1><p>Your request has been received and is being processed.</p>';						
		$('#email_form').html(HTMLstring);
		}
}

//Handle an ERROR on ajax call
var handleError = function(error){
		var HTMLstring = '<h1>Error</h1><p>There has been an error with your request.</p><p>Contact the help desk</p>';
	  $('#email_form').html(HTMLstring);
}
