<!--

note1 = new Image(31,13);
note1.src = "note1.gif";

marker = new Image(31,13);
marker.src = "marker.gif";

rh_0 = new Image(31,13);
rh_0.src = "rh_0.GIF";

lh_0 = new Image(31,13);
lh_0.src = "lh_0.GIF";

note8 = new Image(31,13);
note8.src = "note8.gif";

rroot = new Image(31,13);
rroot.src = "root.gif";

bfing = new Image(31,13);
bfing.src = "plot.GIF";

chroot="";
key=0;
oldkey=0;
nkey=0;
delta=0;
oldroot=-1;

p = new Array();
p[1]='1 E';p[2]='1 F';p[3]='1 F#';p[4]='1 G';p[5]='1 G#';p[6]='1 A'; p[7]='1 A#';p[8]='1 B';
p[9]='1 C';p[10]='1 C#';p[11]='1 D';p[12]='1 D#';p[13]='1 e';p[14]='1 f';p[15]='1 f#';
p[16]='1 g';p[17]='1 g#';p[18]='1 a';p[19]='1 a#';p[20]='1 b';p[21]='1 c';

p[23]='2 B';p[24]='2 C';p[25]='2 C#';p[26]='2 D';p[27]='2 D#';p[28]='2 E';p[29]='2 F';
p[30]='2 F#';p[31]='2 G';p[32]='2 G#';p[33]='2 A';p[34]='2 A#';p[35]='2 b';p[36]='2 c';
p[37]='2 c#';p[38]='2 d';p[39]='2 d#';p[40]='2 e';p[41]='2 f';p[42]='2 f#';p[43]='2 g';

p[45]='3 G';p[46]='3 G#';p[47]='3 A';p[48]='3 A#';p[49]='3 B';p[50]='3 C';p[51]='3 C#';
p[52]='3 D';p[53]='3 D#';p[54]='3 E';p[55]='3 F';p[56]='3 F#';p[57]='3 g';p[58]='3 g#';
p[59]='3 a';p[60]='3 a#';p[61]='3 b';p[62]='3 c';p[63]='3 c#';p[64]='3 d';p[65]='3 d#';

p[67]='4 D';p[68]='4 D#';p[69]='4 E';p[70]='4 F';p[71]='4 F#';p[72]='4 G';p[73]='4 G#';
p[74]='4 A';p[75]='4 A#';p[76]='4 B';p[77]='4 C';p[78]='4 C#';p[79]='4 d';p[80]='4 d#';
p[81]='4 e';p[82]='4 f';p[83]='4 f#';p[84]='4 g';p[85]='4 g#';p[86]='4 a';p[87]='4 a#';

p[89]='5 A';p[90]='5 A#';p[91]='5 B';p[92]='5 C';p[93]='5 C#';p[94]='5 D';p[95]='5 D#';
p[96]='5 E';p[97]='5 F';p[98]='5 F#';p[99]='5 G';p[100]='5 G#';p[101]='5 a';p[102]='5 a#';
p[103]='5 b';p[104]='5 c';p[105]='5 c#';p[106]='5 d';p[107]='5 d#';p[108]='5 e';p[109]='5 f';

p[111]='6 E';p[112]='6 F';p[113]='6 F#';p[114]='6 G';p[115]='6 G#';p[116]='6 A';p[117]='6 A#';
p[118]='6 B';p[119]='6 C';p[120]='6 C#';p[121]='6 D';p[122]='6 D#';p[123]='6 e';p[124]='6 f';
p[125]='6 f#';p[126]='6 g';p[127]='6 g#';p[128]='6 a';p[129]='6 a#';p[130]='6 b';p[131]='6 c';

p[133]='7 B';p[134]='7 C';p[135]='7 C#';p[136]='7 D';p[137]='7 D#';p[138]='7 E';p[139]='7 F';
p[140]='7 F#';p[141]='7 G';p[142]='7 G#';p[143]='7 A';p[144]='7 A#';p[145]='7 b';p[146]='7 c';
p[147]='7 c#';p[148]='7 d';p[149]='7 d#';p[150]='7 e';p[151]='7 f';p[152]='7 f#';p[153]='7 g';

n = new Array();
n[1] = 30;
n[2] = 25;
n[3] = 21;
n[4] = 16;
n[5] = 11;
n[6] = 6;
n[7] = 1;

