///////////////////////////////////////////////////////////////////////////////
// Javascript include "eMail.js" file for Jesse Chisholm's email purposes.
//
// ASSUMPTIONS:
//	1	utils.js has already been included
//
//	2	the primary form has the following fields:
//		id="fName"		First Name
//		id="lName"		Last Name
//		id="eFrom"		eMail address this is from
//		id="eSubject"		one liner
//		id="eBody"		the bulk, may contains HTML
//		id="eTo"		[optional] who is this to?
//		id="eCC"		[optional] who is this to Courtesy Copy?
//		id="eBCC"		[optional] who is this to Blind Courtesy Copy?
//		id="uTo"		[optional] user code tag for eTo?
//
//		If "eTo" is not specified, then "Jesse.Chisholm@gmail.com" is used.
//
//		If "uTo" is specified, then it is used to index a mapping from
//			uTo code to actual address.
//
//	3	there is a form called "HiddenForm" that is used to actually send
//		the collected data to the sendmail.php page
//
//		This form has all the same fields as the primary form, plus a few others:
//		id="oPage"		the URL to return to after send is complete
//		id="realname"		[optional] fName+' '+lName
//
//	4	If you want to massage the eBody for preview, then you may
//		override the function massagePreviewBody( eBody ) and return
//		the massaged string.  The default is to call uglify().
//
//	5	If you want to massage the eBody for shipment, then you may
//		override the function massageEmailBody( eBody ) and return
//		the massaged string.  The default is to return the string unchanged.
//
//	6	Your form has a button to call sendMail(this.form,0) to send
//
//	7	Your form has a button to call sendMail(this.form,1) to preview
//
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
//	initialize the primary fields of the form
//
function initMail(frm)
{
	// initialize the most commonly static fields of the form
	//	clear the most commonly volatile fields of the form
	//
	frm.fName.value     = getCookie("fName",'');
	frm.lName.value     = getCookie("lName",'');
	frm.eFrom.value     = getCookie("eFrom",'');
	frm.eSubject.value  = '';
	frm.eBody.value  = '';
	if (typeof frm.eTo != 'undefined')
		if (frm.eTo.type != 'hidden')
			frm.eTo.value = '';
	if (typeof frm.eCC != 'undefined')
		if (frm.eCC.type != 'hidden')
			frm.eCC.value = '';
	if (typeof frm.eBCC != 'undefined')
		if (frm.eBCC.type != 'hidden')
			frm.eBCC.value = '';
}

function count_at(adr)
{
	return adr.split('@').length - 1;
}
function multiline(adr)
{
	return 1 < adr.split('\n').length;
}

var userName = Object();
userName["~tact"] = "General Information";
userName["~chair"] = "Chairman";
userName["~vice"] = "Vice Chairman";
userName["~webm"] = "WebMaster";

