var geocoder;
var record;
var xmlhttp;
function show_geocode() 
{
	document.getElementById('geocode_wait').style.visibility="visible";
	document.getElementById("status").innerHTML  = '';
	record = null;
	geocoder = new GClientGeocoder();
	var id = document.getElementById('ui_region').value;
	var ci_root = '../../../admin.php?/';
	var searchUrl = ci_root + 'geocode/xml_addresses/' + id + '/' + Math.random();

	var to_google;
    GDownloadUrl(searchUrl, function(data, responseCode) {
    	if(responseCode == 200) {
			var view_xml = GXml.parse(data);
	     	record = view_xml.documentElement.getElementsByTagName('record');
			
			if(record.length == 0) 
			{
				var $msg = '0 Locations Encoded';
				document.getElementById("status").innerHTML  = $msg;
				document.getElementById('geocode_wait').style.visibility="hidden";
			}
			else get_coordinates(0);
		}
		else if(responseCode == -1) {
		    alert("Data request timed out. Please try again.");
		} 
		else { 
		    alert("Request resulted in error. The server may be unavailable.");
		}

    });
}

/*
| FUNCTION: get_coordinates	
*/
function get_coordinates(j) 
{
	for (var i = j; i < record.length; i++) 
	{
		region_id = record[i].getAttribute('region_id');
		location_id = record[i].getAttribute('location_id');
		address = record[i].getAttribute('address_1');
		city = record[i].getAttribute('city');
		state = record[i].getAttribute('state');
		postal_code = record[i].getAttribute('postal_code');
		to_google = address + ' ' + city + ' ' + state + ' ';
		to_google += postal_code;

    	geocoder.getLatLng(to_google, function(latlng) 
    	{
				if (!latlng) 
				{			 
					if (document.getElementById('fallback').value == 'postal') 
					{					 
											geocoder.getLatLng(postal_code, function(latlng_fallback) 
											{
												if (!latlng_fallback) 
												{		
														save_coordinates(parseInt(i) + 1, region_id, location_id, new GLatLng(80, 80));
												} 
												else 
												{
														save_coordinates(parseInt(i) + 1, region_id, location_id, latlng_fallback);
												}	
										});
					}
					else 
					{
						save_coordinates(parseInt(i) + 1, region_id, location_id, new GLatLng(80, 80));
					}	
				} 
				else 
				{
						save_coordinates(parseInt(i) + 1, region_id, location_id, latlng);
				}	
		});
		
		break;
	}
}

/*
// FUNCTION: searchLocations	
*/
function save_coordinates(i, region_id, location_id, latlng) 
{
	var save_url = '../../../admin.php?/geocode/save_coordinates/';
	save_url += region_id + '/' + location_id + '/';			
	save_url += latlng.lat() + '/' + latlng.lng() + '/' + i + '/' + Math.random();

		xmlhttp=null;
		if (window.XMLHttpRequest)
		  {// code for all new browsers
		  xmlhttp=new XMLHttpRequest();
		  }
		else if (window.ActiveXObject)
		  {// code for IE5 and IE6
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		  
		if (xmlhttp!=null)
		  {
		  xmlhttp.onreadystatechange=state_Change;
		  xmlhttp.open("POST",save_url,true);
		  xmlhttp.send(null);
		  }
		else
		  {
		  alert("Your browser does not support XMLHTTP.");
		  }	
}

function state_Change()
{
	if (xmlhttp.readyState==4)
	  {// 4 = "loaded"
	  if (xmlhttp.status==200)
		{// 200 = OK
			
			if(xmlhttp.responseText < record.length)
			{	document.getElementById("status").innerHTML  = xmlhttp.responseText;
				get_coordinates(xmlhttp.responseText);
			}
			else
			{
				document.getElementById("status").innerHTML  = 'Geocoding Complete';
				document.getElementById('geocode_wait').style.visibility="hidden";				
			}
		}
	}
}