/*
 * Functions for astroblog.php
 * 
 * Rick Burton 8/1/2010
 * Data Pathways
 * http://www.datapathways.com
 */

	// Display message form, clear controls for new message post
	function showMsg()
	{
		var msgFormElems = document.forms["msgForm"].elements;
		
		document.getElementById("divBodyFormContainer").style.display = "inline";
		document.getElementById("divBodyControls").style.display = "none";
		document.getElementById("divBodyMsgList").style.display = "none";

		msgFormElems["message"].value = "";
		msgFormElems["subject"].value = "";
		msgFormElems["subject"].focus();
	}

	// Display the message list in the current thread
	function showlist()
	{
		document.getElementById("divBodyFormContainer").style.display = "none";
		document.getElementById("divBodyControls").style.display = "inline";
		document.getElementById("divBodyMsgList").style.display = "inline";
	}
	
	// Check for complete and valid data - submit message form for new or edited messages
	function submitMsg()
	{
		var msgFormElems = document.forms["msgForm"].elements;
		
		if( msgFormElems["message"].value == "" || msgFormElems["subject"].value == "" )
		{
			alert("Please fill in subject and message");
			return;
		}	
			
		/*
		if( msgFormElems["message"].value.search(/;/) > -1 || msgFormElems["subject"].value.search(/;/) > -1 )
		{
			alert("Semicolon characters may not be used");
			return;
		}
		*/
		
		document.forms["msgForm"].submit();
	}
		
	// Display message form and populate controls with values from selecte message for edit
	function editMsg(id)
	{
		msgFormElems = document.forms["msgForm"].elements;
		ctrlMsgFormElems = document.forms["ctrlMsgForm"+id].elements;
		
		document.getElementById("divBodyFormContainer").style.display = "inline";
		document.getElementById("divBodyControls").style.display = "none";
		document.getElementById("divBodyMsgList").style.display = "none";

		msgFormElems["subject"].value = ctrlMsgFormElems["subject"].value;
		msgFormElems["message"].value = cleanString(ctrlMsgFormElems["body"].value);
		msgFormElems["msg_id"].value  = ctrlMsgFormElems["msg_id"].value;
		msgFormElems["u_name"].value  = ctrlMsgFormElems["u_name"].value;
		msgFormElems["form_action"].value = "update_msg";
	}

	// Sumit message to be deleted by id
	function deleteMsg(id)
	{
		ctrlMsgForm = document.forms["ctrlMsgForm"+id];
		
		ctrlMsgForm.elements["form_action"].value = "delete_msg";
		
		if(confirm("WARNING: Delete Message #" + id + "?"))
			ctrlMsgForm.submit();
		else
			return;
	}
	
	// Move message up one place in the list
	function moveMsgUp(id)
	{
		ctrlMsgForm = document.forms["ctrlMsgForm"+id];
		
		ctrlMsgForm.elements["form_action"].value = "move_msg_up";
		
		ctrlMsgForm.submit();
	}
	
	// Move message down one place in the list
	function moveMsgDown(id)
	{
		ctrlMsgForm = document.forms["ctrlMsgForm"+id];
		
		ctrlMsgForm.elements["form_action"].value = "move_msg_down";
		
		ctrlMsgForm.submit();
	}
	
	// Submit Year filter form
	function filterByYear()
	{
		document.forms["filterForm"].submit();
	}
	
	// Set filter drop list to current selection
	function setList(val)
	{
		document.forms["filterForm"].elements["yearFilter"].value = val;
	}
	
	// Restore "bunsanitize()ed" string to usable data state	
	function cleanString(str)
	{
		str = str.replace(/&lt;/g, "<");
		str = str.replace(/&gt;/g, ">");
		str = str.replace(/&amp;/g, "&");
		str = str.replace(/@br\/@/g, "\n");
		str = str.replace(/&quot;/g, "\"");
		
		return str;
	}


	

