

	var SMALL_SIZE_ITEMS = 6;
	var map;
	var geocoder;
    var g_leads = new Array();
    var g_markers = new Array();
    var g_results = new Array();
	var g_nMarkerIndex = -1;
 
	var g_bRefreshState = true;
	var g_nMaxPageLinks = 10;

    var g_nZip;
	var g_nRadius;
	var g_nRadiusCheck;
	var g_sAddress;
	var g_sCity;
	var g_sState;
	var g_sCountry;
	var g_sBizName;
	var g_sCategory;
	var g_sSearchFor;
	var g_WP;
	var g_bSmallSize;
	var g_linksPerPage;

      var arrReasons=[];
      arrReasons[G_GEO_SUCCESS]            = "Success";
      arrReasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
      arrReasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
      arrReasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
      arrReasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
      arrReasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
      arrReasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
      arrReasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
      arrReasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";




	function initMap() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableDoubleClickZoom() 

		showWorld('Denver CO', 8);
		

	
	  }
	  else
		  alert("Browser is not Google Maps compatible");
	}


	function initData(add, city, state, country, zip, radius, radiusCheck, category, searchfor,nZoom, bSmallSize, linksPerPage) {
		g_nZip = zip;
		g_nRadius = radius;
		g_sAddress = add;
		g_sCity = city;
		g_sState = state;
		g_sCountry = country;
		g_nRadiusCheck = radiusCheck;
		g_sCategory = category;
		g_sSearchFor = searchfor;
		g_bSmallSize = bSmallSize;
		g_linksPerPage = linksPerPage;
		
		var sLocation = '';
		
		if (zip != null && zip != '')
			sLocation = 'US Zip code ' + g_nZip;
		else {
			sLocation = g_sAddress +" " + g_sCity + ", " + g_sState + " " + g_sCountry;
		}
		showWorld(sLocation, nZoom);

		loadStates();

	}

    function showWorld(place, zoomlevel) {
		geocoder = new GClientGeocoder();
		if (geocoder) {

			geocoder.getLatLng(place,
				function(point) {
				  if (!point) {
					//alert(place + " not found. Servers could be busy. Please try again");
				  } 
				  else {
					map.setCenter(point, zoomlevel);
					map.setMapType(G_NORMAL_MAP) ; 
					getSearchResults(1);

				  }
				}
			);
		}
		else
			alert("Geocoder not working");

	}


	function zoomIn() {
		map.zoomIn();
	}

	function zoomTo(level) {
		map.setZoom(level);
	}

	function loadMap(lat, lng) {
      if (map) {
        map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(lat, lng), 10);
	
      }
	  else
		  alert("Google Maps not loaded");
    }




	function loadStates() {
		g_bRefreshState = false;		// prevent state combo onchange handler from firing
		var frm = document.frmHeader;
		var sCountry = frm.cboCountry[frm.cboCountry.selectedIndex].value;
		if (sCountry != '') {
			var st = frm.cboState;
			st.options.length = 0;
			
			var ct = frm.cboCity;
			ct.options.length = 0;
			var opt = document.createElement("option");
			opt.text = "Select City";
			opt.value = "0";
			ct.options.add(opt);

			var url  = "/wp-content/plugins/gmapandsearch/getstates.php";
			var pars = "id="+sCountry;
			var d = document.getElementById("divSpinner");
			if (d) {
				d.style.display = "block";
				d.innerHTML = "Fetching States list...&nbsp;<img src='/wp-content/plugins/gmapandsearch/images/spinner.gif' border=0>";
			}
			new Ajax.Request(url, {method: 'get', parameters:pars, onComplete:
				function(r) {
					var data = r.responseText;
					if ('' == url) {
							// do nothing
					} else {
					  var arr = data.split('|');
					  var nLoop = arr.length;
					  var o = document.createElement("option");
					  o.text = "Select State";
					  o.value ="0";
					  st.options.add(o);

					  for(i = 0; i < nLoop; i++) {
					    var entry = arr[i];
						var arrEntry = entry.split(',');
						var testjunk = new String(arrEntry[0]);
						if (arrEntry[0] != '' && testjunk.indexOf("<") == -1) {
							var o = document.createElement("option");
							o.text = arrEntry[0];
							o.value = arrEntry[1];
							st.options.add(o);
						}					  
					  }

					  g_bRefreshState = true;	
					  d.style.display = "none";
					  d.innerHTML = "";
					}

				},
				onSuccess:
					function (r) {
				},
				onFailure:
					function (r) {
					}

			});

		}
		g_bRefreshState = true;	

	}

	function loadCities() {
		if (!g_bRefreshState)	// check if this event handler should run
			return;
		var frm = document.frmHeader;
		var sState = frm.cboState[frm.cboState.selectedIndex].value;
		if (sState != '0') {
			var st = frm.cboCity;
			st.options.length = 0;
			var url  = "/wp-content/plugins/gmapandsearch/getcities.php";
			var pars = "id="+sState;
			var d = document.getElementById("divSpinner");
			if (d) {
				d.style.display = "block";
				d.innerHTML = "Fetching Cities list...&nbsp;<img src='/wp-content/plugins/gmapandsearch/images/spinner.gif' border=0>";
			}
			new Ajax.Request(url, {method: 'get', parameters:pars, onComplete:
				function(r) {
					var data = r.responseText;
					if ('' == url) {
							// do nothing
					} else {
					  var arr = data.split('|');
					  var nLoop = arr.length;
					  for(i = 0; i < nLoop; i++) {
					    var entry = arr[i];
						var arrEntry = entry.split(',');
						var testjunk = new String(arrEntry[0]);
						if (arrEntry[0] != '' && testjunk.indexOf("<") == -1) {
							var o = document.createElement("option");
							o.text = arrEntry[0];
							o.value = arrEntry[1];
							st.options.add(o);
						}
					  
					  }

					  d.style.display = "none";
					  d.innerHTML = "";
					}

				}
			});


		}
	}


