// this file is part of blogQuote http://www.anders.com/projects/blogQuote/
//
// edit engineUrl below to point to the getQuote engine running on your site.
// you might change this to php using a line like:
// var engineUrl = 'http://www.yoursite.com/getQuote.php';

var engineUrl = 'http://charts.grahaminvestor.com/getQuote.jsp';

// no need to edit below this line
// -----------------------------------------------------------------------------

var req;
var quoteArray = new Array( );

function loadXMLDoc( url ) {
    if ( window.XMLHttpRequest ) { // branch for native XMLHttpRequest object
        req = new XMLHttpRequest( );
        req.onreadystatechange = processReqChange;
        req.open( "GET", url, true );
        req.send( null );

    }
    else if ( window.ActiveXObject ) { // branch for IE/Windows ActiveX version
        req = new ActiveXObject( "Microsoft.XMLHTTP" );
        if ( req ) {
            req.onreadystatechange = processReqChange;
            req.open( "GET", url, true );
            req.send( );

        }

    }

}

function processReqChange( ) {
    if ( req.readyState == 4 ) {
        if ( req.status == 200 ) {
            response = req.responseXML.documentElement;
            symbol = response.getElementsByTagName( 'symbol' )[0].firstChild.data;
            companyName = response.getElementsByTagName( 'companyName' )[0].firstChild.data;
            lastTrade = response.getElementsByTagName( 'lastTrade' )[0].firstChild.data;
            lastTradeTime = response.getElementsByTagName( 'lastTradeTime' )[0].firstChild.data;
            change = response.getElementsByTagName( 'change' )[0].firstChild.data;

	    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 = engineUrl + '?symbol=' + symbol;
        loadXMLDoc( url );

    }

}

