$(document).ready(function(){ 	
	refreshEmailForm();
 });

var refreshEmailForm = function() {
	$('#email-form').html(emailFormHtml);	
	$("#psm_form_crosswalk").submit(sendEmail);
	//$("#emailformclose").click(closeEmailForm);	
	$("#emailformclose").click(function(){	
		closeEmailForm();
		return false;
	});	
	
	
}

var sendEmail = function() {
	// create json object of form variables
	var isCrosswalk = 1;
	var formval = { content_id:$("#form_content_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(),
				col_topic_id:$("#form_col_topic_id").val()};
	// validate server side
	$.ajax({
		type: "POST",
		url: "/details_email_action.cfm",
		dataType: "json",
		data: formval,
		error: handleEmailError,
		success: handleEmailAction
	});				
	return false;
}

var handleEmailAction = function(response){					
	if (response.from_name == "true")
		{ $("#from_name").next().text("Required").show() }						
	if (response.from_email == "true")
		{ $("#from_email").next().text("Required").show() }	
	if (response.to_name == "true")
		{ $("#to_name").next().text("Required").show() }						
	if (response.to_email == "true")
		{ $("#to_email").next().text("Required").show() }
	// content id is not valid
	if (response.content_id == 0)						
		{ $('#msg_transaction_return_error').html('<h2>Thank you</h2><p>Unfortunately, ' +
			'the article could not be sent. Please try again.</p><p>' + 
			'<a id="emailformclose" href="#">Close X</a></p>') 
			//$("#emailformclose").click(closeEmailForm);
			$("#emailformclose").click(function(){	
				closeEmailForm();
				return false;
			});	
		}
			
	if ((response.content_id > 0) && (response.from_name == "false") 
			&& (response.from_email == "false") && (response.to_name == "false") 
				&& (response.to_email == "false"))
		{						
		// html for thank you message
		var HTMLstring ='<a id="emailformclose" href="#">Close X</a>' +
			'<p><p>' +
			'<h2>Thank you</h2><p>Your e-mail has been sent. Thank you for referring ' +
			'one of our articles to a colleague.';					
			$('#email-form').html(HTMLstring);
			//$("#emailformclose").click(closeEmailForm);	
			$("#emailformclose").click(function(){	
				closeEmailForm();
				return false;
			});	
		}
}

var handleEmailError = function(XMLHttpRequest, textStatus, errorThrown){	
	var errMsg = textStatus + " ++ " + errorThrown;
	alert(errMsg);
}

var closeEmailForm=function(){	
	$("#email-form").hide();
	refreshEmailForm();
}

var emailFormHtml = "<a id='emailformclose' href='#'>Close X</a>" +
	"<p></p>" +
	"<h2>Email a Colleague</h2>" +
	"<p>Fill out the information below to send a link to this article to a colleague. " +
	"Your colleague will not be added to our marketing lists as a result.</p>" +
	"<form method='post' name='psm_form_crosswalk' id='psm_form_crosswalk' class='psm_form'>" +
	"<input type='hidden' name='content_id' id='form_content_id' value=''>" +
	"<input type='hidden' name='col_topic_id' id='form_col_topic_id' value=''>" +
	"<input type='hidden' name='create_UUID' id='create_UUID' value='1'>" +
	"<fieldset>" +
	"<label for='from_name'>Your Name:</label>" +
	"<input name='from_name' id='from_name' class='required' type='text' />" +
	"<h4 class='error_msg'></h4>" +
	"</fieldset>" +
	"<fieldset>" +
	"<label for='from_email'>Your E-mail:</label>" +
	"<input name='from_email' id='from_email' class='required email' type='text' />" +
	"<h4 class='error_msg'></h4>" +
	"</fieldset>" +
	"<fieldset>" +
	"<label for='to_name'>Colleague's name:</label>" +
	"<input name='to_name' id='to_name' class='required' type='text' />" +
	"<h4 class='error_msg'></h4>" +
	"</fieldset>" +
	"<fieldset>" +
	"<label for='to_email'>Colleague's E-mail:</label>" +
	"<input name='to_email' id='to_email' class='required email' type='text' />" +
	"<h4 class='error_msg'></h4>" +
	"</fieldset>" +
	"<fieldset>" +
	"<label for='comments'>Additional comments:</label>" +
	"<textarea rows='7' cols='35' id='comments'  name='comments'></textarea>" +
	"</fieldset>" +
	"<input type='image' src='/images/submit-button.jpg' class='submit' />" +
	"<div id='msg_transaction_return_error'></div>" +
	"</form>";
