// Bash JavaScript
// (C)2001 Craig Buckler

var loaded=false;
var run="";
var errors;

// start - called when page loaded
function start() {
	loaded = true;
	if (run!="") { eval(run); }
}

// ____________________________________________________________
// check enquiry form
function checkenquiry() {

	var form=document.enquiry;
	errors=false;

	if (form.name.value=="") { showerror(form.name, "You must enter your name."); }
	if (form.tel.value.length<10 && form.email.value=="") { showerror(form.tel, "Please enter your telephone\nnumber or an email address."); }
	if (form.email.value!="" && !checkemail(form.email.value)) { showerror(form.email, "Please enter a correct email address."); }

	if (!errors) { form.submit(); }

}

// ____________________________________________________________
// show error
function showerror(box, message) {

	if (!errors) {
		errors=true;
		alert(message);
		if (box.type=="text") {
			box.focus();
			box.select();
		}
	}

}

// ____________________________________________________________
// check validity of an email address
// returns true/false
function checkemail(email) {

	var noerr=true;
	var valid="abcdefghijklmnopqrstuvwxyz0123456789";
	var validpunc="@.-_";
	var p=0;
	var atcount=0;
	var dat=0;	// dots after @
	var tc, lc;

	email=email.toLowerCase();

	do {
		tc=email.charAt(p);

		// valid character
		if (valid.indexOf(tc)<0 && validpunc.indexOf(tc)<0) { noerr=false; }

		if (validpunc.indexOf(tc)>=0) {
			// punctuation at start, end, or next to more punctuation
			if (p==0 || (p+1)==email.length || validpunc.indexOf(lc)>=0) { noerr=false; }

			// count @'s
			if (tc=="@") { atcount++; }

			// count .'s after @'s
			if (tc=="." && atcount==1) { dat++; }
		}
		
		lc = tc;
		p++;
	}
	while (p<email.length && noerr==true);

	// not 1 @ or no . after @
	if (atcount!=1 || dat==0) { noerr=false; }

	return noerr;
}
