//
// user-login.js
//
// requires :
//   trader.js
//   

var domainModuleInstance = new DomainModule();
domainModuleInstance.pageLoad();

function DomainModule () {

    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.domain_id=null;
        this.action=null;
        this.domain=null;
        this.title=null;
    }
    
    this.target;
    
    this.fetchXmlData = function() {
        var xmlGet = this.generateGet();
        var xmlPost = this.generatePost();
        fetchXmlData(this.URL_XML, xmlGet, xmlPost);
        
        this.initVars();
    }
    
    this.module;
    this.method;
    
    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);
        if(this.domain_id)
            params = appendUrlParam(params, "domain_id", this.domain_id);

        return params;
    }
    
    this.generatePost = function() {
        var params = "";
        
        if(this.action == "domain-new-entry") {
            params = appendUrlParam(params, "domain", this.domain);
            params = appendUrlParam(params, "title", this.title);
        } else if(this.action == "domain-edit") {
            params = appendUrlParam(params, "title", this.title);
        }
        
        return params;
    }
    
    this.action;
    this.domain_id;
    this.domain;
    this.title;
    
    this.viewDomains = function() {
        this.module = "domains";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.newDomain = function() {
        this.module = "domain-new";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.sendNewDomain = function (form) {
        this.action = "domain-new-entry";
        this.domain = form.domain.value;
        this.title = form.title.value;
        
        this.module = "domain-new";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        
        this.fetchXmlData();
        
        return false;
    }
    
    this.sendEdition = function(form) {
        this.action = "domain-edit";
        this.domain_id = form.domain_id.value;
        this.title = form.title.value;
        
        this.module = "domain-edit";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        
        this.fetchXmlData();
        
        return false;
    }
    
    this.editDomain = function(id) {
        this.domain_id = id;
        
        this.module = "domain-edit";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.detailDomain = function(id) {
        this.module = "domain-detail";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
}