window.onload=function()
{
	loadPage(0, "pageRoot", 2);
}

/* returns a 'Page' from factory with unique attributes specified by 'ID'. 
If 'ID' invalid then returns a default "non-existant" version */
function factory(ID)
{
    var page;
    
    switch(ID)
    {
        case 0:
            page = new Page(ID, [1, 2, 3]);
            break;
        case 1:
            page = new Page(ID, [4, 5]);
            page.setTitle("Copy program's executable file");
            page.setHTML("<p>On the client machine used in <a href='manual-deployment.php#step1'>Step 1</a>, copy the 'Outlook Profile 2008.exe' file from the installation folder into a local drive on the desired client machines. This can be achieved by storing the file in a central location, such as a server share, and then using a login script to copy the file or via a software deployment solution like SMS or Altiris.</p><p><b>Note:</b> Unless the default .NET Framework permissions are changed, a .NET executable, eg 'Outlook Profile 2008.exe' cannot be executed directly from a network location.</p>");
            break;
        case 2:
            page = new Page(ID, [6, 7]);
            page.setTitle("Run the MSI installer");
            page.setHTML("<p>Install Outlook Profile 2008 by running the 'Outlook Profile 2008.msi' file on the desired client machines with the <a href='manual-installationswitches.php#CONFIGFILE'>CONFIGFILE</a>, <a href='manual-installationswitches.php#SERIAL'>SERIAL</a> and <a href='manual-installationswitches.php#/QUIET'>/QUIET</a> installation switches. The <a href='manual-installationswitches.php#CONFIGFILE'>CONFIGFILE</a> switch will be the path of the 'Configuration File' created in <a href='manual-deployment.php#step1'>Step 1</a>. The installation can be started on client machines by running the installer MSI file from a login script, via a software deployment solution like SMS or Altiris, or using Active Directory.</p><h5>Examples</h5><p>\"Outlook Profile 2008.msi\" CONFIGFILE=\"\\\\server1\\share1\\Outlook Profile 2008.exe.xml\" SERIAL=\"xxxx-xxxx-xxxx-xxxx\" /QUIET</p><h5>Active Directory</h5><p>To run the 'Outlook Profile 2008.msi' file from an Active Directory domain, visit the following Microsoft support document: <a href='http://support.microsoft.com/kb/302430'>http://support.microsoft.com/kb/302430</a>.</p>");
            break;
        case 3:
            page = new Page(ID, [7]);
            page.setTitle("Manual deployment");
            page.setHTML("<p>Install Outlook Profile 2008 by running the 'Outlook Profile 2008.msi' file on the desired client machines. Start 'Outlook Profile 2008.exe' from the installation folder with the <a href='manual-commandlineswitches.php#/EditOptionsMode'>/EditOptionsMode</a> switch. Access the <a href='manual-defaultconfigurationfile.php'>Default Configuration File</a> from the 'Tools' menu and specify the location of the 'Configuration File' saved in <a href='manual-deployment.php#step1'>Step 1</a>.</p>");
            break;
        case 4:
            page = new Page(ID);
            page.setTitle("Automated user processing");
            page.setHTML("<p>Run the 'Outlook Profile 2008.exe' file that was copied to the local drive of client machines in <a href='manual-deployment.php#step2'>Step 2</a> with the <a href='manual-commandlineswitches.php#/ConfigFile'>/ConfigFile</a> command line switch to specify the location of the 'Configuration File' saved in <a href='manual-deployment.php#step1'>Step 1</a>. Include the <a href='manual-commandlineswitches.php#/Silent'>/Silent</a> command line switch to automatically process the user who is logged in (or logging in). This step can be achieved by incorporating this information into a login script for example.</p><h5>Examples</h5><p>\"Outlook Profile 2008.exe\" /ConfigFile:\"\\\\server\\share\\Outlook Profile 2008.xml\" /Silent</p>");
            break;
        case 5:
            page = new Page(ID);
            page.setTitle("Manual user processing");
            page.setHTML("<p>Create a shortcut within the 'Start Menu' or 'Desktop' pointing to the 'Outlook Profile 2008.exe' file copied to the local drive of client machines in <a href='manual-deployment.php#step2'>Step 2</a> with the <a href='manual-commandlineswitches.php#/ConfigFile'>/ConfigFile</a> command line switch to specify the location of the 'Configuration File' saved in <a href='manual-deployment.php#step1'>Step 1</a>. Instruct users to double click this shortcut and select their name from the list of users displayed. See <a href='manual-applicationsettings.php#userSelection'>User Selection</a> within <a href='manual-applicationsettings.php'>Application Settings</a> to customize the list of users displayed.</p><h5>Examples</h5><p>\"Outlook Profile 2008.exe\" /ConfigFile:\"\\\\server\\share\\Outlook Profile 2008.xml\"</p>");
            break;
        case 6:
            page = new Page(ID);
            page.setTitle("Automated user processing");
            page.setHTML("<p>Run the 'Outlook Profile 2008.exe' file from the installation folder on the client machines used in <a href='manual-deployment.php#step2'>Step 2</a> with the <a href='manual-commandlineswitches.php#/Silent'>/Silent</a> command line switch to automatically process the user who is logged in (or logging in). This step be achieved by incorporating this information into a login script for example.</p><h5>Examples</h5><p>\"Outlook Profile 2008.exe\" /Silent</p>");
            break;
        case 7:
            page = new Page(ID);
            page.setTitle("Manual user processing");
            page.setHTML("<p>Instruct users to double click the associated shortcut located in the 'Start Menu' or 'Desktop' and select their name from a list of users displayed. See <a href='manual-applicationsettings.php#userSelection'>User Selection</a> within <a href='manual-applicationsettings.php'>Application Settings</a> to customize the users listed.</p>");
            break;
        default:
            page = new Page();
            page.setTitle("Non-existent ID: " + ID);
            page.setHTML("Non-existent ID: " + ID);
            break;
    }
    
    return page;
}

