//------------------------------\
//Created:	2-11-2007			|
//Modified:	2-26-2007			|
//FeatherBlaster:	1.0.3		|
//Copyright Michael White 2007	|
//------------------------------/

//Check to see if the user is TRYING to unload the page.
//If they are and the page title matches a title in our list then we need to display the confirmation message.
//Should change this to be an array instead of a switch statement at some point.
window.onbeforeunload = confirmExit;
function confirmExit()
{
	var msg =  "You will lose any unsaved changes if you leave this page. Click cancel to continue working.";
	
	//Simply add something like a * in front of any pages you wish to exlude from this check.
	switch(document.title)
	{
		case "Edit Custom Message":
			return msg;
			break;
		case "Edit Home Page Contents and Title":
			return msg;
			break;
		case "Customize Default Email Contents":
			return msg;
			break;
		case "Prepare A Blast Email":
			return msg;
			break;
		case "Customize New Subscriber Email":
			return msg;
			break;
	}
}

function showTip(tip)
{
	alert(tip);	
}

window.onload = getURLParameters();
function getURLParameters() 
{
	var sURL = window.document.URL.toString();
	
	if (sURL.indexOf("?") > 0)
	{
		var arrParams = sURL.split("?");
			
		var arrURLParams = arrParams[1].split("&");
		
		var arrParamNames = new Array(arrURLParams.length);
		var arrParamValues = new Array(arrURLParams.length);
		
		var i = 0;
		for (i=0;i<arrURLParams.length;i++)
		{
			var sParam =  arrURLParams[i].split("=");
			arrParamNames[i] = sParam[0];
			if (sParam[1] != "")
				arrParamValues[i] = unescape(sParam[1]);
			else
				arrParamValues[i] = "No Value";
		}
		
		for (i=0;i<arrURLParams.length;i++)
		{
			//alert(arrParamNames[i]+" = "+ arrParamValues[i]);
			//If this page was redirected then alert the user that their data has been saved!
			if(arrParamNames[i] == "display_message")
			{
				initializeTimer(2, arrParamValues[i]);
			}
			else if(arrParamNames[i] == "saved")
			{
				if(arrParamValues[i] == "true")
				{
					//alert("The database has been updated.");
					//We COULD just tell the browser to aler the user but then you get an ugly display of a white page.
					//To avoid this we can use a timer to wait for the page to finish loading.
					//Doesn't take long on this site - a second or two should get us by most of the time.
					//ONLY use intergers - 1.5 will not work. 2 seems to work best. 3 or more may be needed on a slower server or a page with graphics, flash ads, etc.
					//alert("This is with no timer");
					initializeTimer(2, "The information you submitted has been saved/updated.");
				}
			}
		}
		
		return 
	}
	else
	{
		//alert("No parameters.");
	}
}

function lockPage()
{
	//document.getElementById('lock_page').style.display = 'block';
}

function unlockPage()
{
	//document.getElementById('lock_page').style.display = 'none';
	//document.getElementById("lock_page").style.display = 'none';
}

// BEGIN TIMER

var original_time;
var secs;
var timer_id = null;
var timer_running = false;
var delay = 1000;
var msg_for_display;

function initializeTimer(timer_len, msg)
{
	//Set the length of the timer in seconds
	msg_for_display = msg;
	secs = timer_len;
	original_time = timer_len;
	stopClock();
	startTimer();
}

function stopClock()
{
    if(timer_running)
	{
		clearTimeout(timer_id);
	}
	timer_running = false;
}

function startTimer()
{
	if(secs==0)
	{
		stopClock();
		//Here's where you put something useful that's supposed to happen after the timer runs out
		alert(msg_for_display);
		unlockPage();
	}
	else if(secs == (original_time - 2))
	{
		lockPage();
		self.status = secs;
		secs = secs - 1;
		timer_running = true;
		timer_id = self.setTimeout("startTimer()", delay);
	}
	else
	{
		self.status = secs;
		secs = secs - 1;
		timer_running = true;
		timer_id = self.setTimeout("startTimer()", delay);
	}
}

