
/*   JavaScript Guitar Chordfinder. Revised and adapted by Victor Hookstra at the GarageDoor (thegaragedoor.com)
	Original Java engine by  Gordon McComb.

*/

// initialize timer handle
var Timerid = 0;
var chords = new Object();

chords["Dim"] = "110000;14;16;21;23"
chords["Aug"] = "110000;17;21;22;26"
chords["Min6"] = "010000;14;18;21;22;23"
chords["Min7b5"] = "100001;13;15;20;22"
chords["Maj7"] = "110000;8;21;22;23"
chords["Min7"] = "110000;14;22;23;27"

chords["A"] = "100000;1;5;14;15;16"
chords["Am"] = "100000;1;5;10;14;15"
chords["A7"] = "100000;1;3;5;14;16"
chords["Am7"] = "100000;1;10;14;15;23"
chords["Amaj7"] = "100000;1;5;9;14;16"

chords["B"] = "100000;13;17;26;27;28"
chords["Bm"] = "100000;13;17;22;26;27"
chords["B7"] = "100000;4;8;13;15;17"
chords["Bm7"] = "100000;2;4;13;15;17"
chords["Bmaj7"] = "100000;13;17;21;26;28"

chords["C"] = "100000;3;5;10;14;19"
chords["Cm"] = "101000;3;19;23;28"
chords["C7"] = "100000;5;10;14;19;21"
chords["Cm7"] = "101000;19;21;23;28"
chords["Cmaj7"] = "100000;3;4;5;14;19"

chords["D"] = "110000;2;15;17;22"
chords["Dm"] = "110000;2;11;15;22"
chords["D7"] = "110000;2;10;15;17"
chords["Dm7"] = "110000;2;10;11;15"
chords["Dmaj7"] = "110000;2;15;16;17"

chords["E"] = "000000;0;4;5;9;13;14"
chords["Em"] = "000000;0;3;4;5;13;14"
chords["E7"] = "000000;0;2;4;5;9;13;"
chords["Em7"] = "000000;0;3;5;13;14;22"
chords["Emaj7"] = "000000;0;4;8;5;9;13;"

chords["F"] = "110000;10;11;15;20"
chords["Fm"] = "110000;9;10;11;20"
chords["F7"] = "000000;6;8;10;11;15;19"
chords["Fm7"] = "010001;6;8;9;10"
chords["Fmaj7"] = "110000;5;10;15;20"

chords["G"] = "000000;2;3;4;13;18;23"
chords["Gm"] = "010000;2;18;21;22;23"
chords["G7"] = "000000;2;3;4;11;13;18"
chords["Gm7"] = "010001;18;20;21;22"
chords["Gmaj7"] = "010000;18;2;3;4;17"

// build the chart in the table
for (Countx = 1; Countx < 6; Countx++) {
	var Count, Countx;
	for (Count = 1; Count <7; Count++) {
		document.write ("<input type=radio onClick='toggle(this)'>")
	}
	document.write ("<br />")
	if (Countx == 1)
        document.write
	else
        document.write ("<!--/////////////////////--><img src=x.gif width=120 height=3 align=absmiddle>")
	document.write ("<br />")
}

function toggle(button) {
	button.checked = !button.checked;
}

// remove the check from all radio buttons
function resetGuitar() {
	var Count;
	clearTimeout(Timerid);
	for (Count=1; Count < 30; Count++) {
		document.guitar[Count-1].checked=false
	}
}

// flash a button for any string that shouldn't be played
function flashString () {
	var Count;
	for (Count = 0; Count < 6; Count++) {
	if (Frets[1].substring(Count,Count+1) == "1")
		document.guitar[Count].checked = !document.guitar[Count].checked
	}
	Timerid = setTimeout ("flashString()", 500)
}

// set the chord pattern
function setGuitar() {
	resetGuitar();
	var Item, Ret, Count, Temp, Skip;
	Item = document.guitar.chord.selectedIndex;
	if (Item != -1) {
		Text = document.guitar.chord.options[Item].text;
		Frets = parser (chords[Text])
		for (Count = 2; Count <= Frets[0]; Count++) {
			Temp = parseInt(Frets[Count]);
			document.guitar[Temp].checked=true;
		}
		if (parseInt(Frets[1]) > 0)
			flashString();
	}
}

// general function for parsing strings using a specified parse character; result is values in
// separate elements of an array
function parser (InString)  {
	var Sep = ";", NumSeps=1, Count, Start, ParseMark, parse;
	for (Count=1; Count < InString.length; Count++)  {
		if (InString.charAt(Count)==Sep)
			NumSeps++;
	}
	parse = new Array ();
	var Start=0, Count=1, ParseMark=0, LoopCtrl=1;
	while (LoopCtrl==1)  {
		ParseMark = InString.indexOf(Sep, ParseMark);
		TestMark=ParseMark+0;
		if ((TestMark==0) || (TestMark==-1)){
			parse[Count]= InString.substring (Start, InString.length);
			LoopCtrl=0;
			break;
		}
		parse[Count] = InString.substring (Start, ParseMark);
		Start=ParseMark+1, ParseMark=Start, Count++;
	}
	parse[0]=Count;
	return (parse);
}
