/*
Managing the javascript for beermappings maps.
*/

// this is managing the css block toggles
function toggle(id){
if (document.getElementById){
var el = document.getElementById(id);
el.style.display = (el.style.display == 'none') ? 'block' : 'none';
}
}

//this was working on hiding the otherMaps block; no longer needed.
/*
window.onload = function(){
toggle('otherMaps');
}
*/

// This function is for the dropdown menu
startList = function() {
   if (document.all&&document.getElementById) {
      navRoot = document.getElementById("nav");
      for (i=0; i<navRoot.childNodes.length; i++) {
         node = navRoot.childNodes[i];
         if (node.nodeName=="LI") {
            node.onmouseover=function() {
               this.className+=" over";
            }
            node.onmouseout=function() {
               this.className=this.className.replace(" over", "");
            }
         }
      }
   }
}
window.onload=startList;

// removing bad crap from strings
function strValidation(string) {
    for (var i=0, output='', valid="123456789abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"; i<string.length; i++)
       if (valid.indexOf(string.charAt(i)) != -1)
          output += string.charAt(i)
    return output;
} 

function submitWeather(zip)
{
  var w = window.open('http://weather.yahoo.com/forecast/' + zip + '.html','Popup_Weather','toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=600');
 // document.urlform.target = 'Popup_Weather';
  //document.urlform.submit();
}

// this is set up for getting bookmark options popup
function submitform()
{
  var w = window.open('http://beermapping.com/maplinks/','Popup_Window','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300');
  document.urlform.target = 'Popup_Window';
  document.urlform.submit();
}

// this is set up for getting the report location popup
function reportLoc()
{
  var w = window.open('http://beermapping.com/maps/loc/locreport.php/','Popup_Window2','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=500');
  document.urlform2.target = 'Popup_Window2';
  document.urlform2.submit();
}

//this is the main xml parsing function
function getCities(){ 
	var cityHolder = '';
	var urlstr="citylistxml.php";
	//firefox and safari should fall for this one
	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
		var request = new XMLHttpRequest();
	//ie6 and such should fall for this one
	}else if(window.ActiveXObject) {
       	try {
			//extra stuff here for older browsers
        	request = new ActiveXObject("Msxml2.XMLHTTP");
      	}catch(e){
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
        	}catch(e){
          		request = false;
        	}
		}
	}
	if(request){
		request.open("GET", urlstr, true); 
  		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				if (request.status == 200){
					var xmlDoc = request.responseXML;
					// obtain the array of cities and loop through it
					var thecities = xmlDoc.documentElement.getElementsByTagName("city");

					for (var i = 0; i < thecities.length; i++) {
						// obtain the attribues of each candidate
						var name = thecities[i].firstChild.nodeValue;
						var id = thecities[i].getAttribute("id");
						var lat = thecities[i].getAttribute("lat");
						var lng = thecities[i].getAttribute("lng");	
						var dist = thecities[i].getAttribute("dist");
						var zoom = thecities[i].getAttribute("zoom");
						var real = thecities[i].getAttribute("real");	
						
						cityHolder += '<li><a href="http://beermapping.com/maps/citymaps.php?m=' + name + '">' + real + ' Map</a></li>';
					}
				}else{ //the status didn't turn up to be 200.  404 is another status code, 200 is OK.
					alert("There was a problem retrieving the XML data:\n" + request.statusText);
				}
			}
			//cityHolder += '</ul>';
			document.getElementById("thecitylist").innerHTML = cityHolder;
			//return cityHolder;
		}
		request.send(null);
	}
}

//this is the main xml parsing function for the Map by Address functions
function getCities2(){ 
	var cityHolder = '';
	var urlstr="http://beermapping.com/maps/citylistxml.php";
	//firefox and safari should fall for this one
	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
		var request = new XMLHttpRequest();
	//ie6 and such should fall for this one
	}else if(window.ActiveXObject) {
       	try {
			//extra stuff here for older browsers
        	request = new ActiveXObject("Msxml2.XMLHTTP");
      	}catch(e){
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
        	}catch(e){
          		request = false;
        	}
		}
	}
	if(request){
		request.open("GET", urlstr, true); 
  		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				if (request.status == 200){
					var xmlDoc = request.responseXML;
					// obtain the array of cities and loop through it
					var thecities = xmlDoc.documentElement.getElementsByTagName("city");

					for (var i = 0; i < thecities.length; i++) {
						// obtain the attribues of each candidate
						var name = thecities[i].firstChild.nodeValue;
						var id = thecities[i].getAttribute("id");
						var lat = thecities[i].getAttribute("lat");
						var lng = thecities[i].getAttribute("lng");	
						var dist = thecities[i].getAttribute("dist");
						var zoom = thecities[i].getAttribute("zoom");
						var real = thecities[i].getAttribute("real");	
						
						cityHolder += '<li><a href="http://beermapping.com/maps/citymaps.php?m=' + name + '">' + real + ' Map</a></li>';
					}
				}else{ //the status didn't turn up to be 200.  404 is another status code, 200 is OK.
					alert("There was a problem retrieving the XML data:\n" + request.statusText);
				}
			}
			//cityHolder += '</ul>';
			document.getElementById("thecitylist").innerHTML = cityHolder;
			//return cityHolder;
		}
		request.send(null);
	}
}