////////////////////////////////////////////////////////////////////////////////
// Function to select all or none of the messages based on the master checkbox
function SelectAllMsgs()
  {
  	var allInputs = document.getElementsByTagName("input")
  
  	// Loop through all the check boxes in the form and check them
	for (var i = 0; i < allInputs.length; i++)
	{
		if (allInputs[i].name.substring(0,6) == 'cbMsg_')
		{
			allInputs[i].checked = inbox.cbSelAll.checked;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
// Function to change the class of a table row
function ChangeClass(msg)
  {
  	if(eval("inbox.cbMsg_"+msg).checked == true)
  	{
  		// Set the class to the selected class
  		eval("Msg_"+msg).className = "msgsel";
  	}
  	else
  	{
  		// Set the class to the default class
  		eval("Msg_"+msg).className = eval("inbox.Msg_"+msg+"_Default").value;
  	}
}
 
//////////////////////////////////////////////////////////////////////////////// 
// Function to check if all the checkboxes are clicked
function CheckAllClicks()
{
  	// Check if the master chekbox should be checked
   	var allCbs = document.getElementsByTagName("input")
  	$allchecked = true
	  
  	// Loop through all the check boxes in the form and see if they are checked
	for (var i = 0; i < allCbs.length; i++)
	{
		if (allCbs[i].name.substring(0,6) == 'cbMsg_')
		{
			if(allCbs[i].checked == false)
			{
				$allchecked = false;
			}
		}
	}
  	
  	// If all the checkboxes are checked, check off the main checkbox
  	if($allchecked == true)
  	{
  		inbox.cbSelAll.checked = true;
  	}
  	else
  	{
	  	inbox.cbSelAll.checked = false;
  	}
}
  
////////////////////////////////////////////////////////////////////////////////
// Function to validate the controls on the send message page
function validateSend()
{
	var allvalid = true;
  
  	// Make sure the user chose a recipient
  	if(send_msg.SendAll)
  	{
  		if((send_msg.SendAll.checked == false) && ((send_msg.msg_to.selectedIndex == 0) || (send_msg.msg_to.value == "")))
  		{
  			valrTo.innerText="*";
	  		allvalid = false;
  		}
  		else
  		{
  			valrTo.innerText="";
  		}
  	}
  	else
  	{
  		if((send_msg.msg_to.selectedIndex == 0) || (send_msg.msg_to.value == ""))
	  	{
	  		valrTo.innerText="*";
	  		allvalid = false;
	  	}
	  	else if((send_msg.msg_to.selectedIndex > 0) || (send_msg.msg_to.value != ""))
	  	{
	  		valrTo.innerText="";
	  	}
  	}
  
  	// Make sure the user entered a subject
  	if(send_msg.msg_subj.value=="")
  	{
  		valrSubj.innerText="*";
  		allvalid = false;
  	}
  	else if(send_msg.msg_subj.value!="")
  	{
  		valrSubj.innerText="";
  	}

	// Make sure the user entered a message
	if(send_msg.msg_text.value=="")
	{
		valrMsg.innerText="*";
		allvalid = false;
	}
	else if(send_msg.msg_text.value!="")
	{
  		if(send_msg.msg_text.value.length>=255)
  		{
  			valrMsg.innerText="* Message must be 255 characters or less";
			allvalid = false;
		}
		else
		{
			valrMsg.innerText="";
		}
	}
	
	// If all of the controls have valid input, enable the send button
	if(allvalid == true)
	{
		send_msg.cmd_send_msg.disabled = false;
	}
	else
	{
		send_msg.cmd_send_msg.disabled = true;
	}
}
  
////////////////////////////////////////////////////////////////////////////////
function sendAll()
{
	if(send_msg.SendAll.checked)
  	{
  		send_msg.msg_to.selectedIndex = 0;
  	}
}

////////////////////////////////////////////////////////////////////////////////
function noSendAll()
{
	if(send_msg.msg_to.selectedIndex > 0)
	{
		send_msg.SendAll.checked = false;
	}
}

////////////////////////////////////////////////////////////////////////////////
// Function to make sure a URL is valid
function isURL (url) 
{
  var urlPattern = /^http:\/\/?(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|edu|biz|org|gov|info|mil|net|name|museum|coop|aero|[a-z][a-z])\b(?:\d+)?(?:\/[^;"'<>()\[\]{}\s\x7f-\xff]*(?:[.,?]+[^;"'<>()\[\]{}\s\x7f-\xff]+)*)?/;

  return urlPattern.test(url.toLowerCase());
}

////////////////////////////////////////////////////////////////////////////////
// Function to check if an email address is in the correct format
function isEmail(url) 
{ 
  var emailPattern=/^[a-z0-9]+([_|-|.][a-z0-9]+)*\@([a-z0-9]+((-*)(.*)[a-z0-9]+)*\.(com|edu|biz|org|gov|int|info|mil|net|arpa|name|museum|coop|aero|[a-z][a-z])|(\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]))$/; 
 
  return emailPattern.test(url.toLowerCase());
} 

////////////////////////////////////////////////////////////////////////////////
// Function to check the validity of the Charity URL
function validateSuggest()
{
	$allgood = true;

	// Make sure we have a name
	if(suggest_charity.char_name.value == "")
	{
		valr_name.innerText="*";
		$allgood = false;
		suggest_charity.cmd_suggest.disabled = true;	
	}
	else
	{
		valr_name.innerText="";
		suggest_charity.cmd_suggest.disabled = false;
	}
	
	// Make sure the email address is valid
	if(suggest_charity.char_email.value == "")
	{
		valr_email.innerText="*";
		$allgood = false;
		suggest_charity.cmd_suggest.disabled = true;	
	}
	else
	{
		if (!isEmail(suggest_charity.char_email.value))
		{
		  	valr_email.innerText="* Invalid Email Address (name@site.domain)";
		 	$allgood = false;
		  	suggest_charity.cmd_suggest.disabled = true;
		}
		else
		{
			valr_email.innerText="";
		  	suggest_charity.cmd_suggest.disabled = false;
		}
	}
	
	// Make sure the URL is valid
	if(suggest_charity.char_www.value == "http://")
	{
		valr_www.innerText="*";
		$allgood = false;
		suggest_charity.cmd_suggest.disabled = true;	
	}
	else
	{
		if (!isURL(suggest_charity.char_www.value))
		{
		  	valr_www.innerText="* Invalid URL (http://name.domain)";
		 	$allgood = false;
		  	suggest_charity.cmd_suggest.disabled = true;
		}
		else
		{
			valr_www.innerText="";
		  	suggest_charity.cmd_suggest.disabled = false;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
function ValidateFeedback()
{
	allvalid = true;
	
	if(feedback.comment.value == "")
	{
		valr_comment.innerText="* Please enter a comment"
		allvalid=false;
	}
	else
	{
		valr_comment.innerText=""
	}
	
	return allvalid;
}
