
function fpA(val) {
	var $v = (!val ? $('#locale').val() : val);
	try {
		var node = new geoNode();
	} catch(e) {
		presentError('Cannot connect to Google AJAX API to fetch latitude and longitude information.');		
		return;
	}
	node.returnHandler = function(result){
		$('#stepper').html('Step 2');
		$('#make-an-appointment').animate({height:'350px'});
		$.post('includes/locator.php',{lat:result.lat,lng:result.lng},function(data) {
			$('#locale_form').html('Please select which clinic you would like an appointment for, press \'Next\' when done.<br/>' + data + startover);
			$colourMe();
			$('#locale').keypress(function(event) {
				if (event.which == '13' && $(this).val() != '') {
					var $v = $(this).val();
					fpA($v);
			   }
			});
		});
	};
	node.geocode($v + ', AU');
}

var startover = '<hr style="border:0;border-bottom:1px solid #114986" />If none of these clinics suit, you can start over.<div class="clear" style="height:10px"></div><input type="text" alt="Enter your Location" name="locale" id="locale" class="box colourme fps1" value="Enter your Location" /><input type="button" class="go fps1" value=" " onclick="fpA()" />';

function getclinic() {
	$('#stepper').html('Step 3');
	var $select = $('.clinic:checked').val();
	if ($select) {
		$('#locale_form').html($('#clinic_'+$select).html());
	} else {
		alert('Please select a clinic or start over');
	}
}

function presentError(message) {
	if (message != null) {
		alert(message);
		$('#lookup').attr('disabled',null);
	}
}

function geoNode(){
	this.gls = new GlocalSearch();
	this.returnHandler = function (latitude, longitude){ };
	this.parseResult = function(){ 
		if(this.gls.results[0]){
			this.returnHandler(this.gls.results[0]);
		} else {
			presentError('Sorry your location was not found.');
		}
	}
	this.geocode = function(loc){
		this.gls.setSearchCompleteCallback(this, this.parseResult);
		this.gls.execute(loc);
	}	
}

function $colourMe() {
	$('.colourme').each(function(i, e) {
		$(this).blur(function() {
			if ($(this).attr('alt') == $(this).attr('value') || $(this).attr('value') == '') {
				$(this).val($(this).attr('alt')).css('color','#cccccc');
			}
		}).focus(function() {
			if ($(this).attr('alt') == $(this).attr('value')) {
				$(this).val('').css('color','#000000');
			}
		});
		if ($(this).attr('alt') == $(this).attr('value')) {
			$(this).css('color','#cccccc');
		}
	});
}