//END TIMER

function saveAndExit(form_name, msg, check, page_name)
{
	form_element = document.forms[form_name];
	
	//when saving the page we need to alter the value of the exit field.
	//exit is a hidden field that is set to 0 by default.
	//When exit is set to "page_name" the redirect will direct it back to the page with that name.
	//Page name DOES include an extension.
	
	if(msg == "")
	{
		msg = "Are you sure you want to submit this form?";	
	}
	if(check == 1)
	{
		if(doubleCheck(msg) === true)
		{
			//Change this page's name so that the confirmExit prompt doesn't display.
			//We don't want to display that prompt on ANY page if they have already confirmed that they want to submit the form.
			form_element.exit.value = page_name;
			document.title = "DoNotDisplay";
			form_element.submit();
		}
	}
	else
	{
		form_element.exit.value = page_name;
		document.title = "DoNotDisplay";
		form_element.submit();
	}
}

function saveAndExitOnKeyPress(event, form_name, msg, check, page_name)
{
	if(msg == "")
	{
		msg = "Are you sure you want to submit this form?";
	}
	if(document.all)
	{
		key = event.keyCode;	//IE
	}
	else
	{
		key = key_event.which;	//Netscape and Firefox.
	}
	
	if(key == '13')
	{
		saveAndExit(form_name, msg, check, page_name);
	}
}

function submitForm(form_name, msg, check)
{
	form_element = document.forms[form_name];
	if(msg == "")
	{
		msg = "Are you sure you want to submit this form?";	
	}
	if(check == 1)
	{
		if(doubleCheck(msg) === true)
		{
			//Change this page's name so that the confirmExit prompt doesn't display.
			//We don't want to display that prompt on ANY page if they have already confirmed that they want to submit the form.
			document.title = "DoNotDisplay";
			form_element.submit();
		}
	}
	else
	{
		document.title = "DoNotDisplay";
		form_element.submit();
	}
}

function doubleCheck(msg)
{
	send_form = confirm(msg);
	return send_form;
}

function checkKeyPress(key_event, form_name, msg, check)
{
	if(msg == "")
	{
		msg = "Are you sure you want to submit this form?";
	}
	if(document.all)
	{
		key = event.keyCode;	//IE
	}
	else
	{
		key = key_event.which;	//Netscape and Firefox.
	}
	
	if(key == '13')
	{
		submitForm(form_name, msg, check);
	}
	
}
//------------------------------

function doubleCheckManualBlast(group_size)
{
	send_form = confirm("Are you certain you want to send "+group_size+" email(s) from the message queue?");
	return send_form;
}

function submitManualBlastForm(form_name)
{
	form_element = document.forms[form_name];
	group_size = form_element.grp_size.value;
	
	if(group_size > 0)
	{
		if(doubleCheckManualBlast(group_size) === true)
		{
			//Change this page's name so that the confirmExit prompt doesn't display.
			document.title = "Title";
			form_element.submit();
		}
	}
	else if(group_size == "*")
	{
		if(doubleCheckManualBlast("ALL") === true)
		{
			//Change this page's name so that the confirmExit prompt doesn't display.
			form_element.grp_size.value = 0;	//Tells the PHP to send all the emails in the queue. No user directly allowed to enter a value of null or zero.
			document.title = "Title";
			form_element.submit();
		}
	}
	else
	{
		alert("You did not enter a valid number in the field. This value must be greater than zero. Enter a * to send the entire list.");	
	}
}

function checkManualBlastKeyPress(key_event, form_name)
{
	if(document.all)
	{
		//key = String.fromCharCode(event.keyCode);
		key = event.keyCode;	//IE
	}
	else
	{
		//key = String.fromCharCode(key_event.which);	
		key = key_event.which;	//Netscape and firefox.
	}
	
	//alert(key);
	
	if(key == '13')
	{
		//alert(form_name);
		//Is this the login form? If so don't ask for confirmation.
		return submitManualBlastForm(form_name);
	}
	
}

