// helps to validate e-mails
var domreg = new RegExp("^[a-z0-9_-]+$","i");

// used for click/key checking
var Clicked = 0;

function CL()
{
	Clicked--;
}

function badFieldInput(ctl,msg)
{
	alert(msg);
	ctl.focus();
	return true;
}

function validateEmail(ctl)
{
	// grab the email thing
	var id=ctl.value;

	// get the '@' location(s)
	var firstat=id.indexOf('@');
	var lastat=id.lastIndexOf('@');

	// check the '@' location(s)
	if (firstat<1) return badFieldInput(ctl,"Bad Email format.\nMissing '@'.");
	if (lastat!=firstat) return badFieldInput(ctl,"Bad Email format.\nToo many '@'s");

	// grab the two bits to check
	var base = id.substr(0,firstat);
	var dom = id.substr((firstat+1),(id.length-1));

	// make sure domain is non-blank
	if (dom=='') return badFieldInput(ctl,"Bad Email format.\nMissing domain.");

	// look for valid domain format
	var bits = dom.split('.');

	// get count of bits and make sure they are not blanks
	var c = bits.length;
	var x = '';
	for (var i=0;i<c;i++)
	{
		// get the string
		x = bits[i];

		// blank not allowed
		if (x.length == 0) return badFieldInput(ctl,"Bad Email format.\nDomain has wrong number of parts.");

		// check to see if contains valid stuff
		if (x.match(domreg)==null) return badFieldInput(ctl,"Bad Email format.\nDomain part has bad characters.");

		// check the length, if not the first element
		if ((i>0)&&((x.length<2)||(x.length>4))) return badFieldInput(ctl,"Bad Email format.\nDomain part has incorrect character count.");
	}

	// check for bad part count
	if ((c<2)||(c>4)) return badFieldInput(ctl,"Bad Email format.\nDomain has wrong number of parts.");

	// check the base
	bits = base.split('.');
	c = bits.length;

	for (var i=0;i<c;i++)
	{
		// get the string
		x = bits[i];

		// blank not allowed
		if (x.length == 0) return badFieldInput(ctl,"Bad Email format.\nBase has blank part.");

		// check to see if contains valid stuff
		if (x.match(domreg)==null) return badFieldInput(ctl,"Bad Email format.\nBase part has bad characters.");
	}

	// okay
	return false;
}

function noBlankAllowed(ctl,fld)
{
	// check
	if (ctl.value == '')
	{
		alert('Please enter '+fld);
		ctl.focus();

		// not good
		return true;
	}

	// okay
	return false;
}
