/* Listing: Get Coordinates */
function listingCoordinates()
{
	/* Declare Variable */
	var listingURL,listingGeoCode;
	
	/* Verify listing address is not empty */
	if (document.getElementById("listing_address").value == "") { alert("Please enter the street address."); document.getElementById("listing_address").focus(); return (false); }
	if (document.getElementById("listing_city").value == "") { alert("Please enter the city."); document.getElementById("listing_city").focus(); return (false); }
	if (document.getElementById("listing_state").value == "") { alert("Please select the state."); document.getElementById("listing_state").focus(); return (false); }
	if (document.getElementById("listing_zipcode").value == "") { alert("Please enter the zip code."); document.getElementById("listing_zipcode").focus(); return (false); }

	/* Show the loading image */
	document.getElementById("sync-loading").style.display = 'inline';

	/* Send the address to the server */
	listingURL = 'index.php?s=api&a=agents.listing.geocode&domain=' + location.hostname;
	listingURL = listingURL + '&address=' + escape(document.getElementById("listing_address").value);
	listingURL = listingURL + '&city=' + escape(document.getElementById("listing_city").value);
	listingURL = listingURL + '&state=' + escape(document.getElementById("listing_state").value);
	listingURL = listingURL + '&zipcode=' + escape(document.getElementById("listing_zipcode").value);
	
	/* Parse the response from the server */
	var geocodeHandler = function(t)
	{
		/* Get the JSON header results */
		json = eval(t.responseText);
		
		/* Determine if we found a match */
		if(json.accuracy >= 1)
		{
			/* Calculate the new coordinates */
			listingGeoCode = new GLatLng(json.latitude,json.longitude);
			
			/* Figure out the correct zoom */
			var mapZoom = 13;
			
			/* Move the map accordingly */
			moveMarker(listingGeoCode, false);
			map.setCenter(listingGeoCode, mapZoom);
			
			/* Update the text box fields */
			document.getElementById("listing_latitude").value = json.latitude;
			document.getElementById("listing_longitude").value = json.longitude;
		
		} else {
		
			/* Let the user know we found no results */		
			alert("No latitude / longitude found for this address.  Please correct it and try again or search for it manually.");
		}
		
		/* Hide the loading image */
		document.getElementById("sync-loading").style.display = 'none';
	}
	
	/* Run the lookup command */
	AjaxAPI(null,listingURL,{success: geocodeHandler});
}

/* Delete the listing */
function confirmListingDelete(listingID,listingMD5)
{
	if (confirm("Do you want to delete listing # " + listingID + "?")) {
		window.location.href='/index.php?s=api&a=agents.listing.delete&id=' + listingMD5;
		return true;
	} else {
		return false;
	}
} 

/* Listings: Update Screen */
function listingImageDelete(listingID,imageID,imageType)
{
	if(imageType == "photo"){ 
		confirmTitle = "Photo"; 
	} else if (imageType == "floorplan") { 
		confirmTitle = "Floor Plan";
	}

	/* Make sure they want to delete it */
	confirmDelete = confirm('Delete ' + confirmTitle + ' #' + imageID + '?');
	
	// Process the deletion */
	if (confirmDelete == true)
	{ 
		AjaxAPI(null,'index.php?s=api&a=agents.listing.delete.image&listing=' + listingID + '&image=' + imageID + '&type=' + imageType);
		
		/* Update the appropriate div layer */
		imageUpload = imageType + "_" + imageID + "_upload";
		imagePreview = imageType + "_" + imageID + "_preview";
		imageText = imageType + "_" + imageID + "_caption";
		
		/* Relace the HTML */
		document.getElementById(imagePreview).style.display = 'none';
		document.getElementById(imageUpload).style.display = 'block';
		document.getElementById(imageText).value = '';
	}
}