/*
 * Created on 16-jan-2006
 * 
 * (c) 2005-2006 Seagull Holding N.V. All rights reserved.
 */

var JRE_MINIMUM_VERSION = "1.5";

function Applet()
{
  Applet('GuiClient');
}

function Applet(aName)
{
  if(typeof(aName) == 'undefined')
	  aName = 'GuiClient';
  this.id_ = aName;

  /*
   * Associative array of applet parameters.
   */
  this.parameters_ = new Array();
  this.parameters_['archive'] = '';

  /*
   * Add standard archives.
   */
  this.addArchive('java/resources.jar');
    
  /*
   * Default size
   */
  this.setSize(600, 400);
}

Applet.prototype.write = function()
{ 
  var versionCheck = deployJava.versionCheck(JRE_MINIMUM_VERSION + "+");
  var runNonTimestamp = deployJava.versionCheck("1.4.2");
  var runApplet = versionCheck;
  
  /*
   * The Sun Deployment Toolkit doesn't handle the Opera browser and Safari for Windows well.
   * Make an exception for these two browsers; show the applet plus a note informing the user
   * that it requires Java 1.4.2 or up.
   */
  if (!versionCheck) {    
    if (navigator.userAgent.toLowerCase().indexOf('opera') != -1)
    {
      runApplet = true;
      
      for (var i = 0; i < navigator.plugins.length; ++i) 
      {            
        t = navigator.plugins[i].description;
        
        if (t.search(/^Java Plug-in (1\.4\.2)/) != -1)
          runNonTimestamp = true;
      }
    }
    else if (deployJava.getBrowser() == 'Safari') 
      runApplet = true;
  }
  
  if (runApplet) 
  {
    if (runNonTimestamp) // needed for java 1.4.2_03 or earlier
      this.addArchive('java/guiclient-nonts.jar');
    else 
      this.addArchive('java/guiclient.jar');  
    
    var attributes = {id:this.id_,codebase:'.',code:'com.seagullsw.client.gui.GuiClient.class',mayscript:'mayscript',height:this.height_,width:this.width_};
    
    if (this.height_ == '100%')
    {
      document.write('<style>');
      document.write('applet {');
      document.write('  position:absolute;');
      document.write('  margin:0 auto;');
      document.write('  height:100%;');
      document.write('  min-height:100%;');
      document.write('}');
      document.write('</style>');
    }
    
    deployJava.runApplet(attributes, this.parameters_, JRE_MINIMUM_VERSION);
  }
  
  if (!versionCheck)
  {    
    document.writeln('<div class="warning">');
    
    if (!runApplet)
      document.writeln('There is no suitable Java Runtime Environment (JRE) found on this system.<br>');
      
    document.writeln('The LegaSuite GUI Java Client requires version ' + JRE_MINIMUM_VERSION + ' or higher to run correctly.<br>');
    document.writeln('<br>');
    document.writeln('<a href="#" onClick="javascript:if(deployJava.installLatestJRE()){deployJava.refresh();location.href = document.location;}">Install the latest Java Runtime Environment</a></div>');
  }
}

Applet.prototype.addParameter = function(aName, aValue)
{
  this.parameters_[aName.toLowerCase()] = aValue;
}

Applet.prototype.addArchive = function(aArchive)
{
  if(this.parameters_['archive'] != '' && aArchive != '')
    this.parameters_['archive'] += ',';
    
  this.parameters_['archive'] += aArchive;
}

Applet.prototype.setSize = function(aWidth, aHeight)
{
  this.width_ = aWidth;
  this.height_ = aHeight;
}

Applet.prototype.getHeight = function() 
{
  return this.height_;
}

Applet.prototype.getWidth = function() 
{
  return this.width_;
}

Applet.prototype.getBooleanParameter = function(aName) 
{
  var lowerCaseName = aName.toLowerCase();
  
  return (this.parameters_[lowerCaseName] != null 
    && (this.parameters_[lowerCaseName].toLowerCase() == 'yes' 
    || this.parameters_[lowerCaseName].toLowerCase() == 'true'
    || this.parameters_[lowerCaseName] == 1));
}

Applet.prototype.getVariable = function(aSessionId, aVariableName)
{
  return this.getElement().getVariable(aSessionId, aVariableName);
}

Applet.prototype.setVariable = function(aSessionId, aVariableName, aVariableValue)
{
  return this.getElement().setVariable(aSessionId, aVariableName, aVariableValue);
}

Applet.prototype.println = function(aString)
{
  return this.getElement().systemErrPrintln(aString);
}

Applet.prototype.getRunningSessionId = function(n)
{
  return this.getElement().getRunningSessionId(n);
}

Applet.prototype.callScript = function(aSessionId, aScriptName, aTimeOutSeconds)
{
  this.getElement().callScript(aSessionId, aScriptName, aTimeOutSeconds);
}

Applet.prototype.getElement = function()
{
  return document.getElementById(this.id_);
}

Applet.prototype.requestFocus = function()
{ 
  return this.getElement().focus(); 
} 
