<!--Hide from old browsers
// place your JavaScript code here (// are JavaScript comments, ! is HTML comment

// Copyright © 2007 Ronald Pulcer (Ron Pulcer Music)

// This Javascript file handles AJAX functionality for MTWS (Music Theory Web Service)

// NOTE: This AJAX Javascript file is dependent upon the presence of these other Javascript files:
// mtws_ajax_common.js
// mtws_generic_fretboard.js

var xmlHttp;
var currentAction='';
var prevPianoNum;


function ajaxFunction(action) {

   // alert('Got here: ajaxFunction():' + action);
   currentAction = action;
	var url2run='';					// Specify URL along with handler function for xmlHttp.onreadystatechange
	var ajaxMethod="GET";			// Can be changed to POST later on, if needed

	document.getElementById('ajaxStatus').innerHTML='';

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) { return; }

	// Here are the possible values for the readyState propery:

	// 0 The request is not initialized 
	// 1 The request has been set up 
	// 2 The request has been sent 
	// 3 The request is in process 
	// 4 The request is complete 

	// One option is to specify an anonymous internal function

	//	xmlHttp.onreadystatechange=function() {
	//		if(xmlHttp.readyState==4) {
	//			document.myForm.time.value+=xmlHttp.responseText;
	//		}
	//	}

	// Another option is to specify an external function.
	// Based on different actions specified, you can assign different handler functions
	// if(document.myForm.action.value=='testAjax') {

	var outputFmt = '';
	if(action.indexOf('_xml')>=0) {
		outputFmt = 'xml';
		xmlHttp.onreadystatechange=UCNotesGuitarXmlStateChanged;
	}
	else {
		outputFmt = 'txt';
		xmlHttp.onreadystatechange=UCNotesGuitarTxtStateChanged;
	}

	var pianoOffset = getPianoOffsetNum();		// alert("pianoOffset = " + pianoOffset);
	var lowNote     = document.getElementById('note_range_low').value;
	var highNote    = document.getElementById('note_range_high').value;
	lowNote         =  Number(lowNote)  + pianoOffset;
	highNote        =  Number(highNote) + pianoOffset;
	// var noteOccur = document.getElementById('note_occur').value;
	// var occ;
	// if(noteOccur=='lowpos')       { occ = 'low'; }
	// else if(noteOccur=='highpos') { occ = 'high'; }
	// else { occ = noteOccur; }

	var occ = 'all';
   // alert('occ = ' + occ);

	if(action=='demo_uc_notes_1_guitar_txt' || action=='demo_uc_notes_1_guitar_xml') {

// http://caught.msie.marlboro.edu:8080/mtws_servlet/mtws.do?
// rb_note_set=natural&note_range_low=27&note_range_high=39&output_fmt=txt&action=note_getSet&submit=View+Notes

		var noteSet = document.getElementsByName('rb_note_set');
		var setName = '';
		for(var i=0; i<5; i++) { 
			if(noteSet[i].checked) { setName = noteSet[i].value; }
		}

		if(setName!='') {

			var queryString = "action=note_getSet&rb_note_set=" + setName + 
				"&note_range_low=" + lowNote + "&note_range_high=" + highNote + "&note_occur=" + occ +
				"&output_fmt=" + outputFmt;
			//alert('queryString = ' + queryString);

			// "http://zonorus.marlboro.edu/~rpulcer/capstone/mtws/design/cgi-bin/ajax_request_2servlet.cgi?"
			url2run = getServerURL() + queryString;
		}
		else {
			alert("Please select a Note Type.");
			return;
		}
	}
	else if(action=='demo_uc_notes_2_guitar_txt' || action=='demo_uc_notes_2_guitar_xml') {

// http://caught.msie.marlboro.edu:8080/mtws_servlet/mtws.do?
// chromatic_note=6&note_range_low=27&note_range_high=39&output_fmt=txt&action=note_get&submit=View+Note

		var noteNum = document.getElementById('chromatic_note').value;

		if(noteNum!='') {

			var queryString = "action=note_get&chromatic_note=" + noteNum + 
				"&note_range_low=" + lowNote + "&note_range_high=" + highNote + "&note_occur=" + occ +
				"&output_fmt=" + outputFmt;
			//alert('queryString = ' + queryString);

			url2run = getServerURL() + queryString;
		}
		else {
			alert("Please select a Note.");
			return;
		}
	}
	else if(action=='demo_uc_intervals_2_guitar_txt' || action=='demo_uc_intervals_2_guitar_xml') {

// http://caught.msie.marlboro.edu:8080/mtws_servlet/mtws.do?
// interval=6&chromatic_note=0&interval_direction=up&note_range_low=27&note_range_high=51&note_occur=low&output_fmt=txt&action=interval_getNote&submit=View+Notes

		var intervalNum = getInterval();
		var intervalDir = getIntervalDirection();
		var noteNum     = document.getElementById('chromatic_note').value;
		
		if(intervalNum!='' && noteNum!='' && intervalDir!='') {

			var queryString = "action=interval_getNote&chromatic_note=" + noteNum +
				"&interval=" + intervalNum + "&interval_direction=" + intervalDir  +
				"&note_range_low=" + lowNote + "&note_range_high=" + highNote + "&note_occur=" + occ +
				"&output_fmt=" + outputFmt;
			//alert('queryString = ' + queryString);

			url2run = getServerURL() + queryString;
		}
		else {
			alert("Please select Note, Interval and Direction.");
			return;
		}
	}
	else if(action=='demo_uc_scales_2_guitar_txt' || action=='demo_uc_scales_2_guitar_xml') {

// http://caught.msie.marlboro.edu:8080/mtws_servlet/mtws.do?
// root_note=2&scale_type=harmin&note_range_low=27&note_range_high=51&note_occur=low&output_fmt=xml&action=scale_getNote&submit=View+Notes

		var noteNum     = document.getElementById('root_note').value;
		var scaleType   = document.getElementById('scale_type').value;
		
		if(noteNum!='' && scaleType!='') {

			var textObj = document.getElementById("interval_seq");
			if(textObj!=null) { textObj.value = display_interval_seq(scaleType); }

			var queryString = "action=scale_getNote&root_note=" + noteNum + "&scale_type=" + scaleType +
				"&note_range_low=" + lowNote + "&note_range_high=" + highNote + "&note_occur=" + occ +
				"&output_fmt=" + outputFmt;
			//alert('queryString = ' + queryString);

			url2run = getServerURL() + queryString;
		}
		else {
			alert("Please select Root Note and Scale Type.");
			return;
		}
	}
	else if(action=='demo_uc_chords_2_guitar_txt' || action=='demo_uc_chords_2_guitar_xml') {

// http://caught.msie.marlboro.edu:8080/mtws_servlet/mtws.do?
// root_note=1&chord_type=maj7&note_range_low=27&note_range_high=51&note_occur=low&output_fmt=txt&action=chord_getNote&submit=View+Notes

		var noteNum     = document.getElementById('root_note').value;
		var chordType   = document.getElementById('chord_type').value;
		
		if(noteNum!='' && chordType!='') {

			var queryString = "action=chord_getNote&root_note=" + noteNum + "&chord_type=" + chordType +
				"&note_range_low=" + lowNote + "&note_range_high=" + highNote + "&note_occur=" + occ +
				"&output_fmt=" + outputFmt;
			//alert('queryString = ' + queryString);

			url2run = getServerURL() + queryString;
		}
		else {
			alert("Please select Root Note and Chord Type.");
			return;
		}
	}
	else {
		alert("Invalid action: " + action);
		return;
	}

	// alert("URL: " + url2run);
	xmlHttp.open(ajaxMethod, url2run, true);
	xmlHttp.send(null);
}


