var bDebug = false;

function getAnHttpRequest()
{
    if (window.XMLHttpRequest)
	{
    	try
		{
			return new XMLHttpRequest();
		}
		catch(e)
		{
			return null;
		}
    }
	else if (window.ActiveXObject)
	{
       	try
		{
        	return new ActiveXObject("MSXML2.XMLHTTP.3.0");
      	}
		catch(e)
		{
			try
			{
				return new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					return new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
					return null;
				}
			}
		}
    }
}

function replaceInnerHtml(url, elementId)
{
	var request = getAnHttpRequest();
	if (request == null)
		return;

	var element = document.getElementById(elementId);
	if (element == null)
		return;

	request.open('GET', url, false);
	request.send(null);
	element.innerHTML = request.responseText;
}

var sInvalidBackgroundColor;
var sInvalidFieldPrefix;
var sInvalidMatchSuffix;
var sInvalidMatchSentence;

function setValidationParameters(invalidBackgroundColor, invalidFieldPrefix, invalidMatchSuffix, invalidMatchSentence)
{
	sInvalidBackgroundColor = invalidBackgroundColor;
	sInvalidFieldPrefix = invalidFieldPrefix;
	sInvalidMatchSuffix = invalidMatchSuffix;
	sInvalidMatchSentence = invalidMatchSentence;
}

var sCgi;
var sProg;

function setSubmitAction(cgi, prog)
{
	sCgi = cgi;
	sProg = prog;
}

function hifs(from, to, host, subject)
{
	var s = hif("replyd", "you.have.sent.html"); <!-- reply document template -->
	s += hif("emaild", "basic.email"); <!-- email document template -->
	s += hif("navigd", "/navigation.html"); <!-- navigation document partial -->
	s += hif("analyticd", "/ga.js"); <!-- analytic document partial -->
	s += hif("replys", "<!--table-->"); <!-- reply document table insertion key -->
	s += hif("emails", "table"); <!-- email document table insertion key -->
	s += hif("navigs", "<!--nav-->"); <!-- navigation document key for search and replace -->
	s += hif("analytics", "<!--analytic-->"); <!-- analytic document key for search and replace -->

	s += hif("from", from + "@" + host);
	s += hif("to", to + "@" + host);
	s += hif("subject", subject);

	s += hif("source page", window.location);

	s += hif("width", "12"); <!-- email tab expansion to align values -->
	s += hif("exclude", "Submit|root"); <!-- regex of parameters to exclude -->
	s += hif("obscure", "source page"); <!-- regex of parameters obscured in reply -->
	s += hif("include", "REMOTE_ADDR|HTTP_REFERER"); <!-- env vars to include in email -->
	s += hif("tableclass", "sent-data-table"); <!-- reply document cell padding -->

	return s;
}

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

function saveBackground(saveElementId)
{
	var element = document.getElementById(saveElementId);
	if (element == null)
		return;
	if (element.fullname == null)
		return;

	sSaveBackground = element.style.backgroundColor;
}

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

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

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

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][-+,\/_().]*) *$/

// returns true iff the form is to be submitted
function wedgeContact(formElementId)
{
	// get the form element
	var formElement = document.getElementById(formElementId);
	if (formElement == null)
	{
		if (bDebug)
			alert("The form element with ID \"" + formElementId + "\" was not found.");
		return ! dDebug;
	}

	// assign the destination on the fly to elude spamers
	formElement.action = sCgi + "/" + sProg;

	// validate fields
	var bSender = formElement.fullname.value.length >= 2;
	var bEmail = emailRegex.test(formElement.email.value);
	var bEmailCopy = formElement.emailcopy == null
			|| emailRegex.test(formElement.emailcopy.value);
	var bEmailMatch = formElement.emailcopy == null
			|| formElement.emailcopy.value == formElement.email.value;
	var bPhone = phoneRegex.test(formElement.phone.value);                                         
	if (bSender && bEmail && bEmailCopy && bEmailMatch && bPhone)
	{
		if (bDebug)
			alert("All okay.");
		return ! bDebug;
	}

	// highlight background of errors and set focus to first invalid field
	// (so check in reverse order)
	markIfInvalid(bPhone, formElement.phone);
	markIfInvalid(bEmailCopy && bEmailMatch, formElement.emailcopy);
	markIfInvalid(bEmail && bEmailMatch, formElement.email);
	markIfInvalid(bSender, formElement.fullname);

	// provide a clean user message
	var s = "";
	var i = 0;
	if (! bSender)
	{
		s = s + ", name";
		++ i;
	}	
	if (! bEmail)
	{
		s = s + ", email address";
		++ 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 initContact()
{
	setValidationParameters(
			"#ffcccc",
			"Please enter a valid ",
			"\nand please get the email addresses to match.",
			"Please get the email addresses to match.");
	setSubmitAction(
			"http://theslaajournal.org/cgi-bin",
			"reply.and.email");
	saveBackground('form1');
}

function writeHidden(source)
{
	document.write(hifs("allau", "correspondence", "theslaajournal.org", source));
}