/* handle event for selection of child page 'index' from page with 'ID' */
function loadChildPageEventHandler(ID, index, elementID, level)
{
    loadPage(factory(ID).getChildID(index), elementID, level);
}

/* load the HTML output from page of 'ID' into the HTML element 'elementID'. Recurse for each default child */
function loadPage(ID, elementID, level)
{
    var page = factory(ID);                                     /* class representing this page */

    var selectElementID = "select" + page.getID();              /* HTML element ID for 'selectControl' listing page's children */
    var childElementID = "child" + page.getID();                /* HTML element ID for DIV where the default child page will be added */

    var selectEventHandler = "loadChildPageEventHandler";       /* name of function called when 'selectControl' change is made */

    var selectControl;                                          /* HTML select control representing the title of all child pages */
    
    var buffer = new BufferOutput(elementID)                    /* buffer for page's HTML Output */

    if(page.isHTMLSet())
        buffer.add("<div>" + page.getHTML() + "</div>");
    
    /* create HTML select control listing title of child pages when more than one */
    if (page.childCount() > 1)
    {
        buffer.add("<h4>Step " + level + "</h4>");

        selectControl = new SelectControl(selectElementID);

        selectControl.setEventHandler(selectEventHandler);
        selectControl.addEventHandlerParam(page.getID(), true);
        selectControl.addEventHandlerParam("document.getElementById(\"" + selectElementID + "\").selectedIndex", false);
        selectControl.addEventHandlerParam(childElementID, true);
        selectControl.addEventHandlerParam(level + 1, true);
        
        for (var i = 0; i < page.childCount(); i++)
            selectControl.addItem((factory(page.getChildID(i))).getTitle(), page.isDefaultChildID(page.getChildID(i)));
        
        buffer.add("<p class='center'>" + selectControl.getHTML() + "</p>");
    }
    
    buffer.add("<div id='" + childElementID + "'></div>");
    
    buffer.flush();     /* write page output now */
    
    if (page.hasChildren())
        loadPage(page.getDefaultChildID(), childElementID, level + 1);
}

