//alert('protoB');

      var objSrcTree, ObjXSLT, objCache, objXSLTProc;
      var XMLroot;
      var iIndex = new Object();  // individuals index
      var pIndex = new Object();  // pair index 
      var surnameTbl = new Array();

      var permitRefocus = false;
      var blockHistory = false;
      var mySelectS, mtSelectG, mySelectV;
      var myHistory;

    /*  assists sorting on displayName   */

      function iItem(iElemPtr, surListIndex, givenListIndex) {
         this.iElem = iElemPtr;
         this.surIndex = surListIndex;
         this.givenIndex = givenListIndex;
         }
      iItem.prototype.displayCompareTo = function(b) {
         var aName = this.iElem.getAttribute("displayName").toUpperCase();
         var bName = b.iElem.getAttribute("displayName").toUpperCase();
         return (aName == bName)?0:((aName > bName)?1:-1);
         }
      iItem.displayCompare = function(a, b) { return a.displayCompareTo(b); }

      function surnameItem(sName, gTbl) {
         this.surname = sName;
         this.givenTbl = gTbl;
         }

      function displayItem(dName, id) {
         this.displayName = dName;
         this.indivId = id;
         }

    /* called at onLoad   */

      function initiate()  {
         document.getElementById('bNavs').style.visiblity = 'hidden';
         document.getElementById('aNavs').style.visiblity = 'hidden';
         document.getElementById('dNavs').style.visiblity = 'hidden';

       /*  sets trigger to resize output area on viewer's resize of window  */
         window.onresize = resizeOutputBox;

       /*  sets up "focus-individuals" history  */
         myHistory = createHistoryQ();

         /* sets up references to focus elements on HTML page  */
         mySelectS = document.getElementById("surnameSelect");;
         mySelectS.onkeypress =  comboSelect;
         mySelectG = document.getElementById("givensSelect");
         mySelectG.onkeypress = comboSelect;
         mySelectV = document.getElementById('viewSelect');
         mySelectV.selectedIndex = 0;

       /*  loadsxml input  */

         objSrcTree = fetchXML();
         if (!(objSrcTree))  {
            }

         setup();
         }


    /*  fills in XML tree; sets up initial conditions  */

      function setup()
      {
         var myXML = objSrcTree;

         XMLroot = myXML.documentElement;

       /*  builds xrefs to 'individual' elements  */
         var iList = XMLroot.getElementsByTagName('indiv');
         var workTbl = new Array();
         for (var i = 0; i < iList.length; i++)
         {
           var thisI = iList[i];
           var thisItem = new iItem(thisI, -1, -1);
           workTbl.push(thisItem);
           var thisIref = thisI.getAttribute('iID');
           iIndex[thisIref] = thisItem;
         }

       /*  builds xrefs to 'pair' elements  */
         var pList = XMLroot.getElementsByTagName('pair');
         for (var j = 0; j < pList.length; j++)
         {
           var thisP = pList[j];
           var thisPref = thisP.getAttribute('pID');
           pIndex[thisPref] = thisP;
         }

       /*  fills in missing 'individual' attributes and sub-elemnts  */
         for (var i = 0; i < iList.length; i++)  
         {   // ********** sweep all individuals **********
           var thisI = iList[i];
           if (thisI.getAttribute('parentPairRef') != null)
           {
             var thisP = pIndex[thisI.getAttribute('parentPairRef')];
             if (thisP)  // guards against child with supressed parents
               {
                var newProgeny = myXML.createElement('progeny');
                newProgeny.setAttribute('childRef', thisI.getAttribute('iID'));
                thisP.appendChild(newProgeny);
               }
           }

         /*  chooses first of the name elements for 'displayNames' attribute */
           var primeName = thisI.getElementsByTagName('iName')[0];
           var thisDN = primeName.getAttribute('surname') + ", " 
                     + primeName.getAttribute('givens') + " (";
           if ((thisBDate = thisI.getAttribute('bDate')) && (thisBDate != "9999-99-99"))
              thisDN = thisDN + thisBDate.substr(0, 4);   
           else
              thisDN = thisDN + "    ";
           if ((thisDDate= thisI.getAttribute('dDate')) && (thisDDate != "9999-99-99"))
              thisDN = thisDN + " - " + thisDDate.substr(0, 4);   
           else
              thisDN = thisDN + " -    ";
           thisDN = thisDN + ")";
           thisI.setAttribute("displayName", thisDN); 
         }

       /*  fills in missing 'pair' attributes and elements   */
         for (var j=0; j < pList.length; j++)  
         {       // ********** sweep all pairs **********
           thisP = pList[j];
           var minSortDate = '9999-99-99';  // ***** insert pairSortDate *****
           var theseMarriages = thisP.getElementsByTagName('marriage');
           if (theseMarriages.length > 0)
           {     // ***** look first in marriage record(s) for each pair  *****
              for (var k = 0; k < theseMarriages.length; k++)
              {
                 if ((thisSortDate = theseMarriages[k].getAttribute('mDate')) && (thisSortDate <  minSortDate))
                    minSortDate = thisSortDate; 
              }
           }  
           var theseProgeny = thisP.getElementsByTagName('progeny');
           if (theseProgeny.length > 0)
           {  // ***** look next in list of children *****
              for (var k = 0; k < theseProgeny.length; k++)
              {
                 var bSortDate = iIndex[theseProgeny[k].getAttribute('childRef')].iElem.getAttribute('bDate');
                 var dSortDate = iIndex[theseProgeny[k].getAttribute('childRef')].iElem.getAttribute('dDate');
                 if (bSortDate < minSortDate) minSortDate = bSortDate;
                 if (dSortDate < minSortDate) minSortDate = dSortDate;
              }
           }
           thisP.setAttribute('pairSortDate', minSortDate);

          /* create xref partner-elements */
           if (thisP.getAttribute('maleRef') != null)
           {         
             var thatI = iIndex[thisP.getAttribute('maleRef')].iElem;
             var newPartner = myXML.createElement('partner');
             newPartner.setAttribute('partnerPairRef', thisP.getAttribute('pID'));
             if (thisP.getAttribute('femaleRef') != null)
               newPartner.setAttribute('partnerRef', thisP.getAttribute('femaleRef'));
             newPartner.setAttribute('partnerSortDate', thisP.getAttribute('pairSortDate'));
             thatI.appendChild(newPartner);
           }
 
          if (thisP.getAttribute('femaleRef') != null)
           {         
             thatI = iIndex[thisP.getAttribute('femaleRef')].iElem;
             newPartner = myXML.createElement('partner');
             newPartner.setAttribute('partnerPairRef', thisP.getAttribute('pID'));
             if (thisP.getAttribute('maleRef') != null)
               newPartner.setAttribute('partnerRef', thisP.getAttribute('maleRef'));
             newPartner.setAttribute('partnerSortDate', thisP.getAttribute('pairSortDate'));
             thatI.appendChild(newPartner);
           }

         }

       /*  sort universe of displayNames  */
         workTbl.sort(iItem.displayCompare);
 
         var oldSur = "zzzzzzzzzzzzzzzzzzzz";
         var nextJ = -1;
         var displayTbl;

       /*  build surnameTbl with items that contain surname and a pointer to an array containing given-names info */
         for (var i=0; i < workTbl.length; i++) {
           var thisElem = workTbl[i].iElem;

           var thisSurname = thisElem.getElementsByTagName("iName")[0].getAttribute("surname");

           if (thisSurname.toUpperCase() != oldSur.toUpperCase()) { 
             if (nextJ >= 0) {
               surnameTbl[nextJ] = new surnameItem(oldSur, displayTbl);
               }  
             displayTbl = new Array();
             nextJ++;
    	     oldSur = thisSurname;
             }
           thisId = thisElem.getAttribute("iID");
           thisDisName = thisElem.getAttribute("displayName");

         /*  displayTbl contains displayName and pointer back to 'individual' element  */
           displayTbl.push(new displayItem(thisDisName, thisId));
           iIndex[thisId].surIndex = nextJ;
           iIndex[thisId].givenIndex = displayTbl.length - 1;
           }
         surnameTbl[nextJ] = new surnameItem(oldSur, displayTbl);

       /*  sets initial conditions  */
         resizeOutputBox();
         outputBlock.innerHTML = '';
         permitRefocus = false;
         populateSurnames();
         permitRefocus = true;
         mySelectV.selectedIndex = document.getElementById('familyOption').index;
         var surIndex = document.getElementById("surnameSelect").value;
         populateNames(surIndex);
         refocusSelect();
         mySelectS.focus();
      }  

