// global lists

// node numbers for the fields, to be set when lists created
  var pProduct = -1;
  var pName    = -1;
  var pTitle   = -1;
  var pOrg     = -1;
  var pAddress = -1;
  var pAddress2= -1;
  var pCity    = -1;
  var pRegion  = -1;
  var pCountry = -1;
  var pDate    = -1;
  var pText    = -1;
  
  function importXML(fileName) {
    if (document.implementation && document.implementation.createDocument) {
      xmlDoc = document.implementation.createDocument('', '', null);
      xmlDoc.onload = createList;
    }
    else if (window.ActiveXObject) {
      xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
      xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState == 4) createList() };
    }
    else {
      alert("Your browser can\'t handle this script");
      return;
    }
    xmlDoc.load(fileName);
  }

  function createList() {
    var x = xmlDoc.getElementsByTagName('testimonial');
    var i, j;
// data should be name, title, organization, address, address2, city, region, country, date, text
// extra type 1 fields will be alerted
    for (j=0; j<x[0].childNodes.length; j++) {
      if (x[0].childNodes[j].nodeType == 1) {
        var curName = x[0].childNodes[j].nodeName;
	switch(curName) {
	  case('product'):      { pProduct = j; break; }
	  case('name'):         { pName    = j; break; }
	  case('title'):        { pTitle   = j; break; }
	  case('organization'): { pOrg     = j; break; }
          case('address'):      { pAddress = j; break; }
          case('address2'):     { pAddress2= j; break; }
          case('city'):         { pCity    = j; break; }
          case('region'):       { pRegion  = j; break; }
          case('country'):      { pCountry = j; break; }
          case('date'):         { pDate    = j; break; }
          case('text'):         { pText    = j; break; }
          default: alert('extra field ' + curName + ' ignored');
	}
      }
    }
    var curProducts = '';
    var nProduct, nName, sProduct;
    var listA = document.forms[0].A;
// populate list A with unique values only
    for (i=0; i < x.length; i++) {
      nProduct = x[i].childNodes[pProduct].firstChild.nodeValue;
      nName    = x[i].childNodes[pName].firstChild.nodeValue;
      sProduct = '|' + nProduct + '|';
      if (curProducts.indexOf(sProduct) < 0) {
	listA.options[listA.options.length] = new Option(nProduct, nProduct, false, false);
	curProducts = sProduct + curProducts;
      }
    }
  }

  function newLine(curEl) {
    curEl.appendChild(document.createElement('BR'));
  }
  
  function addText(xObj, curEl, preText, lineNode, postText, newL) {
    if (xObj.childNodes[lineNode].firstChild) {
       if (newL == true) { newLine(curEl) }
       curEl.appendChild(document.createTextNode(preText + xObj.childNodes[lineNode].firstChild.nodeValue + postText));
    }
  }
  
  function addParagraph(curEl, paraText) {
    if (paraText.length > 0) {
       var curPara = document.createElement('P');
       curPara.appendChild(document.createTextNode(paraText));
       curEl.appendChild(curPara);
    }
  }
  
  function addParagraphs(xObj, curEl, lineNode) {
    if (xObj.childNodes[lineNode].firstChild) {
       var paraStart = '&lt;p&gt;';
       var paraEnd   = '&lt;/p&gt;';
       var allText   = xObj.childNodes[lineNode].firstChild.nodeValue + paraStart;
       while (allText.length > 0) {
         var nextPara  = allText.indexOf(paraStart);
	 addParagraph(curEl, allText.substring(0, nextPara));
	 allText = allText.substring(nextPara + paraStart.length);
       }
    }
  }
  
  function addAnchor(xObj, curEl, preText, aType, aSub, lineNode, postText, newL) {
    if (xObj.childNodes[lineNode].firstChild) {
       if (newL == true) { newLine(curEl) }
       curEl.appendChild(document.createTextNode(preText));
       var theAnchor = document.createElement('a');
       theAnchor.setAttribute(aType, aSub + xObj.childNodes[lineNode].firstChild.nodeValue);
       theAnchor.appendChild(document.createTextNode(xObj.childNodes[lineNode].firstChild.nodeValue));
       curEl.appendChild(theAnchor);
       curEl.appendChild(document.createTextNode(postText));
    }
  }
    
  function createTable() {
    var selectedProduct = document.forms[0].A.options[document.forms[0].A.selectedIndex].value;
    var x = xmlDoc.getElementsByTagName('testimonial');
    var myTable = document.createElement('table');
    var curRow;
    var tableColumns = 8;
    var cellWidth    = 720 / tableColumns;
    var curBody = document.createElement('tbody');
    myTable.appendChild(curBody);
    for (i=0; i<x.length; i++) {
      var nProduct = x[i].childNodes[pProduct].firstChild.nodeValue;
      if (nProduct == selectedProduct) {
        curRow = document.createElement('tr');
        var curCell = document.createElement('td');
	curCell.width = 3 * cellWidth;
        curSpan   = document.createElement('font');
        addText(x[i], curSpan, '', pName, '', false);
	curSpan.style.fontWeight = 'bold';
	curCell.appendChild(curSpan);
	addText(x[i], curCell, '', pTitle, '', true);
	addText(x[i], curCell, '', pOrg, '', true);
	addText(x[i], curCell, '', pAddress, '', true);
	addText(x[i], curCell, '', pAddress2, '', true);
        addText(x[i], curCell, '', pCity,    ', ', true);
        addText(x[i], curCell, '', pRegion,  '  ', false);
        addText(x[i], curCell, '', pCountry, '', false);
        addText(x[i], curCell, '', pDate, '', true);
	curCell.style.verticalAlign = 'text-top';
        curRow.appendChild(curCell);

        curCell = document.createElement('td');
	curCell.width = 5 * cellWidth;
	curCell.style.verticalAlign = 'text-top';
        curSpan   = document.createElement('font');
        addParagraphs(x[i], curSpan, pText);
	curSpan.style.fontStyle = 'italic';
	curCell.appendChild(curSpan);
	newLine(curCell);
	curRow.appendChild(curCell);
	curBody.appendChild(curRow);
      }
    }
    document.getElementById('writeroot').appendChild(myTable);
  }
  
  function clearTable() {
    history.go();
  }
  
