var counter = {

  counterNum : null,

  init : function() {
    if (!document.getElementById || !document.getElementsByTagName) { return; }
    util.sendRequest('includes/setCounter.php?' + Math.random(), counter.theResult);
  },

  theResult : function(xhr) {
    counter.counterNum = xhr.responseText;
    var fo = new FlashObject("../swf/odometer.swf?counterStart=0" + counter.counterNum + "&amp;delayTime=1000", "mainNav", "125", "15", "6", "#B3B3B3");
    fo.addParam("quality", "high");
    fo.addParam("wmode", "transparent");
    fo.addParam("base", "/swf/");
    fo.addParam("align", "l");
    fo.addParam("salign", "tl");
    fo.write("flashCounter");
  }

}

var util = {

  sendRequest : function(url,func,postData) {
    var xhr = util.createXMLHttpObject();
    if (!xhr) { return; }
    var method = (postData) ? "POST" : "GET";
    xhr.open(method, url, true);
    xhr.setRequestHeader('User-Agent','XHR');
    if (postData) {
      xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    }
    xhr.onreadystatechange = function() {
      if (xhr.readyState != 4) { return; }
      if (xhr.status != 200 && xhr.status != 304) {
         return;
      }
      func(xhr);
    }
    if (xhr.readyState == 4) { return; }
    xhr.send(postData);
  },

  XHRoptions : [
   function () {return new XMLHttpRequest()},
   function () {return new ActiveXObject("Msxml2.XMLHTTP")},
   function () {return new ActiveXObject("Msxml3.XMLHTTP")},
   function () {return new ActiveXObject("Microsoft.XMLHTTP")}
  ],

  createXMLHttpObject : function() {
    var xmlhttp = false;
    for (var i=0;i<util.XHRoptions.length;i++) {
      try { xmlhttp = util.XHRoptions[i](); }
      catch (e) { continue; }
      break;
    }
    return xmlhttp;
  },

  addEvent : function(obj, type, func) {
    if (obj.addEventListener) {obj.addEventListener(type, func, false);}
    else if (obj.attachEvent) {
      obj["e" + type + func] = func;
      obj[type + func] = function() {obj["e" + type + func] (window.event);}
      obj.attachEvent("on" + type, obj[type + func]);
    }
    else {obj["on" + type] = func;}
  }

}

counter.init();
