// Scripts for index.php

var popUpMarkup = "<div id='popup_box'><a id='popup_close_button'>x</a><br /><div id='popup_content'></div></div><div id='popup_background'></div>";

$( function() {	  
		  
	$("#blast_registration_link").live('click', function(event){
							
		$("#wrapper").append(popUpMarkup);
		$("#popup_content").load("includes/ajax_scripts/blast_registration.php");
		$("#popup_box").css({'height': '410px', 'width': '900px'});
		centerPopup();loadPopup();	
		$("#popup_box").draggable();	
		event.preventDefault();
											   
	 });
	
	$("#email_my_camper_link").live('click', function(event){
							
		$("#wrapper").append(popUpMarkup);
		$("#popup_content").load("includes/ajax_scripts/email_my_camper.php");
		$("#popup_box").css({'height': '500px', 'width': '750px'});
		centerPopup();loadPopup();	
		$("#popup_box").draggable();	
		event.preventDefault();
											   
	 });
	
	// Blast registration events

	$("#register").live('click', function() {
	
		var email = $("#email").val();
		var cellNumber = $("#cell_number").val();
		var cellProviderId = $("#cell_provider_id").val();
		
		if (email == '' && cellNumber == '')
		{
			alert("Please enter an e-mail address or a cell phone number.");
		}
		
		else if ((cellNumber != '' && cellProviderId == '') || (cellProviderId != '' && cellNumber == ''))
		{
			alert("Please enter your cell phone number AND select your provider, if you wish to receive text message blasts. If you do not, please clear both fields.");
		}
		
		else
		{
			var proceed = true;
			
			if (cellNumber != '')
			{
				var confirmSMS = confirm("I have read the text message service disclaimer, and acknowledge the potential costs associated with receiving text messages.")
				
				if (!confirmSMS)
				{
					proceed = false;
				}
			}
			
			if (proceed)
			{
				$("#registration_area").load("includes/ajax_scripts/process_blast_registration.php", { email: email, cell_number: cellNumber, cell_provider_id: cellProviderId});
			}
		}
	
	});
	
	$(".unsubscribe_link").live('click', function(){
	
		$("#popup_content").load("includes/ajax_scripts/blast_unregistration.php");
	
	});
	
	$("#unsubscribe").live('click',  function() {
	
		var email = $("#email").val();
		var cellNumber = $("#cell_number").val();
		
		if (email == '' && cellNumber == '')
		{
			alert("Please enter either an e-mail address, a cell phone number, or both to unsubscribe.");
		}
		
		else
		{
			$("#unregistration_area").load("includes/ajax_scripts/process_blast_unregistration.php", {email: email, cell_number: cellNumber});
		}
		
	});
	
	// E-mail my camper events
	
	$(".submit_email_button").live('click', function() {
		
		var firstName = $("#first_name").val();
		var lastName = $("#last_name").val();
		var bunk = $("#bunk").val();
		var message = $("#message").val();
		var fromName = $("#from_name").val();
		
		if (firstName == '')
		{
			alert("Please enter the camper's first name.");
		}
		
		else if (lastName == '')
		{
			alert("Please enter the camper's last name.");
		}
		
		else if (bunk == '')
		{
			alert("Please select the camper's bunk. If you don't know the camper's bunk, select 'Unknown'");
		}
		
		else if (message == '')
		{
			alert("Please enter a message.");
		}
		
		else if (fromName == '')
		{
			alert("Please indicate who this message is from.");
		}
		
		else
		{
			$("#email_form_container").load("includes/ajax_scripts/process_submit_email.php", 
			
				{
					first_name: firstName,
					last_name: lastName,
					bunk: bunk,
					message: message,
					from_name: fromName
				}
			);
		}
		
	});
	
	$("#message").live('keyup', function() {
		
		var message = $("#message").val();
		var messageLength = message.length;
		var maxMessageLength = 8000;
		var warningLength = 7800;
		
		if (messageLength == warningLength)
		{
			alert("Note: You have about 200 characters left before you reach the limit.");
		}
		
		if (messageLength >= maxMessageLength)
		{
			alert("You've reached the maximum length of a single message.");
			var newMessage = message.substring(message, maxMessageLength);
			$("#message").val(newMessage);
		}
		
	});
	
});