// -*- c++ -*-
// intervalrequest.js
//
// NISHI Takao <zophos@koka-in.org>
//

function IntervalRequest(uri,interval){
    this.uri=uri;
    this.interval=parseInt(interval);
    this.last_queried=new Date(1970,0,1);
    this.timer=null;
}
new IntervalRequest();
IntervalRequest.prototype.new_req=function(){
    var req;

    if(window.ActiveXObject){
        try{
            req=ActiveXObject('Msxml2.XMLHTTP');
        }
        catch(e){
            try{
                req=new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(e2){}
        }
    }
    else if(window.XMLHttpRequest){
        req=new XMLHttpRequest();
    }

    if(req){
        //
        // using closure to keep 'this'.
        //
        var obj=this;
        req.onreadystatechange=function(){
            if((req.readyState==4)&&(req.status==200)){
                try{
                    obj.show(req.responseXML);
                }
                catch(e){}
            }
        }
    }

    return(req);
}
IntervalRequest.prototype.fetch=function(){
    try{
        var req=this.new_req();
        req.open('GET',this.uri,true);
        req.setRequestHeader('If-Modified-Since',
                             this.last_queried.toGMTString());
        req.send('');
        this.last_queried=new Date();
    }
    catch(e){}

    if(this.interval>0){
        //
        // using closure to keep 'this'.
        //
        var obj=this;
        this.timer=setTimeout(function(){obj.fetch()},this.interval);
    }
}
