/****************************************************/
/*		Contact form functions										*/


//VALIDATE FORM ENTRIES
//	
//	basically, I'm requiring that there be an e-mail address, something in the subject area
//	and something in the message area.  The subject line can be what I've provided, or it
//	can be changed, but it can't be blank.
//	
//	Then I got nerdy and decided to parse the e-mail address to ensure it had proper
//	form.  I don't actually check for specific tld's.  There are too many to check easily
//and they keep changing.  So I just check that the tld has *at least* two characters.
//
//It turns out that the first part of an e-mail address can have quotation marks.  
//I didn't get that part worked out properly.  The rest appears to be ok.
//

function validateContact()
{
	
		var contactOk=true;
		
		var fromOk= true;
		if ((document.forms[0].elements['fromemail'].value == "") ||
			(document.forms[0].elements['fromemail'].value == "your e-mail address"))
		{
			fromOk=false;
		}

		var formatOk = true;
		if (!validEMail(document.forms[0].elements['fromemail'].value))
			formatOk = false;

		var subjectOk=true;
		if (document.forms[0].elements['subject'].value == "")
		{
			subjectOk=false;
		}
				
		var messageOk=true;
		if ((document.forms[0].elements['message'].value == "") ||
			(document.forms[0].elements['message'].value == "Please leave me a message.  Thanks!"))
		{
			messageOk=false;
		}


		if(!fromOk)
		{
			contactOk=false;
			alert ("e-mail address required");
			document.forms[0].elements['fromemail'].focus();
		}
		else if(!formatOk)
		{
			contactOk=false;
			alert ("e-mail address format is invalid");
			document.forms[0].elements['fromemail'].focus();
		}
		else if(!subjectOk)
		{
			contactOk=false;
			alert ("Subject required");
			document.forms[0].elements['subject'].focus();
		}
		else if(!messageOk)
		{
			contactOk=false;
			alert ("Message required");
			document.forms[0].elements['message'].focus();
		}

	return contactOk;
}

/*PARSE E-MAIL ADDRESS to see if it is more-or-less valid in format*/
//	see http://en.wikipedia.org/wiki/E-mail_address
// or http://karmak.org/archive/2003/02/validemail.html
//	I more-or-less made this up myself, however, because I needed to do things
//	in baby steps so I could understand what I was doing.
//
//	there's more to it that I've worked out.  I'm not yet allowing quotation marks in the local part
//	and, as I understand it, one could do a better job of parsing the domain part for instances in which
//	one were using an IP address for the mail.  In the long run, the best bet is to do server-side checking
//	as demonstrated by the illustrious Mark Wilton-Jones: http://www.howtocreate.co.uk/php/
//
function validEMail(address)
{

	//One and only one '@'
	var AT = address.indexOf('@');
	var lastAT =  address.lastIndexOf('@');
	
	if ((AT == -1) || (AT != lastAT))
		return false;
		
	var localPart = address.substring(0, AT);
	var domainPart = address.substring(AT+1);
	
	/*------------------------------------------------*/
	/*parse the local part                                          */

	//64 or fewer characters
	if(localPart.length>64)
		return false;
		
	//no period at beginning or end of string
	var dot = localPart.indexOf('.');
	var lastDot = localPart.lastIndexOf('.');

	if ((dot == 0) || (lastDot == AT-1))
		return false;
		
	//an invalid character {only letters, numbers, hyphen, period and the following:
	//	"!", "#", "$", "%", "&", "'", "*", "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"}
	//	I couldn't work out how to get the quotation-mark issue resolved
	var invalidPattern = /[^A-Za-z\-0-9\.!#\$\%&\*\+\/=\?\^_`\{\}\|~]/g;
	var invalidChar = localPart.match(invalidPattern);
	
	if (invalidChar != null)
		return false;

	
	/*------------------------------------------------*/
	/*parse the domain part                                      */
	
	//255 or fewer characters
	if(domainPart.length>255)
		return false;

	//can't begin domain with a period
	dot = domainPart.indexOf('.');
	if (dot == 0)
		return false;

	//must have at least two characters after last dot
	lastDot = domainPart.lastIndexOf('.');
	if (((domainPart.length - 1) - lastDot) < 2)
		return false;

	//an invalid character {only letters, numbers, hyphen and period allowed}
	invalidPattern = /[^A-Za-z\-0-9\.]/g;
	invalidChar = domainPart.match(invalidPattern);

	if (invalidChar != null)
		return false;

	//	If we're got here, we must have a valid format (other than the quotation issue, and some IP format fine points)
	return true;
	
}