// File: tntc.js
// Date: 10/08/2006
// Author: Jason Johnson - jjohnson@jmjproit.com
//
// Purpose: 
//	Provides form validation for the tntplus.com contact form by ensuring
//	the user provides a question or comment and a valid email address and/or 
//      telephone number. Also tests for a valid fax number if it is entered.
//	Ensures that the user cannot accidentally submit the form more than once.
/////////////////////////////////////////////////////////////////////////////////



// Function:	handleSubmitButton 
// Arguments:	None
// Returns:	None
// 
// Purpose: 
//	When the submit button is pressed, this function takes data from
//	the form and validates it against a set of rules.  If the input(s)
//	are valid, the form is submitted to the script that handles the form.
//	If the input(s) are invalid, we alert the user and allow them to make
//	changes.  Also keeps the user from accidentally submitting the form
//	more than once by disabling the submit button after it is pressed.
function handleSubmitButton() {

	// Disable the submit button for now
	var oButton = document.getElementById("formsubmit");
	oButton.disabled = true;

	// Get the form we're eventually going to submit
	var oForm = document.getElementById("tntcontact");
	
	// Get the input we're working with
	var oComments = document.getElementById("Comments");
	var oUserEmail = document.getElementById("UserEmail");
	var oUserTel = document.getElementById("UserTel");
	var oUserFax = document.getElementById("UserFAX");
		

	// Ensure the comment box is not empty
	if (oComments.value.length == 0) 
	{
		alert("Please enter a question or comment.");
		oButton.disabled = false;
		oComments.focus();
		return;
	}


	// See which of email, telephone, or both are entered.
	var bEmail = false;
	var bTelephone = false;

	if (oUserEmail.value.length > 0)
		bEmail = true;
	if (oUserTel.value.length > 0)
		bTelephone = true;

	// If we don't have either email or tel, we need to alert the user
	// and let them enter either or both.
	if (!bEmail && !bTelephone) {
		alert("Please enter an email address and\/or telephone number.");
		oButton.disabled = false;
		oUserEmail.focus();
		oUserEmail.select();
		return;
	}

	// If there is an email address, check for validity
	if (bEmail) {
		if (!validEmail(oUserEmail.value)) {
			// Alert the user to the error
			error_msg = "You have entered an email address that does not appear to be valid.\n";
			error_msg += "Valid examples:\n";
			error_msg += "johndoe@domain.com\n john.doe@mailserver.com\n john.doe@mail.co.uk\n";
			alert(error_msg);

			// Give user a chance to fix the error
			oButton.disabled = false;
			oUserEmail.focus();
			oUserEmail.select();
			return; 
		}
	} // end check for valid email
			

	// If there is a telephone number, check for validity
	if (bTelephone) {
		if (!validTel(oUserTel.value)) {
			// Alert the user to the error
			error_msg = "You have entered a telephone number that does not appear to be valid.\n";
			error_msg += "To add a telephone extension use:  ";
			error_msg += "x1234  (where 1234 is your extension)\n\n";
			error_msg += "Valid examples:\n";
			error_msg += "6186542711\n618.654.2711\n";
			error_msg += "618-654-2711\n";
			error_msg += "(866) 316-2350\n";
			error_msg += "1-866-316-2350 x1234\n";	
			alert(error_msg);

			// Give user chance to fix the error
			oButton.disabled = false;
			oUserTel.focus();
			oUserTel.select();
			return;			
		}
	} // end check for valid phone

	// Validate the fax number if it is entered.
	if (oUserFax.value.length > 0) {
		if ( !validFax(oUserFax.value) )
		{
			alert("You have entered a fax number that does not appear to be valid.\n");
			oButton.disabled = false;
			oUserFax.focus();
			oUserFax.select();
			return;
		}
	} // end of testing for fax


	// Now we have at least either a valid email, a valid telephone, or both.
	// We are ready to send the form.
	oForm.submit();
	return;

} // end handleSubmitButton



// Function:	validEmail
// Arguments:	String sEmail
// Returns:	True if valid.
//		False if invalid.
// 
// Purpose: 
//	For testing whether a string is in the format of a valid email address.
//	
// Note:
//	Does not test that the email address actually exists.  Only test that the
//	string -could be- a valid email address.
//	Examples: username@mail.com,  user.name@mail.com
//		  username@mail.co.uk, user.name@mail.co.uk, etc...

function validEmail(sEmail) {
	var reValidMail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
	return reValidMail.test(sEmail);
} // end validEmail



// Function:	validTel
// Arguments:	String sTel
// Returns:	True if valid.
//		False if invalid.
// 
// Purpose: 
//	For testing whether a string is in the format of a valid telephone number.
//	(i.e., 3142219999  or (314) 221-9999 or 1-314-221-9999, 314.221.2032, etc.)
//	Tests for an optional 1, an area code, prefix, and suffix, separated by
//	either a space, period, or hyphen, or no separation at all.  Also allows an 
// 	optional extension by placing "x12345" at the end, where 12345 is the extension.

function validTel(sTel) {
	var reValidTel = /^\d?(\s|\.|-)*\(?\d{3}\)?(\s|\.|-)*\d{3}(\s|\.|-)*\d{4}(\s|\.|-)*(?:(x|X){1}\d+)?$/;
	return reValidTel.test(sTel);
} // end validTel


// Function:	validFax
// Arguments:	String sFax
// Returns:	True if valid.
//		False if invalid.
//
// Purpose:
//	Verifies that a string is a valid fax number.
//	Works the same way as validTel, but does not allow an extension.

function validFax(sFax) {
	var reValidFax = /^\d?(\s|\.|-)*\(?\d{3}\)?(\s|\.|-)*\d{3}(\s|\.|-)*\d{4}$/;
	return reValidFax.test(sFax);
} // end validFax