b = new Array();
b[1] = 1;
b[2] = 23;
b[3] = 45;
b[4] = 67;
b[5] = 89;
b[6] = 111;
b[7] = 133;

k = new Array();

s = new Array();
d = new Array();
same = new Array();


function plot(x,n)
{

    for(i=1; i<154; i++) {
    
        if( document.forms[0].note[x].checked ) {
      
          if( i % 22 != 0 )

            if( p[i].substring(2).toUpperCase() == n && 
                !( document.images[i].src.indexOf("lh_0.GIF") != -1 ||
                  document.images[i].src.indexOf("rh_0.GIF") != -1 ) ) hiLite(i,'bfing');
        }
        else {
          document.forms[0].rr[x].checked = false;
        
          if( i % 22 != 0 )
            if( p[i].substring(2).toUpperCase() == n &&
                (document.images[i].src.indexOf("plot.GIF") != -1 ||
                 document.images[i].src.indexOf("root.gif") != -1 ) ) hiLite(i,'note1');
        } 
    }
}


function show_root(x,n)
{
   
   if( oldroot != n && oldroot != -1) {
    document.forms[0].rr[oldcheck].checked = false;
    for(i=1; i<154; i++) {
       if( i % 22 != 0 )
        if( p[i].substring(2).toUpperCase() == oldroot &&
            document.images[i].src.indexOf("root.gif") != -1 ) hiLite(i,'bfing');
    }
   }

   for(i=1; i<154; i++) {
    
        if( document.forms[0].rr[x].checked ) {
          document.forms[0].note[x].checked = true;
          if( i % 22 != 0 )
            if( p[i].substring(2).toUpperCase() == n && 
                !( document.images[i].src.indexOf("lh_0.GIF") != -1 ||
                  document.images[i].src.indexOf("rh_0.GIF") != -1 ) ) hiLite(i,'rroot');
        }
        else {
          if( i % 22 != 0 )
            if( p[i].substring(2).toUpperCase() == n &&
                document.images[i].src.indexOf("root.gif") != -1 ) hiLite(i,'bfing');
        } 
  }
  oldroot=n;
  oldcheck=x;
}


function clrNotes() 
{
  for(i=1; i<154; i++) 
     if( document.images[i].src.indexOf("plot.GIF") != -1 ||
         document.images[i].src.indexOf("root.gif") != -1 ) hiLite(i,'note1');
       
  for(i=0; i<12; i++) {    
     document.forms[0].note[i].checked = false;
     document.forms[0].rr[i].checked = false;
  }
}


function getquality(q) 
{
   /* free up array */
   for(i=0; i<nkey; i++) k[i]=0;
   nkey=0;
   

   for(i=0; i<c.length; i++) {
          //alert( c[i][14]+c[i][14].substring(2,4) );  
      switch(q) {
        case 1 : if( c[i][14].indexOf("/") < 0 && 
                     c[i][14].substring(2,4) == "ma" || c[i][14].substring(3,5) == "ma" || 
                     c[i][14].length == 1 || c[i][14].length == 2 || 
                     c[i][14].charAt(2) == "6" || c[i][14].charAt(3) == "6" ||
                     c[i][14].substring(2,5) == "add" || c[i][14].substring(3,6) == "add" ) {
                   k[nkey]=i;
                   nkey++;
                 } break;
        case 2 : if( ( c[i][14].substring(2,4) == "mi" || c[i][14].substring(3,5) == "mi" ) && 
                       c[i][14].indexOf("mi7-5") < 0 && c[i][14].indexOf("/") < 0 ) {
                   k[nkey]=i;
                   nkey++;
                 } break;
        case 3 : if( ( c[i][14].charAt(2) == "1" || c[i][14].charAt(3) == "1" ||
                     c[i][14].charAt(2)  == "7" || c[i][14].charAt(3)  == "7" ||
                     c[i][14].charAt(2)  == "9" || c[i][14].charAt(3)  == "9") &&
                     c[i][14].indexOf("sus") < 0  && c[i][14].indexOf("6") < 0 ) {
                     
                   k[nkey]=i;
                   nkey++;
                 } break;
        case 4 : if( c[i][14].indexOf("dim") > 0 ) {
                   k[nkey]=i;
                   nkey++;
                 } break;
        case 5 : if( c[i][14].indexOf("mi7-5") > 0 ) {
                   k[nkey]=i;
                   nkey++;
                 } break;
        case 6 : if( c[i][14].indexOf("aug") > 0 ) {
                   k[nkey]=i;
                   nkey++;
                 } break;
        case 7 : if( c[i][14].indexOf("sus") > 0 && c[i][14].indexOf("/") < 0 ) {
                   k[nkey]=i;
                   nkey++;
                 } break;
        case 8 : if( c[i][14].indexOf("/") > 0 ) {
                   k[nkey]=i;
                   nkey++;
                 } break;
        case 9 : if( c[i][14].charAt(2) == "?" || c[i][14].charAt(3) == "?" || 
                     c[i][14].charAt(2) == "5" || c[i][14].charAt(3) == "5" ||
                     c[i][14].charAt(2) == "b" || c[i][14].charAt(3) == "b"   ) {
                   k[nkey]=i;
                   nkey++;
                 } break;
       }
   }

   /* alert( "number of keys"+" "+nkey ); */
  /* for(i=0; i<nkey; i++) {
      alert( "keys "+" "+k[i]+" "+c[ k[i] ][14] );} */
}

