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(43.22119,-71.526489);
		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 New Hampshire</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 (45.0153,-71.5045);
dsl[1] = new GLatLng (45.0532,-71.4977);
dsl[2] = new GLatLng (45.0910,-71.4619);
dsl[3] = new GLatLng (45.1520,-71.4276);
dsl[4] = new GLatLng (45.2043,-71.3960);
dsl[5] = new GLatLng (45.2362,-71.4455);
dsl[6] = new GLatLng (45.2372,-71.3878);
dsl[7] = new GLatLng (45.2710,-71.3617);
dsl[8] = new GLatLng (45.3029,-71.2834);
dsl[9] = new GLatLng (45.2507,-71.2299);
dsl[10] = new GLatLng (45.2391,-71.1818);
dsl[11] = new GLatLng (45.2478,-71.1584);
dsl[12] = new GLatLng (45.2469,-71.1337);
dsl[13] = new GLatLng (45.2846,-71.1145);
dsl[14] = new GLatLng (45.3058,-71.0843);
dsl[15] = new GLatLng (44.9230,-71.0527);
dsl[16] = new GLatLng (44.3592,-71.0143);
dsl[17] = new GLatLng (43.7364,-70.9840);
dsl[18] = new GLatLng (43.5704,-70.9731);
dsl[19] = new GLatLng (43.3621,-70.9813);
dsl[20] = new GLatLng (43.2942,-70.9030);
dsl[21] = new GLatLng (43.2272,-70.8083);
dsl[22] = new GLatLng (43.1411,-70.8261);
dsl[23] = new GLatLng (43.1000,-70.7671);
dsl[24] = new GLatLng (43.0498,-70.6970);
dsl[25] = new GLatLng (42.9222,-70.5583);
dsl[26] = new GLatLng (42.8760,-70.6146);
dsl[27] = new GLatLng (42.8599,-70.8467);
dsl[28] = new GLatLng (42.8860,-70.9195);
dsl[29] = new GLatLng (42.8105,-71.0609);
dsl[30] = new GLatLng (42.8125,-71.1420);
dsl[31] = new GLatLng (42.7904,-71.1873);
dsl[32] = new GLatLng (42.7349,-71.1832);
dsl[33] = new GLatLng (42.7450,-71.2463);
dsl[34] = new GLatLng (42.6986,-71.2972);
dsl[35] = new GLatLng (42.7077,-71.7421);
dsl[36] = new GLatLng (42.7268,-72.4590);
dsl[37] = new GLatLng (42.7621,-72.5098);
dsl[38] = new GLatLng (42.8105,-72.5414);
dsl[39] = new GLatLng (42.8609,-72.5592);
dsl[40] = new GLatLng (42.8981,-72.5317);
dsl[41] = new GLatLng (42.9524,-72.5400);
dsl[42] = new GLatLng (42.9745,-72.4919);
dsl[43] = new GLatLng (42.9906,-72.4658);
dsl[44] = new GLatLng (43.0127,-72.4480);
dsl[45] = new GLatLng (43.0287,-72.4713);
dsl[46] = new GLatLng (43.0508,-72.4713);
dsl[47] = new GLatLng (43.0819,-72.4356);
dsl[48] = new GLatLng (43.1040,-72.4452);
dsl[49] = new GLatLng (43.1180,-72.4342);
dsl[50] = new GLatLng (43.1501,-72.4590);
dsl[51] = new GLatLng (43.2052,-72.4329);
dsl[52] = new GLatLng (43.2562,-72.4384);
dsl[53] = new GLatLng (43.2812,-72.4095);
dsl[54] = new GLatLng (43.3741,-72.4150);
dsl[55] = new GLatLng (43.4988,-72.3807);
dsl[56] = new GLatLng (43.5127,-72.4013);
dsl[57] = new GLatLng (43.5834,-72.3779);
dsl[58] = new GLatLng (43.6420,-72.3134);
dsl[59] = new GLatLng (43.6758,-72.3024);
dsl[60] = new GLatLng (43.7036,-72.3079);
dsl[61] = new GLatLng (43.7344,-72.2694);
dsl[62] = new GLatLng (43.7741,-72.2035);
dsl[63] = new GLatLng (43.8187,-72.1870);
dsl[64] = new GLatLng (43.8672,-72.1843);
dsl[65] = new GLatLng (43.8890,-72.1568);
dsl[66] = new GLatLng (43.9117,-72.1321);
dsl[67] = new GLatLng (43.9454,-72.1198);
dsl[68] = new GLatLng (44.0017,-72.1170);
dsl[69] = new GLatLng (44.0856,-72.0470);
dsl[70] = new GLatLng (44.1093,-72.0552);
dsl[71] = new GLatLng (44.1428,-72.0387);
dsl[72] = new GLatLng (44.1763,-72.0580);
dsl[73] = new GLatLng (44.1999,-72.0703);
dsl[74] = new GLatLng (44.2511,-72.0525);
dsl[75] = new GLatLng (44.2796,-72.0607);
dsl[76] = new GLatLng (44.3199,-72.0291);
dsl[77] = new GLatLng (44.3376,-71.9783);
dsl[78] = new GLatLng (44.3592,-71.9289);
dsl[79] = new GLatLng (44.3366,-71.8712);
dsl[80] = new GLatLng (44.3572,-71.8135);
dsl[81] = new GLatLng (44.3877,-71.8108);
dsl[82] = new GLatLng (44.4014,-71.7778);
dsl[83] = new GLatLng (44.4122,-71.7380);
dsl[84] = new GLatLng (44.4416,-71.6597);
dsl[85] = new GLatLng (44.4642,-71.6542);
dsl[86] = new GLatLng (44.5073,-71.5855);
dsl[87] = new GLatLng (44.5357,-71.5746);
dsl[88] = new GLatLng (44.5601,-71.6048);
dsl[89] = new GLatLng (44.5680,-71.5897);
dsl[90] = new GLatLng (44.5680,-71.5649);
dsl[91] = new GLatLng (44.5914,-71.5485);
dsl[92] = new GLatLng (44.5993,-71.5649);
dsl[93] = new GLatLng (44.6315,-71.5540);
dsl[94] = new GLatLng (44.6608,-71.5924);
dsl[95] = new GLatLng (44.7243,-71.6130);
dsl[96] = new GLatLng (44.7526,-71.6364);
dsl[97] = new GLatLng (44.8198,-71.5746);
dsl[98] = new GLatLng (44.8325,-71.5526);
dsl[99] = new GLatLng (44.8539,-71.5553);
dsl[100] = new GLatLng (44.8744,-71.5279);
dsl[101] = new GLatLng (44.9123,-71.5018);
dsl[102] = new GLatLng (44.9366,-71.5182);
dsl[103] = new GLatLng (44.9697,-71.5237);
dsl[104] = new GLatLng (44.9862,-71.5443);
dsl[105] = new GLatLng (45.0017,-71.5237);
dsl[106] = new GLatLng (45.0163,-71.5045);
        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);

