// page_emailer.js

var mStuff;

//-----------------------------------------
function InitEMailerPage()
	{
	Rich_Init();
	BuildEMailTable();
	}

//-----------------------------------------
function EMail_Save(elem)
	{
	mStuff = Rich_GetHTML();
	}
//-----------------------------------------
function EMail_Restore(elem)
	{
	Rich_SetHTML(mStuff);
	}

//------------------------------------------------------------------------
function BuildEMailTable()
	{
	ui_load_table('email_recips', DoneBuildingEMailTable, null);
	}

//------------------------------------------------------------------------
function DoneBuildingEMailTable(tblObj)
	{	
	}
//------------------------------------------------------------------------
function EMail_CheckAll(elem)
	{
	GetElemByID('table_email_recips').obj.SetAllCheckBoxes(true);
	}
//------------------------------------------------------------------------
function EMail_CheckNone(elem)
	{
	GetElemByID('table_email_recips').obj.SetAllCheckBoxes(false);	
	}
//------------------------------------------------------------------------
function EMail_Send(elem)
	{
	var table = GetElemByID('table_email_recips').obj;
	var subj = GetElemByID("mail_subj").value;
	var body = encodeURIComponent(escape(Rich_GetHTML()));
	var sender = "eddie@eatyourpeas.net";	
	
	subj = encodeURIComponent(escape(subj));
	
	var s = "You are about to send the following email:\n\n"+
	"Subject:\ \ "+subj+"\n\n"+
	"To:\ \ ";

	var firstTime = true;
	for (var row = 0; row < table.filledRows; ++row)
		{
		if (table.IsChecked(row))
			{
			if (firstTime) firstTime = false
			else s += ", ";
			s += table.GetCellData(row,2);
			}
		}

	if (window.confirm(s))
		{
		var baseStr = "request=send&subj="+subj+"&body="+body+"&from="+sender+"&email=";	
		var emails = "";

		for (row = 0; row < table.filledRows; ++row)
			{
			if (table.IsChecked(row))
				{
				if (firstTime) firstTime = false;
				else emails += ",";
				emails += table.GetCellData(row,3);
				}
			}
		
		str = baseStr+emails;
		var ajax = new AJAX("page_emailer_ajax.php", str);
		ajax.post();
		if (ajax.GetRtnStr) alert("Mail has been sent.");
		else alert("Uh Oh.  Unable to send mail.");
		delete ajax;
		}
	}

