/*
   On completion of window load test cycle through all the adzones and 
   test each element for the display = none style.  If found update database.
*/

window.addEvent('domready', function() {
  $$('div.advertPro').each(function(curBlock) {
    var adzone = curBlock.id;
    var userID = '<?=$_GET["userID"]?>';

    if (adzone == 'z_20') {
      var scripts = curBlock.getElementsByTagName("script");
      // alert(scripts.length + ' script tags.');
      if (scripts.length > 99) {
        // dcImage = scripts['3'].getElementsByTagName("img");
        // scripts['3'].setStyle('display') = '';
        // if (scripts['3'].getStyle('display') == 'none') {
        //   alert(adzone + ': ' + scripts['3'].getStyle('display'));
        //  updateDB(adzone, userID);
        // }
      }
    }

    var images = curBlock.getElementsByTagName("img");
    if (images.length > 0) {
      for (y=0; y < images.length; y++) {
        if (images[y].getStyle('display') == 'none') {
          // alert(adzone + ' image blocked.');
          updateDB(adzone, userID);
        }
      }
    }

    var flash = curBlock.getElementsByTagName("embed");
    if (flash.length > 0) {
      for (y=0; y < flash.length; y++) {
        if (flash[y].getStyle('display') == 'none') {
          // alert(adzone + ' flash blocked.');
          updateDB(adzone, userID);
        }
      }
    }

  }); 
});

// Database update function
//

function updateDB(adzone, userID) {
  var http = getHTTPObject();
  if (http) {
    http.open("GET", "/adblock/dbWrite.php?ipaddr=<?=$_SERVER['REMOTE_ADDR']?>&userID=" + userID + "&adzone=" + adzone, true);
    http.onreadystatechange = function() {
      if (http.readyState == 4) {
        var results = http.responseText;
      }
    }
    http.send(null);
  }
}

// Basic AJAX style request object
//

function getHTTPObject() {
  var xhr = false;
  if(window.XMLHttpRequest) {
    var xhr = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
    try {
      var xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        var xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        alert("Your browser does not support AJAX!");
        xhr = false;
      }
    }
  }
  return xhr;
}

