﻿var startValue;
var inc;
var timerID = null;
var delay = 1000;

function InitializeTimer()
{
    InitializeTimer('http://montana.collegesavings.com/ajax/CollegeCost.aspx');
}

function InitializeTimer(url)
{
    req = Ajax();

    req.onreadystatechange = function()
    {
	    if( req.readyState == 4 && req.status == 200 )
	    {
		    setVals(req.responseText);
		    ticker();
		}
	}
    req.open( 'GET', url, true);
    req.send( null );
}

function ticker()
{
    try{
        document.getElementById('tick').innerHTML = '$ ' + addCommas(startValue.toFixed(4));
        startValue = startValue + inc;
        timerID = window.setTimeout('ticker()', delay);
        }
    catch(err)
    {
    }
}

function setVals(nStr)
{
    nStr += '';
    x = nStr.split('|');
    startValue = parseFloat(x[0]);
    inc = parseFloat(x[1]);
}

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function Ajax()
{
    var XmlHttp;

    //Internet Explorer
    try
    {
	    XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
	    try
	    {
		    XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
	    catch(oc)
	    {
		    XmlHttp = null;
	    }
    }
    
    //Other browsers
    if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
    {
        //netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesRead")
	    XmlHttp = new XMLHttpRequest();
    }
    return XmlHttp;
}