var RegionOptions = new Array(17);
RegionOptions[0] = "This address is in our Oakdale service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsOakdale.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInStanislaus.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[1] = "We're sorry, but Gilton Solid Waste is not presently authorized to service this address. If you feel you have gotten this message in error please contact <a href='mailto:admin@gilton.com'>Gilton Solid Waste here</a>.";

RegionOptions[2] = "This address is in our Modesto service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsModesto.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInStanislaus.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[3] = "This address is in our Riverbank service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsRiverbank.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInStanislaus.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[4] = "This address is in our Escalon service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsEscalon.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInSanJoaquin.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[5] = "This address is in our Livingston service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsLivingston.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInMerced.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[6] = "This address is in our Livingston service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsLivingston.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInMerced.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[7] = "This address is in our Ripon service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsRipon.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInSanJoaquin.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[8] = "This address is in our Gustine service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsGustine.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInMerced.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[9] = "This address is in our Stanislaus County service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsStanislausCounty.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInStanislaus.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[10] = "This address is in our Stanislaus County service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsStanislausCounty.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInStanislaus.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[11] = "This address is in our Stanislaus County service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsStanislausCounty.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInStanislaus.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[12] = "This address is in our Stanislaus County service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsStanislausCounty.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInStanislaus.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[13] = "This address is in our San Joaquin County service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsSanJoaquin.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInSanJoaquin.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[14] = "We're sorry, but Gilton Solid Waste is not presently authorized to service the city of Los Banos. Please follow this <a href='http:\/\/www.losbanos.org\/'>link</a> to their web site for more information, or if you feel you have gotten this message in error you can contact Gilton Solid Waste <a href='mailto:admin@gilton.com'>here</a>.";

RegionOptions[15] = "We do not offer residential service in Dos Palos.<br \/><br \/>For commercial and industrial needs please go to our <a href='http:\/\/www.gilton.com\/Gilton_Resource_Recovery.php'>Resource Recovery page.<\/a>";

RegionOptions[16] = "This address is in our Merced County service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsMercedCounty.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInMerced.php'>Disposal/Recycling Alternatives<\/a>";

RegionOptions[17] = "This address is in our Merced County service area, where we offer a variety of services. Please choose from one of the options below, or <a href='http:\/\/www.gilton.com'>click here<\/a> to return to our home page.<br \/><a href='HTTP:\/\/www.Gilton.com\/ServiceOptionsMercedCounty.php'>Service Options<\/a><br \/><a href='http://www.Gilton.com/HowDoIDisposeOfInMerced.php'>Disposal/Recycling Alternatives<\/a>";

var RegionNames = new Array(18);
RegionNames[0] = "ServiceOptionsOakdale.php";
RegionNames[1] = "ServiceOptionsSylvanCoffee.php";
RegionNames[2] = "ServiceOptionsModesto.php";
RegionNames[3] = "ServiceOptionsRiverbank.php";
RegionNames[4] = "ServiceOptionsEscalon.php";
RegionNames[5] = "ServiceOptionsLivingston.php";
RegionNames[6] = "ServiceOptionsLivingston.php";
RegionNames[7] = "ServiceOptionsRipon.php";
RegionNames[8] = "ServiceOptionsGustine.php";
RegionNames[9] = "ServiceOptionsStanislausCounty.php";
RegionNames[10] = "ServiceOptionsStanislausCounty.php";
RegionNames[11] = "ServiceOptionsStanislausCounty.php";
RegionNames[12] = "ServiceOptionsStanislausCounty.php";
RegionNames[13] = "ServiceOptionsSanJoaquin.php";
RegionNames[14] = "ServiceOptionsLosBanos.php";
RegionNames[15] = "ServiceOptionsDosPalos.php";
RegionNames[16] = "ServiceOptionsMercedCounty.php";
RegionNames[17] = "ServiceOptionsMercedCounty.php";
			
