$(document).ready(function(){ 
	
	$("#psm_form").submit(function() {
			// create json object of form variables
			var formval = { content_id:$("#content_id").val(), oc_id:$("#oc_id").val(), from_name:$("#from_name").val(), from_email:$("#from_email").val(), to_name:$("#to_name").val(), to_email:$("#to_email").val(), comments:$("#comments").val(), create_UUID:$("#create_UUID").val() };

	content_id = $("#content_id").val();
	oc_id = $("#oc_id").val();
	
			// validate server side
			$.ajax({
				type: "POST",
				url: "/details_email_action.cfm",
				dataType: "json",
				data: formval,
				success: handleSuccess,
				error: handleError
			});				
		return false;
	});
 });

var content_id = "";
var oc_id = "";

//Handle a SUCCESS on ajax call
var handleSuccess = function(response){
	if (response.from_name == "true")
		{ $("#msg_name").text("Required").show().fadeOut(1000); }						
	if (response.from_email == "true")
		{ $("#msg_email").text("Required").show().fadeOut(1000); }	
	if (response.to_name == "true")
		{ $("#msg_name_colleague").text("Required").show().fadeOut(1000); }						
	if (response.to_email == "true")
		{ $("#msg_email_colleague").text("Required").show().fadeOut(1000); }
	// content id is not valid
	if (response.content_id == 0)						
		{ $('#content-main').html('<h1>Thank you</h1><p>Unfortunately, the article could not be sent. Please try again.</p><p><a href="/index.cfm">Return to homepage.</a></p>') }
			
	if ((response.content_id > 0) && (response.from_name == "false") && (response.from_email == "false") && (response.to_name == "false") && (response.to_email == "false"))
		{	
		var urlString = '';
		if (response.is_crosswalk == 0){
			urlString += response.short_title + '/' + response.oc_id + '-' + response.content_id;
		} else {
			urlString += 'crosswalk/' + response.short_title + '/' + response.content_id;
		}
		
		// html for thank you message
		//var HTMLstring = '<h1>Thank you</h1><p>Your e-mail has been sent. Thank you for referring one of our articles to a colleague.</p><p><a href="/' + response.short_title + '/' + response.oc_id + '-' + response.content_id + '">Back to the article</a></p>';	
		var HTMLstring = '<h1>Thank you</h1><p>Your e-mail has been sent. Thank you for referring one of our articles to a colleague.</p><p><a href="/' + urlString + '">Back to the article</a></p>';		
		$('#content-main').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><p><a href="/' + error.short_title + '/' + oc_id + '-' + content_id + '">Back to the article</a></p>';
	$('#content-main').html(HTMLstring);
}

