var map;
var localSearch = new GlocalSearch();
var currentMarker;

function validatezip(string)
  {
	
   var valid = "0123456789";
   var res;
   var result = true;
   if (string.length == 0) return false;
   for (i = 0; i < string.length && result == true; i++)
      {
      res = string.charAt(i);
      if (valid.indexOf(res) == -1)
         {
         result = false;
         }
      }
   return result;
}


function usePointFromPostcode(zipcode, callbackFunction) 
{
	
	if (zipcode.length!=5)

   { 
   window.alert("Please Enter Valid Zip Code");
   
   return zipcode;
   }
   
   else { 

       if (validatezip(zipcode)==false) 
	   
	      {
		 alert("Invalid Zip Code"); 
		 return true;
		  }
   }
	
	localSearch.setSearchCompleteCallback(null, function() { if (localSearch.results[0])
			                                                   {		
				                                               var resultLat = localSearch.results[0].lat;
				                                               var resultLng = localSearch.results[0].lng;
				                                               var point = new GLatLng(resultLat,resultLng);
				                                               callbackFunction(point, zipcode);
															 														   
															   				
			                                                    }
				                                                else
				                                                {
				                                                alert("Zip Code Not Found!");
																			                                                   
															    }
		                                                   });	

localSearch.execute(zipcode + ", USA");

}


 

function placeMarkerAtPoint(point, zipcode)
            {
	
	    var HTMLform = "<form onsubmit=\"return false;\">";
	
        HTMLform += "<div style=\"font-size:12px; font-weight:bold;\">Select type to get insurance quotes</div><br/>";
		HTMLform += "<input type=\"hidden\" maxlength=\"5\" name=\"zip\" size=\"10\" value=\"" + zipcode + "\" />";
		HTMLform += "<div style=\"font-size:12px; font-weight:bold; margin-top:3px;\">Type of Insurance:<br>  <select name=\"c\" size=\"1\">";
		HTMLform += " <option value=\"auto\" selected=\"selected\">Auto Insurance</option><option value=\"home\">Home Insurance</option><option value=\"health\">Health Insurance</option>";
		HTMLform += "<option value=\"DT\">Dental Insurance</option>  <option value=\"life\">Life Insurance</option>  <option value=\"business\">Business Insurance</option>";
		HTMLform += "<option value=\"moto\">Motorcycle Insurance</option>";
        HTMLform += "<option value=\"renters\">Renters Insurance</option>  <option value=\"condo\">Condo Insurance</option>";
		HTMLform += "<option value=\"annuity\">Annuity</option>  <option value=\"burial\">Burial Insurance</option>";
        HTMLform += "<option value=\"cancer\">Cancer Insurance</option>  <option value=\"disability\">Disability Insurance</option>  <option value=\"ltc\">Long Term Care</option>";
        HTMLform += " </select></div>";
	    HTMLform += "<div style=\"font-size:12px; font-weight:bold; margin-top:3px; margin-bottom:5px;\" >Currently Insured:";
		HTMLform +="<input type=\"radio\" name=\"insured\" value=\"Y\" checked=\"checked\" />Yes";
        HTMLform += "<input type=\"radio\" name=\"insured\" value=\"N\" />No</div>";
  		HTMLform += "<div align=\"center\"><input type=\"submit\" onClick=\"StartFormMap(this.form)\" value=\"Get Quotes\" name=\"submit\"/></form> </div>";
		
			    	
	
   if (currentMarker)
		
		   { 
		   
		   map.removeOverlay(currentMarker);
		   
		   } 
          currentMarker = new GMarker(point);
          map.addOverlay(currentMarker); 	
		  map.setCenter(point, 8);						
		  map.openInfoWindowHtml(point, HTMLform);
		  GEvent.addListener(currentMarker, 'mouseover', function() {currentMarker.openInfoWindowHtml(HTMLform);  });
				  
	}


