Emily Newsom

As Development Director, Emily oversees all of Brightflock's development projects. Prior to co-launching Brightflock in 2008, Emily developed enterprise Java systems for the biotechnology industry. Emily is an avid proponent of open source software (but without the beard!) and has contributed to a number of OSS projects including Firefox, Ubuntu, WordPress and Drupal. Emily holds an honors BSc in Computer Science from the University of Victoria.

My personal site can be found here

Emily Newsom's blog »

Don't get me wrong, E-Junkie works great when tax is not collected on a sale. However, introducing tax into the equation changes the way the cart system behaves.

The UI E-Junkie uses to get location/zip codes from buyers for tax is really clunky--the buyer has to manually select his or her country and enter his or her postal code before being able to pay no matter where the buyer is from. Well, the problem is that most sellers only really care whether a buyer is from their own country or not for tax purposes--why should a seller charging tax in the US care about the buyer's location information if the buyer is from Australia? They shouldn't. So why does E-Junkie force all buyers to select a country and zip for tax purposes? Who knows! What I do know is that I can change the tax UI around on the fly using some JQuery.

Here is the JQuery code I quickly whipped up for a client who collects tax in Canada only from buyers in Canada. It is written using polling, but something like live query may work better.

With this code the UI defaults to the US so non-Canadians can just click "buy" and not even read the section about tax. Canadians can declare they are from Canada by clicking a checkbox which exposes a Postal Code field--much cleaner and much less intrusive to buyers from outside Canada.

(function($) {
	var ZIP_LABEL = 'Postal Code';
	var DECLARE_TEXT = 'Check here if you are Canadian, as tax will apply. We’ll need your postal code to calculate what tax applies in your region.';
	var DEFAULT_COUNTRY = 'US';
	var DEFAULT_ZIP = '90210';
	var TAX_COUNTRY = 'CA';
	var POLL_TIME = 300;
	var POLL_TIMEOUT = 60000;
 
	$(document).ready(function() {
		$('a').click(function() {
			var $ej = $('body > #EJEJC_window');
			var wait = 0;
			var firstTime = true;
 
			var f = function() {
				var $form = $('#EJEJC_iframeContent form:first', $ej)
 
				if ($form.length) {
 
					var $table = $('table#ejejctable', $form);
 
					var $updateCartButton = $('input#btnUpdtCart', $table).click(function() {
						$form.hide();
						wait = 0;
						setTimeout(f, POLL_TIME)
					});
 
					var $country = $('select', $ej);
					var $zip = $('input', $ej);
					var $countrytr = $country.parents('tr:first');
					var $ziptr = $zip.parents('tr:first');
 
					$('div', $ziptr.children('td:first')).html(ZIP_LABEL);
 
					$countrytr.parent().prepend('<tr><td colspan=5><input type="checkbox" name="declare" style="width: 40px;"/> ' +	DECLARE_TEXT + '</td></tr>');
 
					var $declare = $('input', $countrytr.parent());
 
					var declareUpdate = function() {
						if ($declare.filter(':checked').length == 1) {
							$country.val(TAX_COUNTRY);
 
							if ($zip.val() == DEFAULT_ZIP) {
								$zip.val('');
							}
						}
						else {
							$country.val(DEFAULT_COUNTRY);
							$zip.val(DEFAULT_ZIP);
						}
					};
 
					$declare.change(function() {
						declareUpdate();
						$updateCartButton.click();
					});
 
					$countrytr.hide();
 
					if ($country.val() == TAX_COUNTRY) {
						$declare.attr('checked', 'checked');
						$ziptr.show();
					}
					else {
						$declare.removeAttr('checked');
						$ziptr.hide();
					}
 
					declareUpdate();
 
					if ($updateCartButton.length && firstTime) {
						firstTime = false;
 
						$declare.trigger('change');
					}
					else {
 
						$form.show();
					}
				}
				else {
					if (wait < POLL_TIMEOUT) {
						wait += POLL_TIME;
						setTimeout(f, POLL_TIME);
					}
					else {
						$form.show()
					}
				}
			};
 
			setTimeout(function() {
				f($ej);
			}, POLL_TIME);
		});
	});
})(jQuery);