	
function initMenus() {
$("#store_loc").validate({
	rules: {
		addressInput: {
			digits: true,
			minlength: 5
			
		}
			
	},
	errorPlacement: function(error, element) {
			error.appendTo('#error_message');
     }

		
	
});	
$("#store_loc").bind("keypress", function(e) {
  if (e.keyCode == 13) return false;
  if(e.keyCode != 13){
	 $("#addressError").remove();
  }
  if(e.keyCode == 8 || e.keyCode == 46){
	 $("#addressError").remove();
  }
});	
$("#store_loc").bind("keydown", function(e) {
  if(e.keyCode == 8 || e.keyCode == 46){
	 $("#addressError").remove();
  }
});	
$("#store_loc").bind("keyup", function(e) {
  if(e.keyCode == 8 || e.keyCode == 46){
	 $("#addressError").remove();
  }
});	
	
	$('ul.menu ul').hide();
	$.each($('ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	
	$('ul.menu li a').hover(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
			
		}
	
	);
	$('ul.menu ul.menushow').show();	

}


	
	google.load("maps", "3",  {callback: initialize, other_params:"sensor=false"});
	
    var map;
    var markers = [];
    var infoWindow;
    var locationSelect;
	var sideBar;

    function initialize() {
	
		initMenus();
      map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude),
        zoom: 5,
        mapTypeId: 'roadmap',
		panControl: true,
		scaleControl: true,
		zoomControl: true,
		zoomControlOptions: {
     		style: google.maps.ZoomControlStyle.SMALL
    	},
		streetViewControl: true,
		mapTypeControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
      });
	  
      infoWindow = new google.maps.InfoWindow();

      locationSelect = document.getElementById("locationSelect");
	  sideBar = document.getElementById("sidebar");
	  
	  
	  
      locationSelect.onchange = function() {
        var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
        if (markerNum != "none"){
          google.maps.event.trigger(markers[markerNum], 'click');
        }
      };
   }

   function searchLocations() {
	   $("#addressError").remove();   
     var address = document.getElementById("addressInput").value;
	 
     var radius = document.getElementById('radiusSelect').value; 
	 var stateSelect = document.getElementById("stateSelect");
	 var stateSelectValue = stateSelect.options[stateSelect.selectedIndex].value;
	 if(address == "" && stateSelectValue == 'none'){
		$('<label style="float:left; clear:both; margin:0; color:#F00;" id="addressError" generated="true" for="addressInput">Please enter a valid 5 digit zip or select a state.</label>').appendTo('#error_message');
	 }
	 if(address.length > 4 && stateSelectValue == 'none'){
		var centerPoint = "("+google.loader.ClientLocation.latitude + ", " + google.loader.ClientLocation.longitude+")";
		if (address != "") {	 
			var geocoder = new google.maps.Geocoder();
			geocoder.geocode({address: address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK && address != "") {
					searchLocationsNear(results[0].geometry.location, radius, stateSelectValue);
				}
			});		
		}else if(address == "" && stateSelectValue != "none"){
			searchLocationsNear(centerPoint, 50000, stateSelectValue);	
		}else if(address == "" && stateSelectValue == "none"){
		}else{
			alert('Location Not Found');
		}
	 }else if(address.length > 4 && stateSelectValue != 'none'){
		var centerPoint = "("+google.loader.ClientLocation.latitude + ", " + google.loader.ClientLocation.longitude+")";
		if (address != "") {	 
			var geocoder = new google.maps.Geocoder();
			geocoder.geocode({address: address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK && address != "") {
					searchLocationsNear(results[0].geometry.location, radius, stateSelectValue);
				}
			});		
		}else if(address == "" && stateSelectValue != "none"){
			searchLocationsNear(centerPoint, 50000, stateSelectValue);	
		}else if(address == "" && stateSelectValue == "none"){
		}else{
			alert('Location Not Found');
		}	 
	 }else if(address.length == 0 && stateSelectValue != 'none'){
		 $("#addressError").remove(); 
		var centerPoint = "("+google.loader.ClientLocation.latitude + ", " + google.loader.ClientLocation.longitude+")";
		if (address != "") {	 
			var geocoder = new google.maps.Geocoder();
			geocoder.geocode({address: address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK && address != "") {
					searchLocationsNear(results[0].geometry.location, radius, stateSelectValue);
				}
			});		
		}else if(address == "" && stateSelectValue != "none"){
			searchLocationsNear(centerPoint, 50000, stateSelectValue);	
		}else if(address == "" && stateSelectValue == "none"){
		}else{
			alert('Location Not Found');
		}		 
	 }
   }

   function clearLocations() {
     infoWindow.close();
     for (var i = 0; i < markers.length; i++) {
       markers[i].setMap(null);
     }
     markers.length = 0;
	 
	 var div = document.createElement("div");
     sideBar.innerHTML = "";
	 /*sideBar.innerHTML = "No Stores";*/
	 sideBar.appendChild(div);
	 
	 locationSelect.innerHTML = "";
     var option = document.createElement("option");
     option.value = "none";
     option.innerHTML = "See all results:";
     locationSelect.appendChild(option);
   }

   function searchLocationsNear(center, radius, stateSelectValue) {
     clearLocations(); 
	 var address = document.getElementById("addressInput").value;
	 if (address != "") {
     var searchUrl = 'phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius + '&state=' + stateSelectValue;
	 }else{
	 var searchUrl = 'phpsqlsearch_genxml.php?&radius=' + radius + '&state=' + stateSelectValue;
	 }
     downloadUrl(searchUrl, function(data) {
       var xml = parseXml(data);
       var markerNodes = xml.documentElement.getElementsByTagName("marker");
       var bounds = new google.maps.LatLngBounds();
	   if(markerNodes.length == "0"){
map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude),
        zoom: 5,
        mapTypeId: 'roadmap',
		panControl: true,
		scaleControl: true,
		zoomControl: true,
		zoomControlOptions: {
     		style: google.maps.ZoomControlStyle.SMALL
    	},
		streetViewControl: true,
		mapTypeControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
      });
	   $("#addressError").remove();
	if($("#error_message:contains('There are no locations for the zip code entered.  Please try another zip code and search again.')").html()){
	}else if($("#error_message:contains('Please enter a valid 5 digit zip code.')").html()){
		$("#addressError").remove();
	}else{
		$('<label style="float:left; clear:both; margin:0; color:#F00;" id="addressError" generated="true" for="addressInput">There are no locations for the zip code entered.  Please try another zip code and search again.</label>').appendTo('#error_message');
	}	  
	
	 
	  
		   /*alert("Please enter a new Zip Code.");*/
	   }else{
       for (var i = 0; i < markerNodes.length; i++) {
         var name = markerNodes[i].getAttribute("name");
         var address = markerNodes[i].getAttribute("address");
		 var city = markerNodes[i].getAttribute("city");
		 var state = markerNodes[i].getAttribute("state");
		 var zipcode = markerNodes[i].getAttribute("zipcode");
		 var lat = markerNodes[i].getAttribute("lat");
		 var lng = markerNodes[i].getAttribute("lng");
         var distance = parseFloat(markerNodes[i].getAttribute("distance"));
         var latlng = new google.maps.LatLng(
              parseFloat(markerNodes[i].getAttribute("lat")),
              parseFloat(markerNodes[i].getAttribute("lng")));

			createSideBar(name, distance, address, city, state, zipcode, i);	
			createOption(name, distance, i);
			createMarker(latlng, name, address, city, state, zipcode, distance);
			bounds.extend(latlng); 
       }
$('#sidebar div.AL').wrapAll('<div class="AL" />');	   
$('#sidebar div.AL:first').before('<div><h1>Alabama Locations</h1></div>');
$('#sidebar div.AK').wrapAll('<div class="AK" />');
$('#sidebar div.AK:first').before('<div><h1>Alaska Locations</h1></div>');
$('#sidebar div.AZ').wrapAll('<div class="AZ" />');
$('#sidebar div.AZ:first').before('<div><h1>Arizona Locations</h1></div>');
$('#sidebar div.AR').wrapAll('<div class="AR" />');
$('#sidebar div.AR:first').before('<div><h1>Arkansas Locations</h1></div>');
$('#sidebar div.CA').wrapAll('<div class="CA" />');
$('#sidebar div.CA:first').before('<div><h1>California Locations</h1></div>');
$('#sidebar div.DE').wrapAll('<div class="DE" />');
$('#sidebar div.DE:first').before('<div><h1>Delaware Locations</h1></div>');
$('#sidebar div.FL').wrapAll('<div class="FL" />');
$('#sidebar div.FL:first').before('<div><h1>Florida Locations</h1></div>');
$('#sidebar div.GA').wrapAll('<div class="GA" />');
$('#sidebar div.GA:first').before('<div><h1>Georgia Locations</h1></div>');
$('#sidebar div.ID').wrapAll('<div class="ID" />');
$('#sidebar div.ID:first').before('<div><h1>Idaho Locations</h1></div>');
$('#sidebar div.IL').wrapAll('<div class="IL" />');
$('#sidebar div.IL:first').before('<div><h1>Illinois Locations</h1></div>');
$('#sidebar div.IN').wrapAll('<div class="IN" />');
$('#sidebar div.IN:first').before('<div><h1>Indiana Locations</h1></div>');
$('#sidebar div.IA').wrapAll('<div class="IA" />');
$('#sidebar div.IA:first').before('<div><h1>Iowa Locations</h1></div>');
$('#sidebar div.KY').wrapAll('<div class="KY" />');
$('#sidebar div.KY:first').before('<div><h1>Kentucky Locations</h1></div>');
$('#sidebar div.LA').wrapAll('<div class="LA" />');
$('#sidebar div.LA:first').before('<div><h1>Louisiana Locations</h1></div>');
$('#sidebar div.ME').wrapAll('<div class="ME" />');
$('#sidebar div.ME:first').before('<div><h1>Maine Locations</h1></div>');
$('#sidebar div.MD').wrapAll('<div class="MD" />');
$('#sidebar div.MD:first').before('<div><h1>Maryland Locations</h1></div>');
$('#sidebar div.MA').wrapAll('<div class="MA" />');
$('#sidebar div.MA:first').before('<div><h1>Massachusetts Locations</h1></div>');
$('#sidebar div.MI').wrapAll('<div class="MI" />');
$('#sidebar div.MI:first').before('<div><h1>Michigan Locations</h1></div>');
$('#sidebar div.MN').wrapAll('<div class="MN" />');
$('#sidebar div.MN:first').before('<div><h1>Minnesota Locations</h1></div>')
$('#sidebar div.MS').wrapAll('<div class="MS" />');
$('#sidebar div.MS:first').before('<div><h1>Mississippi Locations</h1></div>');
$('#sidebar div.MO').wrapAll('<div class="MO" />');
$('#sidebar div.MO:first').before('<div><h1>Missouri Locations</h1></div>');
$('#sidebar div.MT').wrapAll('<div class="MT" />');
$('#sidebar div.MT:first').before('<div><h1>Montana Locations</h1></div>');
$('#sidebar div.NE').wrapAll('<div class="NE" />');
$('#sidebar div.NE:first').before('<div><h1>Nebraska Locations</h1></div>');
$('#sidebar div.NV').wrapAll('<div class="NV" />');
$('#sidebar div.NV:first').before('<div><h1>Nevada Locations</h1></div>');
$('#sidebar div.NH').wrapAll('<div class="NH" />');
$('#sidebar div.NH:first').before('<div><h1>New Hampshire Locations</h1></div>');
$('#sidebar div.NJ').wrapAll('<div class="NJ" />');
$('#sidebar div.NJ:first').before('<div><h1>New Jersey Locations</h1></div>');
$('#sidebar div.NM').wrapAll('<div class="NM" />');
$('#sidebar div.NM:first').before('<div><h1>New Mexico Locations</h1></div>');
$('#sidebar div.NY').wrapAll('<div class="NY" />');
$('#sidebar div.NY:first').before('<div><h1>New York Locations</h1></div>');
$('#sidebar div.NC').wrapAll('<div class="NC" />');
$('#sidebar div.NC:first').before('<div><h1>North Carolina Locations</h1></div>');
$('#sidebar div.ND').wrapAll('<div class="ND" />');
$('#sidebar div.ND:first').before('<div><h1>North Dakota Locations</h1></div>');
$('#sidebar div.OH').wrapAll('<div class="OH" />');
$('#sidebar div.OH:first').before('<div><h1>Ohio Locations</h1></div>');
$('#sidebar div.OK').wrapAll('<div class="OK" />');
$('#sidebar div.OK:first').before('<div><h1>Oklahoma Locations</h1></div>');
$('#sidebar div.OR').wrapAll('<div class="OR" />');
$('#sidebar div.OR:first').before('<div><h1>Oregon Locations</h1></div>');
$('#sidebar div.PA').wrapAll('<div class="PA" />');
$('#sidebar div.PA:first').before('<div><h1>Pennsylvania Locations</h1></div>');
$('#sidebar div.RI').wrapAll('<div class="RI" />');
$('#sidebar div.RI:first').before('<div><h1>Rhode Island Locations</h1></div>');
$('#sidebar div.SC').wrapAll('<div class="SC" />');
$('#sidebar div.SC:first').before('<div><h1>South Carolina Locations</h1></div>');
$('#sidebar div.SD').wrapAll('<div class="SD" />');
$('#sidebar div.SD:first').before('<div><h1>South Dakota Locations</h1></div>');
$('#sidebar div.TN').wrapAll('<div class="TN" />');
$('#sidebar div.TN:first').before('<div><h1>Tennessee Locations</h1></div>');
$('#sidebar div.TX').wrapAll('<div class="TX" />');
$('#sidebar div.TX:first').before('<div><h1>Texas Locations</h1></div>');
$('#sidebar div.UT').wrapAll('<div class="UT" />');
$('#sidebar div.UT:first').before('<div><h1>Utah Locations</h1></div>');
$('#sidebar div.VT').wrapAll('<div class="VT" />');
$('#sidebar div.VT:first').before('<div><h1>Vermont Locations</h1></div>');
$('#sidebar div.VA').wrapAll('<div class="VA" />');
$('#sidebar div.VA:first').before('<div><h1>Virginia Locations</h1></div>');
$('#sidebar div.WA').wrapAll('<div class="WA" />');
$('#sidebar div.WA:first').before('<div><h1>Washington Locations</h1></div>');
$('#sidebar div.WV').wrapAll('<div class="WV" />');
$('#sidebar div.WV:first').before('<div><h1>West Virginia Locations</h1></div>');	
$('#sidebar div.WI').wrapAll('<div class="WI" />');
$('#sidebar div.WI:first').before('<div><h1>Wisconsin Locations</h1></div>');
$('#sidebar div.WY').wrapAll('<div class="WY" />');
$('#sidebar div.WY:first').before('<div><h1>Wyoming Locations</h1></div>');	



/*SORTING*/

       map.fitBounds(bounds);
	   
       locationSelect.style.visibility = "visible";
	   sideBar.style.visibility = "visible";
	   
       locationSelect.onchange = function() {
         var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
         google.maps.event.trigger(markers[markerNum], 'click');
       };
	   }
      });
    }

    function createMarker(latlng, name, address, city, state, zipcode, distance) {
		function delquote(str){
			return (str=str.replace(/["']{1}/gi,""));
		}	 
		 var address_filled = document.getElementById('addressInput').value;
		 var userInput = escape(address_filled);
		 var addressspace = address + "%20" + city + "%20" + state + "%20" + zipcode;	
		 var fullAddress = addressspace.replace(/\s/g,"%20");
		
		if(address_filled == 0){
			var html = '<strong>' + name + '</strong>' + ' <br/>' + address + ", " + city + ", " + state + " " + zipcode +'<br/> <a href=http://maps.google.com/maps?saddr=&daddr=' + delquote(fullAddress) + ' target=_black>Get Directions</a>';
		}else{
			var html = '<strong>' + name + '</strong>' + ' (' + distance.toFixed(1) + ') <br/>' + address + ", " + city + ", " + state + " " + zipcode + '<br/> <a href=http://maps.google.com/maps?saddr=' + delquote(userInput) + '&daddr=' + delquote(fullAddress) + ' target=_black>Get Directions</a>';
		}		
		
      
      var marker = new google.maps.Marker({
        map: map,
        position: latlng
		
      });
      google.maps.event.addListener(marker, 'click', function() {	  		  
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
$(".gmnoprint img[src='http://maps.gstatic.com/intl/en_us/mapfiles/iws3.png']").each(function(){
	$(this).css('display','none');
	$(this).hide();
});			
      });
      markers.push(marker);
	  
    }

    function createOption(name, distance, num) {
      var option = document.createElement("option");
	  var address_filled = document.getElementById('addressInput').value;
      option.value = num;
      option.innerHTML = name + " (" + distance.toFixed(1) + ")";
	  if(address_filled == 0){  
	  	option.innerHTML = name;
	  }else{
		option.innerHTML = name + " (" + distance.toFixed(1) + ")";  
	  }	  
      locationSelect.appendChild(option);
    }
	function createSideBar(name, distance, address, city, state, zipcode, num) {
      var div = document.createElement("div");
      div.id = num;	 
	  div.className = state + " " + distance;	
	  var address_filled = document.getElementById('addressInput').value;
	  if(address_filled == 0){  
	  var html = '<strong>' + name + '</strong> <br/>' + address + ", " + city + ", " + state + " " + zipcode;
	  }else{
		  var html = '<strong>' + name + '</strong> (' + distance.toFixed(1) + ')<br/>' + address + ", " + city + ", " + state + " " + zipcode;
	  }
      div.innerHTML = html;
      div.style.cursor = 'pointer';
      div.style.marginBottom = '5px';
	  
	  google.maps.event.addDomListener(div, 'click', function() {
		  google.maps.event.trigger(markers[num], 'click');		
      });
      google.maps.event.addDomListener(div, 'mouseover', function() {
        div.style.backgroundColor = '#eee';
      });
      google.maps.event.addDomListener(div, 'mouseout', function() {
        div.style.backgroundColor = '#fff';
      });
      sideBar.appendChild(div);	
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request.responseText, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function parseXml(str) {
      if (window.ActiveXObject) {
        var doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.loadXML(str);
        return doc;
      } else if (window.DOMParser) {
        return (new DOMParser).parseFromString(str, 'text/xml');
      }
    }

    function doNothing() {}
	
	
