/* AJAX functions */


// Print a list of HTML checkboxes for a band's genres
function printBandGenres(band_id) {
	var gURL 		= '/ajax/printBandGenres.php?band_id=' + band_id;
	var element_id 	= 'bandGenres';
	ajaxUpdate(gURL, element_id); // Make XMLHttpRequest
}

// populate the state dropdown with states from the specified country
function updateStates(country_iso) {

	// disable city dropdown until a state is selected
	if (document.getElementById('city_id') && document.getElementById('city_id').type == 'select-one') {
		document.getElementById('city_id').selectedIndex = 0;
		document.getElementById('city_id').disabled = true;
	}

	var gURL 		= 'ajaxStateDropdown.php?country_iso=' + country_iso;
	var element_id 	= 'state_dropdown';
	ajaxUpdate(gURL, element_id); // Make XMLHttpRequest
	
}


// populate the city dropdown with cities from the specified state/country
function updateCities(country_iso, state_id) {

	var gURL 		= 'ajaxCityDropdown.php?country_iso=' + country_iso + '&state_id=' + state_id;
	var element_id 	= 'city_dropdown';
	ajaxUpdate(gURL, element_id); // Make XMLHttpRequest
	
}
 

// insert a new city to the city dropdown
function insertNewCity(city_name, state_id, country_iso) {

	var gURL 		= 'ajaxInsertCity.php?country_iso=' + country_iso + '&state_id=' + state_id + '&city_name=' + city_name;
	var element_id 	= 'city_dropdown';
	ajaxUpdate(gURL, element_id); // Make XMLHttpRequest
	
}


// This function will handle all of the remote XMLHttpRequest calls and update any DHTML
// gURL 		= remote file to call
// element_id 	= name of the element id that will be updated with the response from gURL
// NOTE: BUG WORKAROUND.  We have to have duplicate functions here, because of an AJAX bug where using the same xmlhttp object
// for multiple simultaneous calls would result in odd behavior.  If one call finished before the other, its responseText
// would be overwritten by the call which finished last.  So there are duplicate functions below, but with their
// XMLHttpRequest named xmlhttpN instead of xmlhttp
function ajaxUpdate(gURL, element_id) {

	//create the Cross-browser XMLHttpRequest object
	if (window.XMLHttpRequest) { //################################# Code for Mozilla, Safari, etc 
		xmlhttp=new XMLHttpRequest();
		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType('text/xml');
		}
		xmlhttp.open("GET", gURL, true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) { 
				if (xmlhttp.status==200) { 
					document.getElementById(element_id).innerHTML=xmlhttp.responseText;
					if (xmlhttp.responseText.indexOf('disabled')<=0) {
						document.getElementById(element_id).focus();
					}	
				}
			}
		};
		xmlhttp.send(null);
	} else if (window.ActiveXObject) { //################################# Code for IE 
		xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
		if (xmlhttp) {
			xmlhttp.open('GET', gURL, false);
			xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) { 
					if (xmlhttp.status==200) { 
						document.getElementById(element_id).innerHTML=xmlhttp.responseText;
						if (xmlhttp.responseText.indexOf('disabled')<=0) {
							document.getElementById(element_id).focus();
						}	
					}
				}
			};
			xmlhttp.send();
		}
	}
}

// This function will handle all of the remote XMLHttpRequest calls and update any DHTML
// gURL 		= remote file to call
// element_id 	= name of the element id that will be updated with the response from gURL
// NOTE: BUG WORKAROUND.  We have to have duplicate functions here, because of an AJAX bug where using the same xmlhttp object
// for multiple simultaneous calls would result in odd behavior.  If one call finished before the other, its responseText
// would be overwritten by the call which finished last.  So there are duplicate functions below, but with their
// XMLHttpRequest named xmlhttpN instead of xmlhttp
function ajaxUpdate2(gURL, element_id) {

	//create the Cross-browser XMLHttpRequest object
	if (window.XMLHttpRequest) { //################################# Code for Mozilla, Safari, etc 
		xmlhttp2=new XMLHttpRequest();
		if (xmlhttp2.overrideMimeType) {
			xmlhttp2.overrideMimeType('text/xml');
		}
		xmlhttp2.open("GET", gURL, true);
		xmlhttp2.onreadystatechange=function() {
			if (xmlhttp2.readyState==4) { 
				if (xmlhttp2.status==200) { 
					document.getElementById(element_id).innerHTML=xmlhttp2.responseText;
					if (xmlhttp2.responseText.indexOf('disabled')<=0) {
						document.getElementById(element_id).focus();
					}	
				}
			}
		};
		xmlhttp2.send(null);
	} else if (window.ActiveXObject) { //################################# Code for IE 
		xmlhttp2=new ActiveXObject('Microsoft.XMLHTTP'); 
		if (xmlhttp2) {
			xmlhttp2.open('GET', gURL, false);
			xmlhttp2.onreadystatechange=function() {
				if (xmlhttp2.readyState==4) { 
					if (xmlhttp2.status==200) { 
						document.getElementById(element_id).innerHTML=xmlhttp2.responseText;
						if (xmlhttp2.responseText.indexOf('disabled')<=0) {
							document.getElementById(element_id).focus();
						}	
					}
				}
			};
			xmlhttp2.send();
		}
	}
}