//var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
var xmlhttp = null;

// RSSFeed constructor
function RSSFeed(name, url, parms, frequency, handler) { //constructor
  this.name = name;            // a name to identify this feed
  this.url = url;              // the url of feed without customizable name=value pairs
  this.frequency = frequency;  // how often in ms to trigger feed update
  this.handler = handler;      // function to call that handles XML response
  this.timer = 0;              // internal Interval timer value used by SetInterval()
  this.parms = new Object();  // object for customizable feed attributes
  
  for(var prop in parms)      // copy customizable name/value pairs into local object
    this.parms[prop] = parms[prop];
}


// copy the parms object passed in into the RSSFeed object 
RSSFeed.prototype.setParms = function(parms) {
  for(var prop in parms)
    this.parms[prop] = parms[prop];  
}


// sets the "parms" object passed in with the values 
// of the RSSFeed parms object
RSSFeed.prototype.getParms = function(parms) {
  for(var prop in this.parms)
    parms[prop] = this.parms[prop];  
}


// construct and return the full http request string
RSSFeed.prototype.getRequest = function() {
  //debug('RSSFeed.prototype.getRequest()\n');
  var str = this.url;
  var first_time = 1;
  for(var prop in this.parms) {
    if(first_time) {
      str += '?'+ prop +'='+ this.parms[prop];
      first_time = 0;
    } else {
      str += '&'+ prop +'='+ this.parms[prop];
    }
  }
  //debug('    '+ str +'\n');
  return(str);  
}


// initiate the HTTP request
RSSFeed.prototype.loadXMLDoc = function(request_url) {
  //debug('RSSFeed.prototype.loadXMLDoc('+ request_url +')\n');

  // if a cross domain URL, force through local GetXml.php to avoid cross domain sequrity issues
  var local_url = location.protocol+'//'+location.hostname;
  if(location.port != "")
    local_url += ':'+location.port;
  var match_reg = new RegExp('^'+local_url);
  if(!request_url.match(match_reg))
    request_url = '../php/GetXml.php?url='+request_url;

  if(window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest()
    xmlhttp.onreadystatechange = this.handler;
    xmlhttp.onerror = this.handlerError;
    try {
      xmlhttp.open('GET', encodeURI(request_url), true);        
    } catch (e) {
      alert('Exception:'+e+' URL='+request_url);
    }
    xmlhttp.send(null);
  }
  else if(window.ActiveXObject) {
    if(xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")) {
      xmlhttp.onreadystatechange = this.handler;
    //try {
      xmlhttp.open('GET', encodeURI(request_url), true);
    //} catch (e) {
      //alert('Exception:'+e+' URL='+request_url);
    //}
      xmlhttp.send();
	}
  }
}


// start an Interval timer with request
RSSFeed.prototype.startFeed = function(pre_cmd, post_cmd) {
  //debug('RSSFeed.prototype.startFeed()\n');
  this.timer = window.setInterval(pre_cmd +'this.loadXMLDoc(this.getRequest())'+ post_cmd, this.frequency);
}


// initiate one request
RSSFeed.prototype.triggerFeed = function() {  
  //debug('RSSFeed.prototype.triggerFeed()\n');
  this.loadXMLDoc(this.getRequest());
}


RSSFeed.prototype.stopFeed = function() {
  //debug('RSSFeed.prototype.stopFeed()\n');
  clearInterval(this.timer);
}
