//-----------------------------------------------------------------------------
// openpopup(url, windowtarget)
//
// displays a url in a popup window
//-----------------------------------------------------------------------------
function openpopup(url, windowtarget, w, h) 
{
	window.open
	(
		url, 
		windowtarget,
		"width=" + w + ", height=" + h + ", toolbar=0, location=0, left=300, top=300, directories=0, status=0, menubar=0, scrollbars=1, resizable=0"
	);
}

//-----------------------------------------------------------------------------
// isValidEmail(email)
//
// checks for valid formation of an email address.
// see comments for specific checks.
//-----------------------------------------------------------------------------
function isValidEmail(email)
{
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';	
	var atPos = email.indexOf('@', 0);
	var suffix = email.substring(email.lastIndexOf('.') + 1);
	
	for (i=0; i < invalidChars.length; i++)
	
   		if (email.indexOf(invalidChars.charAt(i), 0) > -1)
      		// email address contains invalid characters
      		return false;

	for (i=0; i < email.length; i++)

		if (email.charCodeAt(i) > 127)
			// email address contains non-ascii characters
   	   		return false;
   
	if (atPos == -1)
   		// email address must contain an @
   		return false;

	if (atPos == 0)
		// email address must not start with @
   		return false;

	if (email.indexOf('@', atPos + 1) > - 1)
   		// email address must contain only one @
   		return false;

	if (email.indexOf('.', atPos) == -1)
   		// email address must contain a period in the domain name
   		return false;

	if (email.indexOf('@.', 0) != -1)
   		// period must not immediately follow @ in email address
   		return false;

	if (email.indexOf('.@', 0) != -1)
   		// period must not immediately precede @ in email address
		return false;

	if (email.indexOf('..', 0) != -1)
		// two periods must not be adjacent in email address
		return false;

	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' &&
		suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' &&
		suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' &&
		suffix != 'museum')
		// invalid primary domain in email address
		return false;

	return true;
}

//-----------------------------------------------------------------------------
// hideAllErrors()
//
// hides all the error message elements matching the IDs by changing their
// display attribute to "none".
//-----------------------------------------------------------------------------
function hideAllErrors()
{
	document.getElementById("tyMsg").style.display = "none";
	document.getElementById("nameError").style.display = "none";
	document.getElementById("emailError").style.display = "none";
	document.getElementById("emailBad").style.display = "none";
	document.getElementById("commentError").style.display = "none";
}

//-----------------------------------------------------------------------------
// resetForm()
//
// hides all the error message elements, resets the form, and sets focus to
// the name field.
//-----------------------------------------------------------------------------
function resetForm()
{
	hideAllErrors();
	document.getElementById('form').reset();
	document.getElementById('name').focus();
}
	
//-----------------------------------------------------------------------------
// checkForm()
//
// validates the form. If invalid, displays error messages. If valid, displays
// success message and posts form to email cgi program.
//
// 4 levels of validation are possible, depending on arguments:
//
// ckName ckEmail
//   0      0          checks only for presence of comment
//   0      1          checks for valid email and comment
//   1      0          checks for presence of name and comment
//   1      1          checks for presence of name, valid email, and comment
//-----------------------------------------------------------------------------
function checkForm(ckName, ckEmail)
{
	var name = document.getElementById("name").value;
	var email = document.getElementById("email").value;
	var comment = document.getElementById("comment").value;
  
	if (ckName && name == "")
	{
		hideAllErrors();
		document.getElementById("nameError").style.display = "inline";
		document.getElementById("name").select();
		document.getElementById("name").focus();

		return false;
	}

	else if (ckEmail && email == "")
	{
		hideAllErrors();
		document.getElementById("emailError").style.display = "inline";
		document.getElementById("email").select();
		document.getElementById("email").focus();

		return false;
	}

	else if (ckEmail && !isValidEmail(email))
	{
		hideAllErrors();
		document.getElementById("emailBad").style.display = "inline";
		document.getElementById("email").select();
		document.getElementById("email").focus();
		
		return false;
	}	

	else if (comment == "")
	{
		hideAllErrors();
		document.getElementById("commentError").style.display = "inline";
		document.getElementById("comment").select();
		document.getElementById("comment").focus();

		return false;
	}

	hideAllErrors();
	document.getElementById("tyMsg").style.display = "inline";

	document.getElementById('form').action = "http://anewtechniquesalon.com/cgi-bin/novice.cgi"; 
	document.getElementById('form').method = "post"; 
	document.getElementById('form').submit(); 

	return true;
}


