// PtAgent.js
//
// Perform installation of PowerTerm WebConnect PtAgent
// using Java Downloader
//
// The Initial Developer of the Original Code is Ericom Software Ltd.
// Portions created by Ericom Software are Copyright (C) 1993-2003 Ericom Software Ltd.
// All Rights Reserved.


//
// Perform installation of PowerTerm WebConnect PtAgent
// using Java Downloader
//

function getOS()
{
	var platform = navigator.platform;
	if (platform.indexOf("Linux") >= 0)
		return "Linux";
	if (platform.indexOf("Solaris") >= 0)
		return "Solaris";
	if (platform.indexOf("Mac") >= 0)
		return "Mac";
	return "Windows";
}

function getBrowser()
{
	var sAppName = navigator.appName;
			
	if( sAppName.indexOf("Microsoft Internet Explorer") >= 0 )
	{
		return "IE";
	}
	else if( sAppName.indexOf("Netscape") >= 0 &&
				navigator.appVersion.indexOf("HotJava") >= 0 )
	{
		return "HJ";
	}
	else if( sAppName.indexOf("Netscape") >= 0 )
	{
		if( navigator.appVersion >= "5.0" )
		{
			return "NS6";
		}
		else
		{
			return "NS4";
		}
	}
	else if( sAppName.indexOf("Mozilla") >= 0 )
	{
		return "MZ";
	}
}
		
function getBrowserVersion( sBrowser )
{
	var _version	= navigator.appVersion;
	var _verspos;
	var _versendpos;
	var _subversion;
			
	switch ( sBrowser )
	{
		case "IE":
		{
			_verspos	= _version.indexOf( "MSIE", 0 );
			_versendpos = _version.indexOf( ";", _verspos );
			_subversion = _version.substring( _verspos + 5, _versendpos );
					
			return sBrowser + ' v.' + _subversion + '">';
		}
		case "HJ":
		{
			return sBrowser + ' v.' + _version + '">';
		}
		case "NS6":
		{
			return 'NS v.' + _version + '">';
		}
		case "NS4":
		{
			return 'NS v.' + _version + '">';
		}
		case "MZ":
		{
			return sBrowser;
		}
				
	}
		
}
		
function getAppletParameters()
{
	// PARAMETERS FOR CUSTOMIZATION
	var sParamCallJS	= '<param name="call_js"    value="' +  call_js     + '"> ';
	var sParamURL       = '<param name="src"        value="' +  url         + '"> ';
	var sParamDestPath  = '<param name="dst"        value="' +  dest        + '"> ';
	var sParamImgPath   = '<param name="img"        value="' +  img_path    + '"> ';
	var sParamLogPath   = '<param name="log"        value="' +  log_path    + '"> ';
	var sParamConsole   = '<param name="console"    value="' +  console     + '"> ';
	var sParamExe       = '<param name="parameters" value="' +  server      + 
	                                                            parameters  +
	                                                            run			+
																component   + '"> ';
	var sAddParam		= '<param name="add_param"  value="' +  add_parameters
																			+ '"> ';
	var sParamInstall	= '<param name="install"    value="' +  install_parameters
																			+ '"> ';
			
		    
	// System-dependent destination folder
	var sOS       = getOS();		    
		    
	// end PARAMETERS FOR CUSTOMIZATION
      
	return      sParamCallJS + sParamURL + sParamDestPath  + sParamImgPath + 
		        sParamLogPath + sParamConsole + sParamExe + sAddParam + sParamInstall;
}
        
function getAppletByOS( sOS )
{
	var sParamApplet  = '<applet width="1" height="1" code="Downloader.class"';
	if( console == "true" || console == "TRUE" )
	    sParamApplet  = '<applet width="700" height="500" code="Downloader.class" ';

	var sParamBrowser = '<param name="brow"  value="' + getBrowserVersion( sBrowser );
	var sParamOS      = '<param name="OS"  value="' + sOS + '">';	
			
    return sParamApplet + getArchive() + sParamOS + sParamBrowser ;
}
		