var arrayArrayLatitude = new Array(18);
arrayArrayLatitude[0] = OakdaleLatitude;
arrayArrayLatitude[1] = SylvanCoffeeLatitude;
arrayArrayLatitude[2] = ModestoLatitude;
arrayArrayLatitude[3] = RiverbankLatitude;
arrayArrayLatitude[4] = EscalonLatitude;
arrayArrayLatitude[5] = LivingstonWestLatitude;
arrayArrayLatitude[6] = LivingstonLatitude;
arrayArrayLatitude[7] = RiponLatitude;
arrayArrayLatitude[8] = GustineLatitude;
arrayArrayLatitude[9] = StanisAreaOneLatitude;
arrayArrayLatitude[10] = StanisAreaM2ATwoLatitude;
arrayArrayLatitude[11] = StanisAreaM2ALatitude;
arrayArrayLatitude[12] = StanisAreaTwoLatitude;
arrayArrayLatitude[13] = SanJoaquinLatitude;
arrayArrayLatitude[14] = LosBanosLatitude;
arrayArrayLatitude[15] = DosPalosLatitude;
arrayArrayLatitude[16] = MercedSixLatitude;
arrayArrayLatitude[17] = MercedSevenLatitude;
			

var arrayArrayLongitude = new Array(18);
arrayArrayLongitude[0] = OakdaleLongitude;
arrayArrayLongitude[1] = SylvanCoffeeLongitude;
arrayArrayLongitude[2] = ModestoLongitude;
arrayArrayLongitude[3] = RiverbankLongitude;
arrayArrayLongitude[4] = EscalonLongitude;
arrayArrayLongitude[5] = LivingstonWestLongitude;
arrayArrayLongitude[6] = LivingstonLongitude;
arrayArrayLongitude[7] = RiponLongitude;
arrayArrayLongitude[8] = GustineLongitude;
arrayArrayLongitude[9] = StanisAreaOneLongitude;
arrayArrayLongitude[10] = StanisAreaM2ATwoLongitude;
arrayArrayLongitude[11] = StanisAreaM2ALongitude;
arrayArrayLongitude[12] = StanisAreaTwoLongitude;
arrayArrayLongitude[13] = SanJoaquinLongitude;
arrayArrayLongitude[14] = LosBanosLongitude;
arrayArrayLongitude[15] = DosPalosLongitude;
arrayArrayLongitude[16] = MercedSixLongitude;
arrayArrayLongitude[17] = MercedSevenLongitude;
			
var map = null;
var geocoder = null;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.614231, -120.574951), 6);
        map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
      }
    }

function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(
		address,
		function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 15);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml('<div id="confirm" style="width: 210px; height: 180px">' + address + '<br /><br />Is this location correct?<br /><br /><button type="button" name="YesButton" onclick="showLocation();" >Yes</button><button type="button" name="NoButton" onclick="responseNo();">No</button></div>');
              document.getElementById("message").innerHTML = "";
            }
          }
        );
      }
    }
    
function addressFocus() {
    
	document.getElementById("address").value = "";
    
    };
			
var latlngvar;
var checkvar = 0;
var address;
			
function showLocation() {
	address = document.forms[0].address.value;
	geocoder.getLatLng(address, convertPoints);
}
function convertPoints(response) {
	allPoints(response.lat(), response.lng());
}
		//Method to test if address is close to perimeter of service area to account for inaccuracies of maps geocode function
			
function testPoints(latitudeArray, longitudeArray, latitudePoint, longitudePoint) {

		checkvar = 0;
		
		latitudePoint = parseFloat(latitudePoint);
		
		longitudePoint = parseFloat(longitudePoint);
		
		latitudePoint += .0001;
		
		longitudePoint += .0001;

	if(pnpoly(latitudeArray, longitudeArray, latitudePoint, longitudePoint) == false) {
		checkvar += 1;
		}//pluslat pluslng
		
		longitudePoint -= .0002;
		
	if(pnpoly(latitudeArray, longitudeArray, latitudePoint, longitudePoint) == false) {
		checkvar += 1;
		}//pluslat minuslng

		latitudePoint -= .0002;
		
		longitudePoint += .0002;
		
	if(pnpoly(latitudeArray, longitudeArray, latitudePoint, longitudePoint) == false) {
		checkvar += 1;
		}//minuslat pluslng
		
		longitudePoint -= .0002;
		
	if(pnpoly(latitudeArray, longitudeArray, latitudePoint, longitudePoint) == false) {
		checkvar += 1;
		}//minuslat minuslng
		
		return checkvar;
		
		};
		