/* Holds an 'ID', title and HTML representation.
ID of child pages can be added and retrieved by their 0 based array index. 
'ID' is mandatory, see 'factory' for more detail.*/
function Page(ID, childIDs)
{
    this.__ID = ID;
    this.__childIDs = childIDs || [];               /* ID for each child page */

    this.__defaultChildID = null;                   /* default child ID of 'this.__childIDs' array */

    this.__HTMLHead = null;                         /* HTML Hx output representation */
    this.__HTML = null;                             /* HTML output representation */
    this.__title = null;                            /* page's title */
    
    this.getID = getID;
    
    this.hasChildren = hasChildren;
    this.childCount = childCount;
    this.getChildID = getChildID;
    
    this.setDefaultChildID = setDefaultChildID;
    this.getDefaultChildID = getDefaultChildID;
    this.isDefaultChildID = isDefaultChildID;

    this.setHTML = setHTML;
    this.getHTML = getHTML;
    this.isHTMLSet = isHTMLSet;

    this.setTitle = setTitle;
    this.getTitle = getTitle;
    this.isTitleSet = isTitleSet;
    
    function getID()
    {
        return this.__ID;
    }
    
    function hasChildren()
    {
        return this.__childIDs.length > 0;
    }
    function childCount()
    {
        return this.__childIDs.length;
    }
    function getChildID(index)
    {
        return this.__childIDs[index];
    }
    /* if 'ID' doesn't exist then ignores */
    function setDefaultChildID(ID)
    {
        for (var i = 0; i < this.__childIDs.length; i++)
        {
            if (this.__childIDs[i] == ID)
                this.__defaultChildID = ID;
        }
    }
    /* if default not set then return ID of first child
    call hasChildren() to ensure atleast one child ID otherwise returns null */
    function getDefaultChildID()
    {
        if (this.__defaultChildID == null && this.__childIDs.length > 0)
            return this.__childIDs[0];
        
        return this.__defaultChildID;
    }
    function isDefaultChildID(ID)
    {
        return this.__defaultChildID == ID;
    }

    function setHTML(HTML)
    {
        this.__HTML = HTML;
    }
    function getHTML()
    {
        return this.__HTML;
    }
    function isHTMLSet()
    {
        return this.__HTML != null;
    }

    function setTitle(title)
    {
        this.__title = title;
    }
    function getTitle()
    {
        return this.__title;
    }
    function isTitleSet()
    {
        return this.__title != null;
    }
}

/* SelectControl() - 'Select' HTML control with no HTML id */
/* SelectControl(elementID) - 'Select' HTML control including HTML id */
function SelectControl(elementID)
{
    this.__elementID = elementID || null;
 
    this.__eventHandlerFunction = null;
    this.__eventHandlerParams = [];
    
    this.__items = [];
    this.__defaultItemIndex = -1;
    
    this.setEventHandler = setEventHandler;
    this.addEventHandlerParam = addEventHandlerParam;
    this.addItem = addItem;
    this.getHTML = getHTML;
    
    /* function name to call when control event occurs */
    function setEventHandler(name)
    {
        this.__eventHandlerFunction = name;
    }
    
    /* add parameters for EventHandler function name
    if makeHTMLSafe then ensure non-numbers are surrounded with "" */
    function addEventHandlerParam(param, makeHTMLSafe)
    {
        if(makeHTMLSafe)
            param = __HTMLSafeString(param);

        this.__eventHandlerParams.push(param);
    }
    
    /* add selectable text item to control */
    function addItem(name, setAsDefault)
    {
        this.__items.push(name);
        
        if(setAsDefault)
            this.__defaultItemIndex = this.__items.length - 1;
    }
    
    function getHTML()
    {
	    var HTML = "";
	    var eventHandler;

	    HTML += "<select";
	    
	    if (elementID)
	        HTML += " id='" + this.__elementID + "'";
	    
	    if (this.__eventHandlerFunction)
	    {
	        eventHandler = "'" + this.__eventHandlerFunction + "(" + this.__eventHandlerParams.join(", ") + ");'";
	        
	        HTML += " onchange=" + eventHandler;
	        HTML += " onkeyup=" + eventHandler;
	    }
	    
	    HTML += ">";

	    for(var i = 0; i < this.__items.length; i++)
	    {
	        HTML += "<option";
	        
	        if(i == this.__defaultItemIndex)
	            HTML += " selected=\"selected\"";
	            
		    HTML += ">" + this.__items[i] + "</option>";
		}
		    
		HTML += "</select>";

		return HTML;
    }
    
    /* format input to be used as HTML formatted string
    if NULL or undefined return "", if a number (including 0) or true/false just return it, otherwise assume string and surround with " */
    function __HTMLSafeString(input)
    {
        if(input == null)
	        return "\"\"";

	    if(isNaN(input))
            return "\"" + input + "\"";

	    return input;
    }
}

/* buffer output until flush() has been called to replace innerHTML of specified elementID. 
If elementID invalid then flush() has no effect */
function BufferOutput(elementID)
{
    this.__buffer = [];
    this.__elementID = elementID || null;
    
    this.add = add;
    this.flush = flush;
    
    function add(input)
    {
        this.__buffer.push(input);
    }

    function flush()
    {
        if (!this.__elementID || !document.getElementById(elementID))
            return;

        document.getElementById(elementID).innerHTML = "";

        for (var i = 0; i < this.__buffer.length; i++)
            document.getElementById(elementID).innerHTML += this.__buffer[i];

        this.__buffer = [];
    }
}
