// requires Prototype version 1.6 or greater (http://www.prototypejs.org)

function generateMailtoForLink(linkElement, to, subject, body)
{
	var result = "mailto:";
	
	if (to)
	{
		result += to;
	}
	
	if (subject || body)
	{
		result += "?";
		
		if (subject)
		{
			result += "subject=" + encodeURI(subject);
		}
		
		if (subject && body)
		{
			result += "&body=" + encodeURI(body);
		}
		else
		{
			result += "body=" + encodeURI(body);
		}
	}
	
	if (linkElement)
	{
		linkElement.href = result;
	}
}

function showEmailForm(emailSectionID, emailFormName)
{
	var emailSection = window.document.getElementById(emailSectionID);
	var emailForm = window.document.forms[emailFormName];
	
	if (emailSection && emailForm)
	{
		emailForm.successfulReturnURL.value = window.location.href;
		emailForm.errorReturnURL.value = window.location.href;
		emailForm.pageURL.value = window.location.href;
		
		//emailSection.style.display = "block";
		Effect.Appear(emailSectionID, { duration: 0.5, afterFinish: function(effect) {
			emailForm.senderName.focus();
		}});
	}
}

function cancelEmailForm(emailSectionID, emailFormName)
{
	var emailSection = $(emailSectionID);
	var emailForm = window.document.forms[emailFormName];
	
	if (emailSection && emailForm)
	{
		//emailSection.style.display = "none";
		Effect.Fade(emailSectionID, { duration: 0.5, afterFinish: function(effect) {		
			clearErrSection(emailSection, "emailErrors");
		
			emailForm.successfulReturnURL.value = "";
			emailForm.errorReturnURL.value = "";
			emailForm.pageURL.value = "";
			emailForm.senderName.value = "";
			emailForm.toAddress.value = "";
		}});
	}
}

function clearErrSection(emailSection, errSectionID)
{
	var errSection = getErrorSection(emailSection, errSectionID);
	
	if (errSection)
	{
		errSection.hide();
		errSection.childElements().each(function(elem) {
			elem.remove();
		});	
	}
}

function getErrorSection(emailSection, errSectionID)
{
	var errSection = null;
	
	if (emailSection && errSectionID && $(emailSection))
	{
		errSection = $(emailSection).select("#" + errSectionID);
		
		if (errSection.length > 0)
		{
			errSection = errSection[0];
		}
		else
		{
			errSection = null;
		}
	}
	
	return errSection;
}

function validateEmailForm(emailSectionID, emailFormID)
{
	if (!emailSectionID || !emailFormID || !$(emailSectionID) || !$(emailFormID))
	{
		return false;
	}

	var emailForm = $(emailFormID);
	
	if (!emailForm.visible())
	{
		return false;
	}
	
	var emailSection = $(emailSectionID);
	
	clearErrSection(emailSection, "emailErrors");
	
	var blankStringRE = /^\s*$/;
	var validEmailFormatRE = /^\s*\S+@\S+\.\S+\s*$/;
	
	var errSection = getErrorSection(emailSection, "emailErrors");
	var bValid = true;
	
	if (blankStringRE.test($F(emailForm.senderName)))
	{
		addErrString(errSection, "You must provide a value for your name");
		bValid = false;
	}
	
	if (blankStringRE.test($F(emailForm.toAddress)))
	{
		addErrString(errSection, "You must provide an email address to send this page to");
		bValid = false;
	}
	else if (!validEmailFormatRE.test($F(emailForm.toAddress)))
	{
		addErrString(errSection, "The email address must be of the form 'someone@somewhere.com'");
		bValid = false;
	}
	
	if (!bValid)
	{
		if (errSection)
		{
			errSection.style.display = "block";
		}
	}
	
	return bValid;
}

function addErrString(errSection, errStr)
{
	if (errSection && $(errSection))
	{
		$(errSection).insert(new Element("p").update(errStr));
	}
}