/**
 * JavaScript Bean to store 'Email' Entities<p>
 * <pre>
 * Properties of object:
 *   - EMAIL_ID
 *   - VALUE
 *   - NICK
 * </pre>
 */

Email.INVALID_ID    = "Campo ID no valido.";
Email.INVALID_VALUE = "El formato de la dirección de correo electrónico no es válido.\nDebes utilizar el formato común\n\nEjemplo: user@domain.com.";
Email.INVALID_OBS   = "Introduce alguna observación que te facilite recordar el motivo por el que introdujiste este dato."; 

/**
 * Constructor
 */
function Email(){
	this._error = "N/A";
};

/** ID Handlers */
Email.prototype.setId = function( nId ) {
	this._id = nId;
};
Email.prototype.getId = function() {
	return this._id;
};
Email.prototype.isValidId = function() {
	return true;
};

/** VALUE Handlers */
Email.prototype.setValue = function( sValue ){
	this._value = sValue;
};
Email.prototype.getValue = function(){
	return this._value;
};
Email.prototype.isValidValue = function() {
	if ( this._value != null ) {
		( this._value.search( /^\s*[\w\-]+(\.[\w\-]+)*@[\w\-]+(\.[\w\-]+)+\s*$/ ) != -1 ) ? result = true: result = false;
	} else {
		result = false;
	}//if-else
	if ( !result ) {
		this._error = Email.INVALID_VALUE;
	}
	return result;
};

/** Observations Handlers */
Email.prototype.setObs = function( sValue ) {
	this._obs = sValue;
};
Email.prototype.getObs = function() {
	return this._obs;
};
Email.prototype.isValidObs = function() {
	result = ( this._obs.trim().length != 0 );
	if ( !result ) {
		this._error = Email.INVALID_OBS;
	}
	return result;	
};

/* GLOBAL VALIDATION */
Email.prototype.isValid = function(){	
	return ( this.isValidValue() );
};

Email.prototype.getErrorMessage = function() {
	return this._error;
};

Email.prototype.toString = function(){
	var sb = new Array( 'Email (JavaScript Object)\n\n' );
	sb.push( 'id',    ' = [', this._id,    ']\n' );
	sb.push( 'value', ' = [', this._value, ']\n' );
	sb.push( 'nick',  ' = [', this._nick,  ']\n' );	
	return sb.join( '' );
};

