/*
 * Copyright (C) 2001-2007 Seagull Holding N.V. All rights reserved.
 */

var PRINTER_CLIENT_PACKAGE_NAME           = 'LegaSuite Printer Plugin 4.1038.1.284';
var PRINTER_CLIENT_XPI_FILENAME           = 'legasuiteprinterclient.xpi';

function PrinterClient()
{
    /*
     * Determine the User Agent.
     */
    var ua = navigator.userAgent.toLowerCase();
    this.isInternetExplorer_ = ua.indexOf("msie") != -1;
    this.isNavigator_ = ((ua.indexOf('mozilla')!=-1)
        && (ua.indexOf('spoofer')==-1)
        && (ua.indexOf('compatible') == -1)
        && (ua.indexOf('opera')==-1)
        && (ua.indexOf('webtv')==-1));
    this.isWindows_ = navigator.appVersion.indexOf("Win") != -1;

    /*
     * Find out if the printer client is already installed.
     */
    this.isInstalled_ = false;

    for (i = 0; i < navigator.plugins.length; ++i)
    {
      if (navigator.plugins[i].name == PRINTER_CLIENT_PACKAGE_NAME)
      {
        this.isInstalled_ = true;

        break;
      }
    }
}

PrinterClient.prototype.writeInstallPage = function()
{
    document.write('<div class="printerClientInstallBox">');
    document.write('<p class="header">The browser you are using does not have the <b>' + PRINTER_CLIENT_PACKAGE_NAME + '</b> ');
    document.write('installed.  To get the Printer Client please press button below.</p>');
    document.write('<p>Installing the <B>' + PRINTER_CLIENT_PACKAGE_NAME + '</b> takes approximately 4 minutes with a 33K modem.</b>');
    document.write('<p>The Printer Client will automatically be downloaded and installed by');
    document.write(' your browser. The only thing you have to do is to restart the browser when the installation procedure asks you to do so.</p>');
    document.write('<input id="printerClientInstallButton" type="button" class="button" value="Install Now" />');
    document.write('</div>');

    /*
     * Add the on-click handler to the printer client install button.
     */
    document.getElementById('printerClientInstallButton').onclick = PrinterClient.install;
}

PrinterClient.prototype.mustInstall = function()
{
    return this.isWindows_ && this.isNavigator_ && !this.isInstalled_;
}

PrinterClient.prototype.write = function()
{
	if (!this.isWindows_)
	    return;
	
    document.write('<iframe src="" width="1", height="1", scrolling="no" frameborder="0" name="printerClientFrame" ></iframe>');
    document.write('<div class="printerClientStartBox">');
    document.write('<p>Use this button to start the Printer Client, if it was accidentally stopped.</p>');
    document.write('<input id="printerClientStartButton" class="button" type="button" value="Start the Printer Client" />');
    document.write('</div>');

    /*
     * Store a reference to "this" object, so that we can use it in the anonymous function below.
     */
    var printerClient = this;
    document.getElementById('printerClientStartButton').onclick = function () {printerClient.load();};

    this.load();
}

PrinterClient.prototype.load = function()
{
    if (this.isInternetExplorer_)
        parent.printerClientFrame.document.location = 'taonlineprinterclientie.html';
    else if (this.isNavigator_)
        parent.printerClientFrame.document.location = 'taonlineprinterclientns.html';
}

PrinterClient.install = function ()
{
    var xpiInstallation = new Object();
    var ABSOLUTE_XPI_STRING;
    var fullpath = window.location.pathname;
    var pathname = fullpath.substring(0,fullpath.lastIndexOf("/"));

    ABSOLUTE_XPI_STRING =  window.location.protocol + "//";
    ABSOLUTE_XPI_STRING += window.location.hostname;

    if (window.location.port != "")
        ABSOLUTE_XPI_STRING += ":" + window.location.port;

    ABSOLUTE_XPI_STRING += pathname + "/" + PRINTER_CLIENT_XPI_FILENAME;
    xpiInstallation[PRINTER_CLIENT_PACKAGE_NAME] = ABSOLUTE_XPI_STRING;
    InstallTrigger.install(
        xpiInstallation,
        function (url,status)
        {
        if (status == 999 )
           alert("Please, restart the browser to complete the installation process.");
         else if (status == 210 )
           alert("Install failed: User cancelled installation");
         else if (status != 0)
           alert("Install FAILED with error: " + status);
        });
}
