//define reg_exps
var name_regexp = /^[a-zA-Z \'\-]+$/;
var email_regexp = /^[^@ ,]+@[^@ ,]+\.[a-zA-Z]{2,10}$/;
var telephone_regexp = /^[0-9 \(\)\[\]\{\}\-\+]+$/;
var message_regexp = /^[\s\S]+$/;

//hide the HTML selector, show the DHTML selector
if( $('name') ) { //if this element exists -i.e. not just processed a request
	
	//if the form items are filled in then validate
	if( $('name').value.length > 0 ) validate_input( $('name'), name_regexp );
	if( $('email').value.length > 0 ) validate_input( $('email'), email_regexp );
	if( $('telephone').value.length > 0 ) validate_input( $('telephone'), telephone_regexp );
	if( $('company').value.length > 0 ) validate_input( $('company'), message_regexp );
	if( $('message').value.length > 0 ) validate_input( $('message'), message_regexp );
	
	//stop the enter key submitting the form
	$('name').onkeypress = cancel_enter;
	$('email').onkeypress = cancel_enter;
	$('telephone').onkeypress = cancel_enter;
	$('company').onkeypress = cancel_enter;
	$('message').onkeypress = cancel_enter;

}

function cancel_enter( e ) {
	var key_pressed = window.event ? window.event.keyCode : e.which;
	if( key_pressed == 13 ) return false; //if enterr
}

function validate_input( elm, reg_exp ) {

	$(  elm.id + '_result' ).src = 'images/' + ( elm.value.length == 0 ? 'spacer' : ( elm.value.match( reg_exp ) ? 'tick' : 'cross' ) ) + '.png';
	
}

function check_quote_form() {

	var name = $('name').value;
	var email = $('email').value;
	var telephone = $('telephone').value;
	var message = $('message').value;
	
	//check everything
	if( name.length == 0 ) show_error( 'name', 'quote' );
	else if( !name.match( name_regexp ) ) show_error( 'name_invalid', 'quote' );
	else if( email.length > 0 && !email.match( email_regexp ) ) show_error( 'email_invalid', 'quote' );
	else if( telephone.length == 0 ) show_error( 'telephone', 'quote' );
	else if( !telephone.match( telephone_regexp ) ) show_error( 'telephone_invalid', 'quote' );
	//else if( message.length == 0 || message.value == '<br>' ) show_error( 'message', 'quote' );
	else return true;

	return false;

}


function show_error( type, form ) {

	close_callback();

	set_opacity( $('mask'), 0 );
	fade( $('mask'), 0, 100 );
	setTimeout( function() {
		$('error_container').style.visibility = 'visible';
	}, 400 );

	var msg =
		'Sorry, your request couldn\'t be sent just then because there was a problem, detailed below. ' + 
		'Please review this and try again.\n\n';

	switch( type ) {
		
		case 'name': msg+= "You didn't enter a contact name.\n"; break;
		case 'name_invalid': msg+= "You didn't enter a valid contact name. A contact name should only contain letters, spaces, hyphens and single quote marks.\n"; break;
		case 'email_invalid': msg+= "You didn't enter a valid email address. An email address should be in the format yourname@something.com\n"; break;
		case 'telephone': msg+= "You didn't enter a contact telephone number.\n"; break;
		case 'telephone_invalid': msg+= "You didn't enter a valid contact telephone number. \n\nA telephone number should only contain numbers, <br />spaces and, if needed, hyphens and brackets.\n"; break;
		case 'message': msg+= "You didn't enter a message. Please leave a short message telling us briefly about the work you need quoting for.\n"; break;

	} //end switch

	alert( msg );

}