var emailRegex = /^ *("[^"]+"|[-!#-'*+\/0-9=?A-Z\\^_`a-z{-~]+)@(([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}) *$/
var phoneRegex = /^ *([-+,\/_ ().]*([0-9a-zA-Z][-+,\/_ ().]*){6,}[0-9a-zA-Z][-+,\/_().]*) *$/
var generalRegex = /^ *(n.a|([a-zA-Z0-9].*){4}) *$/

var sCgi;
var sProg;
var elementForm;
var elementHiddenParent;
var sSaveBackground;
var sFrom;
var sTo;
var sCc;
var sHost;
var sInvalidBackgroundColor;
var sInvalidFieldPrefix;
var sInvalidMatchSuffix;
var sInvalidMatchSentence;
var sReply;
var sEmail;

function setOnSubmit(formId, hiddenParentId, saveBackgroundId, cgi, prog, from, to, cc, host,
		invalidBackgroundColor, invalidFieldPrefix, invalidMatchSuffix, invalidMatchSentence,
		reply, email)
{
	elementHiddenParent = document.getElementById(hiddenParentId);
	if (elementHiddenParent == null)
		alert("The hidden parent element with ID \"" + hiddenParentId + "\" was not found.");

	elementForm = document.getElementById(formId);
	if (elementForm == null)
		alert("The form element with ID \"" + formId + "\" was not found.");

	var element = document.getElementById(saveBackgroundId);
	if (element == null)
		alert("The save element with ID \"" + saveBackgroundId + "\" was not found.");
	else if (element.style == null)
		alert("The save element with ID \"" + saveBackgroundId + "\" had no associated style.");
	else
		sSaveBackground = element.style.backgroundColor;

	sCgi = cgi;
	sProg = prog;

	sFrom = from;
	sTo = to;
	sCc = cc;
	sHost = host;

	sInvalidBackgroundColor = invalidBackgroundColor;
	sInvalidFieldPrefix = invalidFieldPrefix;
	sInvalidMatchSuffix = invalidMatchSuffix;
	sInvalidMatchSentence = invalidMatchSentence;
	
	sReply = reply;
	sEmail = email;
}

function onSubmit(subject)
{
	elementHiddenParent.innerHTML
		= hif("replyd", sReply)
		+ hif("emaild", sEmail)
		+ hif("navigd", "../navigation.html")
		+ hif("analyticd", "../ga.js")
		+ hif("replys", "table")
		+ hif("emails", "table")
		+ hif("navigs", "nav")
		+ hif("analytics", "analytic")
		+ hif("tableclass", "sent-table")
		+ hif("from", sFrom + "@" + sHost)
		+ hif("to", sTo + "@" + sHost)
		+ hif("cc", sCc == null ? "" : (sCc + "@" + sHost))
		+ hif("subject", subject)
		+ hif("source page", window.location)
		+ hif("width", "12")
		+ hif("obscure", "^(submit[0-9]*|source page)$")
		+ hif("exclude", "^submit[0-9]*$")
		+ hif("include", "^(REMOTE_ADDR|REQUEST_URI)$");

	elementForm.action = sCgi + "/" + sProg;

	var bSender = generalRegex.test(elementForm.fullname.value);
	var bEmail = emailRegex.test(elementForm.email.value);
	var bEmailCopy = elementForm.emailcopy == null
			|| emailRegex.test(elementForm.emailcopy.value);
	var bEmailMatch = elementForm.emailcopy == null
			|| elementForm.emailcopy.value == elementForm.email.value;
	var bPhone = phoneRegex.test(elementForm.phone.value);   
	if (bSender && bEmail && bEmailCopy && bEmailMatch && bPhone)
		return true;

	markIfInvalid(bPhone, elementForm.phone);
	markIfInvalid(bEmailCopy && bEmailMatch, elementForm.emailcopy);
	markIfInvalid(bEmail && bEmailMatch, elementForm.email);
	markIfInvalid(bSender, elementForm.fullname);

	var s = "";
	var i = 0;
	if (! bSender) { s = s + ", name"; ++ i; }	
	if (! bEmail) { s = s + ", email address (i.e. joe@gmail.com)"; ++ i; }
	if (! bEmailCopy) { s = s + ", email address copy"; ++ i; }
	if (! bPhone) { s = s + ", phone number"; ++ i; }
	if (i > 0)
	{
		s = s.replace(/^, /, sInvalidFieldPrefix);
		if (i == 1)
			s = bEmailMatch
				? s + "."
				: s + "," + sInvalidMatchSuffix;
		else
			s = bEmailMatch
				? s.replace(/, ([^,]+)$/, " and $1.")
				: s.replace(/, ([^,]+)$/, " and $1;" + sInvalidMatchSuffix);
	}
	else if (! bEmailMatch)
	{
		s = sInvalidMatchSentence;
	}

	alert(s);

	return false;
}

function restoreBackground(element)
{
	element.style.backgroundColor = sSaveBackground;
}

function markIfInvalid(bValid, inputElement)
{
	if (bValid)
		return;

	inputElement.style.backgroundColor = sInvalidBackgroundColor;
	inputElement.focus();
}

function hif(sName, sValue)
{
	return "<input type=\"hidden\" name=\"" + sName + "\" value=\"" + sValue + "\" />";
}
