function doNav( id ) {
	// if the area exists
	if( document.getElementById( 'nav_area_' + id ) != 'null' ) {
		// if its closed
		if( document.getElementById( 'nav_area_' + id ).style.display == 'none' ) {
			// open it
			document.getElementById( 'nav_area_' + id ).style.display = '';
			// change image
			document.getElementById( 'nav_image_' + id ).src = 'images/menu-open.gif';
		} else {
			// close it
			document.getElementById( 'nav_area_' + id ).style.display = 'none'
			// change image
			document.getElementById( 'nav_image_' + id ).src = 'images/menu-closed.gif';
		}
	}
}
function closeNav() {
	var navCount = 0;
	while( document.getElementById( 'nav_area_' + navCount ) != null ) {
		document.getElementById( 'nav_area_' + navCount ).style.display = 'none';
		navCount++;
	}
}
function inputBoxFocus( input, defaultText ) {
	if( input.value == defaultText ) {
		input.value = '';
	} 
}
function inputBoxBlur( input, defaultText ) {
	if( input.value == '' ) {
		input.value = defaultText;
	}
}
function checkForm() {
	var validator = new validateForm();
	if( document.getElementById( 'lhs_contact_name' ).value != ' - Contact Name' ) {
		validator.checkText( 'lhs_contact_name', 'Contact name' );
	} else {
		validator.addCustomError( 'Contact name' );
	}
	if( document.getElementById( 'lhs_company_name' ).value != ' - Company Name' ) {
		validator.checkText( 'lhs_company_name', 'Company Name' );
	} else {
		validator.addCustomError( 'Company Name' );
	}
	if( document.getElementById( 'lhs_contact_email' ).value != ' - Contact Email' ) {
		validator.validateEmailAddress( 'lhs_contact_email', 'Contact Email' );
	} else {
		validator.addCustomError( 'Contact Email' );
	}
	if( document.getElementById( 'lhs_contact_telephone' ).value != ' - Contact Telephone' ) {
		validator.checkText( 'lhs_contact_telephone', 'Contact Telephone' );
	} else {
		validator.addCustomError( 'Contact Telephone' );
	}
	if( document.getElementById( 'lhs_enquiry_summary' ).value != ' - Enquiry Summary' ) {
		validator.checkText( 'lhs_enquiry_summary', 'Enquiry Summary' );
	} else {
		validator.addCustomError( 'Enquiry Summary' );
	}
	
	if ( validator.numberOfErrors() ) {
		validator.displayErrors();
		return false;
	} else {
		return true;
	}
}

function checkRMAForm() {
	var validator = new validateForm();
	validator.checkText( 'rma_contact_name', 'Contact name' );
	validator.validateEmailAddress( 'rma_email_address', 'Email Address' );
	validator.checkText( 'rma_contact_telephone', 'Contact Telephone' );
	validator.checkText( 'rma_company_name', 'Company name' );
	validator.checkText( 'rma_company_address_1', 'Company Address' );
	validator.checkText( 'rma_town', 'Town' );
	validator.checkText( 'rma_postcode', 'Postcode' );
	validator.checkText( 'rma_part_numbers', 'Part Number(s)' );
	validator.checkText( 'rma_serial_numbers', 'Serial Number(s)' );
	if ( validator.numberOfErrors() ) {
		validator.displayErrors();
		return false;
	} else {
		return true;
	}
}

function validateForm() {
		
	// holds an array of errors to output
	var fieldErrorArray = new Array();
	
	// private accessor methods
	this.checkText = checkText;
	this.validateEmailAddress = validateEmailAddress;
	this.displayErrors = displayErrors;
	this.numberOfErrors = numberOfErrors;
	this.addCustomError = addCustomError;
	
	// returns the array of errors
	function getErrors() {
		return fieldErrorArray;
	}
	
	// ensures text has been entered
	function checkText ( element, output ) {
		if (document.getElementById( element ).value == '') {
			fieldErrorArray.push(output);
		}
	}
	
	// checks the value chosen is not the passed default value
	function validateEmailAddress ( emailAddress, output ){
		if( document.getElementById( emailAddress ) )
			emailAddress = document.getElementById( emailAddress ).value;
		if( typeof( output ) == 'undefined' )
			output = 'Email address is invalid';
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if ( !filter.test( emailAddress )){
			fieldErrorArray.push( output );
		}
	}

	
	// add a custom error to the array
	function addCustomError( errorMessage ){
		
		fieldErrorArray.push( errorMessage );
		
	}
	
	// returns the number of errors found
	function numberOfErrors(){
		
		return fieldErrorArray.length;
		
	}
	
	// alerts all current errors to the user	
	function displayErrors(){
		
		var output = '';
		
		for(i=0;i<fieldErrorArray.length;i++){
			
			output += ' - ' +fieldErrorArray[i] + '\n';
			
		}
		
		alert('The following fields have not been completed:\n\n' + output);
		
	}
	
}
