/*
** AmiBroker/Win32 scripting Example
**
** File:	 WebID.js
** Created:  Tomasz Janeczko, November 26th, 2000
** Last updated: Tomasz Janeczko, December 17th, 2000
** Purpose:  Import WebID assignments
** Language:	 JavaScript (Windows Scripting Host)
**
** The data is stored in lines with following format
** <ticker>,<webid>
**
*/

WScript.Echo( "Script Started" );

/* change this line according to your data file name */
ImportCodes("JSECodes.txt");

WScript.Echo( "Finished" );

function ImportCodes( filename )
{
	var fso, f, r;
	var ForReading = 1;
	var AmiBroker;
	var fields;
	var stock;


	/* Create AmiBroker app object */
	AmiBroker = new ActiveXObject( "Broker.Application" );

	/* ... and file system object */
	fso = new ActiveXObject( "Scripting.FileSystemObject" );

	/* open ASCII file */
	f = fso.OpenTextFile( filename, ForReading);

	i = 1;
	/* read the file line by line */
	while ( !f.AtEndOfStream )
	{
		r =  f.ReadLine();
		
		/* split the lines using comma as a separator */
		fields = r.split(","); 
		
		try
		{
			
			/* add a ticker - this is safe operation, in case that	 */
			/* ticker already exists, AmiBroker returns existing one */
			/* note that ticker is in the second column */
			stock = AmiBroker.Stocks.Add( fields[ 1 ] ); 
				
			stock.WebID = fields[ 0 ];

		}
		catch( e )
		{
				WScript.echo( "There is a problem in line no." + i + ".\nThe line looks as follows:\n'" + r + "'\nIt will be skipped and next lines will be processed as normal" );
		}
					
		i++;	
	}

	/* refresh ticker list and windows */
	AmiBroker.RefreshAll();

}


