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(42.431566,-71.40564);
		map.setCenter(state, 7, 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 Massachusetts</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 (42.0003,-72.7789);
dsl[1] = new GLatLng (42.0330,-72.7405);
dsl[2] = new GLatLng (42.0330,-72.3779);
dsl[3] = new GLatLng (42.0228,-71.7984);
dsl[4] = new GLatLng (42.0085,-71.8011);
dsl[5] = new GLatLng (42.0197,-71.3850);
dsl[6] = new GLatLng (41.8961,-71.3837);
dsl[7] = new GLatLng (41.8982,-71.3411);
dsl[8] = new GLatLng (41.8358,-71.3370);
dsl[9] = new GLatLng (41.8245,-71.3493);
dsl[10] = new GLatLng (41.7816,-71.3342);
dsl[11] = new GLatLng (41.7529,-71.2628);
dsl[12] = new GLatLng (41.6719,-71.1914);
dsl[13] = new GLatLng (41.6616,-71.1351);
dsl[14] = new GLatLng (41.6124,-71.1433);
dsl[15] = new GLatLng (41.5939,-71.1310);
dsl[16] = new GLatLng (41.4973,-71.1214);
dsl[17] = new GLatLng (41.3149,-71.0266);
dsl[18] = new GLatLng (41.1590,-70.8316);
dsl[19] = new GLatLng (41.1662,-69.9225);
dsl[20] = new GLatLng (41.3201,-69.7948);
dsl[21] = new GLatLng (41.8133,-69.7398);
dsl[22] = new GLatLng (42.1939,-70.0337);
dsl[23] = new GLatLng (42.2173,-70.5144);
dsl[24] = new GLatLng (42.4133,-70.6984);
dsl[25] = new GLatLng (42.6420,-70.3647);
dsl[26] = new GLatLng (42.8286,-70.4759);
dsl[27] = new GLatLng (42.8760,-70.6133);
dsl[28] = new GLatLng (42.8619,-70.8440);
dsl[29] = new GLatLng (42.8890,-70.9154);
dsl[30] = new GLatLng (42.8075,-71.0651);
dsl[31] = new GLatLng (42.8226,-71.1337);
dsl[32] = new GLatLng (42.7873,-71.1859);
dsl[33] = new GLatLng (42.7369,-71.1832);
dsl[34] = new GLatLng (42.7470,-71.2189);
dsl[35] = new GLatLng (42.7400,-71.2560);
dsl[36] = new GLatLng (42.6986,-71.2985);
dsl[37] = new GLatLng (42.7127,-71.9151);
dsl[38] = new GLatLng (42.7309,-72.5441);
dsl[39] = new GLatLng (42.7450,-73.2541);
dsl[40] = new GLatLng (42.7460,-73.2664);
dsl[41] = new GLatLng (42.5460,-73.3406);
dsl[42] = new GLatLng (42.2671,-73.4436);
dsl[43] = new GLatLng (42.1349,-73.4917);
dsl[44] = new GLatLng (42.0880,-73.5081);
dsl[45] = new GLatLng (42.0483,-73.4985);
dsl[46] = new GLatLng (42.0452,-73.1841);
dsl[47] = new GLatLng (42.0371,-72.8146);
dsl[48] = new GLatLng (41.9962,-72.8160);
dsl[49] = new GLatLng (42.0024,-72.7803);
        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);