function doheaderForm(page_id) {
	var frm = document.frmHeader;
	var cats = frm.cboCats[frm.cboCats.selectedIndex].value;
	var searchFor = frm.txtSearchFor.value;
	var g_WP = 1;
	var sformaction = "?page_id=" + page_id;
	var linksPerPage = frm.cboPageLinks[frm.cboPageLinks.selectedIndex].value;

	if (cats == null || cats == '') {
		alert("No search category was selected");
		return;
    }

	if (frm.ckRadius.checked)
	   g_nRadiusCheck = 1;
	else
	   g_nRadiusCheck = 0;


	if (trim(frm.txtZip.value) != "") {
		var rcheck = 0;
		if (frm.ckRadius.checked)
			rcheck = 1;
		var params = '&z=' + trim(frm.txtZip.value) + '&cat=' + cats + "&r=" + frm.cboRadius[frm.cboRadius.selectedIndex].value + "&radcheck=" + rcheck +"&sn=" + searchFor + "&gid=" + page_id+"&lpp=" + linksPerPage;
		window.location=sformaction + params;
	}
	else {
		var country = frm.cboCountry[frm.cboCountry.selectedIndex].text;
		var state = frm.cboState[frm.cboState.selectedIndex].text;
		var city = frm.cboCity[frm.cboCity.selectedIndex].text;
		var add = frm.txtAdd.value;

		if (country != "Select Country" && state != "Select State") {
			window.location=sformaction + '&add=' + add + '&city='+city +'&state=' + state + '&country=' + country+ '&cat=' + cats + "&sn=" + searchFor + "&gid=" + page_id + "&lpp=" + linksPerPage; 	
		}
		else if (g_sState != null && g_sState != '' && g_sCity != null && g_sCity != '') {
			  city = g_sCity; 
			  state = g_sState;
			window.location=sformaction + '&add=' + add + '&city='+city +'&state=' + state + '&country=' + country+ '&cat=' + cats + "&sn=" + searchFor + "&gid=" + page_id + "&lpp=" + linksPerPage; 	
		}
		else
			alert("At least a country and a region need to be selected");
	}
}


  var rowCounter = 3;




	function getSearchResults(page) {
		var nRadius = g_nRadius;
		var nRadCheck = 1;
		if (g_nRadiusCheck != 1) {
		    nRadius = 0;
			nRadCheck = 0;
		}	
		var sZip = g_nZip;
		var country = g_sCountry;
		var state = g_sState;
		var city = g_sCity;
		var add = g_sAddress;
		var cats = g_sCategory;
		var searchFor = g_sSearchFor;
		var bSmallSize = g_bSmallSize;
		var linksPerPage = g_linksPerPage;


		if (cats == null || cats =='') {
			return;
		}
			g_markers.clear();
			g_results.clear();
			map.clearOverlays();

			var url  = "/wp-content/plugins/gmapandsearch/getzipsinradius.php";
			var pars = "z=" + sZip+ "&r=" + nRadius+"&country="+country+"&state="+state+"&city="+city+"&add="+add+"&cat=" + cats+"&page=" + page +"&sn=" + searchFor+"&radcheck=" + nRadCheck+"&lpp=" + linksPerPage;
			var d = document.getElementById("divSpinner");
			if (d) {
				d.style.display = "block";
				d.innerHTML = "Locating data...&nbsp;<img src='images/spinner.gif' border=0>";
			}
			var divOutput = document.getElementById("divZipRadius");
			var output = "<table width=100% cellspacing=1 cellpadding=2 >";
			new Ajax.Request(url, {method: 'get', parameters:pars, onComplete:
				function(r) {
					var data = r.responseText;
					if ('' == url) {
							// do nothing
					} else {
				      window.g_markers = new Array();	// clear array of markers
					  window.g_results = new Array();
					  window.g_nMarkerIndex = -1;

					  if (isNaN(g_linksPerPage))
						  g_linksPerPage = 10;
					  var f = document.frmHeader;
					  f.cboPageLinks.value=g_linksPerPage;

					if (data.indexOf("</object>") > -1) {
						  data = data.substring(0,data.indexOf("</object>"));
					}

					  var fontStyleStart = "";
					  var fontStyleStop = "";
					  var tdStyle = "";
					  var arr = data.split('\r\n');
					  var nLoop = arr.length;
					  var result = '';
					  if (bSmallSize == 1 && parseInt(nLoop) > parseInt(SMALL_SIZE_ITEMS)) {
						  nLoop = SMALL_SIZE_ITEMS;
						  fontStyleStart = "<font size=1>";
						  fontStyleStop = "</font>";
					  }
					  else {
						//tdStyle = "class=\"normal_line_height_big\"";
						tdStyle = "style=\"font-size:12px;line-height:13px;\"";
					  }
 					  for(i = 0; i < nLoop-1; i++) {
					    var entry = arr[i];
						var sURL = '';
						if (entry != null && entry != '') {
							var arrEntry = entry.split('|');
							 result = "<b>" + arrEntry[6] + "</b><br>" + arrEntry[7] + "<br>" + arrEntry[1]+"&nbsp;" + arrEntry[2]+"&nbsp;" + arrEntry[0];


							if (cats != "ATM" ) {
								result += "<br>Ph:" + arrEntry[8] + ", Fax:" + arrEntry[9] + "<br>URL:&nbsp;" + arrEntry[10];

								if (arrEntry[10] != "N.A") {
									if (arrEntry[10].indexOf("http://") == -1)
										arrEntry[10] = "http://" + arrEntry[10];
									sURL = "<A style=\"color:blue;\" href=\"" + arrEntry[10] + "\" target=_blank>" + arrEntry[10] + "</a>";
								}
								else
									sURL = "N.A";
							}
							var place = arrEntry[7] + ", " + arrEntry[1] + " " + arrEntry[2] + ", USA";

							output += "<tr valign=top>";
							output += "<td " + tdStyle + " name=\"td" + i + "\" >" + fontStyleStart + "<a style=\"color:blue;\" href=\"#\" onclick=\"javascript:popupMarker(" + i + ",'" + result + "');return false;\"><b>" + arrEntry[6] + "</b></a><br>" + arrEntry[7] + "&nbsp;" + arrEntry[1]+"&nbsp;" + arrEntry[2]+"&nbsp;" + arrEntry[0];
							if (cats != "ATM" ) {
								if (bSmallSize)
									output += "<br>";
								else
									output += "<b>&nbsp;&rsaquo;&rsaquo;&nbsp;</b>";
								output += "Ph:" + arrEntry[8] + ", Fax:" + arrEntry[9] + "<br>URL:&nbsp;" + sURL;
							}

							output+= "<br><br>" + fontStyleStop + "</td>";
							output += "</tr>";
							createMarker(place, result, "td" + i, i==0);

						}
					  }
					  output += "</table>";
					  if (bSmallSize != 1) {
						  var pageInfo = arr[nLoop-1];
						  var arrPageInfo = pageInfo.split('|');
						  var nPageLinks = parseInt(arrPageInfo[1]);
							
						  output += "Total " + arrPageInfo[0] + " results, Pages:";
						  var nStartLink = 1;
						  var nStopLink = nPageLinks;
						  if (page > g_nMaxPageLinks) {
							  nStartLink = parseInt(page/g_nMaxPageLinks) * g_nMaxPageLinks;
							  if (nStartLink == page)
								  nStartLink -= g_nMaxPageLinks;
							  if (nStartLink < 1)
								  nStartLink = 1;
						  }
						  if (nStopLink > (nStartLink + g_nMaxPageLinks))
							  nStopLink = (nStartLink + g_nMaxPageLinks+1);

						//alert(nPageLinks +", from " + nStartLink + " to " + nStopLink);
						  for(i=nStartLink;i <= nStopLink; i++) {
							  if (i != page)
								output += "<a href=\"#\" onclick=\"getSearchResults(" + i + "); return false;\">" + i + "</a>&nbsp;";
							  else
								output += "&nbsp;" + i + "&nbsp;";
						  }
					  }	
					  output += "<br><br>";
					  divOutput.innerHTML = output;
					  d.style.display = "none";
					  d.innerHTML = "";

					}

				}
			});

		g_bRefreshState = true;	

	}



	function createMarker(place, result, resultid, openFlag) {
		geocoder = new GClientGeocoder();
		var marker;
		if (geocoder) {

			geocoder.getLatLng(place ,
				function(point) {
				  if (!point) {
					//alert(place + " not found. Servers could be busy. Please try again");
				  } 
				  else {
					  
					  marker = new GMarker(point);

					  marker.bindInfoWindowHtml(result);
					  g_markers[++g_nMarkerIndex] = marker;
					  g_results[g_nMarkerIndex] = resultid;
					  map.addOverlay(window.g_markers[g_nMarkerIndex]);
								// if first marker then show it
					  if (openFlag == true) {
					     marker.openInfoWindowHtml(result);
					  }

				  }
				}
			);
		}
		else
			alert("Geocoder not working");


	  return ;
	}



function popupMarker(item, data) {

	var m = g_markers[item];
	if (m) {
		m.openInfoWindowHtml(data);
	}
}



