var req;
var quoteArray = new Array( );

function loadPage( url ) {
    req = false;
    if ( window.XMLHttpRequest ) { // branch for native XMLHttpRequest object
        try {
            req = new XMLHttpRequest( );

        }
        catch( e ) {
            req = false;

        }

    }
    else if ( window.ActiveXObject ) { // branch for IE/Windows ActiveX version
        try {
            req = new ActiveXObject( "Msxml2.XMLHTTP" );

        }
        catch( e ) {
            try {
                req = new ActiveXObject( "Microsoft.XMLHTTP" );

            }
            catch( e ) {
                req = false;

            }

        }

    }
    if ( req ) {
        try {
            req.onreadystatechange = processStateChange;
            req.open( "GET", url, true );
            req.send( );

        }
        catch( e ) {
            alert( "whoops, problem trying to get data: " + e );

        }

    }

}

function processStateChange( ) {
    if ( req.readyState == 4 ) {
        if ( req.status == 200 ) {
            alert( req.responseText );
            response = req.responseText.split( '"' );
            symbol = response[1];
            companyName = response[3];
            lastTrade = response[4].substring( 1, ( response[4].length - 1 ) ); // lop off first and last chars as they are commas because this field is unquoted
            lastTradeDate = response[5];
            lastTradeTime = response[7];
            change = response[9];

	    quoteArray[ symbol ] = new Date( ).getTime( );

            var color = "#000000";
            if ( change.substring( 0, 1 ) == '-' ) {
                color = "#FF0000";

            }
            else if ( change.substring( 0, 1 ) == '+' ) {
                color = "#00AA00";

            }
            html = "<center><table border='0' cellspacing='0' cellpadding='2' width='130'><tr><td colspan='2' align='center' style='font-family:Arial;font-size:12px;'>" 
                   + companyName + "</td></tr><tr><td style='font-family:Arial;font-size:14px;'><b>$" + lastTrade + "</b></td><td><img src='http://ichart.finance.yahoo.com/h?s=" 
                   + symbol + "' width='60' height='16' border='0'></a></td></tr><tr><td colspan='2' style='font-family:Arial;font-size:10px;'>Change: <font color='" 
                   + color + "'>" + change + "</font><br>as of " + lastTradeTime + "</td></tr></table></center>";

            eval( "document.getElementById( '" + symbol + "' ).innerHTML = html" );

        }
        else {
            // alert( "There was a problem retrieving the XML data:\n" + req.statusText );

        }

    }

}

function showQuote( symbol ) {
    if ( quoteArray[ symbol ] == null || quoteArray[ symbol ] < new Date( ).getTime( ) - 100000 ) {
	eval( "document.getElementById( '" + symbol + "' ).innerHTML = '<center>Loading...</center>'" );
        url = 'http://quote.yahoo.com/d/quotes.csv?s=' + symbol + '&f=snl1d1t1c';
        loadPage( url );

    }

}

