function jsoncallback(petCount) {
	$('#counter').text(addCommas(petCount.count));
}

function track_over(track, isOver) {
	if (isOver)track.src=track.src.replace('up','dw');
	else track.src=track.src.replace('dw','up');
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	return x1 + x2;
}

function changeLocale(lang) {
	document.langSelect.lang.value = lang;
	document.langSelect.page.value = document.URL;
	document.langSelect.submit();
}

function select(elementName) {
	var elm = document.getElementById(elementName);
	elm.focus();
	elm.select();
} 

function geoCodePetition(cityYes) {
	// for offline testing
	//document.petitionForm.submit();
	
	// online testing and production
	var address= cityYes && $('#petition-city').val()!='' ?
			$('#petition-city').val() +","+$('#petition-country').val()
			: $('#petition-country').val();
	
	var geocoder = new google.maps.Geocoder();
	if (geocoder) {
		geocoder.geocode({'address':address}, function (results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var location = results[0].geometry.location;
	            $('#petition-latitude').val(location.lat());
	            $('#petition-longitude').val(location.lng());
	            document.petitionForm.submit();
	         } else if (cityYes) {
	        	 geoCodePetition(false);
	         } else {
	        	 $('#petition-latitude').val('0.0');
		         $('#petition-longitude').val('0.0');
		         document.petitionForm.submit();
	         }
		})
	}
	return false;
}
