function showMessage () {
	document.write ('<p>The total, with tax (if any) and <a href="#shipping">shipping<\/a> will be calculated as you complete the form.<\/p>');
}

function showTotals () {
	document.write ('<tr>\r<th colspan="4">Order details<\/th>\r<\/tr>\r');
	var titles = new Array (
		'Goods subtotal:',
		'Sales tax (NY State only):',
		'Shipping:',
		'Total:'
	);
	var ids = new Array ('goods', 'tax', 'shipping', 'total');
	for (var i = 0; i < 4; i++) {
		document.write ('<tr>\r<td>');
		if (i == 3) {
			document.write ('<strong>');
		}
		document.write (titles[i]);
		if (i == 3) {
			document.write ('<\/strong>');
		}
		document.write ('<\/td>\r<td>');
		if (i == 3) {
			document.write ('<strong>');
		}
		document.write ('$<span id="' + ids[i] + '">0.00<\/span>');
		if (i == 3) {
			document.write ('<\/strong>');
		}
		document.write ('<\/td>\r<td colspan="2"><\/td>\r<\/tr>\r');
	}
}

var items = new Array (
	'b_tull',
	'b_zepp',
	'b_mann',
	'b_yard',
	'b_zappa',
	'b_zombies',
	'c_illum',
	'c_retro',
	'c_believe',
	'c_belairs',
	'c_stampede',
	'c_nostalgia',
	'c_nmbfrank',
	'c_jcbbeer',
	'c_works',
	'c_bunk',
	'c_ectoplasm',
	'c_jcbpaid',
	'c_charge',
	'c_neon',
	'c_torn',
	'c_randb'
);
var prices = new Array (
	22.95,
	14,
	11.95,
	19.95,
	21.95,
	16.95,
	15,
	10,
	20,
	10,
	10,
	10,
	10,
	10,
	10,
	10,
	10,
	10,
	10,
	10,
	10,
	19
);

function doCountry (state) {
	form = document.getElementById ('myform');
	if (state.value != 'non') {
		form.country.selectedIndex = 0;
	}
	showHide();
}

function doState (country) {
	if (!country.value) {
		country.selectedIndex = 0;
	}
	
	form = document.getElementById ('myform');
	if (country.selectedIndex) {
		form.state.selectedIndex = form.state.length - 1;
	}
	showHide();
}

function showHide () {
	var vis = 'none';
	form = document.getElementById ('myform');
	if (!form.country.selectedIndex) {
		for (i = 0; i < items.length; i++) {
			if (items[i].substr (0, 1) == 'b' && form[items[i]].value != '0') {
				vis = 'inline';
				break;
			}
		}
	}
	document.getElementById('ship').style.display = vis;
	document.getElementById('shipl').style.display = vis;
}

var us_cd = 2;
var us_first_book = 6;
var us_media_book = 4;
var rest_cd = 5;
var can_book = 8;
var mex_book = 12;
var rest_book = 20;
var goods;
function calculate () {
	showHide();
	form = document.getElementById ('myform');
	var tax, shipping, total;
	
	// tot up basic cost of goods, and get nos. of books & cds ordered
	goods = 0;
	var bk = 0;
	var cd = 0;
	var val;
	for (i = 0; i < items.length; i++) {
		val = parseInt (form[items[i]].value);
		if (val != 0) {
			goods += val * prices[i];
			if (items[i].substr (0, 1) == 'b') {
				bk += val;
			}
			else {
				cd += val;
			}
		}
	}
	
	if (goods) {
		// sales tax is 7% if ordering from NY
		tax = (form.state.value != 'NY') ? 0 : goods * 0.07;
		
		// now... the shipping!
		var bookrate, cdrate;
		// first establish the rates for books and cds
		// depending on the country and shipping method selected
		if (!form.country.selectedIndex) {
			bookrate = (form.ship[0].checked) ? us_first_book : us_media_book;
			cdrate = us_cd;
		}
		else {
			cdrate = rest_cd;
			if (form.country.value == 'Canada') {
				bookrate = can_book;
			}
			else if (form.country.value == 'Mexico') {
				bookrate = mex_book;
			}
			else {
				bookrate = rest_book;
			}
		}
		// initialise shipping according to whether any books are selected
		// note: first item always a book if any books are ordered
		if (bk) {
			shipping = bookrate;
			bk--;
		}
		else {
			shipping = cdrate;
			cd--;
		}
		// now add the rest
		shipping += (bk * bookrate + cd * cdrate) / 2;
		
		total = goods + tax + shipping;
	}
	else {
		tax = shipping = total = 0;
	}
	
	document.getElementById('goods').innerHTML = currency (goods);
	document.getElementById('tax').innerHTML = currency (tax);
	document.getElementById('shipping').innerHTML = currency (shipping);
	document.getElementById('total').innerHTML = currency (total);
}

function currency (val) {
	var amt = (Math.round (val * 100)) / 100;
	var intr = Math.floor (amt);
	var dec = Math.round ((amt - intr) * 100);
	return (intr + '.' + ((dec.toString() + '00').substr (0, 2)));
}

function checkForm (form) {
	calculate();
	
	if (!goods) {
		alert ("You haven't selected anything to buy!");
		return false;
	}
	if (!form.thename.value) {
		alert ('Please enter your name.');
		form.thename.focus();
		return false;
	}
	if (!isEmail (form.email.value)) {
		alert ('Please enter a valid email address.');
		form.email.focus();
		return false;
	}
	if (!form.add1.value || !form.add2.value) {
		alert ('Please enter at least two address lines.');
		form.add1.focus();
		return false;
	}
	if (!form.zip.value) {
		alert ('Please enter your postal/zip code.');
		form.zip.focus();
		return false;
	}
	if (!form.country.selectedIndex && form.state.selectedIndex == form.state.length - 1) {
		alert ('If you are in the US, please select a US state; otherwise please select a different country.');
		return false;
	}
	
	return true;
}

function isEmail (str) {
	if (!str) {
		return false;
	}

	var iChars = '*|,"<:>[]{}`\';()&$#%';
	for (var i = 0; i < str.length; i++) {
		if (iChars.indexOf (str.charAt (i)) != -1) {
			return false;
		}
	}
	
	var iAt = str.indexOf ('@');
	var jAt = str.indexOf ('@', iAt + 1);
	var iDot = str.lastIndexOf ('.');
	if (iAt < 1 || jAt != -1 || iDot > str.length - 3 || iDot - iAt < 2) {
		return false;
	}
	
	return true;
}
