﻿// JScript File

function Search(zipcode)
{
	if(zipcode =="")
	{
		alert("Please specify a zip code to start searching.");
		return false;
	}

	if(zipcode !="" & zipcode.substring(0,2) != "05")
	{
		alert("Zip code has to start with 05.");
		return false;
	}
	//show loader
	document.getElementById('loader').style.display="";
	
	map.clearOverlays();
  GDownloadUrl(XMLAddress + zipcode, function(data) {
          var xml = GXml.parse(data);
          if(xml==null){
						document.getElementById('loader').style.display="none"; 	
          
						alert("Sorry, no address found.");
						return false;
          }
          
          var markers = xml.documentElement.getElementsByTagName("Marker");         
          if(markers.length==0){
						document.getElementById('loader').style.display="none"; 	
          
						alert("Sorry, no address found.");
						return false;
          }
          //alert(markers.length);
    
          for (var i = 0; i < markers.length; i++) {
         
          //var address = markers[i].getElementsByTagName("Address")[0].firstChild.nodeValue;
          var addresstype =  markers[i].getElementsByTagName("AddressType")[0].firstChild.nodeValue;
          var infoHTML = markers[i].getElementsByTagName("InfoHTML")[0].firstChild.nodeValue;
          var x = markers[i].getElementsByTagName("Latitude")[0].firstChild.nodeValue;
          var y = markers[i].getElementsByTagName("Longitude")[0].firstChild.nodeValue;
          
          if(x != "0"){
						addGeoPointToMap(x,y,addresstype,infoHTML);
          }
          /*
          if(i==0 & x != "0"){
              point = new GLatLng(x, y);
              //alert(zipcode.length);
              if(zipcode.length == "5")
								map.setCenter(point, 8);
							else
								map.setCenter(point, initialZoom);
					}
					*/
         }//end for
         
      ShowRetailers();  
			document.getElementById('loader').style.display="none";       	
  });
  
 /* 
  GEvent.addListener(map, "moveend", function() {
  var center = map.getCenterLatLng();
  var latLngStr = '(' + center.y + ', ' + center.x + ')';
  document.getElementById("message").innerHTML = latLngStr;
});
*/

  //hide loader
}

function SearchZip(zipcode)
{
	if(zipcode =="")
	{
		alert("Please specify a zip code to start searching.");
		return false;
	}

	if(zipcode !="" & zipcode.substring(0,2) != "05")
	{
		alert("Zip code has to start with 05.");
		return false;
	}
	
	map.closeInfoWindow();
  GDownloadUrl(ZipSearchAddress + zipcode, function(data) {
          var xml = GXml.parse(data);
          
          if(xml==null){
						alert("Sorry, no address found.");
						return false;
          }
          
          var markers = xml.documentElement.getElementsByTagName("Marker");         
          if(markers.length==0){
						alert("Sorry, no address found.");
						return false;
          }
					//alert(markers.length);
					//for (var i = 0; i < markers.length; i++) {   
						var zip = markers[0].getElementsByTagName("ZipCode")[0].firstChild.nodeValue;
						var x = markers[0].getElementsByTagName("Latitude")[0].firstChild.nodeValue;
						var y = markers[0].getElementsByTagName("Longitude")[0].firstChild.nodeValue;
	          //alert(x);
						//if(i==0 & x != "0"){
								point = new GLatLng(x, y);
								//alert(initialZipZoom);
								//alert(point);
								//alert(map.getZoom() );
								//map.panTo(point);
								//map.zoomIn();
								//map.setZoom(initialZipZoom)
								//map.setCenter(point, initialZoom);
								map.setCenter(point, parseInt(initialZipZoom));
								
					//	}
         // }
   });
}          

function MatchZip(zipcode)
{
	if(zipcode =="")
	{
		alert("Please specify a zip code to start searching.");
		return false;
	}

	if(zipcode !="" & zipcode.substring(0,2) != "05")
	{
		alert("Zip code has to start with 05.");
		return false;
	}
	map.closeInfoWindow();
	var hasZip = false;
//	alert(ZipArray.length);
//	alert(ZipArray[1][0]);
	for(i=0; i < ZipArray.length;i++)
	{
		if(ZipArray[i][0] == zipcode)
		{
			//alert(ZipArray[i][0]);
			//alert(ZipArray[i][1]);
			point = new GLatLng(ZipArray[i][1],ZipArray[i][2]);
			map.setCenter(point, parseInt(initialZipZoom));
			hasZip = true;
			break;
		}
	}
	//alert(hasZip);
	if(!hasZip)
	alert("Sorry, no address found.");
}

function kH(e) {
	var pK = document.all? window.event.keyCode:e.which;
	
	if(pK==13){
	var zip = document.getElementById('ZipCode');
	MatchZip(zip.value);
	return pK != 13;
	}
}
document.onkeypress = kH;