var warningPrefix = "ERRORE - ";

function SiteUser()
{
    this.id = null;
    this.username = null;
    this.nome = null;
    this.nome_completo = null;
    this.cognome = null;
    this.email = null;
    this.subscriptionDate = null;
    this.groupId = 1;
    this.groupName = null;
    this.feedback = 0;
    this.indirizzo = "";
    this.telefono = "";
    this.skype = null;
    this.msn = null;
    this.myspace = null;
    this.facebook = null;
    this.twitter = null;
    this.newsletter = false;
    this.townId = null;
    this.siteId = null;
    this.agencyId = null;
    this.agencyName = "";
    this.prefs = {};

    this.isActive = true;
    this.isLogged = false;
    this.isAdmin = false;
    this.isManager = false;
    this.isStandard = true;

    this.searchedKeywords = []; // parole chiave usate nel campo di ricerca testuale

    this.update = function (user) {
        this.id = user.id;
        this.username = user.username;
        this.nome = user.nome;
        this.cognome = user.cognome;
        this.nome_completo = user.nome + ' ' + user.cognome;
        this.email = user.email;
        this.subscriptionDate = user.data_iscrizione;
        this.groupId = parseInt(user.group_id);
        this.groupName = user.group_name;
        this.feedback = user.feedback;
        this.indirizzo = user.indirizzo;
        this.telefono = user.telefono;
        this.skype = user.skype;
        this.msn = user.msn;
        this.myspace = user.myspace;
        this.facebook = user.facebook;
        this.twitter = user.twitter;
        this.newsletter = user.newsletter;
        this.townId = user.town_id;
        this.siteId = user.siteId;
        this.agencyId = user.agencyId;
        this.agencyName = user.agency;
        this.prefs = user.prefs;

        this.isActive = user.active;
        this.isLogged = true;

        this.setRole();
        //alert('Sei loggato come ' + this.username + ', hai id n.' + this.id + ' e appartieni al gruppo ' + this.groupName);
    };

    this.setRole = function () {
        switch (this.groupId) 
        {
        	case 1:
        		this.isAdmin    = false;
        		this.isManager  = false;
        		this.isStandard = true;
        		break;
        	case 2:
        		this.isAdmin    = false;
        		this.isManager  = true;
        		this.isStandard = false;
        		break;
        	case 3:
        		this.isAdmin    = true;
        		this.isManager  = false;
        		this.isStandard = false;
        		break;
        	default:
        		this.isAdmin    = false;
            	this.isManager  = false;
            	this.isStandard = true;
        }
    };

    // Aggiorna e conserva le parole chiave 
    // digitate dall'utente nel campo di ricerca
    this.updateKeywords = function (text) {
        try
        {
            var keywords = text.split(" ");
            var len = keywords.length;

            for (var i = 0; i < len; i++)
            {
                var keyword = keywords[i].replace(/^\s+|\s+$/g,""); // trim
                var keywordIsStorable = keyword.length > 3 ? true : false;

                if (keywordIsStorable)
                {
                    this.searchedKeywords.push(keyword);
                }
            }

            this.searchedKeywords = this.searchedKeywords.unique();
            //alert("keywords: " + this.searchedKeywords.join(', '));
        }
        catch (e)
        {
            alert(warningPrefix + e);
        }
    };
}

var User = new SiteUser();
