/*====================================================================*\

Applet installer

\*====================================================================*/


// PARAMETER CLASS


////////////////////////////////////////////////////////////////////////
//  Constructors
////////////////////////////////////////////////////////////////////////

	function Parameter( name, value )
	{
		try
		{
			if ( !Parameter.isString( name ) )
				throw new Error( "name" );
			if ( !Parameter.isString( value ) )
				throw new Error( "value" );

			this.name = name;
			this.value = value;
		}
		catch ( e )
		{
			throw new TypeError( "function Parameter( name, value ): " + e.message );
		}
	}

	//------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////
//  Class methods
////////////////////////////////////////////////////////////////////////

	Parameter.isString = function( value )
	{
		return ( (typeof value == "string") || (value instanceof String) );
	}

	//------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////
//  Instance methods
////////////////////////////////////////////////////////////////////////

	Parameter.prototype.createElement = function( namePrefix )
	{
		var element = document.createElement( "param" );
		element.setAttribute( "name", namePrefix ? namePrefix + this.name : this.name );
		element.setAttribute( "value", this.value );
		return element;
	}

	//------------------------------------------------------------------

//----------------------------------------------------------------------


// PARAMETER LIST CLASS


////////////////////////////////////////////////////////////////////////
//  Constructors
////////////////////////////////////////////////////////////////////////

	function ParameterList( )
	{
		this.items = new Array( );
	}

	//------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////
//  Instance methods
////////////////////////////////////////////////////////////////////////

	ParameterList.prototype.getItems = function( )
	{
		return this.items;
	}

	//------------------------------------------------------------------

	ParameterList.prototype.getLength = function( )
	{
		return this.items.length;
	}

	//------------------------------------------------------------------

	ParameterList.prototype.get = function( index )
	{
		return ( ((index >= 0) && (index < this.items.length)) ? this.items[index] : null );
	}

	//------------------------------------------------------------------

	ParameterList.prototype.indexOf = function( name )
	{
		if ( Parameter.isString( name ) )
		{
			for ( var i = 0; i < this.items.length; ++i )
			{
				if ( this.items[i].name == name )
					return i;
			}
		}
		return -1;
	}

	//------------------------------------------------------------------

	ParameterList.prototype.getValue = function( name )
	{
		var index = this.indexOf( name );
		return ( (index < 0) ? null : this.items[index].value );
	}

	//------------------------------------------------------------------

	ParameterList.prototype.add = function( name, value )
	{
		if ( Parameter.isString( name ) && Parameter.isString( value ) )
			this.items.push( new Parameter( name, value ) );
	}

	//------------------------------------------------------------------

	ParameterList.prototype.addItem = function( item )
	{
		if ( item instanceof Parameter )
			this.items.push( item );
	}

	//------------------------------------------------------------------

	ParameterList.prototype.remove = function( name )
	{
		var index = this.indexOf( name );
		if ( index >= 0 )
			this.items.splice( index, 1 );
	}

	//------------------------------------------------------------------

	ParameterList.prototype.set = function( name, value )
	{
		if ( Parameter.isString( name ) && Parameter.isString( value ) )
		{
			var index = this.indexOf( name );
			if ( index < 0 )
				this.items.push( new Parameter( name, value ) );
			else
				this.items[index].value = value;
		}
	}

	//------------------------------------------------------------------

	ParameterList.prototype.addFromLocation = function( )
	{
		var strs = decodeURIComponent( location.search.substring( 1 ) ).split( "&" );
		for ( var i = 0; i < strs.length; ++i )
		{
			var index = strs[i].indexOf( "=" );
			if ( index >= 0 )
				this.add( strs[i].substring( 0, index ), strs[i].substring( index + 1 ) );
		}
	}

	//------------------------------------------------------------------

	ParameterList.prototype.addFromList = function( params, prefix )
	{
		if ( (params instanceof ParameterList) && Parameter.isString( prefix ) )
		{
			for ( var i = 0; i < params.getLength( ); ++i )
			{
				var param = params.get( i );
				if ( param.name.substring( 0, prefix.length ) == prefix )
				{
					param = new Parameter( param.name.substring( prefix.length ), param.value );
					var index = this.indexOf( param.name );
					if ( index < 0 )
						this.items.push( param );
					else
						this.items[index] = param;
				}
			}
		}
	}

	//------------------------------------------------------------------

