app = {

    initialLocation: null,
    
    server: "server",
    
    errorCount: 0,

    // This should be called by onload() to setup the initialLocation and
    // request the first page.  
    //
    // The dhtmlHistory stuff is about handling the back button in an AJAX
    // environment. Google dhtmlHistory to find out about it.
    //
    start: function() {
//        Top.debug("START");
        dhtmlHistory.initialize();
        dhtmlHistory.addListener(app.handleHistoryChange);
        this.initialLocation = dhtmlHistory.getCurrentLocation();
        this.handleHistoryChange(this.initialLocation);
//        Top.send("views/selectEntryView.xml");
    },
    
    // This is more of the dhtmlHistory stuff. This gets called above
    // and also when something happens (like loading a new View) that
    // would indicate a point where we want to return to if someone 
    // goes to a new link, then comes back with the BACK button.
    // See std.view, it calls this on loading a new view.
    //
    handleHistoryChange: function(loc,dat) {
        if ((loc == null) || (loc.length == 0)) loc = "selectEntryView";
        //Top.debug("handleHistoryChange: loc="+loc+" dat="+dat);
        Top.send("views/"+loc+".xml");
    },

    personnelData: null,
    personnelForm: false,

    username: null,
    newEntry: false,
    
    // This routine is called from the std.view response handler
    // when a view named "personnelInfoView" is received, as set up
    // in the routine below.
    //
    loadPersonnelInfo: function() {
        Top.sendXml(app.server,"<loadEntry username='"+app.username+"' newEntry='"+app.newEntry+"'/>");
    },
            
    // This is the routine to load a personnelInfo entry. It
    // requests the static view, but first specifies a callback
    // routine to be invoked when the view arrives.
    //
    loadEntry: function(newEntry) {
        app.newEntry = newEntry;
        app.username = util.getValueOf("username");
        if ((app.username == null) || (app.username.length == 0)) {
            //Top.debug("showing message");
            util.showMessage("No Username specified");
        } else {
            std.setViewAction("personnelInfoView",app.loadPersonnelInfo);
            Top.send("views/personnelInfoView.xml");
        }
    },
    
    saveEntry: function() {
        Top.sendXml(app.server,"<saveEntry>\n<model:person"
                   +util.renderAsAttributes(app.personnelInfoNames)
                   +"/>\n</saveEntry>");
    },
    
    deleteEntry: function() {
        Top.sendXml(app.server,"<deleteEntry username='"+util.getValueOf("username")+"'/>");
    },
    
    loadData: function() {
        Top.sendXml(app.server,"<loadData/>");
    },
    
    doSelectEntry: function() {
        Top.send("views/selectEntryView.xml");
    },
    
    printMode: "PDF",
    
    setMode: function(mode) {
        app.printMode = mode;
    },
    
    reportWindow: null,
    
    runReport: function(report) {
        //Top.debug("RUN REPORT report="+report+" mode="+app.printMode);
        if (app.printMode == "RTF") {
            // Run as an Attachment in the current window
            location.href = "server/runReport"
                       +"?mode=RTF"
                       +"&report="+report
                       +"&inline=false";
        } else if (app.printMode == "HTML") {
            // Run it INLINE in a new window
            if ((app.reportWindow == null) || app.reportWindow.closed) {
                app.reportWindow = window.open("","reports");
            }
            app.reportWindow.location.href = "server/runReport"
                       +"?mode=HTML"
                       +"&report="+report
                       +"&inline=true";
        } else if (app.printMode == "PDF") {
            // Run it INLINE in a new Window
            location.href = "server/runReport"
                       +"?mode=PDF"
                       +"&report="+report
                       +"&inline=false";
        } else if (app.printMode == "EXCEL") {
            // Run as Attachment in the current window
            location.href = "server/runReport"
                       +"?mode=EXCEL"
                       +"&report="+report
                       +"&inline=false";
        }
    },
    
    doSelectReport: function() {
        Top.send("views/selectReportView.xml");
    },
    
    personnelInfoNames: [
        "username"
       ,"firstName"
       ,"lastName"
       ,"address"
       ,"city"
       ,"state"
       ,"zip"
       ,"phone"
       ],
       
    theEnd: true
}

// Register app so that received XML does not have to use a Namespace.
Top.registerPrefix("default",app);

// This registers a routine that will display a Loading message
// and handle error messages that are displayed in a message box
// utnil they time out.
//
Top.statusListener = util.statusListener;
    
app.loadEntryResponse = function() {}
app.loadEntryResponse.prototype = new Tag();
app.loadEntryResponse.prototype.init = function() {
    //Top.debug("loadEntryResponse.init app.errorCount="+app.errorCount);
    this.errorCount = app.errorCount;
}
app.loadEntryResponse.prototype.apply = function() {
    //Top.debug("LoadEntryResponse.apply errors="+this.errorCount+" : "+app.errorCount);
    if (this.errorCount < app.errorCount) {
        app.doSelectEntry();
    }
}

// An inputField is an hbox that sets default style values for its contents
app.inputField = function() {}
app.inputField.prototype = new Tag();
app.inputField.prototype.show = function(target) {
    var style = "width:120px; text-align:right; padding-right:5px; font-weight:bold;";
    if (this.attrs.style) {
        style += this.attrs.style;
    }
    return "<table><tr><td style='"+style+"'>"
         + "<span>"+this.attrs.label+":</span></td><td>"
         + this.showBody(target)+"</td></tr></table>\n";
}

app.saveEntryResponse = function() {}
app.saveEntryResponse.prototype = new Tag();
app.saveEntryResponse.prototype.apply = function() {
    //Top.debug("SaveEntryResponse.apply");
}

app.deleteResponse = function() {}
app.deleteResponse.prototype = new Tag();
app.deleteResponse.prototype.apply = function() {
    //Top.debug("DeleteResponse.apply");
    util.showMessage("Entry Deleted");
    app.doSelectEntry();
}

app.loadDataResponse = function() {}
app.loadDataResponse.prototype = new Tag();
app.loadDataResponse.prototype.apply = function() {
    //Top.debug("loadDataResponse.apply");
    util.showMessage("Data Loaded");
}

app.person = function() {}
app.person.prototype = new Tag();
app.person.prototype.apply = function() {
    //Top.debug("person.apply username="+this.attrs.username);
    util.applyToForm(app.personnelInfoNames,this.attrs);
}

app.message = function() {}
app.message.prototype = new Tag();
app.message.prototype.apply = function() {
    app.errorCount++;
    //Top.debug("Message: errorCount="+app.errorCount);
    util.showMessage(this.attrs.value);
}


