  var generationsData = new Object();
  generationsData.genYou = 0;
  generationsData.genD = 0;
  generationsData.genArray = new Array(3);
  generationsData.genArray[0] = "Child";
  generationsData.genArray[1] = "Grandchild";
  generationsData.genArray[2] = "Great grandchild";

  function initialBuild()
  {
    document.write('<center>\n');
    document.write('  <form name=generations action="" method=post>\n');
    document.write('  <table border=5><tr><td><table>\n');
    document.write(
      '<tr><td COLSPAN=3><b>Given a common blood ancestor, A...</b></td></tr>\n');
    document.write('<tr><td><b>If You are the</b></td>\n');
    document.write('<td><input type=text name=yores value="">\n');
    document.write('<input type=button value="+" onClick="incGen(1)">\n');
    document.write('<input type=button value="--" onClick="decGen(1)"></td>\n');
    document.write('<td><b>of A and</b></td></tr>\n');
    document.write('<tr><td><b>D is the</b></td>\n');
    document.write('<td><input type=text name=thares value="">\n');
    document.write('<input type=button value="+" onClick="incGen(2)">\n');
    document.write('<input type=button value="--" onClick="decGen(2)"></td>\n');
    document.write('<td><b>of A,</b></td></tr>\n');
    document.write('<tr><td COLSPAN=3><b>then </b><input type=text name=therelation value="" size=40></td></tr>\n');

    document.write('<tr><td COLSPAN=3><center><input style="background-color: #64B07C" type=button value="Table Version" onClick="openTableVersion()'
     + '"><br></center></td></tr>\n');

    document.write('</table></td></tr></table>\n');
    document.write('</form>\n');
    document.write('</center>\n');
   
    updateDisplays(); // initialize form text contents
  }

  function popupTable(yores, thares)
  {
     while( generationsData.genYou < yores )
        incGen(1);

     while( generationsData.genD < thares )
        incGen(2);

     generationsData.genYou = yores;
     generationsData.genD = thares;

     updateDisplays();
  }
  
  function openTableVersion()
  {
  var genCount = prompt("How many generations should I show?", "");

    var win = window.open("", "Cousins", 
         "width=600,height=400,status=yes,resizable=yes,scrollbars=yes");
    win.document.open();

    win.document.write('<BODY BACKGROUND="utilities/bggreen.jpg"><center><p><b>Find your Generation in the column and the generation of the ancestor in the row'
  + ' - the intersect is your relation.  Click on a field to update the '
  + 'main window.<br>You can just close this window to return to the '
  + 'referring page.</b><br><i>Note: The table will take longer to build '
  + 'the greater the number of generations you chose<\i></p>\n');
    
    win.document.write('<table BORDER=1><tr>\n');
    var col=0; // the column/comparison gen
    var row=0; // your generation
         //build the table col heads
    win.document.write('<td><center><font size=+2><b>X</b></font></center></td>');
         for( col=0; col<genCount; col++)
         {
            var genDisp = generationDisplay(col);
            win.document.write('<td><b>You are the ' 
                  + genDisp 
                  + ' of A</b></td>');
         }
         win.document.write('</tr>\n');

         for( col=0; col<genCount; col++)
         {
            var genRow = generationDisplay(col);
            win.document.write('<tr><td><b>D is the ' + genRow + ' of A</b></td>');
            for(row=0; row<genCount; row++)
            {
                var related = relationshipDisplay(row, col);
                win.document.write('<td onMouseOver="bgColor=\'#64B07C\'"'
                       + ' onClick="self.opener.popupTable(' 
                       + row + ', ' + col + ')"' 
                       + 'onmouseout="bgColor=\'#FFFFFF\'" '
                       + 'alt="Click for summary">' 
                       + related + '</td>');
            }
            win.document.write('</tr>\n');
         }
         win.document.write('</table>\n');

         win.document.close();
  }

  function buildTableVersion(genCount)
  {
    ///  Build a table in a separate window for the generations Count
         document.write('<center><p><b>Find your Generation in the '
            + 'column and the generation of the ancestor in the row '
            + '- the intersect is your relation.<br>You can just close '
            + 'this window to return to the referring page.</b></p>\n');
         document.write('<table BORDER=1><tr>\n');
         var col=0; // the column/comparison gen
         var row=0; // your generation
         //build the table col heads
         document.write('<td><center><font size=+2><b>X</b></font></center></td>');
         for( col=0; col<genCount; col++)
         {
            var genDisp = generationDisplay(col);
            document.write('<td><b>You are the ' 
                  + genDisp 
                  + ' of A</b></td>');
         }
         document.write('</tr>\n');

         for( col=0; col<genCount; col++)
         {
            var genRow = generationDisplay(col);
            document.write('<tr><td><b>D is the ' + genRow + ' of A</b></td>');
            for(row=0; row<genCount; row++)
            {
                var related = relationshipDisplay(row, col);
                document.write('<td onMouseOver="bgColor=\'#64B07C\'" '
                       + 'onClick="self.opener.popupTable(' 
                       + row + ', ' + col + ')"' 
                       + 'onmouseout="bgColor=\'#FFFFFF\'" '
                       + 'alt="Click for summary">' 
                       + related + '</td>');
            }
            document.write('</tr>\n');
         }
         document.write('</table>\n');
  }

  function numSuffix(n)
  {
    var numString = " " + n;
    var retStr = "th"; // default return
	    
    if(numString.length > 0)
    {	    
       if( numString.match(/11$/)
            || numString.match(/12$/)
            || numString.match(/13$/)
         )
            retStr = "th";
       else if( numString.charAt(numString.length - 1) == '1' )
            retStr = "st";
       else if( numString.charAt(numString.length - 1) == '2' )
            retStr = "nd";
       else if( numString.charAt(numString.length - 1) == '3' )
            retStr = "rd";
    }
	       
   return n + retStr;
  }

  function generationDisplay(genNumber)
  {
     var lgth = generationsData.genArray.length;
     if( genNumber >= lgth )
     {
       for(var ix=lgth; ix<genNumber+1; ix++)
         ///
         // Increase array size and init values
         ///
         generationsData.genArray[ix] = numSuffix(ix -1) + " " 
                     + generationsData.genArray[2];
     }
    return( generationsData.genArray[genNumber] );
  }

  function relationshipDisplay(genAsker, genComp)
  {
    var theirrelation = "You and D are siblings";
    var t1=genAsker; // your generation
    var t2=genComp;  // generation to compare
    ///
    //  return the relationship display for the two generations
    ///
        if( t1 == t2 )
        {
                if( t1 == 0 ) // both 1st gen
                {
		            theirrelation="You and D are Siblings";
		}
                else
                {
                    theirrelation = "You and D are " 
                           + numSuffix(t1) + " Cousins";
                }
        }
        else if( t1 == 0 && t2 > 0 )
        {
                var grandind="";
                if( t2 == 2 )
                {
                    grandind="Grand";
                }
                else if( t2 == 3 )
                {
                    grandind="Great Grand";
                }
                else if( t2 > 3 )
                {
                    grandind=  numSuffix(t2 - 2) + " Great Grand";
                }
          
                theirrelation="D is your " + grandind + " niece/nephew";
                    
        }
        else if( t1 > 0 && t2 == 0 )
        {
                var grandind="";
                if( t1 == 2 )
                {
                    grandind="Grand";
                }
                else if( t1 == 3 )
                {
                    grandind="Great Grand";
                }
                else if( t1 > 3 )
                {
                    grandind = numSuffix(t1 - 2) + " Great Grand";
                }
          
                theirrelation="You are the " + grandind + " niece/nephew of D";
        }
        else
        {
            var lesser = 1;
            var removed = 0;
            if( t1 > t2 )
            {
                lesser = t2;
                removed = t1 - t2;
            }
            else
            {
                lesser = t1;
                removed = t2 - t1;
            }
              
            if( removed > 0 )
            {
                theirrelation = "You and D are " 
                     + numSuffix(lesser) + " cousins " + removed; 

                if( removed == 1 )
                    theirrelation += " time removed";
                else
                    theirrelation += " times removed";
            }
            else
                theirrelation="You and D are " + numSuffix(lesser)
                    + " cousins";
        }
     return theirrelation;
  }

  function updateDisplays()
  {
    var t1 = generationsData.genYou;
    var t2 = generationsData.genD;

    document.generations.yores.value = generationDisplay(t1);
    document.generations.thares.value = generationDisplay(t2);
    document.generations.therelation.value =
         relationshipDisplay(t1, t2);
  }

  function incGen(gen)
  {
     var lgth = generationsData.genArray.length;
     if( gen == 1 )
        generationsData.genYou++;
     else
        generationsData.genD++;

     if( generationsData.genYou == lgth ||
         generationsData.genD == lgth)
     {
         ///
         // Increase array size and init value
         ///
         generationsData.genArray[lgth] = numSuffix(lgth -1) + " " 
                     + generationsData.genArray[2];
     }
     updateDisplays();
  }

  function decGen(gen)
  {
     if( gen == 1 && generationsData.genYou > 0 )
          generationsData.genYou--;
 
     if( gen == 2 && generationsData.genD > 0 )
          generationsData.genD--;

     updateDisplays();
  }
