/* Dump the google ads in a usable format */
function google_ad_request_done(google_ads) {
  try {
    if (google_ads.length > 0) {
      var text = '<table class="google">\n';
      text += '<tr><th>Ads by Google</th></tr>';
      for (var i = 0; i < google_ads.length; i++) {
        text += '<tr><td><a href="' + google_ads[i].url + '" target="_blank" ';
        text += 'onmouseover="status=\'' + google_ads[i].visible_url + '\'; return true;" ';
        text += 'onmouseout="status=\'\'; return true;">';
        text += '<div class="headline">' + google_ads[i].line1 + '</div>';
        text += '<div class="description">' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '</div>';
        text += '<div class="location">' + google_ads[i].visible_url + '</div>';
        text += '</a></td></tr>';
      }
      text += '</table>';
      document.write(text);
    }
  } catch (error) {
    // swallow errors
  }
}

/* Recursively strip SCRIPT and NOSCRIPT nodes from a node */
function stripscripts(node) {
  // If the current node is null or not an element return
  if (node == null) return;
  if (node.nodeType != 1) return;
  // Use an array to remove elements (as we're modifying th
  // collection we're iterating on
  var removable = new Array();
  // Iterate over the collection of the children of this node
  for (var x = 0; x < node.childNodes.length; x++) {
    var child = node.childNodes[x];
    // If this is an element, remove it or process it recursively
    if (child.nodeType == 1) {
      var name = child.nodeName.toLowerCase();
      if ((name == 'script') || (name == 'noscript')) {
        removable.push(child);
      } else {
        stripscripts(child);
      }
    }
  }
  // Now that we have identified all the nodes to delete, strip them
  for (var x = 0; x < removable.length; x++) {
    node.removeChild(removable[x]);
  }
}

/* Reposition an element */
function reposition(nodeid) {
  // Retrieve the unique mpu node
  var node = document.all?
             document.all[nodeid]:
             document.getElementById(nodeid);
  // If the node was removed already, simply exit
  if (node == null) return null;
  // The node was not moved yet, remove the scripts,
  // get the HTML as a string and remove it
  stripscripts(node);
  var text = node.innerHTML;
  node.parentNode.removeChild(node);
  // Dump the html in this position back on the document
  document.write(text);
}

/* Handle remote loading of an URL */
function remoteload_handler(source, target) {
  var data = source.contentWindow.document;
  if (data.title == "success") {
    var replacement = data.body.innerHTML;
    if (document.all) {
      document.all[target].innerHTML = replacement;
    } else {
      document.getElementById(target).innerHTML = replacement;
    }
  }
}

/* Load an URL remotely and position it on the page */
function remoteload(url) {
  var uniq = 'remote' + (Math.floor(Math.random() * 1000000));
  var data = '<iframe src="' + url + '" id="iframe' + uniq + '" ';
  data += 'onload="remoteload_handler(this, \'' + uniq + '\');" ';
  data += 'style="display: block; top: -100px; left: -100px; ';
  data += 'position: absolute; width: 1px; height: 1px;"></iframe>';
  data += '<div id="' + uniq + '"></div>';
  document.write(data);
}

/* Perform a remotely-loaded search */
function search() {
  var query = location.search.substring(1);
  remoteload('/app/search?MaxResults=25&' + query);
}

/* Print a DART ad tag with a random number */
var dartNum = Math.round(Math.random()*10000000000);
function printAdvert(dartUrl) {
  dartUrl += (";ord=" + dartNum + "?");
  document.write("<SCRIPT language=\"JavaScript\" src=\"" + dartUrl + "\"><\/SCRIPT>");
}

