/*********************************************************************
 * Function: setSpan
 *
 * Description: Write the new note to the specified cell.
 *
 * Input: id - String id of the table cell to be updated.
 * Input: key - The HTML form select object, contains the
 *        selectedIndex and also a list of all 12 notes.
 * Input: step - The index of the new note to write.
 *
 * Output: None.
 *********************************************************************/
function setSpan( id, key, step )
{
    var index = (key.selectedIndex + step) % 12;
    var cell = document.getElementById( id );
    cell.innerHTML = key.options[index].text;
}

/*********************************************************************
 * Function: harpLayout
 *
 * Description: This function is called when the user selects a new
 * harmonica key.  Rewrite all cells in the table.
 *
 * Input: hp - String id of the harmonica layout form.
 *
 * Output: None.
 *********************************************************************/
function harpLayout( hp )
{
    var obj = document.getElementById( hp );

   // Hole 1
   setSpan( "B1",    obj.harpkey,  0 );
   setSpan( "D1b",   obj.harpkey,  1 );
   setSpan( "D1",    obj.harpkey,  2 );
   setSpan( "ob1",   obj.harpkey,  3 );
   
   // Hole 2
   setSpan( "B2",    obj.harpkey,  4 );
   setSpan( "D2bb",  obj.harpkey,  5 );
   setSpan( "D2b",   obj.harpkey,  6 );
   setSpan( "D2",    obj.harpkey,  7 );
   setSpan( "ob2",   obj.harpkey,  8 );

   // Hole 3
   setSpan( "B3",    obj.harpkey,  7 );
   setSpan( "D3bbb", obj.harpkey,  8 );
   setSpan( "D3bb",  obj.harpkey,  9 );
   setSpan( "D3b",   obj.harpkey, 10 );
   setSpan( "D3",    obj.harpkey, 11 );
   setSpan( "ob3",   obj.harpkey,  0 );

   // Hole 4
   setSpan( "B4",    obj.harpkey,  0 );
   setSpan( "D4b",   obj.harpkey,  1 );
   setSpan( "D4",    obj.harpkey,  2 );
   setSpan( "ob4",   obj.harpkey,  3 );

   // Hole 5
   setSpan( "B5",    obj.harpkey,  4 );
   setSpan( "D5",    obj.harpkey,  5 );
   setSpan( "ob5",   obj.harpkey,  6 );

   // Hole 6
   setSpan( "B6",    obj.harpkey,  7 );
   setSpan( "D6b",   obj.harpkey,  8 );
   setSpan( "D6",    obj.harpkey,  9 );
   setSpan( "ob6",   obj.harpkey, 10 );

   // Hole 7
   setSpan( "D7",    obj.harpkey, 11 );
   setSpan( "B7",    obj.harpkey,  0 );
   setSpan( "od7",   obj.harpkey,  1 );

   // Hole 8
   setSpan( "D8",    obj.harpkey,  2 );
   setSpan( "B8b",   obj.harpkey,  3 );
   setSpan( "B8",    obj.harpkey,  4 );
   setSpan( "od8",   obj.harpkey,  5 );

   // Hole 9
   setSpan( "D9",    obj.harpkey,  5 );
   setSpan( "B9b",   obj.harpkey,  6 );
   setSpan( "B9",    obj.harpkey,  7 );
   setSpan( "od9",   obj.harpkey,  8 );

   // Hole 10
   setSpan( "D10",   obj.harpkey,  9 );
   setSpan( "B10bb", obj.harpkey, 10 );
   setSpan( "B10b",  obj.harpkey, 11 );
   setSpan( "B10",   obj.harpkey,  0 );
   setSpan( "od10",  obj.harpkey,  1 );
}

/*********************************************************************
 * Function: calcharp
 *
 * Description: Calculate the harmonica key (harpkey) based on song
 * key (songkey) and position (position).
 *
 * Input: hp - String id of the harmonica layout form.
 * 
 * Output: None.
 *********************************************************************/
function calcharp( hp )
{
    var obj = document.getElementById( hp );
    var indx = (obj.songkey.selectedIndex + 
		5 * obj.position.selectedIndex) % 12;
    obj.harpkey.selectedIndex = indx;
}

/*********************************************************************
 * Function: calcposition
 *
 * Description: Calculate the position based on the song key (songkey)
 * and the harmonica key (harpkey).
 *
 * Input: hp - String id of the harmonica layout form.
 * 
 * Output: None.
 *********************************************************************/
function calcposition( hp )
{
    var indx = 0;
    var obj = document.getElementById( hp );
    while( obj.harpkey.selectedIndex != 
	   (obj.songkey.selectedIndex + 5*indx)%12 )
	{
	    indx++;
	}
    obj.position.selectedIndex = indx;
}

/*********************************************************************
 * Function: cgilayout
 *
 * Description: Invoke the harmonica layout CGI script.  This is an
 * HTTP GET with a query string of harpkey=index, where index is the
 * harmonica key which can have a value of 0 (C) to 11 (B).
 *
 * Input: cgikey - String id of the layout form.
 *
 * Output: None.
 *********************************************************************/
function cgilayout( cgikey )
{
    var obj = document.getElementById( cgikey );

    url = "harplayout.php?harpkey=" + obj.harpkey.selectedIndex;
    window.open( url, "cgilayoutwindow",
         "width=550, height=350 location=no, menubar=no," +
         "status=no, toolbar=no, scrollbars=no, resizable=yes" );
}

