var req;
var details;
var supply;
var thepopup;
var intervalID;

function loadXMLDoc(url) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
   else { alert('request failed');
   } 
   }
   
}


function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...

      response  = req.responseXML.documentElement;

      method = response.getElementsByTagName('method')[0].firstChild.data;

      result =  response.getElementsByTagName('result')[0].firstChild.data;
//      details =  response.getElementsByTagName('details')[0].firstChild.data;

      eval( method + '(\'' + result + '\')');

        } else {
            alert("There was a problem retrieving the XML  data:\n" + req.statusText);
        }
    }
  }



function thetopic(response) {
 var r;
r = new String;
 r = response;
 r = r.replace(/#183;/g,String.fromCharCode(183));
 response = r.replace(/&/g,'');

 updateselect('thetopic',response);
}


function updateselect(objid,response)
{
   var options = response.split("%%");
   var  oSelect   = document.getElementById(objid);
  
  for (i = oSelect.length ; i>0; i--) {
          oSelect.remove(i);
      }

   for(i = 0; i < options.length; i++){
     thisone = options[i].split("=");

      var oOption = document.createElement("OPTION");
      oOption.text= thisone[1];
      oOption.value= thisone[0];

      try {
         oSelect.add(oOption,null);  // standards compliant; doesn't work in IE
      }
      catch(ex) {
        oSelect.add(oOption);; // IE only
     }
  }  


  //  oSelect.className = "show";
}






function doit(mode,input) {
    // Input mode

    url  = 'http://www.viewtech.co.uk/scripts/topiclist.pl?q=' + input +'&m=' + mode;

    loadXMLDoc(url);
  }

function findtopic() {
 // find the topics for this subject  by AJAX
 var subject =  document.forms['theform'].elements['subject'].value;
 var  oDiv   = document.getElementById('thetopics');
 
  if (subject != 'ALL') {
   oDiv.className= "show" 
    doit('thetopic',subject)
//     alert(oDiv.className);
  } else {
     oDiv.className = "hidden";
//     alert(oDiv.className);
   } 
 }
