// -- -   T H e   J a V a S C R i P T   P o S T a L   W o R K e R   - -- //
// Author: mattc
// contains hacks of code by Stephen Poley
// http://www.xs4all.nl/~sbpoley/webmatters/formval.js

// == =                        G L o B a L S                        = == //
var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
emptyString = /^\s*$/

// == =           F u N C T i o N   D e F i N i T i o N S           = == //
function trim(str) {
// trim leading and trailing whitespace from str
  return str.replace(/^\s+|\s+$/g, '')
} // end function trim
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //

function alertMessage (elem_id, err_type, message_str) {
// put an alert for the user that there is a problem
// an empty message_str erases a previous message
	
	// setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var message_disp;
  if (emptyString.test(message_str)) 
    message_disp = String.fromCharCode(nbsp);    
  else  
    message_disp = message_str;

  var elem = document.getElementById(elem_id);
  elem.firstChild.nodeValue = message_disp;
  
  elem.className = err_type;
} // end function errorAlert
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //

function isEmpty (element) {
// returns whether the 'element' is empty or not
	if ( !element.value ) return true;
	if ( element.value == null || element.value == '' ) return true;
	return false;
}

function validateInput (element) {
// validate the input 'element'
	
	// alert (element.id + ': ' + element.value);
	
	switch (element.id) {
		case 'sender_name':
			if (isEmpty (element)) {
				alertMessage ('name_error', 'error', ' Name is required.');
				return;
			}
			alertMessage ('name_error', 'error', ''); // no problems
			break;
		case 'sender_email':
			if (isEmpty (element)) {
				alertMessage ('email_error', 'error', ' e-mail address is required.');
				return;
			}
			
			var return_addr = trim(element.value);
			var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
			if (!email.test(return_addr)) {
				document.getElementById(element.id).focus();
				alertMessage ('email_error', 'error', ' Not a valid e-mail address.');
				return false;
			}
		
			var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
			if (!email2.test(return_addr)) {
				alertMessage ('email_error', 'warning', 'Unusual address - check if correct.');
				return true;
			}
			alertMessage ('email_error', 'warning', ''); // no problems
			break;
		case 'sender_url':
			break;
		case 'sender_subject':
			break;
		case 'sender_message':
			if (isEmpty (element)) {
				alertMessage ('message_error', 'error', ' Message is required.');
				return;
			}
			alertMessage ('message_error', 'error', ''); // no problems
			break;
	}
	return true;
} // end function validateInput
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //

function validatePostage () {
// This function checks a mail form
// to ensure that all required information
// is submitted and valid
	
	// REQUIRED FIELDS: name, email, message
	
	var error = 0;
	
	if ( !document.getElementById ('contact_form') ) {
		// they aren't even on the contact page
		alert ('What are trying to do, mail a bomb?');
		return;
	}
	
	// check for sender name
	if ( !validateInput (document.getElementById ('sender_name')) ){
		if (!error) document.getElementById ('sender_name').focus();
		error++;
	}
	
	// check for sender email
	if ( !validateInput (document.getElementById ('sender_email')) ){
		if (!error) document.getElementById ('sender_email').focus();
		error++;
	}
	
	// check for message
	if ( !validateInput (document.getElementById ('sender_message')) ){
		if (!error) document.getElementById ('sender_message').focus();
		error++;
	}
	
	if ( error ) {
		alert ('There is an error in the information you have submitted.\nPlease correct before submitting your message.');
		return false;
	}
	
	// if you got this far, everything is good 
	// so send the message
	document.getElementById('contact_form').submit();
	return true;
	
} // end function validatePostage
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //

//                                                        . . .  e o F  // 