function UCNotesGuitarTxtStateChanged() {

	if(xmlHttp.readyState==4) {

		divObj = document.getElementById('div_xml');
		if(divObj!=null) {
			divObj.innerHTML = 
				'<textarea cols="100" rows="5">\n<!--\n' + xmlHttp.responseText + '\n-->\n</textarea>\n';
		}

		var noteSpanObj = document.getElementById('span_note_name_freq');

		// Note: These functions are in external js files
		if(isAutoClear()) {
			resetFretboard();
			if(noteSpanObj!= null) { noteSpanObj.innerHTML = ''; }
		}

// Sample output from MTWS Servlet
//0,A,220.0,36,a
//2,B,246.94,38,b
//3,C,130.81,27,c
//5,D,146.83,29,d
//7,E,164.81,31,e
//8,F,174.61,32,f
//10,G,196.0,34,g

		var pianoOffset = getPianoOffsetNum();		// alert("pianoOffset = " + pianoOffset);
		var lowNote     = document.getElementById('note_range_low').value;
		var highNote    = document.getElementById('note_range_high').value;
		lowNote  = Number(lowNote);
		highNote = Number(highNote);

		var resultRow = new Array();
		resultRow = xmlHttp.responseText.split('\n');
		var noteTableRow = '';

		for(var r=0; r<resultRow.length; r++) {

			if(resultRow[r]!='') {

				var noteInfo = new Array();
				noteInfo = resultRow[r].split(',');

				if(noteInfo.length==5) {
					noteInfo[3]= Number(noteInfo[3]);
					var instrumentNum = noteInfo[3] - pianoOffset;
					var midiNum   = getMIDINum(noteInfo[3]);
					var midiFile  = getMIDIFileName(midiNum);

					noteDisplay(noteInfo[3], instrumentNum, lowNote, highNote);

					noteTableRow += 
						'<tr>\n<td align="right">' + noteInfo[0] + '</td>\n<td>' + noteInfo[1] + 
						'</td>\n<td align="right">' + noteInfo[2] + 
						'</td>\n<td align="right">' + noteInfo[3] + '</td>\n<td>' + noteInfo[4] + 
						'</td>\n<td align="right"><embed id="embed_note_'  + midiNum + 
						'" src="' + midiFile + '" volume="100" autostart="false" />' +
						'</td>\n<td align="right"><input type="button" value="Play" onclick="playnote(\'embed_note_' + 
						midiNum + '\')" />' + '</td>\n</tr>\n';
//alert(noteTableRow);
				}
			}
		}

		if(noteSpanObj!= null) {
			noteSpanObj.innerHTML += 
				'<table border="1">\n<tr>\n<th>Base<br />Octave #</th>\n<th>Base<br />Octave Name</th>\n' + 
				'<th>Frequency<br />(Hz = Hertz)</th>\n' + 
				'<th>Piano<br />Key #</th>\n<th>Piano<br />Key Name</th>\n' + 
				'<th>Audio</th>\n<th>Play<br />Note</th>\n</tr>\n' + 
				noteTableRow + '</table><br />\n';
		}
	}

	// Update status for all status values
	updateAjaxStatus();
}


