function validateCheckboxes (checkboxes) {
	var i;
	for (i = 0; i < checkboxes.length; i++) {
		if (checkboxes[i].checked) {
			return true;
		}
	}
	return false;
}

function validateRadioButtons (buttons) {
	var i;
	for (i=0; i < buttons.length; i++) {
		if (buttons[i].checked) {
			return true;
		}
	}
	return false;
}

function getHTTPObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Mozilla, Safari ...
	} 
	else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} 
	else {
		alert("Your browser doesn't support the XmlHttpRequest object.");
		return null;
	}
}

//We don't want to delete the security image if it has been submitted.  To tell if the submit button has been pressed or if it has navigated from the page I use this function.  If itsGood is true, then it will not destroy the security image on the client.  Then I can verify it again on the server side, and they can't bypass this page.
var itsGood = new Boolean(false);
function validateForm() {
	itsGood = true;
	var returnString = "";
	if (document.uploadForm.uploadedPDFFile.value == "") {
		returnString = returnString + "Please choose a FILE LOCATION\n";
		itsGood = false;
	}
	if (document.uploadForm.songName.value == "") {
		returnString = returnString + "You must choose a SONG NAME\n";
		itsGood = false;
	}
	if (document.uploadForm.author.value == "") {
		returnString = returnString + "You must enter an AUTHOR\n";
		itsGood = false;
	}
	var email = document.uploadForm.email.value;
	if (email == "") {
		returnString = returnString + "You must enter an EMAIL ADDRESS\n";
		itsGood = false;
	} else {
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(email)) {
			returnString += "You must enter a valid EMAIL ADDRESS\n";
			itsGood = false;
		}
	}
	var genreBoxes = document.getElementsByName("genre[]");
	if (!validateCheckboxes(genreBoxes)) {
		returnString = returnString + "Please select a GENRE\n";
		itsGood = false;
	}
	if (genreBoxes[genreBoxes.length-1].checked && document.uploadForm.genreOther.value == "") {
		returnString = returnString + "Please describe the OTHER GENRE you selected\n";
		itsGood = false;
	}
	var topicBoxes = document.getElementsByName("topic[]");
	if (!validateCheckboxes(topicBoxes)) {
		returnString = returnString + "Please select a TOPIC\n";
		itsGood = false;
	}
	if (topicBoxes[topicBoxes.length-1].checked && document.uploadForm.topicOther.value == "") {
		returnString = returnString + "Please describe the OTHER TOPIC you selected\n";
		itsGood = false;
	}
	if (!validateRadioButtons(document.getElementsByName("mainDifficulty"))) {
		returnString += "Please select a MAIN PART DIFFICULTY\n";
		itsGood = false;
	}
	if (!validateRadioButtons(document.getElementsByName("accompanimentDifficulty"))) {
		returnString += "Please select an ACCOMPANIMENT DIFFICULTY (or NA if it is not applicable)\n";
		itsGood = false;
	}
	if (!document.getElementsByName("userAgreementChecked")[0].checked) {
		returnString += "You must check and abide by the USER AGREEMENT\n";
		itsGood = false;
	}
	if (document.uploadForm.captchaInput.value == "") {
		returnString += "You must type the letters on the SECURITY IMAGE\n";
		itsGood = false;
	}
	else if (!captchaMatches(document.uploadForm.captchaInput.value)) {
		returnString += "The letters you typed does not match the SECURITY IMAGE.  Please try again.\n";
		itsGood = false;
	}
	if (!itsGood) {
		alert(returnString);
	}
	return itsGood;
}

function unload() {
	if (itsGood == false) {
		destroyCaptcha();
	}
}
