function setKey(newKey){
	position=offset*7;
	while (position>12)position-=12;
	setBG(position,0);
	offset=newKey;
	insRow();
	position=offset*7;
	while (position>12)position-=12;
	setBG(position,0);
}
function setScale(newScale){
	setBG(scale,2);
	scale=newScale;
	insRow();
	setBG(scale,2);
}
function setScaleOffset(newOffset){
	position=scaleOffset*7;
	while (position>12)position-=12;
	setBG(position,1);
	scaleOffset=newOffset;
	if (!scale)setScale(1);
	//if (scale==0) scale=1;
	insRow();
	position=scaleOffset*7;
	while (position>12)position-=12;
	setBG(position,1);
}

function setChord(newChord){
	setBG(chord,4);
	chord=newChord;
	insRow();
	setBG(chord,4);
}
function setChordOffset(newOffset){
	position=chordOffset*7;
	while (position>12)position-=12;
	setBG(position,3);
	chordOffset=newOffset;
	chordRootIndex=0;
	if (!chord)setChord(1);
	insRow();
	position=chordOffset*7;
	while (position>12)position-=12;
	setBG(position,3);
}


function setHarmonica(newHarmonica,all){
	if (all){
		setBGHarm(harmonica);
		harmonica=newHarmonica;
		insRow();
		setBGHarm(harmonica);
	}else{
		harmonica=newHarmonica;
		insRow();
		writeHarNames(all);
	}
}

function setHarmonicaSub(newHarmonica){
	setBGHarm(harmonica);
	harmonica=newHarmonica;
	insRow();
	setBGHarm(harmonica);
}


function setBG(row,col){
	cell=document.getElementById("myKeys").rows[row].cells[col];
	if (cell.style.backgroundColor=="white" || !cell.style.backgroundColor) cell.style.backgroundColor=newGreen;
	else cell.style.backgroundColor="white";
}
function setBGHarm(row){
	try{
		cell=document.getElementById("myHarm").rows[row+3].cells[0];
		if (cell.style.backgroundColor=="white" || cell.style.backgroundColor=="#eeeeee" || !cell.style.backgroundColor){
			cell.style.backgroundColor=newGreen;
			cell.innerHTML+="<a NAME=hier"+harmonica+"></a>";
		}
		else if (harmonicaNames[row])cell.style.backgroundColor="white";
		else cell.style.backgroundColor="#eeeeee";
	}catch(e){}
}

function writeScales(){                          
	while (document.getElementById("myKeys").rows.length)document.getElementById("myKeys").deleteRow(0);
	for (i=0;i<12;i++){
		rows[i]=document.getElementById("myKeys").insertRow(i);
		position=i*7;
		while (position>12)position-=12;
		var newCell=rows[i].insertCell(0);
		newCell.width="10%";
		newCell.innerHTML="<a href=# onClick=blur();setKey("+position+"); title=\"Change the key of the harmonica to "+notes[0][position]+"\">"+notes[0][position]+"</a>";
		var newCell=rows[i].insertCell(1);
		newCell.width="10%";
		newCell.innerHTML="<a href=# onClick=blur();setScaleOffset("+position+"); title=\"Change the key of the highlighted scale to "+notes[0][position]+"\">"+notes[0][position]+"</a>";
	}

	for (i=0;i<scales.length;i++){
		if (i>=12) {
			rows[i]=document.getElementById("myKeys").insertRow(i);
			var newCell=rows[i].insertCell(0);
			var newCell=rows[i].insertCell(1);
		}
		var newCell=rows[i].insertCell(2);
		newCell.width="50%";
		if (i==0) newCell.innerHTML="<a href=# onClick=blur();setScale("+i+") title=\"remove scale highlighting\">"+scaleNames[i]+"</a>";
		else newCell.innerHTML="<a href=# onClick=blur();setScale("+i+") title=\"Highlight a "+scaleNames[i]+" scale\">"+scaleNames[i]+"</a>";
	}     
    
    for (i=0;i<12;i++){
		position=i*7;
		while (position>12)position-=12;
		var newCell=rows[i].insertCell(3);
		newCell.width="10%";
		newCell.innerHTML="<a href=# onClick=blur();setChordOffset("+position+"); title=\"Change the key of the highlighted chord to "+notes[0][position]+"\n(chordnotes are shown in red)\">"+notes[0][position]+"</a>";
	}

	for (i=0;i<chords.length;i++){
		if (i>=12 && i<chords.length) {
			var newCell=rows[i].insertCell(3);         	              
        }
		if (i>=chords.length) {
			rows[i]=document.getElementById("myKeys").insertRow(i);
			var newCell=rows[i].insertCell(0);
			var newCell=rows[i].insertCell(1);
			var newCell=rows[i].insertCell(2);
			var newCell=rows[i].insertCell(3);
		}
		var newCell=rows[i].insertCell(4);
		newCell.width="20%";
		if (i==0)newCell.innerHTML="<a href=# onClick=blur();setChord("+i+") title=\"Remove chord highlighting\">"+chordNames[i]+"</a>";
		else newCell.innerHTML="<a href=# onClick=blur();setChord("+i+") title=\"Highlight a "+chordNames[i]+" chord\n(chordnotes are shown in red)\">"+chordNames[i]+"</a>";
	}     

	
	position=offset*7;
	while (position>12)position-=12;
	setBG(position,0);
	position=scaleOffset*7;
	while (position>12)position-=12;
	setBG(position,1);
	position=chordOffset*7;
	while (position>12)position-=12;
	setBG(position,3);
	setBG(scale,2);
	setBG(chord,4);
}
