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(35.594786,-86.583252);
		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 Tennessee</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.6739,-88.0678);
dsl[1] = new GLatLng (36.6354,-87.8522);
dsl[2] = new GLatLng (36.6023,-83.6787);
dsl[3] = new GLatLng (36.5946,-81.9402);
dsl[4] = new GLatLng (36.6144,-81.9209);
dsl[5] = new GLatLng (36.6111,-81.6518);
dsl[6] = new GLatLng (36.3295,-81.7163);
dsl[7] = new GLatLng (36.3516,-81.7973);
dsl[8] = new GLatLng (36.2974,-81.9072);
dsl[9] = new GLatLng (36.1212,-82.0308);
dsl[10] = new GLatLng (36.1024,-82.1255);
dsl[11] = new GLatLng (36.1434,-82.1475);
dsl[12] = new GLatLng (36.1323,-82.2450);
dsl[13] = new GLatLng (36.1168,-82.3521);
dsl[14] = new GLatLng (36.0702,-82.4167);
dsl[15] = new GLatLng (35.9669,-82.5389);
dsl[16] = new GLatLng (35.9702,-82.6076);
dsl[17] = new GLatLng (36.0602,-82.6378);
dsl[18] = new GLatLng (35.9925,-82.7751);
dsl[19] = new GLatLng (35.9169,-82.8177);
dsl[20] = new GLatLng (35.8623,-82.9042);
dsl[21] = new GLatLng (35.7755,-83.0017);
dsl[22] = new GLatLng (35.7131,-83.2393);
dsl[23] = new GLatLng (35.5635,-83.4961);
dsl[24] = new GLatLng (35.5501,-83.6938);
dsl[25] = new GLatLng (35.5233,-83.8284);
dsl[26] = new GLatLng (35.5065,-83.8847);
dsl[27] = new GLatLng (35.4014,-84.0248);
dsl[28] = new GLatLng (35.2905,-84.0276);
dsl[29] = new GLatLng (35.2322,-84.1113);
dsl[30] = new GLatLng (35.2624,-84.2294);
dsl[31] = new GLatLng (35.2198,-84.2926);
dsl[32] = new GLatLng (34.9884,-84.3201);
dsl[33] = new GLatLng (34.9996,-90.3131);
dsl[34] = new GLatLng (35.1233,-90.2843);
dsl[35] = new GLatLng (35.1379,-90.1758);
dsl[36] = new GLatLng (35.1985,-90.1112);
dsl[37] = new GLatLng (35.2849,-90.1524);
dsl[38] = new GLatLng (35.4372,-90.1346);
dsl[39] = new GLatLng (35.5568,-90.0192);
dsl[40] = new GLatLng (35.7343,-89.9547);
dsl[41] = new GLatLng (35.8579,-89.7638);
dsl[42] = new GLatLng (35.9180,-89.6635);
dsl[43] = new GLatLng (35.9947,-89.7130);
dsl[44] = new GLatLng (36.0902,-89.6759);
dsl[45] = new GLatLng (36.1279,-89.5894);
dsl[46] = new GLatLng (36.1856,-89.6484);
dsl[47] = new GLatLng (36.2343,-89.7006);
dsl[48] = new GLatLng (36.2531,-89.5331);
dsl[49] = new GLatLng (36.2996,-89.6210);
dsl[50] = new GLatLng (36.3494,-89.5784);
dsl[51] = new GLatLng (36.3406,-89.5180);
dsl[52] = new GLatLng (36.4964,-89.5345);
dsl[53] = new GLatLng (36.5107,-89.3051);
dsl[54] = new GLatLng (36.4986,-88.1667);
dsl[55] = new GLatLng (36.4997,-88.0692);
dsl[56] = new GLatLng (36.6871,-88.0637);
        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);

