/* Ajax Functionality */
function AjaxAPI(divID,divURL,AjaxCallback,parseMethod,parseParams)
{
	/* Use Get or Post method */
	var AjaxMethod = (parseMethod == 'POST') ? 'POST' : 'GET';
	var AjaxParams = (parseParams !== undefined) ? parseParams : null;

	// Check if a custom callback was used
	if(!AjaxCallback)
	{
		// Create the standard callback function
		var AjaxCallback = {
			// Success function
			success: function(o) {
				if(o.responseText !== undefined && divID !== null)
				{	
					// Load the contents into the div
					document.getElementById(divID).innerHTML = o.responseText;
				}},
			// Failure function
			failure: function(o) { }
		};
	}
	
	YAHOO.util.Connect.asyncRequest(AjaxMethod, divURL, AjaxCallback, AjaxParams);
}

/* User: Get Coordinates */
function userCoordinates(form)
{
	/* Declare Variable */
	var userURL,userGeoCode,returnStatus;
	
	returnStatus = false;
	errors = false;
	
	/* Verify user information is not empty */
	if (form.user_fullname.value == "" && errors === false) { alert("Please tell us your name."); form.user_fullname.focus(); errors = true; return (false); }
	if (form.user_type.value == "" && errors === false) { alert("Please tell us about yourself."); form.user_type.focus(); errors = true; return (false); }
	if (form.user_address.value == "" && errors === false) { alert("Please enter the street address."); form.user_address.focus(); errors = true; return (false); }
	if (form.user_city.value == "" && errors === false) { alert("Please enter the city."); form.user_city.focus(); errors = true; return (false); }
	if (form.user_state.value == "" && errors === false) { alert("Please select the state."); form.user_state.focus(); errors = true; return (false); }
	if (form.user_zipcode.value == "" && errors === false) { alert("Please enter the zip code."); form.user_zipcode.focus(); errors = true; return (false); }
	if (form.user_country.value == "" && errors === false) { alert("Please enter the country."); form.user_country.focus(); errors = true; return (false); }

	/* Show the loading image */
	document.getElementById("sync-loading").style.display = 'inline';

	/* Send the address to the server */
	userURL = 'includes/geocode.php?';
	userURL = userURL + 'address=' + escape(form.user_address.value);
	userURL = userURL + '&city=' + escape(form.user_city.value);
	userURL = userURL + '&state=' + escape(form.user_state.value);
	userURL = userURL + '&zipcode=' + escape(form.user_zipcode.value);
	userURL = userURL + '&country=' + escape(form.user_country.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 */
			userGeoCode = new GLatLng(json.latitude,json.longitude);
			
			/* Update the text box fields */
			document.getElementById("user_latitude").value = json.latitude;
			document.getElementById("user_longitude").value = json.longitude;
			
			/* Submit the form */
			document.getElementById("user_form").submit();
			return(true);
		
		} else {
		
			/* Let the user know we found no results */		
			alert("No latitude / longitude found for this address.  Please correct it and try again.");
			return (false);
		}
		
		/* Hide the loading image */
		document.getElementById("sync-loading").style.display = 'none';
	}
	
	/* Run the lookup command */
	//if(errors === false) {
	//	AjaxAPI(null,userURL,{success: geocodeHandler});
	//} else {
		/* Hide the loading image */
	//	document.getElementById("sync-loading").style.display = 'none';
	//	return (false);
	//}
	
	return (true);
}
