/*Javascript On-Page Checking scripts*/

function artistpiececheck() { //Checks BOTH the artist and the piece is selected; only 1 field
	var tabform = document.getElementById('tabform');
	if (tabform.artistpiece.selectedIndex == -1 || tabform.artistpiece.selectedIndex == 0) {
		document.getElementById('artistpiececheck').innerHTML = "<img src='" + window.path + "/img/x.png' alt='Error!' /> Select an artist and song!"; //No artist/piece
		return true;
	} else {
		document.getElementById('artistpiececheck').innerHTML = "<img src='" + window.path + "/img/ok.png' alt='OK!' />";
		return false;
	}
}

//artistcheck and piececheck use AJAX; see ajax.php
function artistcheck() { //Parameter tells it to run the other check too
	var tabform = document.getElementById('tabform');
	if (tabform.artist.value.length == 0) {
		document.getElementById('artistcheck').innerHTML = "<img src='" + window.path + "/img/x.png' alt='Error!' /> Enter an artist!"; //No artist
		return true;
	} else {
		if (window.XMLHttpRequest) { // Mozilla, Safari, etc
			http_request = new XMLHttpRequest();
		} else if (window.ActiveXObject) { //IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
				} catch (e) {return true;} 
			}
		}
		http_request.onreadystatechange = function() { 
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {
					if (http_request.responseText == 'indb') { //If is
						document.getElementById('artistcheck').innerHTML = "<img src='" + window.path + "/img/ok.png' alt='OK!' /> <strong>" + tabform.artist.value + "</strong> found.";
					} else if (http_request.responseText == 'notindb') { //If artist isn't in database; possible typo
						document.getElementById('artistcheck').innerHTML = "<img src='" + window.path + "/img/ok.png' alt='OK!' /> The artist <strong>" + tabform.artist.value + "</strong> doesn't seem to be in our database. This may be just us, but <strong>check your spelling!</strong>"; //Adds text
					} else {
						document.getElementById('artistcheck').innerHTML = "<img src='" + window.path + "/img/ok.png' alt='OK!' />";
					}
				}
			}
		};
		http_request.open('GET', window.path + "/ajax.php?artist=" + tabform.artist.value, true);
		http_request.send(null);
	}
	setTimeout("piececheck();", 1500);
}

function piececheck() {
	var tabform = document.getElementById('tabform');
	if (tabform.piece.value.length == 0) { //Empty piece field
		document.getElementById('piececheck').innerHTML = "<img src='" + window.path + "/img/x.png' alt='Error!' /> Enter a piece!";
		return true;
	} else { //Piece entered
		if (window.XMLHttpRequest) { // Mozilla, Safari, etc
			http_request = new XMLHttpRequest();
		} else if (window.ActiveXObject) { //IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
				} catch (e) {} 
			}
		}
		http_request.onreadystatechange = function() { 
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {
					if (http_request.responseText == 'indb') { //If artist isn't in database; possible typo
						document.getElementById('piececheck').innerHTML = "<img src='" + window.path + "/img/ok.png' alt='OK!' /> <strong>\"" + tabform.piece.value + "\"</strong> by \"" + tabform.artist.value + "\" was found in the database!";
					} else if (http_request.responseText == 'notindb') { //If is
						document.getElementById('piececheck').innerHTML = "<img src='" + window.path + "/img/ok.png' alt='OK!' /> <strong>\"" + tabform.piece.value + "\"</strong> by \"" + tabform.artist.value + "\" was not found. It may be just us, but <strong>Check your spelling!</strong>";
					} else {
						document.getElementById('piececheck').innerHTML = "<img src='" + window.path + "/img/ok.png' alt='OK!' />";
					}
				}
			}
		};
		http_request.open('GET', window.path + "/ajax.php?musician=" + tabform.artist.value + "&piece=" + tabform.piece.value, true);
		http_request.send(null);
	}
}

function instrumentcheck() { //Shows/hides the instrument tip boxes; has no real effect on submission.
	var tabform = document.getElementById('tabform');
	if (tabform.instrument.selectedIndex == 1 || tabform.instrument.selectedIndex == 2) { //If guitar or bass; displays the guitar and bass tipboxes
		document.getElementById('guitar1').style.display = '';
		document.getElementById('guitar2').style.display = '';
		document.getElementById('drums1').style.display = 'none';
		document.getElementById('drums2').style.display = 'none';
		document.getElementById('drums3').style.display = 'none';
		document.getElementById('tablature1').style.display = '';
		document.getElementById('tablature2').style.display = '';
		document.getElementById('submit1').style.display = '';
	} else if (tabform.instrument.selectedIndex == 3) {
		document.getElementById('guitar1').style.display = 'none';
		document.getElementById('guitar2').style.display = 'none';
		document.getElementById('drums1').style.display = '';
		document.getElementById('drums2').style.display = '';
		document.getElementById('drums3').style.display = '';
		document.getElementById('tablature1').style.display = '';
		document.getElementById('tablature2').style.display = '';
		document.getElementById('submit1').style.display = '';
	} else {
		document.getElementById('guitar1').style.display = 'none';
		document.getElementById('guitar2').style.display = 'none';
		document.getElementById('drums1').style.display = 'none';
		document.getElementById('drums2').style.display = 'none';
		document.getElementById('drums3').style.display = 'none';
		document.getElementById('tablature1').style.display = 'none';
		document.getElementById('tablature2').style.display = 'none';
		document.getElementById('submit1').style.display = 'none';
	}
}

function tabcheck() {
	var tabform = document.getElementById('tabform');
	if (tabform.tabuse[0].checked) { //Using file upload
		if (tabform.tabfile.value.length == 0) { //No file being uploaded
			document.getElementById('tabcheck').innerHTML = "<img src='" + window.path + "/img/x.png' alt='Error!' /> You have chosen to upload a text file. Please choose a file to upload!";
			return false;
		} else {
			var fileext = tabform.tabfile.value.split('.');
			if (fileext.pop() == 'txt') { //Good extension (.txt) check
				document.getElementById('tabcheck').innerHTML =  "<img src='" + window.path + "/img/ok.png' alt='OK!' /> You'll be able to see how your tablature looks on the next page.";
				return true;
			} else {
				document.getElementById('tabcheck').innerHTML =  "<img src='" + window.path + "/img/x.png' alt='Error!' /> Your file must be a .txt (ASCII plaintext) file.";
				return false;
			}
		}
	} else if (tabform.tabuse[1].checked) {
		if (tabform.tabtextarea.value.length === 0) { //Length of tab check
			document.getElementById('tabcheck').innerHTML = "<img src='" + window.path + "/img/x.png' alt='Error!' /> You have chosen to enter a tablature; enter one!";
			return false;
		} else {
			document.getElementById('tabcheck').innerHTML = "<img src='" + window.path + "/img/ok.png' alt='OK!' /> You'll be able to see how your tablature looks on the next page.";
			return true;
		}
	} else { //Neither of the radios are selected
		document.getElementById('tabcheck').innerHTML = "<img src='" + window.path + "/img/x.png' alt='Error!' /> You must choose to either to upload a file or to enter the tablature into the text box.";
		return false;
	}
}