/*  Copyright 2005  Andrew Hill  (email : andy@hillhome.org)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

var req;
var quoteArray = new Array( );
var quoteHtmlArray = 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 = "<table border='0' cellspacing='0' cellpadding='2' width='135'><tr><td colspan='2' align='center' style='font-family:Arial;font-size:12px;'>" 
                   + companyName + "</td></tr><tr align=center><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 align=center colspan='2' style='font-family:Arial;font-size:10px;'>Change: <font color='" 
                   + color + "'>" + change + "</font><br>as of " + lastTradeTime + "</td></tr></table>";

            quoteHtmlArray[ symbol ] = html; 

            var spans = document.getElementsByTagName('span');
            if (spans) {
                for (var i = 0; i < spans.length; i++) {
                    if (spans[i].className == symbol) {
                        spans[i].innerHTML = html;
                    }
                }
            }   

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

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

        }

    }

}

