// =======================================================================
//       ActiveScripting example (JavaScript)
// =======================================================================
//       Displays a table containing the filenames of all open documents,
//       whether or not each document has been modified, and whether each
//       document is read-only.
// =======================================================================
function Main() {
    var newline = '\n';
    var br = '<br>';
    var tab = '\t';
    
    // create app reference.  note that the Application object is only
    // available from within HomeSite and Studio - to create the app object from an
    // external script, use CreateObject('AllaireClientApp.TAllaireClientApp')    
    var app = Application;
    
    // save the index of the current document so it can be returned to
    var nCurrentIdx = app.DocumentIndex;
     
    // start the table
    var sTable = '<b><font color=Blue>Names of all open'
               + ' documents:</font></b>'
               + br + newline + br + newline
           	   + '<table border=1 width=460>'
               + newline + '<tr>'
               + '<td><b>Document</b></td>'
               + '<td><b>Modified</b></td>'
               + '<td><b>Read-Only</b></td>'
               + '</tr>' + newline;

    // loop through all open documents (note that DocumentCount is 1-based,
    // whereas DocumentCache() is 0-based)           
    for (idx = 0; idx < app.DocumentCount; idx++) {
         // get document name (uses function example)
         fname = GetDisplayName(app.DocumentCache(idx).Filename);
         sTable = sTable + tab + '<tr><td>' + fname;
         sTable = sTable + '</td><td> ';
         
         // is this document modified?         
         if (app.DocumentCache(idx).Modified) 
				sTable = sTable + 'Yes';
         sTable = sTable + '</td><td> ';
         
         // is this document read-only?         
         if (app.DocumentCache(idx).ReadOnly)
				sTable = sTable + 'Yes';
         
         // close row
         sTable = sTable + '</td></tr>' + newline;
    }
    sTable = sTable + newline + '</table>';

    // add a new document (False = don't create from default template - blank)
    app.NewDocument(false);
    
    // insert the table (note that the ActiveDocument will be the new document
    // created above)
    app.ActiveDocument.InsertText(sTable, false);
    
    // switch to browse mode     
    app.CurrentView = 2;
    
    // wait for user to re-enter edit mode
    while (app.CurrentView != 1) {
        // Wait is a home-grown routine to make up for the loss of
        // DoEvents in VBScript. it will pause for a given number of
        // milliseconds without locking the interface
        app.Wait(100);
    }    
    
    // return to the original document
    app.DocumentIndex = nCurrentIdx;
    
    alert('Script Completed.');
}

// function call example
function GetDisplayName(fname) {
    if (fname == '')
       return '(untitled)'
    else
        return fname
}