function getXMLCode (url, textOnly) {
  try {    
  	// Firefox, Opera 8.0+, Safari    
	xmlHttp=new XMLHttpRequest(); 
  } catch (e) {    
	  	// Internet Explorer
		try {      
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");     
		} catch (e) {      
		  	try	{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {       
			 return false;        
			}      
		}   
	}  
	xmlHttp.onreadystatechange=function() {
   		if(xmlHttp.readyState==4)
       	{
			if (!textOnly) {
				xmlCode = xmlHttp.responseXML.documentElement;
			}
       		xmlText = xmlHttp.responseText;
			return false;
	   	}
	}
   	xmlHttp.open("GET", url, true);
   	xmlHttp.send(null); 
}

function addOption(selectObject, optionText, optionValue) {
    var optionObject = new Option(optionText, optionValue);
    var optionRank = selectObject.options.length;
    selectObject.options[optionRank]=optionObject;
}
function deleteOption(selectObject, optionRank) {
    if (selectObject.options.length != 0) { 
		selectObject.options[optionRank]=null;
	}
}

function closeOffBoxes() {
	formCode += optionsCode;
	formCode += '</form>\n';
	var locationListHTML = '<h2>Filter Bowling Alleys</h2>\n';
	locationListHTML += '<p>Filter bowling alleys using the form below.  <strong>The alley list below will change</strong> as you select various options from the fields below:</p>\n<p>';
	locationListHTML += formCode;
	locationListHTML +=  '</p>\n';
	locationList.innerHTML =  locationListHTML;
	theForm = document.getElementById("AlleySelectForm");
	locationList.style.height = "8em";
	locationList.style.overflow = "hidden";
	if (postURL == "country") {
		theForm.Province_State.disabled = true;
		theForm.Province_State.style.display = "none";
		theForm.City.disabled = true;
		theForm.City.style.display = "none";
	} else if (postURL == "province/state") {
		theForm.City.disabled = true;
		theForm.City.style.display = "none";
	}
}
function createTitles(curPage) {
	var theTitle = '';
	theTitle = addToTitle (theTitle, City, true);
	theTitle = addToTitle (theTitle, Province_State, true);
	theTitle = addToTitle (theTitle, Country, true);
	if (fivePinFlag == 1) {
		theTitle = addToTitle (theTitle, "5-Pin", false);
	}
	if (tenPinFlag == 1) {
		theTitle = addToTitle (theTitle, "10-Pin", false);
	}
	if (duckPinFlag == 1) {
		theTitle = addToTitle (theTitle, "Duckpin", false);
	}
	if (candlePinFlag == 1) {
		theTitle = addToTitle (theTitle, "Candlepin", false);
	}
	theTitle = addToTitle (theTitle, "Bowling Alleys", false);
	if (curPage > 1) {
		theTitle += " Page " + curPage;
	}
	document.title = theTitle;
	document.getElementById("Page_Title").innerHTML = theTitle;
}
function displayListingPage(curPage) {
	createTitles(curPage);
	document.getElementById("bowling-alley-page").innerHTML = xmlText;
	xmlText = null;
	return true;
}
function createListingPage(curPage) {
	if (xmlText == null || xmlText == 'undefined') {
		var timer = setTimeout ("createListingPage(" + curPage + ")", 10);
	} else {
		displayListingPage(curPage);
	}
}
function retrieveListingPage() {
	xmlText = null;
	if (Country == undefined) { Country = ''; };
	if (Province_State == undefined) { Province_State = ''; };
	if (City == undefined) { City = ''; };
	url = pageRoot + "XML-pages/retrieve-alley-listings.asp?country=" + Country + "&province_state=" + Province_State + "&city=" + City + "&five_pin_flag=" + fivePinFlag + "&ten_pin_flag=" + tenPinFlag + "&duckpin_flag=" + duckPinFlag + "&candlepin_flag=" + candlePinFlag;
	getXMLCode (url, true);
	createListingPage(1);
}
function createProvinceStateBox() {
	if (xmlText != null && xmlText != 'undefined') {
		var provStateTags = xmlCode.getElementsByTagName("Province_State");
		addOption (theForm.Province_State, "-- Select Province/State --", "");
		for (var i = 0; i < provStateTags.length; i++) {
			addOption (theForm.Province_State, provStateTags[i].firstChild.data, "");
		}
		xmlText = null;
		readyFlag = true;
		retrieveListingPage();
		Country = theForm.Country.options[theForm.Country.selectedIndex].text;
		return true;
	} else { // xml Code wasn't retrieved in time.
		var tryAgain = setTimeout ("createProvinceStateBox()", 10)	
	}	
}
function createCityBox() {
	if (xmlText != null && xmlText != 'undefined') {
		var cityTags = xmlCode.getElementsByTagName("City");
		addOption (theForm.City, "-- Select City --", "");
		for (var i = 0; i < cityTags.length; i++) {
			addOption (theForm.City, cityTags[i].firstChild.data, "");
		}
		xmlText = null;
		readyFlag = true;
		retrieveListingPage();
		return true;
	} else { // xml Code wasn't retrieved in time.
		var tryAgain = setTimeout ("createCityBox()", 10)	
	}	
}
function showProvincesStates() {
	Country = theForm.Country.options[theForm.Country.selectedIndex].text;
	if (Country == undefined) { Country = ''; };
	while (theForm.Province_State.options.length > 0)
	{
		deleteOption (theForm.Province_State, theForm.Province_State.options.length - 1);
	} 
	while (theForm.City.options.length > 0)
	{
		deleteOption (theForm.City, theForm.City.options.length - 1);
	} 
	theForm.City.disabled = true;
	theForm.City.style.display = "none";
	Province_State = '';
	City = '';
	if (Country == '-- Select Country --') {
		theForm.Province_State.disabled = true;
		theForm.Province_State.style.display = "none";
		Country = '';
		retrieveListingPage();
	} else {
		xmlText = null;
		url = pageRoot + "XML-pages/create-province-list.asp?country=" + Country + "&five_pin_flag=" + fivePinFlag + "&ten_pin_flag=" + tenPinFlag + "&duckpin_flag=" + duckPinFlag + "&candlepin_flag=" + candlePinFlag;
		getXMLCode (url, false);
		var postFunction = setTimeout ("createProvinceStateBox()", 10);
		theForm.Province_State.disabled = false;
		theForm.Province_State.style.display = "inline";
	}
	xmlText = null;
}
function showCities() {
	if (Country == undefined) { Country = ''; };
	Province_State = theForm.Province_State.options[theForm.Province_State.selectedIndex].text;
	while (theForm.City.options.length > 0)
	{
		deleteOption (theForm.City, theForm.City.options.length - 1);
	} 
	City = '';
	if (Province_State == '-- Select Province/State --') {
		theForm.City.disabled = true;
		theForm.City.style.display = "none";
		Province_State = '';
		retrieveListingPage();
	} else {
		xmlText = null;
		url = pageRoot + "XML-pages/create-city-list.asp?country=" + Country + "&province_state=" + Province_State + "&five_pin_flag=" + fivePinFlag + "&ten_pin_flag=" + tenPinFlag + "&duckpin_flag=" + duckPinFlag + "&candlepin_flag=" + candlePinFlag;
		getXMLCode (url, false);
		var postFunction = setTimeout ("createCityBox()", 10);
		theForm.City.disabled = false;
		theForm.City.style.display = "inline";
	}
	xmlText = null;
}
function filterCityResults() {
	if (Country == undefined) { Country = ''; };
	if (Province_State == undefined) { Province_State = ''; };
	City = theForm.City.options[theForm.City.selectedIndex].text;
	if (City == '-- Select City --') {
		City = '';
	}
	url = pageRoot + "XML-pages/create-city-list.asp?country=" + Country + "&province_state=" + Province_State + "&city=" + City + "&five_pin_flag=" + fivePinFlag + "&ten_pin_flag=" + tenPinFlag + "&duckpin_flag=" + duckPinFlag + "&candlepin_flag=" + candlePinFlag;
	getXMLCode (url, false);
	var postFunction = setTimeout ("retrieveListingPage()", 10);
}
function createCountryBoxes() {
	if (xmlText != null && xmlText != 'undefined') {
		var countryTags = xmlCode.getElementsByTagName("Country");
		optionsCode = '<select name="Country" class="Javascript-select" onchange="showProvincesStates();">\n';
		optionsCode += '<option>-- Select Country --</option>\n';
		for (var i = 0; i < countryTags.length; i++) {
			optionsCode += '<option>' + countryTags[i].firstChild.data + '</option>\n';
		}
		optionsCode += '</select>\n';
		optionsCode += '<select name="Province_State" class="Javascript-select" onchange="showCities();">\n';
		optionsCode += '<option>Unavailable</option>\n';
		optionsCode += '</select>\n';
		optionsCode += '<select name="City" class="Javascript-select" onchange="filterCityResults();">\n';
		optionsCode += '<option>Unavailable</option>\n';
		optionsCode += '</select>\n';
		closeOffBoxes();
		xmlText = null;
		return true;
	} else { // xml Code wasn't retrieved in time.
		var tryAgain = setTimeout ("createCountryBoxes()", 10)	
	}	
}
function createProvinceBoxes() {
	if (xmlText != null && xmlText != 'undefined') {
		var provStateTags = xmlCode.getElementsByTagName("Province_State");
		optionsCode = '<select name="Province_State" class="Javascript-select" onchange="showCities();">\n';
		optionsCode += '<option>-- Select Province/State --</option>\n';
		for (var i = 0; i < provStateTags.length; i++) {
			optionsCode += '<option>' + provStateTags[i].firstChild.data + '</option>\n';
		}
		optionsCode += '</select>\n';
		optionsCode += '<select name="City" class="Javascript-select" onchange="filterCityResults();">\n';
		optionsCode += '<option>Unavailable</option>\n';
		optionsCode += '</select>\n';
		closeOffBoxes();
		xmlText = null;
		return true;
	} else { // xml Code wasn't retrieved in time.
		var tryAgain = setTimeout ("createProvinceBoxes()", 10)	
	}	
}
function createInitCityBox() {
	if (xmlText != null && xmlText != 'undefined') {
		var cityTags = xmlCode.getElementsByTagName("City");
		optionsCode = '<select name="City" class="Javascript-select" onchange="filterCityResults();">\n';
		optionsCode += '<option>-- Select City --</option>\n';
		for (var i = 0; i < cityTags.length; i++) {
			optionsCode += '<option>' + cityTags[i].firstChild.data + '</option>\n';
		}
		optionsCode += '</select>\n';
		closeOffBoxes();
		xmlText = null;
		return true;
	} else { // xml Code wasn't retrieved in time.
		var tryAgain = setTimeout ("createInitCityBox()", 10)	
	}	
}
function createFormBoxes(alleyCount) {
	formCode = '<form action=' + pageRoot + 'bowling-alleys.asp" method="post" id="AlleySelectForm">\n';
	if (alleyCount > 999) {
		url = pageRoot + "XML-pages/create-country-list.asp?five_pin_flag=" + fivePinFlag + "&ten_pin_flag=" + tenPinFlag + "&duckpin_flag=" + duckPinFlag + "&candlepin_flag=" + candlePinFlag;
		postURL = "country";
	} else if (alleyCount > 248) {
		url = pageRoot + "XML-pages/create-province-list.asp?five_pin_flag=" + fivePinFlag + "&ten_pin_flag=" + tenPinFlag + "&duckpin_flag=" + duckPinFlag + "&candlepin_flag=" + candlePinFlag;
		postURL = "province/state";
	} else {
		url = pageRoot + "XML-pages/create-city-list.asp?five_pin_flag=" + fivePinFlag + "&ten_pin_flag=" + tenPinFlag + "&duckpin_flag=" + duckPinFlag; + "&candlepin_flag=" + candlePinFlag
		postURL = "city"
	}
	if (url != '') {
		getXMLCode (url, false);
		if (postURL == "country") {
			createCountryBoxes();
		} else if (postURL == "province/state") {
			createProvinceBoxes();
		} else {
			createInitCityBox();
		}
	}
}
function addToTitle (theTitle, term, commaFlag) {
	if (term != '' && term != undefined && term != null) {
		if (theTitle != '') {
			if (commaFlag) {
				theTitle += ","
			}
			theTitle += " "
		}
		theTitle += term;
	}
	return theTitle;
}
function pageAlleys(curPage) {
	xmlText = null;
	if (Country == undefined) { Country = ''; };
	if (Province_State == undefined) { Province_State = ''; };
	if (City == undefined) { City = ''; };
	url = pageRoot + "XML-pages/retrieve-alley-listings.asp?country=" + Country + "&province_state=" + Province_State + "&city=" + City + "&five_pin_flag=" + fivePinFlag + "&ten_pin_flag=" + tenPinFlag + "&duckpin_flag=" + duckPinFlag + "&candlepin_flag=" + candlePinFlag + "&page=" + curPage;
	getXMLCode (url, true);
	createListingPage(curPage);
	return false;
}
var xmlHttp, xmlCode, xmlText, url, postURL, optionsCode, theForm, pageRoot, fivePinFlag, tenPinFlag, duckPinFlag, candlePinFlag;
var locationList = document.getElementById("alley-location-list");
var formCode, Country, Province_State, City, readyFlag;