///////////////////////////////////////////////////////////////////////////////
//	preview or send the final form of this email
//
function sendMail(frm,preview)
{
	var fName     = frm.fName.value;
	var lName     = frm.lName.value;
	var eFrom     = frm.eFrom.value;
	var eSubject  = frm.eSubject.value;
	var eBody     = frm.eBody.value;
//
	var uTo       = (typeof frm.uTo != 'undefined') ? frm.uTo.value : '';
	var eTo       = (typeof frm.eTo != 'undefined') ? frm.eTo.value : '';
	var eCC       = (typeof frm.eCC != 'undefined') ? frm.eCC.value : '';
	var eBCC      = (typeof frm.eBCC != 'undefined') ? frm.eBCC.value : '';
	var oPage     = (typeof frm.oPage != 'undefined') ? frm.oPage.value : location.href;

	if ((1 < count_at(eFrom))
	 || (1 < count_at(eTo))
	 || (1 < count_at(eCC))
	 || (1 < count_at(eBCC))
	 || (0 != count_at(fName))
	 || (0 != count_at(lName))
	 || (0 != count_at(eSubject))
	 || (multiline(eFrom))
	 || (multiline(eTo))
	 || (multiline(eCC))
	 || (multiline(eBCC))
	 || (multiline(fName))
	 || (multiline(lName))
	   )
	{
		alert("ERROR: there are malformed email addresses.  Please correct them.");
		return;
	}

	var oDate = new Date();
	eDate = oDate.toLocaleString();
	gDate = oDate.toGMTString();

	if (fName.length == 0)
	{
		frm.fName.focus()
		alert('Please enter your first name.');
		return;
	}
	if (lName.length == 0)
	{
		frm.lName.focus()
		alert('Please enter your last name.');
		return;
	}
	if (eFrom.length == 0)
	{
		frm.eFrom.focus()
		alert('Please enter your eMail address.');
		return;
	}
	if ((eFrom.indexOf('@')==-1)
	 || (eFrom.lastIndexOf('.') < eFrom.indexOf('@'))
	 || (eFrom.indexOf('@') != eFrom.lastIndexOf('@'))
	   )
	{
		frm.eFrom.focus()
		alert('Please enter a valid FROM eMail address, this one is not valid:\n\n\t' + eFrom);
		return;
	}
	// OK for eTo to be empty, but if not, then it must be legal.
	//
	if ((eTo.length > 0)
	 && ((eTo.indexOf('@')==-1)
	  || (eTo.lastIndexOf('.') < eTo.indexOf('@')))
	  || (eTo.indexOf('@') != eTo.lastIndexOf('@'))
	   )
	{
		if (typeof frm.eTo != 'undefined')
		{
			if (frm.eTo.type != 'hidden')
			{
				frm.eTo.focus()
				alert('Please enter a valid TO eMail address, this one is not valid:\n\n\t' + eTo);
				return;
			}
		}
		eTo = "";
	}
	if ((typeof frm.eCC != 'undefined')
	 && (eCC.length > 0)
	 && ((eCC.indexOf('@')==-1)
	  || (eCC.lastIndexOf('.') < eCC.indexOf('@'))
	  || (eCC.lastIndexOf('@') != eCC.indexOf('@'))))
	{
		frm.eCC.focus()
		alert('Please enter a valid CC eMail address, this one is not valid:\n\n\t' + eCC);
		return;
	}
	if ((typeof frm.eBCC != 'undefined')
	 && (eBCC.length > 0)
	 && ((eBCC.indexOf('@')==-1)
	  || (eBCC.lastIndexOf('.') < eBCC.indexOf('@'))
	  || (eBCC.lastIndexOf('@') != eBCC.indexOf('@'))))
	{
		frm.eBCC.focus()
		alert('Please enter a valid BCC eMail address, this one is not valid:\n\n\t' + eBCC);
		return;
	}
	if (eSubject.length == 0)
	{
		eSubject = frm.eSubject.value = 'General Comment'
	}
	if (eSubject.indexOf('\n') != -1)
	{
		frm.eSubject.focus()
		alert('Please enter a short, one line, subject.');
		return;
	}

	// call overridable body massager
	//
	if (preview)
	{
		eBody = massagePreviewBody( eBody );
	}
	else
	{
		eBody = massageEmailBody( eBody );
	}

	if (eBody.length == 0)
	{
		alert('What?  You have nothing to say?  Please enter a comment or question.');
		frm.eBody.focus()
		return;
	}

	if (eBody.length >= 32768)
	{
		alert('Too much content!  Cannot send that much!');
		frm.eBody.focus()
		return;
	}
	// save as convenience for next time
	//
	setCookie("fName", fName, largeExpDate());
	setCookie("lName", lName, largeExpDate());
	setCookie("eFrom", eFrom, largeExpDate());

	var tst = '';
	if (preview)
	{
		tst += '<html><head><title>Preview Letter</title>'+"\n";
		tst += '<scr'+'ipt language="JavaScr'+'ipt" type="text/javascr'+'ipt" src="js/utils.js"></scr'+'ipt>'+"\n";
		tst += '</head>'+"\n";
		tst += '<body bgcolor="white" text="black" link="red" vlink="purple" alink="orange"><h1>Preview Letter</h1><hr>'+"\n";
		tst += '<table border="0">'+"\n";
		tst += '<tr><th valign="top" align="right">From:</th>';
		tst +=     '<td valign="top" align="left">'+fName+' '+lName+' &lt;'+eFrom+'&gt;</td></tr>'+"\n";
		if (eTo)
			tst += '<tr><th valign="top" align="right">To:</th><td valign="top" align="left">'+eTo+'</td></tr>'+"\n";
		else if (uTo)
			tst += '<tr><th valign="top" align="right">To:</th><td valign="top" align="left">'+userName[uTo]+'&nbsp;(at)&nbsp;fraserclan-cal.net</td></tr>'+"\n";
		if (eCC)
			tst += '<tr><th valign="top" align="right">CC:</th><td valign="top" align="left">'+eCC+'</td></tr>'+"\n";
		if (eBCC)
			tst += '<tr><th valign="top" align="right">BCC:</th><td valign="top" align="left">'+eBCC+'</td></tr>'+"\n";

		tst += '<tr><th valign="top" align="right">Local&nbsp;Date:</th><td valign="top" align="left">'+eDate+'</td></tr>'+"\n";
		tst += '<tr><th valign="top" align="right">GMT&nbsp;Date:</th><td valign="top" align="left">'+gDate+'</td></tr>'+"\n";
		tst += '<tr><th valign="top" align="right">Subject:</th><td valign="top" align="left">'+eSubject+'</td></tr>'+"\n";

		tst += '<tr><th valign="top" align="right">Body:</th><td valign="top" align="left">'+"\n";
			tst += eBody;
		tst += "\n"+'</td></tr>'+"\n";
		tst += '</table><hr>'+"\n";

		tst += '<form name="HiddenForm" method="post" action="' + frm.action + '">'+"\n";
		tst += '<input type="hidden" name="oPage"    value="'+oPage          +'" />'+"\n";
		tst += '<input type="hidden" name="fName"    value="'+fName          +'" />'+"\n";
		tst += '<input type="hidden" name="lName"    value="'+lName          +'" />'+"\n";
		tst += '<input type="hidden" name="realname" value="'+fName+' '+lName+'" />'+"\n";
		tst += '<input type="hidden" name="eFrom"    value="'+eFrom          +'" />'+"\n";
		tst += '<input type="hidden" name="eSubject" value="'+eSubject       +'" />'+"\n";
		tst += '<input type="hidden" name="eBody"    value="'+escape(eBody)  +'" />'+"\n";
		tst += '<input type="hidden" name="eTo"      value="'+eTo            +'" />'+"\n";
		tst += '<input type="hidden" name="uTo"      value="'+uTo            +'" />'+"\n";
		tst += '<input type="hidden" name="eCC"      value="'+eCC            +'" />'+"\n";
		tst += '<input type="hidden" name="eBCC"     value="'+eBCC           +'" />'+"\n";
		tst += '<input type="hidden" name="eDate"    value="'+eDate          +'" />'+"\n";
		tst += '<input type="hidden" name="gDate"    value="'+gDate          +'" />'+"\n";
		tst += '<input type="submit" value="Send Email" />'+"\n";
		tst += '</form>'+"\n";

		tst += '</body>'+"\n"+'</html>'+"\n";

		openHtmlWindow( tst, "Preview" );
	}
	else
	{
		var hForm = safeGetElementById('HiddenForm');	// = document.HiddenForm;
		hForm.method = "post";
		hForm.action = frm.action;

		if (typeof hForm.fName != 'undefined') hForm.fName.value = fName;
		if (typeof hForm.lName != 'undefined') hForm.lName.value = lName;
		if (typeof hForm.realname != 'undefined') hForm.realname.value = fName + " " + lName;
		hForm.oPage.value = oPage;
		hForm.eFrom.value = eFrom;
		hForm.eSubject.value = eSubject;
		hForm.eBody.value = escape(eBody);
		if (typeof hForm.eTo != 'undefined') hForm.eTo.value = eTo;
		if (typeof hForm.uTo != 'undefined') hForm.uTo.value = uTo;
		if (typeof hForm.eCC != 'undefined') hForm.eCC.value = eCC;
		if (typeof hForm.eBCC != 'undefined') hForm.eBCC.value = eBCC;
		if (typeof hForm.eDate != 'undefined') hForm.eDate.value = eDate;
		if (typeof hForm.gDate != 'undefined') hForm.gDate.value = gDate;
		hForm.submit();
	}
}
function massageEmailBody( eBody ) { return eBody; }			// override this if you want to
function massagePreviewBody( eBody ) { return uglify(eBody); }	// override this if you want to