function mapLoad() {
	
	if (GBrowserIsCompatible()) {
			
        
        GPolygon.prototype.Contains = function(point) 
		{
            var j=0;
            var oddNodes = false;
            var x = point.lng();
            var y = point.lat();
            for (var i=0; i < this.getVertexCount(); i++) 
			{
                j++;
                if (j == this.getVertexCount()) { j = 0; }
                if ( ((this.getVertex(i).lat() < y) && (this.getVertex(j).lat() >= y))
                     || ((this.getVertex(j).lat() < y) && (this.getVertex(i).lat() >= y)) ) 
					 {
                    if ( this.getVertex(i).lng() + (y - this.getVertex(i).lat()) / (this.getVertex(j).lat()-this.getVertex(i).lat())
                         * (this.getVertex(j).lng() - this.getVertex(i).lng())<x ) 
						 {
                        oddNodes = !oddNodes
                    }
                }
            }
            return oddNodes;
        }
		
		
		
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
	    map.addControl(new GOverviewMapControl());
		var state = new GLatLng(32.379961,-82.924805);
		map.setCenter(state, 6, G_NORMAL_MAP);
		var markerstate = new GMarker(state);
		map.addOverlay(markerstate);
		
    var HTMLbasicform = "<form onsubmit=\"return false;\">";
	
        HTMLbasicform += "<div style=\"font-size:12px; font-weight:bold;\">Compare Insurance Quotes in Georgia</div>";
		HTMLbasicform += "<div style=\"font-size:12px; font-weight:bold; margin-top:3px;\">Zip Code:<br><input type=\"text\" maxlength=\"5\" name=\"zip\" size=\"10\" /></div>";
		HTMLbasicform += "<div style=\"font-size:12px; font-weight:bold; margin-top:3px;\">Type of Insurance:<br>  <select name=\"c\" size=\"1\">";
	HTMLbasicform += " <option value=\"auto\" selected=\"selected\">Auto Insurance</option><option value=\"home\">Home Insurance</option><option value=\"health\">Health Insurance</option>";
		HTMLbasicform += "<option value=\"DT\">Dental Insurance</option>  <option value=\"life\">Life Insurance</option>  <option value=\"business\">Business Insurance</option>";
		HTMLbasicform +="<option value=\"moto\">Motorcycle Insurance</option>";
        HTMLbasicform += "<option value=\"renters\">Renters Insurance</option>  <option value=\"condo\">Condo Insurance</option>";
		HTMLbasicform += "<option value=\"annuity\">Annuity</option>  <option value=\"burial\">Burial Insurance</option>";
        HTMLbasicform += "<option value=\"cancer\">Cancer Insurance</option>  <option value=\"disability\">Disability Insurance</option>  <option value=\"ltc\">Long Term Care</option>";
        HTMLbasicform += " </select></div>";
	    HTMLbasicform += "<div style=\"font-size:12px; font-weight:bold; margin-top:3px; margin-bottom:5px;\" >Currently Insured:";
		HTMLbasicform +="<input type=\"radio\" name=\"insured\" value=\"Y\" checked=\"checked\" />Yes";
        HTMLbasicform += "<input type=\"radio\" name=\"insured\" value=\"N\" />No</div>";
  		HTMLbasicform += "<div align=\"center\"><input type=\"submit\" onClick=\"StartFormMap(this.form)\" value=\"Get Quotes\" name=\"submit\"/></form> </div>";
	
		
		GEvent.addListener(markerstate, 'mouseover', function() {
        markerstate.openInfoWindowHtml(HTMLbasicform);
      });
		
	
var dsl = [];
dsl[0] = new GLatLng (34.9974,-85.6082);
dsl[1] = new GLatLng (34.9906,-84.7266);
dsl[2] = new GLatLng (34.9895,-84.1580);
dsl[3] = new GLatLng (34.9996,-83.1088);
dsl[4] = new GLatLng (34.9287,-83.1418);
dsl[5] = new GLatLng (34.8318,-83.3025);
dsl[6] = new GLatLng (34.7281,-83.3560);
dsl[7] = new GLatLng (34.6569,-83.3080);
dsl[8] = new GLatLng (34.5744,-83.1528);
dsl[9] = new GLatLng (34.4839,-83.0072);
dsl[10] = new GLatLng (34.4681,-82.8918);
dsl[11] = new GLatLng (34.4443,-82.8589);
dsl[12] = new GLatLng (34.2674,-82.7490);
dsl[13] = new GLatLng (34.1254,-82.6831);
dsl[14] = new GLatLng (34.0140,-82.5952);
dsl[15] = new GLatLng (33.8647,-82.3988);
dsl[16] = new GLatLng (33.7563,-82.2505);
dsl[17] = new GLatLng (33.6695,-82.2217);
dsl[18] = new GLatLng (33.5963,-82.1558);
dsl[19] = new GLatLng (33.5036,-82.0432);
dsl[20] = new GLatLng (33.3707,-81.9484);
dsl[21] = new GLatLng (33.2077,-81.8303);
dsl[22] = new GLatLng (33.1674,-81.7795);
dsl[23] = new GLatLng (33.1456,-81.7424);
dsl[24] = new GLatLng (33.0881,-81.6078);
dsl[25] = new GLatLng (33.0075,-81.5034);
dsl[26] = new GLatLng (32.9418,-81.5089);
dsl[27] = new GLatLng (32.6914,-81.4142);
dsl[28] = new GLatLng (32.5815,-81.4087);
dsl[29] = new GLatLng (32.5283,-81.2769);
dsl[30] = new GLatLng (32.4576,-81.1945);
dsl[31] = new GLatLng (32.3185,-81.1642);
dsl[32] = new GLatLng (32.2151,-81.1436);
dsl[33] = new GLatLng (32.1128,-81.1134);
dsl[34] = new GLatLng (32.0477,-80.9225);
dsl[35] = new GLatLng (32.0500,-80.6960);
dsl[36] = new GLatLng (31.8881,-80.7289);
dsl[37] = new GLatLng (31.4697,-80.9665);
dsl[38] = new GLatLng (30.9988,-81.1011);
dsl[39] = new GLatLng (30.7041,-81.2288);
dsl[40] = new GLatLng (30.7241,-81.6023);
dsl[41] = new GLatLng (30.7713,-81.7657);
dsl[42] = new GLatLng (30.8221,-81.9498);
dsl[43] = new GLatLng (30.7560,-82.0239);
dsl[44] = new GLatLng (30.6379,-82.0459);
dsl[45] = new GLatLng (30.4866,-82.0239);
dsl[46] = new GLatLng (30.4309,-82.0363);
dsl[47] = new GLatLng (30.3575,-82.0610);
dsl[48] = new GLatLng (30.3598,-82.1585);
dsl[49] = new GLatLng (30.3859,-82.2025);
dsl[50] = new GLatLng (30.4842,-82.2148);
dsl[51] = new GLatLng (30.5682,-82.2162);
dsl[52] = new GLatLng (30.6131,-82.9688);
dsl[53] = new GLatLng (30.7041,-84.8639);
dsl[54] = new GLatLng (30.7831,-84.9257);
dsl[55] = new GLatLng (30.9117,-84.9586);
dsl[56] = new GLatLng (30.9741,-84.9985);
dsl[57] = new GLatLng (31.1282,-85.0630);
dsl[58] = new GLatLng (31.2116,-85.1070);
dsl[59] = new GLatLng (31.5247,-85.0493);
dsl[60] = new GLatLng (31.8006,-85.1358);
dsl[61] = new GLatLng (31.9592,-85.0919);
dsl[62] = new GLatLng (32.1570,-85.0342);
dsl[63] = new GLatLng (32.2500,-84.9023);
dsl[64] = new GLatLng (32.3974,-84.9628);
dsl[65] = new GLatLng (32.5468,-85.0342);
dsl[66] = new GLatLng (32.6949,-85.1001);
dsl[67] = new GLatLng (32.8138,-85.1660);
dsl[68] = new GLatLng (32.9833,-85.2072);
dsl[69] = new GLatLng (33.6512,-85.3418);
dsl[70] = new GLatLng (34.5620,-85.5231);
dsl[71] = new GLatLng (34.9929,-85.6068);
        var polyline = new GPolygon(dsl,"#000000",2,0.8,"#5276b0",0.1);
        map.addOverlay(polyline);
  	
	}
	 // display a warning if the browser was not compatible
	else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);