function UCNotesGuitarXmlStateChanged() {

	if(xmlHttp.readyState==4) {

		divObj = document.getElementById('div_xml');
		if(divObj!=null) {
			divObj.innerHTML = 
				'<textarea cols="100" rows="5">\n<!--\n' + xmlHttp.responseText + '\n-->\n</textarea>\n';
		}

		var resultXML = loadXMLFromText(xmlHttp.responseText);

		var noteSpanObj = document.getElementById('span_note_name_freq');

		// Note: These functions are in external js files
		if(isAutoClear()) {
			resetFretboard();
			if(noteSpanObj!= null) { noteSpanObj.innerHTML = ''; }
		}

		var pianoOffset = getPianoOffsetNum();		// alert("pianoOffset = " + pianoOffset);
		var lowNote     = document.getElementById('note_range_low').value;
		var highNote    = document.getElementById('note_range_high').value;
		lowNote  = Number(lowNote);
		highNote = Number(highNote);

		var noteTableRow = '';

		for(var r=0; r<resultXML.getElementsByTagName("note_number").length; r++) {

			var noteNum   = resultXML.getElementsByTagName("note_number")[r].childNodes[0].nodeValue;
			var noteName  = resultXML.getElementsByTagName("note_name")[r].childNodes[0].nodeValue;
			var noteFreq  = resultXML.getElementsByTagName("frequency")[r].childNodes[0].nodeValue;
			var pianoNum  = resultXML.getElementsByTagName("piano_number")[r].childNodes[0].nodeValue;
			var pianoName = resultXML.getElementsByTagName("piano_name")[r].childNodes[0].nodeValue;

			pianoNum      = Number(pianoNum);
			var instrumentNum = pianoNum - pianoOffset;
			var midiNum   = getMIDINum(pianoNum);
			var midiFile  = getMIDIFileName(midiNum);

			noteDisplay(pianoNum, instrumentNum, lowNote, highNote);

// hidden="true"
			noteTableRow += 
				'<tr>\n<td align="right">' + noteNum  + '</td>\n<td>' + noteName + 
				'</td>\n<td align="right">' + noteFreq + 
				'</td>\n<td align="right">' + pianoNum + '</td>\n<td>' + pianoName + 
				'</td>\n<td align="right"><embed id="embed_note_'  + midiNum + 
				'" src="' + midiFile + '" volume="100" autostart="false" />' +
				'</td>\n<td align="right"><input type="button" value="Play" onclick="playnote(\'embed_note_' + 
				midiNum + '\')" />' + '</td>\n</tr>\n';
//alert(noteTableRow);
		}

		if(noteSpanObj!= null) {
			noteSpanObj.innerHTML += 
				'<table border="1">\n<tr>\n<th>Base<br />Octave #</th>\n<th>Base<br />Octave Name</th>\n' + 
				'<th>Frequency<br />(Hz = Hertz)</th>\n' + 
				'<th>Piano<br />Key #</th>\n<th>Piano<br />Key Name</th>\n' + 
				'<th>Audio</th>\n<th>Play<br />Note</th>\n</tr>\n' + 
				noteTableRow + '</table><br />\n';
		}
	}

	// Update status for all status values
	updateAjaxStatus();
}


