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(44.370987,-85.550537);
		map.setCenter(state, 5, 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 Michigan</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 (48.3033,-88.3713);
dsl[1] = new GLatLng (48.0101,-87.6050);
dsl[2] = new GLatLng (46.8902,-84.8584);
dsl[3] = new GLatLng (46.6362,-84.7650);
dsl[4] = new GLatLng (46.4606,-84.5563);
dsl[5] = new GLatLng (46.4525,-84.4780);
dsl[6] = new GLatLng (46.4894,-84.4450);
dsl[7] = new GLatLng (46.5008,-84.4203);
dsl[8] = new GLatLng (46.4989,-84.3956);
dsl[9] = new GLatLng (46.5093,-84.3750);
dsl[10] = new GLatLng (46.5069,-84.3386);
dsl[11] = new GLatLng (46.4927,-84.2905);
dsl[12] = new GLatLng (46.4951,-84.2651);
dsl[13] = new GLatLng (46.5343,-84.2253);
dsl[14] = new GLatLng (46.5404,-84.1951);
dsl[15] = new GLatLng (46.5272,-84.1779);
dsl[16] = new GLatLng (46.5348,-84.1347);
dsl[17] = new GLatLng (46.5041,-84.1113);
dsl[18] = new GLatLng (46.4189,-84.1457);
dsl[19] = new GLatLng (46.3720,-84.1395);
dsl[20] = new GLatLng (46.3218,-84.1058);
dsl[21] = new GLatLng (46.3147,-84.1203);
dsl[22] = new GLatLng (46.2672,-84.1148);
dsl[23] = new GLatLng (46.2563,-84.0969);
dsl[24] = new GLatLng (46.2411,-84.1093);
dsl[25] = new GLatLng (46.2098,-84.0859);
dsl[26] = new GLatLng (46.1879,-84.0777);
dsl[27] = new GLatLng (46.1508,-84.0097);
dsl[28] = new GLatLng (46.1180,-84.0070);
dsl[29] = new GLatLng (46.1018,-83.9761);
dsl[30] = new GLatLng (46.0570,-83.9555);
dsl[31] = new GLatLng (46.0604,-83.9040);
dsl[32] = new GLatLng (46.1185,-83.8264);
dsl[33] = new GLatLng (46.1028,-83.7598);
dsl[34] = new GLatLng (46.1218,-83.6547);
dsl[35] = new GLatLng (46.1056,-83.5723);
dsl[36] = new GLatLng (45.9993,-83.4343);
dsl[37] = new GLatLng (45.8211,-83.5977);
dsl[38] = new GLatLng (45.3396,-82.5197);
dsl[39] = new GLatLng (43.5918,-82.1221);
dsl[40] = new GLatLng (43.0112,-82.4119);
dsl[41] = new GLatLng (42.9956,-82.4249);
dsl[42] = new GLatLng (42.9579,-82.4236);
dsl[43] = new GLatLng (42.9021,-82.4648);
dsl[44] = new GLatLng (42.8543,-82.4689);
dsl[45] = new GLatLng (42.8100,-82.4826);
dsl[46] = new GLatLng (42.7863,-82.4723);
dsl[47] = new GLatLng (42.7339,-82.4847);
dsl[48] = new GLatLng (42.6855,-82.5032);
dsl[49] = new GLatLng (42.6380,-82.5108);
dsl[50] = new GLatLng (42.6036,-82.5307);
dsl[51] = new GLatLng (42.5672,-82.5774);
dsl[52] = new GLatLng (42.5490,-82.5993);
dsl[53] = new GLatLng (42.5521,-82.6501);
dsl[54] = new GLatLng (42.5354,-82.6680);
dsl[55] = new GLatLng (42.4746,-82.7257);
dsl[56] = new GLatLng (42.4726,-82.7250);
dsl[57] = new GLatLng (42.3738,-82.8280);
dsl[58] = new GLatLng (42.3469,-82.9440);
dsl[59] = new GLatLng (42.3382,-82.9550);
dsl[60] = new GLatLng (42.3098,-83.0779);
dsl[61] = new GLatLng (42.2392,-83.1294);
dsl[62] = new GLatLng (42.1741,-83.1342);
dsl[63] = new GLatLng (42.1267,-83.1212);
dsl[64] = new GLatLng (42.0411,-83.1493);
dsl[65] = new GLatLng (41.9600,-83.1116);
dsl[66] = new GLatLng (41.7344,-83.4164);
dsl[67] = new GLatLng (41.7211,-83.8724);
dsl[68] = new GLatLng (41.7057,-84.3736);
dsl[69] = new GLatLng (41.6965,-84.8062);
dsl[70] = new GLatLng (41.7611,-84.8076);
dsl[71] = new GLatLng (41.7621,-87.2067);
dsl[72] = new GLatLng (42.4934,-87.0241);
dsl[73] = new GLatLng (43.3771,-87.1477);
dsl[74] = new GLatLng (43.7056,-87.1216);
dsl[75] = new GLatLng (43.9958,-87.0474);
dsl[76] = new GLatLng (44.1674,-86.9939);
dsl[77] = new GLatLng (44.4720,-86.8662);
dsl[78] = new GLatLng (44.8841,-86.6849);
dsl[79] = new GLatLng (45.0813,-86.5009);
dsl[80] = new GLatLng (45.2353,-86.2495);
dsl[81] = new GLatLng (45.4438,-86.7563);
dsl[82] = new GLatLng (45.4438,-87.0996);
dsl[83] = new GLatLng (45.3772,-87.1518);
dsl[84] = new GLatLng (45.3502,-87.1710);
dsl[85] = new GLatLng (45.2401,-87.3166);
dsl[86] = new GLatLng (45.2024,-87.4059);
dsl[87] = new GLatLng (45.0774,-87.4416);
dsl[88] = new GLatLng (45.0910,-87.5912);
dsl[89] = new GLatLng (45.1036,-87.6407);
dsl[90] = new GLatLng (45.2207,-87.6970);
dsl[91] = new GLatLng (45.3367,-87.6476);
dsl[92] = new GLatLng (45.3878,-87.6984);
dsl[93] = new GLatLng (45.3425,-87.8494);
dsl[94] = new GLatLng (45.5025,-87.7959);
dsl[95] = new GLatLng (45.6726,-87.7890);
dsl[96] = new GLatLng (45.7570,-87.9318);
dsl[97] = new GLatLng (45.7953,-87.9922);
dsl[98] = new GLatLng (45.8058,-88.1186);
dsl[99] = new GLatLng (45.8585,-88.0870);
dsl[100] = new GLatLng (45.9531,-88.1955);
dsl[101] = new GLatLng (45.9722,-88.3438);
dsl[102] = new GLatLng (45.9836,-88.3891);
dsl[103] = new GLatLng (46.0113,-88.5457);
dsl[104] = new GLatLng (45.9970,-88.7022);
dsl[105] = new GLatLng (46.0227,-88.8135);
dsl[106] = new GLatLng (46.0418,-88.8547);
dsl[107] = new GLatLng (46.1408,-89.0936);
dsl[108] = new GLatLng (46.3384,-90.1222);
dsl[109] = new GLatLng (46.5692,-90.4175);
dsl[110] = new GLatLng (46.9034,-90.2019);
dsl[111] = new GLatLng (47.2913,-89.9547);
dsl[112] = new GLatLng (48.0129,-89.4946);
dsl[113] = new GLatLng (47.9743,-89.3381);
dsl[114] = new GLatLng (48.2448,-88.6761);
dsl[115] = new GLatLng (48.3042,-88.3726);
        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);

