var clearSetting = true;

function clearField( theField ){
	if ( clearSetting ) {
		if (theField.defaultValue == theField.value) {
			theField.value = "";
		}

		clearSetting = false;
		showError( "Enter E-mail Address" + getPadding( 13 ), theField );
	}
}

function isEmpty( theField ) {
	if ( trim(new String( theField.value )).length == 0 ) {
		return true;
	} else {
		return false;
	}
}

function trim( s ) {
	return s.replace( / /g, "" );
}

function showError( errorMsg, field2Select ) {
	document.getElementById( "errorSection" ).innerHTML = "<font color='red'><b>" + errorMsg + "</b></font>";
	field2Select.focus();
}

function getPadding( howMuch ) {
	padding = "";

	for ( i=0; i<howMuch; i++ ) {
		padding += "&nbsp;";
	}

	return padding;
}

function validate( theField ) {

	if ( isEmpty( theField ) ) {
		showError( "E-mail Address must be entered" + getPadding( 2 ), theField );
		return;
	}

	atPos = theField.value.indexOf( "@" );

	if ( atPos < 0 ) {
		showError( "E-mail Address is missing the @ symbol" + getPadding( 1 ), theField );
		return;
	}

	if ( atPos == 0 ) {
		showError( "E-mail Address cannot begin with the @ symbol" + getPadding( 1 ), theField );
		return;
	}

	if ( atPos != theField.value.lastIndexOf( "@" ) ) {
		showError( "E-mail Address can only have one @ symbol" + getPadding( 1 ), theField );
		return;
	}

	periodPos = theField.value.indexOf( "." );

	if ( periodPos < 0 ) {
		showError( "E-mail Address is missing a period" + getPadding( 1 ), theField );
		return;
	}

	if ( periodPos == 0 ) {
		showError( "E-mail Address cannot begin with a period" + getPadding( 1 ), theField );
		return;
	}

	periodPos = theField.value.lastIndexOf( "." );

	if (periodPos < atPos) {
		showError( "E-mail Address period missing after the @ symbol" + getPadding( 1 ), theField );
		return;
	}

	if ( atPos+1 == periodPos ) {
		showError( "E-mail Address has an improper domain name" + getPadding( 1 ), theField );
		return;
	}

	if ( periodPos+1 == theField.value.length ) {
		showError( "E-mail Address has an improper domain name" + getPadding( 1 ), theField );
		return;
	}

    document.joinOurEmailListForm.submit();
}