function State(isEnd,chars) {
	this.isEnd = isEnd;
	this.chars = chars;
	this.followUps = new Array();
	///
	this.connect = function(state) {
		this.followUps[this.followUps.length] = state;
	};
	this.accepts = function(c) {
		return (this.chars.indexOf(c) != -1);
	};
	this.getNext = function(c) {
		var next;
		///
		for (var i = 0;i < this.followUps.length;i++) {
			if (this.followUps[i].accepts(c)) {
				next = this.followUps[i];
				break;
			}
		}
		return next;
	};
}

function EmailStates() {
	var digits = '0123456789';
	var basicLatin = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var codepageOne = 'àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿßÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ';
	var alphanumeric = digits + basicLatin;
	var stringChars = '!#$%&\'*+-/=?^_`{|}~' + alphanumeric;
	var quotedChars = '';
	///
	for (var i = 127;i >= 0;i--) {
		if ((i != 10) && (i != 13) && (i != 92) && (i != 34)) {
			quotedChars += String.fromCharCode(i);
		}
	}
	this.START = new State(false,'');
	this.STRING = new State(false,stringChars);
	this.DOT = new State(false,'.');
	this.STRING_ESC = new State(false,'\\');
	this.QUOTED_START = new State(false,'"');
	this.QUOTED = new State(false,quotedChars);
	this.QUOTED_ESC = new State(false,'\\');
	this.QUOTED_END = new State(false,'"');
	this.AT = new State(false,'@');
	this.DOMAIN = new State(true,'.');
	this.NUMBER_START = new State(false,'#');
	this.NUMBER = new State(true,digits);
	this.IP_START = new State(false,'[');
	this.IP_NUMBER = new State(false,digits);
	this.IP_DOT = new State(false,'.');
	this.IP_END = new State(true,']');
	this.NAME_START = new State(true,alphanumeric + codepageOne);
	this.NAME = new State(true,alphanumeric + codepageOne);
	this.NAME_HYPHEN = new State(false,'-');
	///
	this.START.connect(this.QUOTED_START);
	this.START.connect(this.STRING);
	this.START.connect(this.STRING_ESC);
	this.STRING.connect(this.STRING);
	this.STRING.connect(this.DOT);
	this.STRING.connect(this.STRING_ESC);
	this.STRING.connect(this.AT);
	this.DOT.connect(this.STRING);
	this.DOT.connect(this.STRING_ESC);
	this.STRING_ESC.connect(this.STRING);
	this.QUOTED_START.connect(this.QUOTED);
	this.QUOTED_START.connect(this.QUOTED_ESC);
	this.QUOTED.connect(this.QUOTED);
	this.QUOTED.connect(this.QUOTED_ESC);
	this.QUOTED.connect(this.QUOTED_END);
	this.QUOTED_END.connect(this.AT);
	this.QUOTED_ESC.connect(this.QUOTED);
	this.AT.connect(this.NUMBER_START);
	this.AT.connect(this.IP_START);
	this.AT.connect(this.NAME_START);
	this.DOMAIN.connect(this.NUMBER_START);
	this.DOMAIN.connect(this.IP_START);
	this.DOMAIN.connect(this.NAME_START);
	this.NUMBER_START.connect(this.NUMBER);
	this.NUMBER.connect(this.NUMBER);
	this.NUMBER.connect(this.DOMAIN);
	this.NAME_START.connect(this.NAME);
	this.NAME_START.connect(this.NAME_HYPHEN);
	this.NAME_START.connect(this.DOMAIN);
	this.NAME.connect(this.NAME);
	this.NAME.connect(this.NAME_HYPHEN);
	this.NAME.connect(this.DOMAIN);
	this.NAME_HYPHEN.connect(this.NAME);
	this.IP_START.connect(this.IP_NUMBER);
	this.IP_NUMBER.connect(this.IP_NUMBER);
	this.IP_NUMBER.connect(this.IP_DOT);
	this.IP_NUMBER.connect(this.IP_END);
	this.IP_DOT.connect(this.IP_NUMBER);
	this.IP_END.connect(this.DOMAIN);
};

function checkEmailField(field) {
	var str = field.value;
	var ok = (str != '');
	///
	if (str != '') {
		var states = new EmailStates();
		var state = states.START;
		var ipCount = 0;
		var ipStart = 0;
		var domainCount = 0;
		///
		for (var i = 0;(state) && (i < str.length);i++) {
			var temp = state.getNext(str.charAt(i));
			///
			if (temp) {
				if ((temp != state) && (temp == states.IP_NUMBER)) {
					ipStart = i;
					ipCount++;
				}
				if ((temp == states.IP_DOT) || (temp == states.IP_END)) {
					var ip = parseInt(str.substring(ipStart,i));
					///
					if ((ip < 0) || (ip > 255)) {
						temp = null;
					}
				}
				if (temp == states.IP_END) {
					if (ipCount != 4) {
						temp = null;
					}
					ipCount = 0;
				}
				if (temp == states.DOMAIN) {
					domainCount++;
				}
			}
			state = temp;
		}
		if ((!state) || (!state.isEnd)) {
			ok = false;
		}
		else {
			if ((str.substring(0,4) == 'www.') && (!confirm('Sind Sie sicher, dass die E-Mail-Adresse mit "www." beginnt?'))) {
				field.value = field.value.substring(4);
			}
			if (domainCount == 0) {
				field.value = field.value + '.de';
			}
		}
	}
	return ok;
}
