
function loadXMLDoc(url) {	
	req = false;
    // branch for native XMLHttpRequest object
	
	if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		switch (task) {
			case "logon":
//				alert ('logon task switch');
//				document.getElementById("LoginFavStatus").innerHTML = 'Validating User Credentials';
//				document.getElementById("LoginFavStatus").style.visibility='visible';
//				document.getElementById("lblSUsername").style.visibility='hidden';
				req.onreadystatechange = logonReqChange;
				break;
			case "category":
//				alert ('category task switch');
//				document.getElementById("dataStatus").innerHTML = 'Gathering the information from the Server';
//				document.getElementById("dataStatus").style.visibility='visible';
//				var newtext = document.createTextNode('Gathering the information from the Server');
//				var datab = document.getElementById("dataBox");
//				datab.appendChild(newtext);
				req.onreadystatechange = categoryReqChange;
				break;
			default:
				alert ('default task switch');
				req.onreadystatechange = processReqChange;
		}
		req.open("GET", url, true);
		req.send(null);
	}
}


function processReqChange() {
// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		switch (req.status)  {
			case 200:
				// ok, http success
//				alert("req.statusText = " + req.statusText);
				if (req.responseText) {
					if (req.responseText.indexOf("Error - ") == 0)  {
						document.getElementById('lblSUsername').innerHTML = req.responseText;
						document.getElementById('TxtUsername').focus;
						document.getElementById('TxtPassword').value = "";
						break;
					}
				}
				if (req.responseXML) {
//					alert ('req.responseXML.documentElement = ' + req.responseXML.documentElement);
//					alert ('req.responseText.documentElement = ' + req.responseText);
					xml = req.responseXML;
					i = 0;
					html = "<ol>\n";
					while (i >= 0) {
						t = xml.getElementsByTagName("name")[i];
						l = xml.getElementsByTagName("action")[i];
//						alert ("t = " + t);
//						alert ("l = " + l);
						if (t != null && l != null) {
							t = t.firstChild.data;
							l = l.firstChild.data;
//							alert ("loop t = " + t);
//							alert ("loop l = " + l);
							html += "<li><a href=\"" + l + "\">" + t + "</a></li>\n";
							i++;
						} else {
							i = -1;
						}
					}
					html = html + "</ol>";
//					alert("html = " + html);
					document.getElementById("LoginFavBox").innerHTML = html;
					hideLogonPrompts();
					rewritePageMenu();
				} else {
					alert("processReqChange() - Problem: " + req.statusText);
				}
				break;
			case 404:
				alert('Server process '+url + ' missing');
				break;
			default:
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}


function categoryReqChange() {
// only if req shows "complete"
	if (req.readyState == 4) {
		switch (req.status)  {
			case 200:
				// ok, http success
//				alert("req.statusText = " + req.statusText);
				if (req.responseXML) {
//					alert ('req.responseXML.documentElement = ' + req.responseXML.documentElement);
//					alert ('req.responseText.documentElement = ' + req.responseText);
					xml = req.responseXML;
					i = 0;
					html = '<h4>&nbsp;  Popular City & Coffee Shop Selection</h4>';
					html = html + "<h5>\n"; 
					html = html + '&nbsp; <input type="radio" name="categoryRadio" value=1 ';
					if (lastCategory == 1) {
						html = html + 'checked=1 '
					}
					html = html + 'onclick="validateCategory(1)">City';
/*					
					html = html + '&nbsp; <input type="radio" name="categoryRadio" value=2 '
					if (lastCategory == 2) {
						html = html + 'checked=1 '
					}
					html = html + 'onclick="validateCategory(2)">Beer\n';
					html = html + '&nbsp; <input type="radio" name="categoryRadio" value=3 '
					if (lastCategory == 3) {
						html = html + 'checked=1 '
					}
					html = html + 'onclick="validateCategory(3)">Red Wine\n';
					html = html + '&nbsp; <input type="radio" name="categoryRadio" value=4 '
					if (lastCategory == 4) {
						html = html + 'checked=1 '
					}
					html = html + 'onclick="validateCategory(4)">White Wine\n';
					html = html + '&nbsp; <input type="radio" name="categoryRadio" value=5 '
					if (lastCategory == 5) {
						html = html + 'checked=1 '
					}
					html = html + 'onclick="validateCategory(5)">Coffee\n';
	*/				
					html = html + '&nbsp; <input type="radio" name="categoryRadio" value=6 '
					if (lastCategory == 6) {
						html = html + 'checked=1 '
					}
					html = html + 'onclick="validateCategory(6)">Seattle Coffee Shops\n';
					html = html + '</h5>\n';
					html = html + '<ol>\n';
					while (i >= 0) {
						t = xml.getElementsByTagName("name")[i];
						l = xml.getElementsByTagName("action")[i];
//						alert ("t = " + t);
//						alert ("l = " + l);
						if (t != null && l != null) {
							t = t.firstChild.data;
							l = l.firstChild.data;
//							alert ("loop t = " + t);
//							alert ("loop l = " + l);
							if (lastCategory == 6) {
								html += "<li class=wide>";
							} else {
								html += "<li>";
							}
							html += "<a href=\"" + l + "\">" + t + "</a></li>\n";
							i++;
						} else {
							i = -1;
						}
					}
					html = html + "</ol>";
//					alert("html = " + html);
					document.getElementById("dynamicDataContent").innerHTML = html;
				} else {
					alert("categoryReqChange() - Problem: " + req.statusText + "\n req.responseText = "  + req.responseText + "\n req.readyState = " + req.readyState);
				}
				break;
			case 404:
				alert('Server process '+url + ' missing');
				break;
			default:
				alert("There was a problem retrieving the response from the server\n" + req.statusText);
//				alert ("req.responseText = "  + req.responseText);
		}
	} 
}


function validateSearch() {
	if ((document.searchForm.city.value == null) || (document.searchForm.city.value == "")) {
		document.searchForm.city.focus;
		return false;
	}
	if ((document.searchForm.zip.value == null) || (document.searchForm.zip.value == "")) {
		document.searchForm.zip.focus;
		return false;
	}
	
	// Input mode
	url = 'vSearch.php?city=' + escape(document.searchForm.city.value);
	url = url + "&zip=" + escape(document.searchForm.zip.value);
	
}


function validateCategory(cat)  {
	task = 'category';
	lastCategory = cat;
	switch (cat) {
		case 1: {
			url = 'include/xml/city.xml';
			break;
		}
		case 2: {
			url = 'include/xml/sub-category-beer.xml';
			break;
		}
		case 3: {
			url = 'include/xml/sub-category-redwine.xml';
			break;
		}
		case 4: {
			url = 'include/xml/sub-category-whitewine.xml';
			break;
		}
		case 5: {
			url = 'include/xml/sub-category-coffee.xml';
			break;
		}
		case 6: {
			url = 'include/xml/coffeeshops.xml';
			break;
		}
	}
	
	loadXMLDoc(url);
}

function rewritePageMenu()  {
	var html = '<li><a href=\"logout.html\">Logout</a></li>';
	html = html + '<li><a href=\"about.html\">About</a></li>';
	html = html + '</ul>';
	document.getElementById("pageMenu").innerHTML = html;
}



