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(37.453057,-85.297852);
		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 Kentucky</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 (36.4986,-89.5372);
dsl[1] = new GLatLng (36.5074,-89.3010);
dsl[2] = new GLatLng (36.5008,-88.6871);
dsl[3] = new GLatLng (36.4931,-88.0568);
dsl[4] = new GLatLng (36.6695,-88.0692);
dsl[5] = new GLatLng (36.6343,-87.8535);
dsl[6] = new GLatLng (36.6265,-86.5654);
dsl[7] = new GLatLng (36.5979,-83.6375);
dsl[8] = new GLatLng (36.6860,-83.3423);
dsl[9] = new GLatLng (36.7466,-83.1377);
dsl[10] = new GLatLng (36.9762,-82.8589);
dsl[11] = new GLatLng (37.2894,-82.3192);
dsl[12] = new GLatLng (37.4934,-82.0308);
dsl[13] = new GLatLng (37.6653,-82.2121);
dsl[14] = new GLatLng (37.8618,-82.4016);
dsl[15] = new GLatLng (37.9908,-82.5073);
dsl[16] = new GLatLng (38.1778,-82.6392);
dsl[17] = new GLatLng (38.3761,-82.5952);
dsl[18] = new GLatLng (38.5030,-82.7477);
dsl[19] = new GLatLng (38.5825,-82.8369);
dsl[20] = new GLatLng (38.7316,-82.9015);
dsl[21] = new GLatLng (38.7027,-83.0196);
dsl[22] = new GLatLng (38.6190,-83.1418);
dsl[23] = new GLatLng (38.5986,-83.2819);
dsl[24] = new GLatLng (38.6941,-83.5291);
dsl[25] = new GLatLng (38.6351,-83.6595);
dsl[26] = new GLatLng (38.7487,-83.8930);
dsl[27] = new GLatLng (38.7701,-84.0440);
dsl[28] = new GLatLng (38.8119,-84.2184);
dsl[29] = new GLatLng (38.9872,-84.3228);
dsl[30] = new GLatLng (39.1013,-84.4917);
dsl[31] = new GLatLng (39.1183,-84.6277);
dsl[32] = new GLatLng (39.1439,-84.7554);
dsl[33] = new GLatLng (39.0523,-84.8914);
dsl[34] = new GLatLng (38.9263,-84.8735);
dsl[35] = new GLatLng (38.7894,-84.8131);
dsl[36] = new GLatLng (38.7691,-84.9957);
dsl[37] = new GLatLng (38.6866,-85.1921);
dsl[38] = new GLatLng (38.7209,-85.4407);
dsl[39] = new GLatLng (38.5653,-85.4077);
dsl[40] = new GLatLng (38.4461,-85.5972);
dsl[41] = new GLatLng (38.2748,-85.7455);
dsl[42] = new GLatLng (38.2716,-85.8087);
dsl[43] = new GLatLng (38.2069,-85.8650);
dsl[44] = new GLatLng (38.0286,-85.9323);
dsl[45] = new GLatLng (37.9550,-86.0422);
dsl[46] = new GLatLng (38.0135,-86.1108);
dsl[47] = new GLatLng (38.0643,-86.2756);
dsl[48] = new GLatLng (38.1389,-86.2770);
dsl[49] = new GLatLng (38.1864,-86.3690);
dsl[50] = new GLatLng (38.0308,-86.5283);
dsl[51] = new GLatLng (37.9204,-86.5874);
dsl[52] = new GLatLng (37.8423,-86.6423);
dsl[53] = new GLatLng (37.9041,-86.6547);
dsl[54] = new GLatLng (37.9864,-86.8250);
dsl[55] = new GLatLng (37.9095,-87.0406);
dsl[56] = new GLatLng (37.7935,-87.1381);
dsl[57] = new GLatLng (37.9420,-87.4168);
dsl[58] = new GLatLng (37.9745,-87.5858);
dsl[59] = new GLatLng (37.8749,-87.6929);
dsl[60] = new GLatLng (37.9215,-87.8906);
dsl[61] = new GLatLng (37.7761,-87.9552);
dsl[62] = new GLatLng (37.7903,-88.0307);
dsl[63] = new GLatLng (37.6479,-88.1584);
dsl[64] = new GLatLng (37.5097,-88.0664);
dsl[65] = new GLatLng (37.4193,-88.4180);
dsl[66] = new GLatLng (37.2784,-88.5086);
dsl[67] = new GLatLng (37.1428,-88.4248);
dsl[68] = new GLatLng (37.0738,-88.5059);
dsl[69] = new GLatLng (37.1461,-88.7421);
dsl[70] = new GLatLng (37.2249,-88.9522);
dsl[71] = new GLatLng (37.1406,-89.0964);
dsl[72] = new GLatLng (37.0278,-89.1815);
dsl[73] = new GLatLng (36.9488,-89.1032);
dsl[74] = new GLatLng (36.8214,-89.1733);
dsl[75] = new GLatLng (36.7411,-89.1925);
dsl[76] = new GLatLng (36.6265,-89.2007);
dsl[77] = new GLatLng (36.5449,-89.2529);
dsl[78] = new GLatLng (36.6232,-89.3518);
dsl[79] = new GLatLng (36.4986,-89.5345);
        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);