function getArchiveByJVM( sJVMVersion, sJVMVendor, sOS ) 
{
	if( ( sJVMVendor.indexOf( "MS" ) >= 0 || 
			sJVMVendor.indexOf( "Microsoft" ) >= 0 ) 
		&& sJVMVersion <= "1.1.8" )
	{
		return "";
	}
	
	if( sJVMVersion <= "1.4.0" )
	    return "";
	
	return getArchive();
}

function getArchive()		
{
	return ' archive="' + SUN_archive + '" mayscript="mayscript" > '; 
}
		
function runJavaClientIE( sJVMVersion, sJVMVendor, sOS )
{
	var sParamArch  = getArchiveByJVM( sJVMVersion, sJVMVendor, sOS );
	if( sParamArch == "" )
	{
	    alert( "Java Virtual Machine version "  +  
	                sJVMVersion                 + 
	                " supplied by "          +
	                sJVMVendor                  +
	                " is not supported any more.");
        return;	                
	}

    var sDocParam   = getAppletByOS( sOS ) + getAppletParameters( sOS );		    

    var componentName = document.title;
    
    document.open();

    var sTable = 
'<html>'															+
'<HEAD bgcolor="#003359">'                                          +
    '<TITLE>' + componentName + '</TITLE>'                          + 
	'<SCRIPT SRC="PtAgent.js"></SCRIPT>'							+
    '<TABLE width="700" bgcolor="#ffffff" align="center" cellpadding="0" cellspacing="0" border="0" >' + 
        '<TR VALIGN="middle">'                                      + 
		    '<TD ALIGN="left" width="300">'                         +
			    '<img src="../images/BackgroundUp.jpg" border="0">' +
		    '</TD>'                                                 +
		    '<TD  width="400" valign="bottom" align="right" style="border-left: #ffffff 1px solid;">'        +
			    '<FONT style="font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 9pt; font-weight:bold; color:#CCCCCC;" >' +
					'<b>' + componentName + '&nbsp;&nbsp;&nbsp;&nbsp;</b>' +
			    '</FONT>'                                           +
		    '</TD>'                                                 +
        '</TR>'                                                     +
        '<TR>'                                                      +
            '<TD colspan="2"><img src="../images/BackgroundDown.jpg" border="0">' +
            sDocParam + '</applet>'                                 +
            '</TD>'                                                 +
        '</TR>'                                                     +
    '</TABLE>'                                                      +
'</HEAD>'                                                           + 
'<BODY bgcolor="#003359" >'                                         +
'</BODY>'                                                           +
'</html>';
    
    document.writeln( sTable );
	document.close();

}

function runIdentifyJVM()		
{
    var sOS             = getOS();		    
    // For Linux Mozilla bug & Windows FireFox bug
	switch ( sBrowser )
	{
		case "MZ":
		case "NS6":
		{
	        runJavaClientIE( "1.4.2", "Sun Microsystems Inc.", sOS );
	        return;
		}
		case "NS4":
		{
			alert("This browser version is not supported.\n"+ 
					"For more information about system requirements, "+
					"see the PowerTerm WebConnect Readme.\n" + 
					"To upgrade your browser please contact your System Administrator.");

	        return;
		}
		default:
		{
            document.writeln( '<APPLET height = "1" width = "1" code = "IdentifyJVM.class" name = "Identifier" MAYSCRIPT = "mayscript" ></APPLET>');
            return;
		}
	}
    
}

function onStateChange( sState )
{
	// On Downloader state was changed
	// type the state processing here
	switch ( sState )
	{
		case "-1":
		{
			// Failure during client download or installation
			return;
		}
		case "0":
		{
			// Not started. The initial state before any operation takes place
	        return;
		}
		case "1":
		{
			// Downloading is not needed: already installed
	        return;
		}
		case "2":
		{
			// A new downloading as result of comparing the remote version to local version
	        return;
		}
		case "4":
		{
			// Executable is run
	        return;
		}
		case "5":
		{
			// Download and installation cancelled by the user
	        return;
		}
	}

}