function getindex()
{
  if( key < 0 ) key = 0;
  
  if( nkey > 0 ) {
    
    reset_chord(k[key]);
  }   
  key=-1;

  switch( document.solene.quality.selectedIndex ){
    case 1 : getquality(1); break;
    case 2 : getquality(2); break;
    case 3 : getquality(3); break;
    case 4 : getquality(4); break;
    case 5 : getquality(5); break;
    case 6 : getquality(6); break;
    case 7 : getquality(7); break;
    case 8 : getquality(8); break;
    case 9 : getquality(9); break;
    default: break;
  }
  
  if( document.solene.quality.selectedIndex == 0 ) {
    document.solene.chordcount.value = "";
    document.solene.chordname.value = "";  
  }
  else {
    // reset transpose
    delta=0;
    document.solene.root.selectedIndex=1;

    show('next');
    document.solene.chordcount.value = "1 of "+nkey;
  }
  
}


function transpose(x) {

   r="";
   y = c[x][14].indexOf("/");
   
   if( y > 0 ) {
       
       r = c[x][14].substring(y+1,y+3);   
   }
   else 
   { r = c[x][14].substring(0,2);  }

   if( r == "C " ) { return 1; }
   else if( r== "C#" ) { return 2; }
   else if( r== "D " ) { return 3; }
   else if( r== "D#" ) { return 4; }
   else if( r== "E " ) { return 5; }
   else if( r== "F " ) { return 6; }
   else if( r== "F#" ) { return 7; }
   else if( r== "G " ) { return 8; }
   else if( r== "G#" ) { return 9; }
   else if( r== "A " ) { return 10; }
   else if( r== "A#" ) { return 11; }
   else if( r== "B " ) { return 12; }
}

function getroot() {

if( c[k[key]][14].indexOf(" ") >= 0 ) {
  chquality = c[k[key]][14].substring( c[k[key]][14].indexOf(" ") );
}

switch( document.solene.root.selectedIndex ) {
    case 1: chroot ="C" + chquality;break;
    case 2: chroot ="C#" + chquality;break;
    case 3: chroot ="D" + chquality;break;
    case 4: chroot ="D#" + chquality;break;
    case 5: chroot ="E" + chquality;break;
    case 6: chroot ="F" + chquality;break;
    case 7: chroot ="F#" + chquality;break;
    case 8: chroot ="G" + chquality;break;
    case 9: chroot ="G#" + chquality;break;
    case 10: chroot ="A" + chquality;break;
    case 11: chroot ="A#" + chquality;break;
    case 12: chroot ="B" + chquality;break;
    default: chroot = c[k[key]][14];break;
  }
  space="";
  cl = Math.ceil( chroot.length / 2 );
  for(ss=0; ss<7-cl; ss++) { space = space + " "; }

  document.solene.chordname.value = space + chroot;

}

function hiLite(x,imgObjName){ document.images[x].src = eval(imgObjName + ".src"); }