//sets response to Service Area Lookup and sets cookies "LocationName" and "Region""
var LocationVar;
var LocationHolderVar;
var RegionNameForCookie = "None";
function allPoints(latitudePoint, longitudePoint) {
	for(latlngvar = 0; latlngvar < 18; latlngvar++) {
		switch(testPoints(arrayArrayLatitude[latlngvar], arrayArrayLongitude[latlngvar], latitudePoint, longitudePoint))
		{
		case 0:
		break;
		document.getElementById("confirm").innerHTML = "Sorry, we are unable to confirm service at that address. Please <a href='mailto:admin@gilton.com'>email us<\/a> or call us at 209-527-3781.";
		latlngvar = 18;
		break;
		case 2:
		document.getElementById("confirm").innerHTML = "Sorry, we are unable to confirm service at that address. Please <a href='mailto:admin@gilton.com'>email us<\/a> or call us at 209-527-3781.";
		latlngvar = 18;
		break;
		case 3:
		document.getElementById("confirm").innerHTML = "Sorry, we are unable to confirm service at that address. Please <a href='mailto:admin@gilton.com'>email us<\/a> or call us at 209-527-3781.";
		latlngvar = 18;
		break;
		case 4:
		if(location.href=="http://gilton.com/rentabin.php") {
		addRentABin(RegionNames[latlngvar]);
		} else {
		document.getElementById("confirm").innerHTML = RegionOptions[latlngvar];}
		RegionNameForCookie = RegionNames[latlngvar];
		LocationVar = document.forms[0].address.value;
		var expireDate = new Date();
		var LocationHolderVar = LocationVar
		expireDate.setMonth(expireDate.getMonth()+6);
		document.cookie = "LocationName=" + encodeURIComponent(LocationVar) +";expires=" + expireDate.toGMTString(),'/';
		document.cookie = "Region=" + encodeURIComponent(RegionNameForCookie) +";expires=" + expireDate.toGMTString(),'/';
		latlngvar = 18;
		break;
		};
		
	};
	
if(latlngvar == 18) { 

document.getElementById("confirm").innerHTML = 'Although we do not provide routine residential and commercial waste collection services in this area, Gilton Solid Waste may be able to provide services related to construction, demolition or industrial wastes.  Please <a href="mailto:admin@gilton.com">email us</a> and describe your waste management needs, or call us at (209) 527-3781.';
	var expireDate = new Date();
	var LocationHolderVar = LocationVar
	expireDate.setMonth(expireDate.getMonth()+6);
	document.cookie = "LocationName=No Service Area";"expires=" + expireDate.toGMTString(),'/';
	document.cookie = "Region=No Service Region";"expires=" + expireDate.toGMTString(),'/';


}
		
};
		//method of testing to see if point is in polygon. Thanks to Randolph Franklin for this bit o math. Also thanks to Fotiman for making it work in javascript.
				
		function pnpoly(xp, yp, x, y) { 
	var i, j = 0, c = 1, npol = xp.length; 
	for (i = 0, j = npol-1; i < npol; j = i++) { 
  if ((((yp[i] <= y) && (y < yp[j])) || ((yp[j] <= y) && (y < yp[i]))) && (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])) { c=!c;}} return c;};
  
  // response to user if they choose "No" to the "Is this the right address" question
 function responseNo(){
  document.getElementById("confirm").innerHTML = 'Please start over and type in the correct address in the space provided above. Or, if you feel you have received this message in error, <a href="mailto:admin@gilton.com">email</a> or phone us at (209) 527-3781.';
  }