/**
 * @namespace
 * Class User
 * 
 * This class is intended to encapsulate all the methods and attributes of a
 * User of the system.
 * 
 * Created on 04/03/2009
 * 
 * @class User
 * 
 * @property {number} _id
 * @property {string} _username
 * @property {string} _name
 * @property {string} _familyName
 * @property {string} _email
 * 
 * @author "Pablo Casado (pcasado@layers.com)"
 * @version <1.0>
 *  
 */
function User(){
	this._id = "";
	this._username = "";
	this._email = "";
	this._identities = new Array();
	this._mainIdentity = "";
	this._applications = new Array();
}

User.prototype._id;
User.prototype._username;
User.prototype._email;
User.prototype._identities;
User.prototype._mainIdentity;
User.prototype._applications;



/**
 * parse
 * 
 * This function returns a user from an XMLDocument returned by the server. It parses the XML received and
 * assign all the fields to the attributes of a user.
 *
 * Created on 04/03/2009
 * @author Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf User
 * @funtion
 * 
 * @param {mydata} XMLDocument An XMLDocument previously constructed by the makeRequest function of the WSProxy class
 *
 * @return {User} A User object
 */
User.prototype.parse = function(mydata){
	try{
		myuser = mydata.getElementsByTagName("user")[0];

		if(myuser != undefined){
	
			this._username = myuser.attributes.getNamedItem("id").value;
			
			try{
				this._email = myuser.getElementsByTagName('email')[0].childNodes[0].nodeValue;
			}catch(e){
				this._email = "";
			}
			
			try{
				var tmp = myuser.getElementsByTagName("identity");

				var i = 0;
				
				while (i < tmp.length){				
					this._identities[i] = new Identity().parse(tmp[i]);
					i = i + 1;
				}
			}catch(e){
				throw e;
			}
			
			try{
				 strIdentity = myuser.getElementsByTagName('mainIdentity')[0].childNodes[0].nodeValue;
				 
				 var t = 0;
				 var foundt = false;
				 while(!foundt && t < this._identities.length ){
					 if(this._identities[t]._displayName == strIdentity){					 	
						 foundt = true;
						 this._mainIdentity=this._identities[t];
					 }
					 t++;
				 }
				 
			}catch(e){
				this._mainIdentity="";
			}
			
			
		}else{
			throw new Exception("Error creating user from XML");
		}
		
		return this;
		
	}catch(e){
		throw e;
	}
}


/*
 * getUsername
 * 
 * Getter of the attribute username
 *
 * Created on 04/03/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 *
 * @memberOf User
 * @function
 *
 * @return {string} A User object
 */
User.prototype.getUsername = function(){
	return this._username;
}


/*
 * setUsername
 * 
 * Setter of the attribute username
 *
 * Created on 04/03/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 *
 * @memberOf User
 * @function
 *
 * @param {username} String The username of the user
 *
 * @return {void}
 */
User.prototype.setUsername = function(username){
	this._username = username;
}


/*
 * getId
 * 
 * Getter of the attribute id
 *
 * Created on 04/03/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf User
 * @function
 *
 * @return {string} A User object
 */
User.prototype.getId = function(){
	return this._id;
}


/*
 * setId
 * 
 * Setter of the attribute id
 *
 * Created on 04/03/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf User
 * @function
 *
 * @param {id} String The id of the user
 *
 * @return {void}
 */
User.prototype.setId = function(id){
	this._id = id;
}



/*
 * getIdentities()
 * 
 * Getter of the identities Array of the current user
 * 
 * Created on 22/03/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf User
 * @function
 *
 *
 * @return {Array} A String Array containing the names of the identites of the
 * current User
 */
User.prototype.getIdentities = function(){
	 return this._identities;
}



/*
 * getMainIdentity()
 * 
 * Returns the main identity (default) for the current User
 * 
 * Created on 22/03/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf User
 * @function
 *
 *
 * @return {String} The name of the main Identity of the current User
 * current User
 */
User.prototype.getMainIdentity = function(){
	 return this._mainIdentity;
}

