/*
*	Copyright 2005 Connecticut Business Applications, LLC. All rights reserved.
*	@author	Jacques Almeida
*	@version	1.0
*/
var EmailValidator={
	max_email_length:255, /*255 = max email length; local + @ + domain*/
	max_domain_length:190, /*190 = max domain length; domain*/
	max_local_length:64, /*64 = max local part size*/
	isDomainValid:function(address){
		var index=address.length;
		if(this.max_email_length<index) return -1;
		var i=index;
		var dotPosition=index;
		var hyphenPosition=index;
		while(-1<--i){
			var c=address.charCodeAt(i);
			if((96<c&&c<123)||(64<c&&c<91)||(47<c&&c<58)) continue;
			else if(c==45){
				if(i<index-1&&i<dotPosition-1){
					hyphenPosition=i;
					continue;
				}
				else return -1;
			}
			else if(c==46){
				if(i<index-1&&i<dotPosition-1&&i<hyphenPosition-1){
					dotPosition=i;
					continue;
				}
				else return -1;
			}
			else if(c==64){
				if(i<index-1&&i<dotPosition-1&&i<hyphenPosition-1){
					c=address.charCodeAt(i+1);
					return (47<c&&c<58)?-1:((index-i)<256?i:-1);
				}
				return -1;
			}
			return -1;
		}
		return -1;
	},
	isLocalValid:function(address,index){
		if(this.max_local_length<index) return -1;
		var i=index;
		var dotPosition=index;
		while(-1<--i){
			var c=address.charCodeAt(i);
			if(32<c&&c<127){
				if((39<c&&c<42)||(57<c&&c<61)||(90<c&&c<94)||c==34||c==44||c==62||c==64) return -1;
				else if(c==46){
					if(0<i&&i<index-1&&i<dotPosition-1){
						dotPosition=i;
						continue;
					}
					return -1;
				}
			}
			else return -1;
		}
		return 0;
	},
	validate:function(address){
		var i=this.isDomainValid(address);
		return -1<i?(-1<this.isLocalValid(address,i)):false;
	}
};

