// JavaScript Document
//<![CDATA[
function doSomething(){
		var tagged = document.getElementById("loading");
		tagged.className = "enabled";
		if (document.getElementById("scottsdale-city").checked){ var datafile = document.getElementById("scottsdale-city").value;}
		if (document.getElementById("phoenix-city").checked){ var datafile = document.getElementById("phoenix-city").value;}
		if (document.getElementById("cavecreek-city").checked){ var datafile = document.getElementById("cavecreek-city").value;}
		if (document.getElementById("paradisevalley-city").checked){ var datafile = document.getElementById("paradisevalley-city").value;}
		if (document.getElementById("peoria-city").checked){ var datafile = document.getElementById("peoria-city").value;}
		if (document.getElementById("glendale-city").checked){ var datafile = document.getElementById("glendale-city").value;}
		if (document.getElementById("gilbert-city").checked){ var datafile = document.getElementById("gilbert-city").value;}
		if (document.getElementById("tempe-city").checked){ var datafile = document.getElementById("tempe-city").value;}
		if (document.getElementById("mesa-city").checked){ var datafile = document.getElementById("mesa-city").value;}
		if (document.getElementById("chandler-city").checked){ var datafile = document.getElementById("chandler-city").value;}
		if (document.getElementById("fountainhills-city").checked){ var datafile = document.getElementById("fountainhills-city").value;}
		if (document.getElementById("rioverde-city").checked){ var datafile = document.getElementById("rioverde-city").value;}
		if (document.getElementById("apachejunction-city").checked){ var datafile = document.getElementById("apachejunction-city").value;}
		if (document.getElementById("suncity-city").checked){ var datafile = document.getElementById("suncity-city").value;}
		if (document.getElementById("surprise-city").checked){ var datafile = document.getElementById("surprise-city").value;}
		if (document.getElementById("goodyear-city").checked){ var datafile = document.getElementById("goodyear-city").value;}
		datafile = "http://www.phoenixhomes.com/maps/xml/"+datafile;
		loadall(datafile);
		}

function setbackground()
{

var bgcolorlist=new Array("#FF0000", "#3399FF", "#990000", "#00CC00", "#006600", "#FF000", "#663300", "#FFF00", "#FF9900")

return bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)]

}


function mapload() {
      if (GBrowserIsCompatible()) { 
		var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        //Scottsdale
		map.setCenter(new GLatLng(33.511057,-111.898499), 10);
		}
}

// Creates a marker at the given point with the given number label
function createMarker(point, infos) {
		  var marker = new GMarker(point);
		  GEvent.addListener(marker, "click", function() {
		    marker.openInfoWindowHtml(infos);
		  });
		  return marker;
}
   
function loadall(datafile) {
      if (GBrowserIsCompatible()) { 
		var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        //Scottsdale
		map.setCenter(new GLatLng(33.511057,-111.898499), 10);
		
      }		
		var bounds = new GLatLngBounds();
		
		
	
// Download the data in data.xml and load it on the map. The format we
GDownloadUrl(datafile, function(data) {	  
		  var xml = GXml.parse(data);
		  var polygons = xml.documentElement.getElementsByTagName("polygon");
		  var markers = xml.documentElement.getElementsByTagName("marker");
		  var info = xml.documentElement.getElementsByTagName("info");
		  var html = xml.documentElement.getElementsByTagName("html");
		  var zipcodes = xml.documentElement.getElementsByTagName("zipcodes");
		  
	     
		  
		  	for (var i = 0; i < info.length; i++) {
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            	var infos = info[i].getAttribute("zip");
				var img = info[i].getAttribute("img")
				
				var htmlcode = [];
				
				for (var h = 0; h < info[i].getElementsByTagName("html").length; h++) {
					//alert("htmlcode: "+"<a href=http://"+info[i].getElementsByTagName("html")[h].getAttribute("link")+" rel='nofollow'>"+info[i].getElementsByTagName("html")[h].getAttribute("text")+"</a>");
					htmlcode.push("<a target='blank' rel='nofollow' href="+info[i].getElementsByTagName("html")[h].getAttribute("link")+">"+info[i].getElementsByTagName("html")[h].getAttribute("text")+"</a>");
				}
				
				var text = "<div style='width: 300px; text-align: left;'><img src='"+img+"' align='right' width='120'><strong>"+infos+"</strong>" + "<br>";
					for(var t=0; t < htmlcode.length; t++){
						text += htmlcode[t]+"<br>";
					}
					text += "<br clear='all'></div><br clear='all'>";
				//map.addOverlay(new GMarker(point));
				var newpoint = new GMarker(point);
				map.addOverlay(newpoint);
        		newpoint.openInfoWindowHtml(text);
				map.addOverlay(createMarker(point, text));
				
				bounds.extend(point);
	//			for (var z = 0; z < info[i].length; i++) {
    	    }
			//Begin Polylines		  
			var lines = xml.documentElement.getElementsByTagName("line");
			var pline = [];		
			
			for (var p = 0; p < polygons.length; p++) {		 
			 //alert("Number of lines in "+ p + " polygon : " + polygons[p].getElementsByTagName("line").length);
				for (var z = 0; z < polygons[p].getElementsByTagName("line").length; z++) {
					pline.push(new GLatLng(parseFloat(polygons[p].getElementsByTagName("line")[z].getAttribute("lat")),
                    parseFloat(polygons[p].getElementsByTagName("line")[z].getAttribute("lng"))));
				}
				//map.addOverlay(new GPolyline(polyline, "#FF0000", 4, 0));
				var color = setbackground();
				map.addOverlay(new GPolygon(pline, "#ff0000",  2,  1, color,  0));
				pline.length = 0;
				var tagged = document.getElementById("loading");
				tagged.className = "disabled";
			 
				map.setZoom(10);
				map.setCenter(bounds.getCenter());
				//End Polylines
    		}
		});

}
    //]]>