//
// user-login.js
//
// requires :
//   trader.js
//   

var userModuleInstance = new UserModule();
userModuleInstance.pageLoad();

function UserModule () {

    this.URL_XML = "./";
    this.MENU_CONTAINER_ID = "menu";
    this.PAGE_CONTAINER_ID = "page";
    this.BLOCK_CONTAINER_ID = "block";
    
    this.pageLoad = function() {
        this.initVars();
    }
    
    this.initVars = function() {
        this.rng = null;
        this.pass = null;
        this.email = null;
        this.action = null;
        this.autologin = null;
    }
    
    this.container;
    
    this.fetchXmlData = function() {
        var xmlGet = this.generateGet();
        var xmlPost = this.generatePost();
        fetchXmlData(this.URL_XML, xmlGet, xmlPost);
        
        this.initVars();
    }
    
    this.module;
    this.method;
    this.target;
    
    this.generateGet = function() {
        var params = "";

        params = appendUrlParam(params, "m", this.module);
        params = appendUrlParam(params, "method", this.method);
        params = appendUrlParam(params, "target", this.target);
        if(this.action) {
            params = appendUrlParam(params, "action", this.action);
        }
        
        return params;
    }
    
    this.generatePost = function() {
        var params = "";

        if(this.action=="session-login") {
            params = appendUrlParam(params, "email", this.email);
            params = appendUrlParam(params, "pass", this.pass);
            params = appendUrlParam(params, "rng", this.rng);
            params = appendUrlParam(params, "autologin", this.autologin);
        } else if(this.action == "account-register") {
            params = appendUrlParam(params, "email", this.email);
            params = appendUrlParam(params, "pass", this.pass);
            params = appendUrlParam(params, "pass-repeat", this.pass_repeat);
            params = appendUrlParam(params, "name", this.name);
            params = appendUrlParam(params, "lastname", this.lastname);
            params = appendUrlParam(params, "country", this.country);
            params = appendUrlParam(params, "icq", this.icq);
            params = appendUrlParam(params, "msn", this.msn);
            params = appendUrlParam(params, "aim", this.aim);
            params = appendUrlParam(params, "yahoo", this.yahoo);
            params = appendUrlParam(params, "verif_code", this.verif_code);
        } else if(this.action == "account-edit") {
            params = appendUrlParam(params, "name", this.name);
            params = appendUrlParam(params, "lastname", this.lastname);
            params = appendUrlParam(params, "country", this.country);
            params = appendUrlParam(params, "icq", this.icq);
            params = appendUrlParam(params, "msn", this.msn);
            params = appendUrlParam(params, "aim", this.aim);
            params = appendUrlParam(params, "yahoo", this.yahoo);
        } else if(this.action == "account-change-password") {
            params = appendUrlParam(params, "pass", this.pass);
            params = appendUrlParam(params, "new-pass", this.new_pass);
            params = appendUrlParam(params, "pass-repeat", this.pass_repeat);
            params = appendUrlParam(params, "rng", this.rng);
        } else if(this.action == "account-reset-password") {
            params = appendUrlParam(params, "email", this.email);
        }
        
        return params;
    }
    
    this.action;
    this.rng;
    this.pass;
    this.new_pass;
    this.pass_repeat;
    this.email;
    this.name;
    this.lastname;
    this.country;
    this.icq;
    this.msn;
    this.aim;
    this.yahoo;
    this.verif_code;
    this.autologin;
    
    this.login = function(form) {
        this.action = "session-login";
        this.rng = form.rng.value;
        this.pass = hex_md5(form.pass.value);
        this.pass = this.pass.concat(this.rng);
        this.pass = hex_md5(this.pass);
        this.email = form.email.value;
        if(form.autologin.checked)
            this.autologin = 1;
        else
            this.autologin = 0;

        this.module = "ajax";
        this.method = "ajax";
        this.target = this.MENU_CONTAINER_ID;

        this.fetchXmlData();

        return false;
    }
    
    this.logout = function() {
        this.action = "session-logout";
        
        this.module = "ajax";
        this.method = "ajax";
        this.target = this.MENU_CONTAINER_ID;

        this.fetchXmlData();
        
        return false;
    }
    
    this.register = function() {
        this.module = "account-register";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;

        this.fetchXmlData();
    }
    
    this.sendRegistration = function(form) {
        this.email = form.email.value;
        this.pass = hex_md5(form.pass.value);
        this.pass_repeat = hex_md5(form.pass_repeat.value);
        this.name = form.name.value;
        this.lastname = form.lastname.value;
        this.country = form.country.value;
        this.icq = form.icq.value;
        this.msn = form.msn.value;
        this.aim = form.aim.value;
        this.yahoo = form.yahoo.value;
        this.verif_code = form.code_verif.value;
        
        this.action = "account-register";
        this.module = "account-register";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        
        this.fetchXmlData();
        
        return false;
    }
    
    this.sendEdition = function(form) {
        //this.email = form.email.value;
        this.name = form.name.value;
        this.lastname = form.lastname.value;
        this.country = form.country.value;
        this.icq = form.icq.value;
        this.msn = form.msn.value;
        this.aim = form.aim.value;
        this.yahoo = form.yahoo.value;
        
        this.action = "account-edit";
        this.module = "account-edit";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        
        this.fetchXmlData();
        
        return false;
    }
    
    this.changePassword = function(form) {
        this.rng = form.rng.value;
        this.pass = hex_md5(form.pass.value);
        this.pass = this.pass.concat(this.rng);
        this.pass = hex_md5(this.pass);
        this.new_pass = hex_md5(form.new_pass.value);
        this.pass_repeat = hex_md5(form.pass_repeat.value);

        this.action = "account-change-password";
        this.module = "account";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        
        this.fetchXmlData();
        
        return false;
    }
    
    this.resetPassword = function(form) {
        this.action = "reset-password";
        this.email = form.email.value;
        
        this.action = "account-reset-password";
        this.module = "ajax";
        this.method = "ajax";
        this.target = this.MENU_CONTAINER_ID;
        
        this.fetchXmlData();
        
        return false;
    }
    
    this.viewAccount = function() {
        this.module = "account";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;

        this.fetchXmlData();
    }
    
    this.editAccount = function() {
        this.module = "account-edit";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;

        this.fetchXmlData();
    }

    this.closeAccount = function() {
        this.module = "account-close";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;

        this.fetchXmlData();
    }
    
    this.mergeOldAccount = function() {
        this.module = "account-merge-old";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        
        this.fetchXmlData();
    }
}