function reset_chord(x)
{
for(i=0; i<7; i++) 
  if( c[x][i+7] != "" ) {
    // alert( "reset: "+c[x][i]+" "+delta );
    hiLite( c[x][i]+delta , 'note1');
  }
}

function load_chord(x)
{
  
  delta = 0;
  upper=true;
  lower=true;

  if( document.solene.root.selectedIndex != 0 ) {
     delta = document.solene.root.selectedIndex - transpose(x);
  }  

  /* if below nut then add 12 frets */ 
  for(i=0; i<7; i++) {
     if( c[x][i] != 0 && (c[x][i] + delta <= i*22) ) {
         delta = delta + 12;
         break;
     }
  }
  

  /* if not in upper bounds then can't fit */
  for(i=0; i<7; i++) {
     //alert( c[x][i]+delta + " " + (i+1)*22);
     if( c[x][i] + delta >= (i+1)*22 ) { 
       upper=false;
       break;
     }
  }

 /* try finding the lowest position by dropping down 12 frets */
 for(i=0; i<7; i++) {
    //alert( c[x][i]+ delta-12 +" " + i*22);
    if( c[x][i] != 0 && (c[x][i] + delta - 12 <= i*22 ) ) {
      lower=false;
      break;
    }
  } 

 if( lower ) { delta = delta - 12; }

 if( !lower && !upper ) {
  document.solene.chordname.value = c[k[key]][14];
  delta=0; 
 }
 else { getroot(); }
 
   /* alert("adjust "+delta);  */
 
  for(i=0; i<7; i++) {
      if( c[x][i+7] == 'r') hiLite( c[x][i]+delta, 'rh_0' );
      if( c[x][i+7] == 'l') hiLite( c[x][i]+delta, 'lh_0' ); 
  }

  if( !lower && !upper ) {
    alert("Transposed chord cannot fit on fingerboard"); 
  }
 
}

function numberorder(a,b) { return a - b; }

function equiv_chord(x,s){

/* transpose to a common root */

  del = 5 - transpose(x);
  

  /* check bounds and adjust if neccesary */ 
  for(i=0; i<7; i++) {
      /* alert( c[x][i] + del + " " + i*22 +"  " + (i+1)*22 );  */
      if( c[x][i] != 0 && (c[x][i] + del <= i*22 || c[x][i] + del > (i+1)*22) ) {
        del = del + 12;
        break;
      }
  }

/* calculate actual pitches (low B = 1). Mark open string with 200 */

 for(i=0; i<7; i++) {
     if( c[x][i] == 0 ) { s[i] = 200 }
     else { s[i] = ( (c[x][i]+del) - b[i+1] ) + n[i+1] }

  }

  s.sort(numberorder);

}


function changeRoot() {
	if( key >= 0 ) {
  	  reset_chord(k[key]);
  	  load_chord(k[key]); 
  	}
}

function show(button)
{
  
	  if( nkey == 0 || document.solene.quality.selectedIndex == 0 ) { 
	     return; 
	  }

	  oldkey=key;
	  if( oldkey < 0) oldkey=0; 

	  if(button == 'first') key=0
	  else if(button == 'last') key=nkey-1
	  else {
	    if(button == 'next') key++
	    else key--;

	    if(key > nkey-1) key = nkey-1
	    else if(key < 0) key = 0; 
	  }


	  reset_chord(k[oldkey]);
	  load_chord(k[key]);   

	  document.solene.chordcount.value = key+1+" of "+nkey;

}

function init()
{
  var splitData = "&";
  
  document.solene.chordname.value = "";  
  document.solene.chordcount.value = "";
  document.solene.quality.selectedIndex = 0; 
  document.solene.root.selectedIndex = 0;  
   
  key=1;
  oldkey=0;
  nkey=0;
  delta=0;
    
  var searchInfo = window.location.search;
  searchInfo = searchInfo.substring(1, searchInfo.length);

  //create and fill a new Array that stores the search strings
  var searchArray = new Array();
  searchArray = searchInfo.split(splitData);
  
  if( searchArray.length > 0 ) {

    document.solene.quality.selectedIndex = searchArray[0];     
    getindex(); 
  }

 
}

function redraw() {
  if( key >= 0 ) {
    load_chord(k[key]);   
  }
}

// -->