//----------------------------------------------------------------------


// APPLET INSTALLER CLASS


////////////////////////////////////////////////////////////////////////
//  Constructors
////////////////////////////////////////////////////////////////////////

	function AppletInstaller( )
	{
	}

	//------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////
//  Constants
////////////////////////////////////////////////////////////////////////

	AppletInstaller.MIME_TYPE			= "application/x-java-applet;version=";
	AppletInstaller.MICROSOFT_IE_NAME	= "Microsoft Internet Explorer";
	AppletInstaller.MICROSOFT_CLASS_ID	= "8AD9C840-044E-11D1-B3E9-00805F499D93";
	AppletInstaller.PARAM_NAME_PREFIX	= "app.";
	AppletInstaller.ID_SUFFIX			= "-object";
	AppletInstaller.NO_JAVA_STR1		= "To run this applet, your browser must have a plug-in that " +
											"supports Java ";
	AppletInstaller.NO_JAVA_STR2		= " applets.";

////////////////////////////////////////////////////////////////////////
//  Class methods
////////////////////////////////////////////////////////////////////////

	AppletInstaller.install = function( id, javaVersion, codeBase, appletClass, archive, width, height,
										params, addText )
	{
		if ( !(params instanceof ParameterList) )
			throw new Error( "The parameter list is of an incorrect type." );

		var tempElement;
		var element = document.getElementById( id );
		if ( element != null )
		{
			var objectElement = AppletInstaller.createObjectElement( id + AppletInstaller.ID_SUFFIX,
																	 javaVersion, codeBase, appletClass,
																	 archive, width, height, params,
																	 addText );
/*
// Doesn't work with IE or Opera

			while ( element.hasChildNodes( ) )
				element.removeChild( element.firstChild );
			element.appendChild( objectElement );
*/
			tempElement = document.createElement( "div" );
			tempElement.appendChild( objectElement );
			element.innerHTML = tempElement.innerHTML;
		}
	}

	//------------------------------------------------------------------

	AppletInstaller.createObjectElement = function( id, javaVersion, codeBase, appletClass, archive, width,
													height, params, addText )
	{

		// Create <object> element for applet

		var codeType = AppletInstaller.MIME_TYPE + javaVersion;

		var element = document.createElement( "object" );
		element.setAttribute( "id", id );
		if ( navigator.appName == AppletInstaller.MICROSOFT_IE_NAME )
		{
			element.setAttribute( "classid", "clsid:" + AppletInstaller.MICROSOFT_CLASS_ID );
			element.appendChild( new Parameter( "code", appletClass ).createElement( ) );
			element.appendChild( new Parameter( "codebase", codeBase + "/" ).createElement( ) );
			if ( archive )
				element.appendChild( new Parameter( "archive", archive ).createElement( ) );
		}
		else
		{
			element.setAttribute( "codetype", codeType );
			element.setAttribute( "classid", "java:" + appletClass );
			element.setAttribute( "codebase", codeBase + "/" );
			if ( archive )
				element.setAttribute( "archive", archive );
		}
		element.setAttribute( "width", width );
		element.setAttribute( "height", height );

		// Add application-specific parameters to <object> element

		for ( var i = 0; i < params.getLength( ); ++i )
			element.appendChild( params.get( i ).createElement( AppletInstaller.PARAM_NAME_PREFIX ) );

		// Add default text -- but not to IE, because it won't allow an <object> element to contain any
		// child nodes apart from <param> elements

		if ( addText && (navigator.appName != AppletInstaller.MICROSOFT_IE_NAME) )
		{
			var message = AppletInstaller.NO_JAVA_STR1 + javaVersion + AppletInstaller.NO_JAVA_STR2;
			element.appendChild( document.createTextNode( message ) );
		}

		return element;

	}

	//------------------------------------------------------------------

//----------------------------------------------------------------------
