///<Documentation>
///<Class name="Email">
Email = function(address) 
{
	this.Constructor(address);
}

///<Constructor>
///<summary>Email constructor</summary>
///<param name="address" type="string" required="false">
///     The text representation of the Internet Protocol address.
///</param>
Email.prototype.Constructor=function(address) 
{
	this.Exception=null;
	this.User=null;
	this.Domain=null;
	
	if(address!=null)
		this.IsValid(address);
}///</Constructor>

///<Methods>
///<Method name="IsValid">
///<summary>Determines whether the number represents a valid e-mail address</summary>
///<param name="address" type="string" required="false">
///     The text representation of the e-mail address.
///</param>
///<returns>Boolean</returns>
Email.prototype.IsValid=function(address)
{
	if(address==null)
		return this.Exception==null;
		
	this.Exception==null;

	var addressParts = address.split('@');
	
	if(addressParts.length==2)
	{
		this.User = addressParts[0],
		this.Domain = addressParts[1];

		var userParts = this.User.split('.');
		for(var i=0;i<userParts.length;i++)
		{
			if(userParts[i]=='')
			{
      		this.Exception = new Exception("Email", "IsValid", "User is invalid. It can not start with, end with, or contain multiple \".\"'s");
				return false;
			}
			
			if(/[<>()[\]\\,;:\s\"]/.test(userParts[i]))
			{
      		this.Exception = new Exception("Email", "IsValid", "User contains invalid characters.");
				return false;
			}
		}

		
		if(/(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})$/.test(this.Domain))
			return true;
		
		var ip = new IPAddress(this.Domain);
		if(!ip.IsValid())
		{
      	this.Exception = new Exception("Email", "IsValid", 'Invalid e-mail domain: '+ip.Exception.Description);
			return false;
		}
		
		return true;
	}

	this.Exception = new Exception("Email", "IsValid", 'Invalid e-mail addess. No or too many "@"\'s present.');
	return false;
}
///</Method>


///</Methods>
///</Class>

///<Class name="IPAddress">
IPAddress=function(address)
{
	this.Constructor(address);
}

///<Constructor>
///<summary>IPAddress constructor</summary>
///<param name="address" type="string" required="false">
///     The text representation of the Internet Protocol address.
///</param>
IPAddress.prototype.Constructor=function(address) 
{
	this.Value=address;
	this.Octets=null;
	this.Reserved=false;
	this.Exception=null;
	
	if(address!=null)
		this.Parse(address);
}
///</Constructor>

///<Methods>
///<Method name="Parse">
///<summary>Parses the ip address into it's representative parts</summary>
///<param name="address" type="string" required="false">
///     The text representation of the ip address.
///</param>
///<returns>null</returns>
IPAddress.prototype.Parse=function(address)
{
	if(this.IsValid(address))
		this.Reserved=this.Octets[0]==10||(this.Octets[0]==172&&this.Octets[1]==16)||(this.Octets[0]==192&&this.Octets[1]==168)
}
///</Method>

///<Method name="IsValid">
///<summary>Determines whether the number represents a valid ip address</summary>
///<param name="address" type="string" required="false">
///     The text representation of the ip address.
///</param>
///<returns>Boolean</returns>
IPAddress.prototype.IsValid=function(address)
{
	if(address==null)
		return this.Exception==null;
		
	this.Exception==null;
	
	var octets=address.split('.');
	if (octets!=null && octets.length==4) 
	{
		for (var i=0;i<octets.length;i++) 
		{
			if (isNaN(octets[i]) || octets[i]>255) 
			{
				this.Exception = new Exception("IPAddress", "IsValid", "One of the octets is not in a valid range of numbers (must be 0 to 255).");
				return false;
   		}
		}
		
		this.Octets = octets;
		return true;
	}
	
	this.Exception = new Exception("IPAddress", "IsValid", "The address is not in a correct IP format.");
	return false;
}
///</Method>
///</Methods>
///</Class>

///<UnitTests>
try
{
	if(__JSUnit_version>0)
	{
		
		__jsUnit.AddTest('Email','IsValid','Tests for a syntactically valid e-mail address','positive',
			function()
			{
				var 	expected={"Exception":null,"User":"george","Domain":"irth.com"},
						actual=new Email('george@irth.com');
						
				return new JSUnitTestResult(actual,expected);
			});
			
		__jsUnit.AddTest('Email','IsValid','Tests for a syntactically invalid user in the e-mail address','negative',
			function()
			{
				var 	expected={"Exception":{"Object":"Email","Function":"IsValid","Description":"User contains invalid characters."},"User":"geo(rge","Domain":"irth.com"},
						actual=new Email('geo(rge@irth.com');

				return new JSUnitTestResult(actual,expected);
			});
			
		__jsUnit.AddTest('Email','IsValid','Tests for an e-mail address with an invalid ip','negative',
			function()
			{
				var 	expected={"Exception":{"Object":"Email","Function":"IsValid","Description":"Invalid e-mail domain: One of the octets is not in a valid range of numbers (must be 0 to 255)."},"User":"george","Domain":"22.18.266.86"},
						actual=new Email('george@22.18.266.86');

				return new JSUnitTestResult(actual,expected);
			});
			
		__jsUnit.AddTest('Email','IsValid','Tests for an e-mail address with a valid ip','positive',
			function()
			{
				var 	expected={"Exception":null,"User":"george","Domain":"22.18.56.86"},
						actual=new Email('george@22.18.56.86');

				return new JSUnitTestResult(actual,expected);
			});
	}
}
catch(err){}
///</UnitTests>
///</Documentation>