// validates email address
//function checkEmail(email) {
//	//if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/(email)) {
//	var regexp = /^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$/
//	if (regexp.test(email)) {
//		return true;
//	} else {
//		return false;
//	}
//} 


// This function opens links in an external window in a standards compliant way.
// To use it, code the link like this:
// 		<a href="document.html" onclick="setTargetBlank(this);">external link</a>
function setTargetBlank(thelink) {
	thelink.target = "_blank";
}


// hide a given HTML element
function hideElement(id) {
	var element = document.getElementById(id);
	element.style.display = 'none';
}


// show a given HTML element
function showElement(id) {
	var element = document.getElementById(id);
	element.style.display = '';
}


// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioName) {

	var radioObj = document.getElementsByName(radioName);

	if (!radioObj) {
		return '';
	}
	var radioLength = radioObj.length;
	if (radioLength == undefined) {
		if(radioObj.checked) {
			return radioObj.value;
		} else {
			return '';
		}
	}
	for (var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return '';
}


// Validate if a required radio selection has been made or not.
function radioIsChecked(radio_element) {
	// In the case where a user has access only to one band, form.catbandid.length will return UNDEFINED
	// because HTML forms require at least TWO radio buttons.  This simple check will return true under that
	// condition, because if there is only one band to choose from, it is automatically checked by default, so
	// we always return true to escape the band check.
	if(!radio_element.length) {
		return true;
	}
	for (i = 0; i < radio_element.length; i++) {
	   	if (radio_element[i].checked) {
	    	return true;
	    }
	}
   	return false;
}


// confirm before submitting a form
function confirmSubmit(msg) {
	if (typeof msg == 'undefined') {
		msg = 'Are you sure you wish to delete this?' + "\n" + 'This cannot be undone!!';
	}
	var agree = confirm(msg);
	if (agree) {
		return true;
	} else {
		return false;
	}
}


// Check all songs in a setlist
function checkAllSongBoxes(){
	var songs_are_checked	= document.getElementById('checkall_songs');
	var checkbox_elements 		= document.getElementsByName('song_array[]');

	to_be_checked = toggleCheckTracker(songs_are_checked);
	toggleChecks(checkbox_elements, to_be_checked);
}


// Check all genres
function checkAllGenres(){
	var genres_are_checked	= document.getElementById('checkall_genres');
	var checkbox_elements 	= document.getElementsByName('genres[]');

	to_be_checked = toggleCheckTracker(genres_are_checked);
	toggleChecks(checkbox_elements, to_be_checked);
}


// loop through all checkboxes and toggle their checked status
function toggleChecks(element, checked) {
	for (var i=0; i < element.length; i++){
		element[i].checked = checked;
	}
}


// sets the tracker element so we know if all checks are currently ON or OFF
function toggleCheckTracker(element) {
	if (element.value == 'false'){
		to_be_checked = 1
		element.value = 'true';
	} else {
		to_be_checked = 0
		element.value = 'false';
	}
	return to_be_checked;
}
