// CJ Keyword Ad Demo Static JavaScript

var xmlhttp;
var destinationElement;

function loadAds(keywds) {
  var url = "http://www.wine-refrigerators.net/cgi-bin/search.pl?keywords=" + keywds;
  xmlhttp = null;
  if (window.XMLHttpRequest) {
    // code for Mozilla, etc.
    xmlhttp=new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    // code for IE
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  if (xmlhttp != null) {
    xmlhttp.onreadystatechange=onResponse;
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
  } else {
    alert("Your browser does not support XMLHTTP.");
  }
}

function checkReadyState(obj) {
  if (obj.readyState == 4) {
    if (obj.status == 200) {
      return true;
    } else {
      alert("Problem retrieving XML data");
    }
  }
}

function onResponse() {
  if (checkReadyState(xmlhttp)) {
    var response = xmlhttp.responseXML.documentElement;
    txt = "<table border='0'>";
    x = response.getElementsByTagName("product");
    for (i = 0; i < x.length; i++) {
      txt += "<tr><td>";

      xx = x[i].getElementsByTagName("buyurl");
      try {
        txt += "<a href=\"" + xx[0].firstChild.data + "\">";
      } catch (er) {
        txt += "<!-- No Buy URL -->";
      }

      xx = x[i].getElementsByTagName("imgurl");
      try {
        txt += "<img src=\"" + xx[0].firstChild.data + "\" border=\"0\" align=\"left\" style=\"max-width: 155px; max-height: 155px;\"/>";
      } catch (er) {
        txt += "<!-- No Img URL -->";
      }
      //txt += "<br/>";

      xx = x[i].getElementsByTagName("name");
      try {
        txt += xx[0].firstChild.data;
      } catch (er) {
        txt += "<!-- No name -->";
      }
      txt += "</a></td></tr>";

    }

    txt += "</table>";
    document.getElementById(destinationElement).innerHTML = txt;
  }
}

function loadKeywordAds(dstElement) {
  destinationElement = dstElement;
  var metaArray = document.getElementsByName('keywords');
  var kkeywords = '';
  for (var i=0; i<metaArray.length; i++) {
    kkeywords += metaArray[i].content + ", ";
  }
  loadAds(kkeywords);
}

