// validation functions by Toby Thain (July 2001)

function bad(o,blankok){
	alert("Please "+(o.value.length?"check":"enter")+" your "+o.name
		+ (blankok ? " (or leave blank)" : "") + ".");
	o.focus();
	return false;
}
function minlength(o,min){
	return (o.value.length >= min) || bad(o,false);
}
function notblank(o){
	return o.value.length > 0;
}
function chosen(o,desc){
	if(o.selectedIndex)
		return true;
	else{
		alert("Please select a "+desc+".");
		o.focus();
		return false;
	}
}
function phoneok(o){
	var c,d;
	for(var i=o.value.length,d=0;i--;){
		c = o.value.charAt(i);
		if(c>='0' && c<='9')
			++d;
		else if( c!=' ' && c!='.' && c!='-' && c!='+' && c!='(' && c!=')' )
			return false;
	}
	return d>=8;
}
function postcodeok(o){
	var c,d;
	for(var i=o.value.length,d=0;i--;){
		c = o.value.charAt(i);
		if(c>='0' && c<='9')
			++d;
		else
			return false;
	}
	return d==4;
}
function nameok(s){
	// valid e-mail user name; refer http://email.about.com/library/weekly/aa062298.htm?once=true&
	var c;
	for(var i=s.length;i--;){
		c = s.charAt(i);
		if( c<=' ' || c>'~' || c=='(' || c==')' || c=='[' || c==']' || c=='<' 
				|| c=='>' || c==',' || c==';' || c==':' || c=='\\' || c=='\"' )
			return false;
	}
	return true;
}
function domainok(s){
	var c;
	for(var i=s.length;i--;){
		c = s.charAt(i);
		if( !( (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || c=='-' || c=='.') )
			return false;
	}
	return true;
}
function stripspaces(s){
	for( var i=s.length ; i && s.charAt(--i)==' ' ; )
		;
	return s.slice(0,i+1);
}
function emailok(o){
	var atsign;
	o.value = stripspaces(o.value); // user might not see spaces at the end; delete any
//	alert('emailok:\"'+o.value+'\"');
	if( (atsign = o.value.indexOf("@")) > 0 ){ // there's an @ sign, and it's not the first character
		var usr = o.value.substr(0,atsign),
			dom = o.value.substr(atsign+1); // (could use String.split() in JS 1.1 ; better still, could use RegExp :)
		return nameok(usr) // the user name part is sensible
			 && domainok(dom) // the domain name part is sensible
			 && dom.indexOf(".")>0 // the domain has a dot, and it's not the first character
			 && dom.lastIndexOf(".")<(dom.length-1); // it doesn't end with a dot
	}else
		return false;
}
function nocontact(){
	alert("Please make sure you have provided either e-mail, telephone or fax number."
		+ " (If you have filled one or more in, check that you haven't mistyped.)");
	return false;
}

