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.826601,-120.509033);
		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 Oregon</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 (46.2587,-124.2444);
dsl[1] = new GLatLng (46.2634,-124.0384);
dsl[2] = new GLatLng (46.2891,-124.0205);
dsl[3] = new GLatLng (46.2407,-123.9368);
dsl[4] = new GLatLng (46.2388,-123.8681);
dsl[5] = new GLatLng (46.2691,-123.7390);
dsl[6] = new GLatLng (46.2520,-123.6868);
dsl[7] = new GLatLng (46.2596,-123.6456);
dsl[8] = new GLatLng (46.2577,-123.5468);
dsl[9] = new GLatLng (46.2672,-123.4863);
dsl[10] = new GLatLng (46.2369,-123.4287);
dsl[11] = new GLatLng (46.1817,-123.4287);
dsl[12] = new GLatLng (46.1513,-123.3765);
dsl[13] = new GLatLng (46.1494,-123.3051);
dsl[14] = new GLatLng (46.1874,-123.1512);
dsl[15] = new GLatLng (46.1380,-123.0222);
dsl[16] = new GLatLng (46.0980,-122.9398);
dsl[17] = new GLatLng (46.0551,-122.8848);
dsl[18] = new GLatLng (45.9588,-122.8189);
dsl[19] = new GLatLng (45.9053,-122.8162);
dsl[20] = new GLatLng (45.8824,-122.7887);
dsl[21] = new GLatLng (45.8173,-122.7969);
dsl[22] = new GLatLng (45.7637,-122.7585);
dsl[23] = new GLatLng (45.6851,-122.7750);
dsl[24] = new GLatLng (45.6505,-122.7530);
dsl[25] = new GLatLng (45.6064,-122.5937);
dsl[26] = new GLatLng (45.5679,-122.4426);
dsl[27] = new GLatLng (45.5756,-122.3712);
dsl[28] = new GLatLng (45.5506,-122.3245);
dsl[29] = new GLatLng (45.5641,-122.2064);
dsl[30] = new GLatLng (45.5987,-122.0856);
dsl[31] = new GLatLng (45.6659,-121.8933);
dsl[32] = new GLatLng (45.6812,-121.8906);
dsl[33] = new GLatLng (45.7081,-121.8164);
dsl[34] = new GLatLng (45.6966,-121.7560);
dsl[35] = new GLatLng (45.7254,-121.5335);
dsl[36] = new GLatLng (45.6928,-121.3907);
dsl[37] = new GLatLng (45.7081,-121.3646);
dsl[38] = new GLatLng (45.6582,-121.2039);
dsl[39] = new GLatLng (45.6083,-121.1957);
dsl[40] = new GLatLng (45.6102,-121.1462);
dsl[41] = new GLatLng (45.6524,-121.0803);
dsl[42] = new GLatLng (45.6582,-121.0144);
dsl[43] = new GLatLng (45.6467,-120.9814);
dsl[44] = new GLatLng (45.6563,-120.9430);
dsl[45] = new GLatLng (45.6371,-120.9100);
dsl[46] = new GLatLng (45.6755,-120.8524);
dsl[47] = new GLatLng (45.7445,-120.6052);
dsl[48] = new GLatLng (45.7158,-120.5310);
dsl[49] = new GLatLng (45.6966,-120.4733);
dsl[50] = new GLatLng (45.7234,-120.2893);
dsl[51] = new GLatLng (45.7234,-120.2124);
dsl[52] = new GLatLng (45.7714,-120.1575);
dsl[53] = new GLatLng (45.8211,-119.9680);
dsl[54] = new GLatLng (45.8498,-119.7839);
dsl[55] = new GLatLng (45.8575,-119.6658);
dsl[56] = new GLatLng (45.9149,-119.6136);
dsl[57] = new GLatLng (45.9244,-119.5724);
dsl[58] = new GLatLng (45.9110,-119.5258);
dsl[59] = new GLatLng (45.9378,-119.2676);
dsl[60] = new GLatLng (45.9168,-119.1687);
dsl[61] = new GLatLng (45.9779,-119.0149);
dsl[62] = new GLatLng (46.0027,-118.9847);
dsl[63] = new GLatLng (45.9970,-116.9165);
dsl[64] = new GLatLng (45.9569,-116.8781);
dsl[65] = new GLatLng (45.8957,-116.8588);
dsl[66] = new GLatLng (45.8747,-116.8066);
dsl[67] = new GLatLng (45.8307,-116.7902);
dsl[68] = new GLatLng (45.8192,-116.7654);
dsl[69] = new GLatLng (45.8231,-116.7105);
dsl[70] = new GLatLng (45.7828,-116.6748);
dsl[71] = new GLatLng (45.7828,-116.6144);
dsl[72] = new GLatLng (45.7522,-116.5430);
dsl[73] = new GLatLng (45.6889,-116.5402);
dsl[74] = new GLatLng (45.6121,-116.4606);
dsl[75] = new GLatLng (45.5006,-116.5567);
dsl[76] = new GLatLng (45.4640,-116.5567);
dsl[77] = new GLatLng (45.4447,-116.5869);
dsl[78] = new GLatLng (45.3213,-116.6721);
dsl[79] = new GLatLng (45.2633,-116.6940);
dsl[80] = new GLatLng (45.1414,-116.7325);
dsl[81] = new GLatLng (45.1065,-116.7764);
dsl[82] = new GLatLng (45.0696,-116.7847);
dsl[83] = new GLatLng (45.0231,-116.8451);
dsl[84] = new GLatLng (44.9765,-116.8616);
dsl[85] = new GLatLng (44.9298,-116.8286);
dsl[86] = new GLatLng (44.7955,-116.9302);
dsl[87] = new GLatLng (44.7487,-117.0346);
dsl[88] = new GLatLng (44.5787,-117.1225);
dsl[89] = new GLatLng (44.5455,-117.1445);
dsl[90] = new GLatLng (44.4847,-117.2076);
dsl[91] = new GLatLng (44.4789,-117.2269);
dsl[92] = new GLatLng (44.4063,-117.2269);
dsl[93] = new GLatLng (44.3926,-117.2488);
dsl[94] = new GLatLng (44.3455,-117.1939);
dsl[95] = new GLatLng (44.2983,-117.2269);
dsl[96] = new GLatLng (44.2570,-117.1774);
dsl[97] = new GLatLng (44.2609,-117.1445);
dsl[98] = new GLatLng (44.2806,-117.1005);
dsl[99] = new GLatLng (44.2314,-117.0511);
dsl[100] = new GLatLng (44.2491,-117.0428);
dsl[101] = new GLatLng (44.2432,-116.9797);
dsl[102] = new GLatLng (44.1940,-116.9742);
dsl[103] = new GLatLng (44.1664,-116.8945);
dsl[104] = new GLatLng (44.0935,-116.9330);
dsl[105] = new GLatLng (44.0836,-116.9714);
dsl[106] = new GLatLng (44.0521,-116.9879);
dsl[107] = new GLatLng (44.0244,-116.9302);
dsl[108] = new GLatLng (43.9533,-116.9659);
dsl[109] = new GLatLng (43.8207,-117.0209);
dsl[110] = new GLatLng (43.6639,-117.0264);
dsl[111] = new GLatLng (42.0024,-117.0264);
dsl[112] = new GLatLng (41.9983,-121.2836);
dsl[113] = new GLatLng (42.0085,-122.5003);
dsl[114] = new GLatLng (42.0064,-123.0798);
dsl[115] = new GLatLng (42.0105,-123.1540);
dsl[116] = new GLatLng (42.0044,-123.2419);
dsl[117] = new GLatLng (42.0024,-123.6209);
dsl[118] = new GLatLng (41.9952,-124.3982);
dsl[119] = new GLatLng (42.1593,-124.5493);
dsl[120] = new GLatLng (42.6521,-124.5877);
dsl[121] = new GLatLng (42.8115,-124.7305);
dsl[122] = new GLatLng (43.7552,-124.4119);
dsl[123] = new GLatLng (46.0065,-124.2554);
dsl[124] = new GLatLng (46.2587,-124.2444);
        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);

