/*	
	======================================================================
	
	javascript/public.js

	======================================================================
*/


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Subscribe
	//
	/////////////////////////////////////////////////////////
	
		function Subscribe () {

			var f = document.subscribeForm;

		// ---------------------------------------------
		// get e-mail address
		// ---------------------------------------------

			var e_mail = Trim (f.e_mail.value);
			
			if (e_mail == "") {
			
				alert ("Please enter your email address in the box above before subscribing");
				
				return;
			}
			
		// ---------------------------------------------
		// add to mailing list
		// ---------------------------------------------
 			
			if (Validate_Email_Address (e_mail)) {
			
				var processingScript = "/php/join-mailing-list.php";
				
				var ajaxRequestString = processingScript + "?e_mail=" + escape (e_mail);
				
				AJAX_Make_Request (ajaxRequestString);
			}

		// ---------------------------------------------
		// if invalid e-mail address
		// ---------------------------------------------
 			
			else {
			
				alert ("Sorry...that e-mail address does not appear to be valid");
			}
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  SendFeedback
	//
	/////////////////////////////////////////////////////////
	
		function SendFeedback () {

			var f = document.feedbackForm;

		// ---------------------------------------------
		// get feedback
		// ---------------------------------------------

			var feedback = Trim (f.feedbackField.value);
			
			if (feedback == "") {
			
				alert ("Please enter feedback in the box above before sending");
				
				return;
			}
			
			var escapedFeedback = escape (feedback);
			
		// ---------------------------------------------
		// get feedback page
		// ---------------------------------------------

			var feedbackPage = Trim (f.feedbackPage.value);
			
			if (feedbackPage == '') feedbackPage = 'home page';
			
			var escapedFeedbackPage = escape (feedbackPage);
			
		// ---------------------------------------------
		// add to mailing list, if requested
		// ---------------------------------------------
 			
			var eMailString = '';
			
			var eMail = Trim (f.eMailFeedback.value);

			if (eMail) {
			
				if (Validate_Email_Address (eMail)) {
			
					eMailString = "&eMail=" + escape (eMail);
				}
				
				else {
				
					eMailString = "&eMail=invalid";
				}
			}

		// ---------------------------------------------
		// send feedback
		// ---------------------------------------------
 			
			var processingScript = "/php/process-feedback.php";
			
			var ajaxRequestString = processingScript + "?feedback=" + escape (escapedFeedback) + "&feedbackPage=" + escapedFeedbackPage + eMailString;
			
			AJAX_Make_Request (ajaxRequestString);
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  SendToAFriend
	//
	/////////////////////////////////////////////////////////
	
		function SendToAFriend () {

			var f = document.tellAFriendForm;

		// ---------------------------------------------
		// get e-mail address
		// ---------------------------------------------

			var eMail = Trim (f.tellAFriendEmail.value);
			
			if (eMail == "") {
			
				alert ("Please enter your friend's email address before sending");
				
				return;
			}
			
			var escapedEmail = escape (eMail);
			
		// ---------------------------------------------
		// get sender's name
		// ---------------------------------------------

			var senderName = Trim (f.tellAFriendSenderName.value);
			
			var escapedSenderName = escape (senderName);
			
		// ---------------------------------------------
		// get message
		// ---------------------------------------------

			var message = Trim (f.tellAFriendMessage.value);
			
			var escapedMessage = escape (message);
			
		// ---------------------------------------------
		// send to friend
		// ---------------------------------------------
 			
			var processingScript = "/php/send-to-a-friend.php";
			
			var ajaxRequestString = processingScript + "?eMail=" + escapedEmail + "&senderName=" + escapedSenderName + "&message=" + escapedMessage;
			
			AJAX_Make_Request (ajaxRequestString);
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  AJAX_Handle_Response
	//
	/////////////////////////////////////////////////////////
	
		function AJAX_Handle_Response (responseArray) {

		// ---------------------------------------------
		// parse response
		// ---------------------------------------------

 			responseParts = responseArray.split ('_');
 			
 			source = responseParts[0];

 			response = responseParts[1];

		// ---------------------------------------------
		// mailing list
		// ---------------------------------------------
 		
			if (source == 'mailing-list') {
				
				if (response == 'success') {
					
					alert ("Thanks...we've added you to the mailing list!  Please check your inbox for a confirmation email...click the link in the email to activate your subscription. Please add 'info@dailysciencefiction.com' to your email address book to ensure that the confirmation email doesn't get tagged as spam.");
				}
				
				else if (response == 'already-on') {
					
					alert ("Thanks...you're already on our mailing list!");
				}
				
				else if (response == 'resent-confirmation') {
				
					alert ("You've signed up for the mailing list already, but haven't yet confirmed your subscription.  We've just sent you the confirmation email again...please open it and click the confirmation link.  Please contact us at info@dailysciencefiction.com if you don't receive this email within 24 hours.");
				}
				
				else {
				
					alert ("Sorry...there was an error adding your email address to the mailing list.  Please email info@dailysciencefiction.com to join the mailing list");
				}
			}
			
		// ---------------------------------------------
		// feedback form
		// ---------------------------------------------
 		
 			else if (source == 'feedback') {
 			
				if (response == 'success') {
				
					Hide_Div ('feedbackDiv');
					
					document.feedbackForm.feedbackField.value = '';
					
					document.feedbackForm.eMailFeedback.value = '';
					
					alert ("Thanks for your feedback!");
				}
				
				else {
				
					alert ("Sorry...there was an error sending your feedback.  Please email info@dailysciencefiction.com directly.");
				}
			}
			
		// ---------------------------------------------
		// tell-a-friend form
		// ---------------------------------------------
 		
 			else if (source == 'tell-a-friend') {
 			
				if (response == 'success') {
				
					Hide_Div ('tellAFriendDiv');
					
					var alertMessage = "Thanks...your message was sent to " + document.tellAFriendForm.tellAFriendEmail.value;
					
					alert (alertMessage);
					
					document.tellAFriendForm.tellAFriendEmail.value = '';
				}
				
				else {
				
					alert ("Sorry...there was an error sending the message to your friend.  Please email info@dailysciencefiction.com directly.");
				}
			}
			
		// ---------------------------------------------
		// unknown source
		// ---------------------------------------------
 		
 			else if (source != 'null') {

				alert ("Sorry...there was a Javascript error...please contact Daily Science Fiction for assistance.");
			}
		}
 