//-------------------

function doubleCheckQueueDump()
{
	send_form = confirm("Are you CERTAIN that you want to DELETE the ENTIRE queue list? This operation cannot be undone.");
	return send_form;
}

function submitQueueDumpForm(form_name)
{
	form_element = document.forms[form_name];
	
	actual_code = form_element.actual_code.value;
	code = form_element.code.value;
	
	if(actual_code == code)
	{
		if(doubleCheckQueueDump() === true)
		{
			//Change this page's name so that the confirmExit prompt doesn't display.
			document.title = "Title";
			form_element.submit();
		}
	}
	else
	{
		alert("The code you entered does not match the code supplied for you on this page.");	
	}
}

function checkQueueDumpKeyPress(key_event, form_name)
{
	if(document.all)
	{
		//key = String.fromCharCode(event.keyCode);
		key = event.keyCode;	//IE
	}
	else
	{
		//key = String.fromCharCode(key_event.which);	
		key = key_event.which;	//Netscape and firefox.
	}
	
	//alert(key);
	
	if(key == '13')
	{
		//alert(form_name);
		//Is this the login form? If so don't ask for confirmation.
		return submitQueueDumpForm(form_name);
	}
}

//-------------------

function doubleCheckBlast()
{
	send_form = confirm("Are you absolutely SURE you are ready to send the message you selected to the people you selected/added? Disabled members will not receive this email.");
	return send_form;
}

function submitBlastForm(form_name)
{
	form_element = document.forms[form_name];
	
	//Check to make sure that they have either checked the mailing list option or filled in at least one email for the custom list - or both.
	
	var custom_list_len = form_element.custom_list.length;
	var mailing_list_checked = form_element.list.checked;
	var message_id = form_element.message_id.value;
	//alert(mailing_list_checked);
	if(custom_list_len > 0)
	{
		if(message_id != "")
		{
			if(doubleCheckBlast() === true)
			{
				//Change this page's name so that the confirmExit prompt doesn't display.
				document.title = "Title";
				selectCustomList(form_name);
				form_element.submit();
			}
		}
		else
		{
			alert("You have not selected a message to send.");	
		}
	}
	else if(mailing_list_checked == true)
	{
		if(message_id != "")
		{
			if(doubleCheckBlast() === true)
			{
				//Change this page's name so that the confirmExit prompt doesn't display.
				document.title = "Title";
				selectCustomList(form_name);
				form_element.submit();
			}
		}
		else
		{
			alert("You have not selected a message to send.");	
		}
	}
	else
	{
		alert("You have no one selected to receive this message.");	
	}
}

function checkBlastKeyPress(key_event, form_name)
{
	if(document.all)
	{
		//key = String.fromCharCode(event.keyCode);
		key = event.keyCode;	//IE
	}
	else
	{
		//key = String.fromCharCode(key_event.which);	
		key = key_event.which;	//Netscape and firefox.
	}
	
	//alert(key);
	
	if(key == '13')
	{
		//alert(form_inst);
		//Is this the login form? If so don't ask for confirmation.
		submitBlastForm(form_name);
	}	
}

//-------------------

function displayCustomListForm()
{
	document.getElementById('custom_list_form_1').style.display = 'block';
	document.getElementById('custom_list_form_2').style.display = 'block';
}

function hideCustomListForm()
{
	document.getElementById('custom_list_form_1').style.display = 'none';
	document.getElementById('custom_list_form_2').style.display = 'none';
}

function switchCustomListVisibility(visible_status_box)
{
	var selection_status = visible_status_box.checked;
	
	if(selection_status == false)
	{
		hideCustomListForm();
	}
	else
	{
		displayCustomListForm();
	}
}

