/*-----------------------------------------------------------------------------------------------
EmailAddress

$Id: emailAddress.js,v 1.3 2005/10/10 02:18:48 cvs Exp $

Description: Provides a constructor, properties and methods for creating and manipulating 
an emailAddress object.

Copyright (c) Communicat

Usage: To create an object:
	var emailAddress = new EmailAddress(emailAddress);
------------------------------------------------------------------------------------------------*/
//
// EmailAddress
/**
 * This class creates an email address.
 *
 * @author glenn.seymon@communicat.com.au
 * @version $Id: emailAddress.js,v 1.3 2005/10/10 02:18:48 cvs Exp $
 * @constructor
 */
function EmailAddress(emailAddress)
{
	// PRIVATE PROPERTIES.

	// PUBLIC PROPERTIES
	this.emailAddress = emailAddress;
	
	// PUBLIC METHODS.
	
	//
	// getEmailAddress()
	/**
	 * Returns the email address.
	 *
	 * @returns <code>emailAddress</code> the email address.
	 */
	this.getEmailAddress = function()
	{
		return this.emailAddress;
	}

	//
	// setEmailAddress()
	/**
	 * Set a new email address.
	 *	
	 * @argument <code>emailAddress</code> the new email address.
	 */
	this.setEmailAddress = function(emailAddress)
	{
		this.emailAddress = emailAddress;
	}

	//
	// valid()
	/**
	 * Returns a boolean to indicate if the email address is valid or not.
	 *	
	 * @returns a boolean to indicate if the email address is valid or not.
	 */
	this.valid = function()
	{
		// check for spaces in e-mail address.
		var containsSpace = new RegExp(" ");
		if (containsSpace.test(this.emailAddress))
			return false;
		
		var supported = 0;
		
		if (window.RegExp) 
		{
			var tempStr = "a";
			var tempReg = new RegExp(tempStr);
			
			if (tempReg.test(tempStr)) 
				supported = 1;
		}
		
		if (!supported) 
			return (this.emailAddress.indexOf(".") > 2) && (this.emailAddress.indexOf("@") > 0);
			
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$");

		return (!r1.test(this.emailAddress) && r2.test(this.emailAddress));
	}

	//
	// toString()
	/**
	 * Returns a string representation of the email.
	 *	
	 * @returns a string representation of the email.
	 */
	this.toString = function()
	{
		return this.emailAddress;
	}
}