function noteDisplay(pianoNum, instrumentNum, lowNote, highNote) {

	var noteName = "";

	// Handle exceptions for multi-colored note displays, before toggling the note
	if(currentAction.indexOf('demo_uc_intervals_2_guitar')==0) {

		// Handle exception for Unison or Octave intervals
		// (so that previous notes do not get toggled OFF).
		var intervalNum = getInterval();
		if( (intervalNum==0 || intervalNum==12) && prevPianoNum==pianoNum ) { return; }
		noteName = 'chromatic_note';
	}
	else if(currentAction.indexOf('demo_uc_scales_2_guitar')==0 ||
			  currentAction.indexOf('demo_uc_chords_2_guitar')==0) {
		noteName = 'root_note';
	}

	if(noteName!="") {

		var chromNoteNum  = document.getElementById(noteName).value;
		var baseOctaveNum = pianoNum % 12;
		var noteColor = document.getElementById('select_color_opt').value;

		if(baseOctaveNum!=chromNoteNum) {
			noteColor = document.getElementById('interval_color').value;
		}
		setSelectColorOpt(noteColor);
	}

	// Note: This function is external to this js file
	toggleNoteByNum(instrumentNum, lowNote, highNote);

	prevPianoNum = pianoNum;
}


// Copyright © 2007 Ronald Pulcer (Ron Pulcer Music)

//-->