function addCustomEmail(form_name)
{
	var form_element = document.forms[form_name];

	var email = form_element.email.value;
	var first = form_element.first.value;
	var last = form_element.last.value;
	var visible_value = email;
	var value = email + "|" + first + "|" + last;

	var list_element = form_element.custom_list;
	
	if(email.length >= 4)
	{
	
		if (list_element.length == 0)
		{
			var newOpt1 = new Option(visible_value, value);
			list_element.options[0] = newOpt1;
			list_element.selectedIndex = 0;
		}
		else if(list_element.selectedIndex != -1)
		{
			var selected_text = new Array();
			var selected_values = new Array();
			var selected_is_selected = new Array();
			var new_count = -1;
			var new_selected = -1;
			var i;
	
			for(i=0; i<list_element.length; i++)
			{
				new_count++;
				selected_text[new_count] = list_element.options[i].text;
				selected_values[new_count] = list_element.options[i].value;
				selected_is_selected[new_count] = list_element.options[i].selected;
			  
				if (new_count == list_element.selectedIndex)
				{
					new_count++;
					selected_text[new_count] = visible_value;
					selected_values[new_count] = value;
					selected_is_selected[new_count] = false;
					new_selected = new_count - 1;
				}
			}
			for(i=0; i<=new_count; i++)
			{
				var newOpt = new Option(selected_text[i], selected_values[i]);
				list_element.options[i] = newOpt;
				list_element.options[i].selected = selected_is_selected[i];
			}
		}
		//Clear the fields used to add the email.
		form_element.first.value = "";
		form_element.last.value = "";
		form_element.email.value = "";
	}
	else
	{
		alert("Please enter a longer email address. No email address can have fewer than 5 characters");
	}
}


function removeCustomEmail(form_name)
{
	list_element = document.forms[form_name].custom_list;
	var selectionIndex = list_element.selectedIndex;
	if (selectionIndex != -1)
	{
		for(i=list_element.length-1; i>=0; i--)
		{
			if(list_element.options[i].selected)
			{
				list_element.options[i] = null;
			}
		}
		if(list_element.length > 0)
		{
			list_element.selectedIndex = selectionIndex == 0 ? 0 : selectionIndex - 1;
		}
	}
}

function selectCustomList(form_name)
{
	var list_element = document.forms[form_name].custom_list;
	
	if(list_element.length >= 1)
	{
		for(i=0; i<list_element.length; i++)
		{
			//list_element.options[i].selected = 1;
			if(compiled_list)
			{
				compiled_list += list_element.options[i].value + "\r\n";
			}
			else
			{
				var compiled_list = list_element.options[i].value + "\r\n";
			}
		}
		document.forms[form_name].compiled_list.value = compiled_list;
	}
}


function switchSetting(check_all_box, obj_set_name)
{
	var selection_status = check_all_box.checked;
	
	if(selection_status == false)
	{
		uncheckAll(obj_set_name);
	}
	else
	{
		checkAll(obj_set_name);
	}
}

function switchSettingUsingLink(obj_status_name, set_id, obj_set_name)
{
	var checked_status_list = document.getElementsByName(obj_status_name);
	var selection_status = checked_status_list[set_id].value;
	
	if(selection_status == "false")
	{
		checkAll(obj_set_name);
		checked_status_list[set_id].value = "true";
	}
	else
	{
		uncheckAll(obj_set_name);
		checked_status_list[set_id].value = "false";
	}
}

function checkAll(obj_set_name)
{
	var checkboxes = document.getElementsByName(obj_set_name);
	var total_boxes = checkboxes.length;
	
	for(i=0; i<total_boxes; i++)
	{
		current_value = checkboxes[i].checked;
		
		if(current_value == false)
		{
			checkboxes[i].checked = true;
		}
	}
}

function uncheckAll(obj_set_name)
{
	var checkboxes = document.getElementsByName(obj_set_name);
	var total_boxes = checkboxes.length;
	
	for(i=0; i<total_boxes; i++)
	{
		current_value = checkboxes[i].checked;
		
		if(current_value == true)
		{
			checkboxes[i].checked = false;
		}
	}
}
