


function validateForm() {
	var name = document.getElementsByName( "contactName" )[0];
	var nameOk = name.value == "" ? false : true;
	var email = document.getElementsByName( "email" )[0];
	var emailOk = checkEmail( email.value );
	var tel = document.getElementsByName( "tel" )[0];
	var telOk = checkTel( tel.value );
	
	var msg = "";
	var smthWrong = false;
	if( !emailOk || !nameOk || !telOk ) {
		msg = "The following details are missing or invalid: "; 
		smthWrong = true;
	}
	
	if( !emailOk ) {
		msg += "<br/>* E-mail address *" ;
	}
	
	if( !nameOk ) { 
		msg += "<br/>* Name *";
	}
	
	if( !telOk ) { 
		msg += "<br/>* Telephone no *";
	}
		
	if( smthWrong ) { 
		$( "messageSpace" ).innerHTML = msg + "<br/><br/>Your enquiry has not been sent!";
		showMessageDialog();
		return false;
	}
	
	return true;
}

function doForm() {
	var ok = validateForm();
	
	if( !ok ) {
		return;
	}
	
	var values = HTTP.encodeFormData( document.forms[0] );
	HTTP.post( "processEnquiry.php", values, handleResponse, null, false ); 
}

function $( id ) {
	return document.getElementById( id );
}

function handleResponse( text ) {
	
	mTextArea = $( "messageSpace" );
	
	if( text == "Success" ) {
		mTextArea.innerHTML = "Thank you ... your enquiry has been sent succesfuly. We will endevour to give you a satisfactory response as soon as possible. Chimo!";
	} else {
		mTextArea.innerHTML = "There was an error sending your enquiry, possibly because the e-mail address you provided is invalid. Please try again!";  
	}
	
	showMessageDialog();
}

function checkEmail( email ) {
	
	atPos = email.indexOf("@")
	stopPos = email.lastIndexOf(".")

	if (email == "") {
		return false;
	} 

	if (atPos == -1 || stopPos == -1) {
		return false;
	}

	if (stopPos < atPos) {
		return false;
	}

	if (stopPos - atPos == 1) {
		return false;
	}

  	return true;
}

function checkTel( tel ) {
	if( !tel || tel == "" ) {
		return false;
	}
	
	return true;
}