window.onload = init;
window.onscroll = update_position;

function init() {
	if ($('shipping_details'))
		$('shipping_details').style.display = ($('shipping_check').checked ? 'none' : 'block');
		
	if ($('shipping_update'))
		$('shipping_update').style.display = 'none';

	if ($('gift_details') && $('gift_check'))
		$('gift_details').style.display = ($('gift_check').checked ? 'block' : 'none');
		
	if ($('in_my_area') && $('interest_check'))
		$('in_my_area').style.display = ($('interest_check').checked ? 'block' : 'none');
		
	if ($('scroll'))
		document.body.scrollTop = $('scroll').value;
}

function update_position() {
	if ($('scroll'))
		$('scroll').value = document.body.scrollTop;
}

function update_shipping() {
	var rows = $$('#your_order tr');
	var qty = $$('#your_order tr.order_item td input');
	var book_count = 0, dvd_count = 0, subtotal = 0;
	var f_spc = 0;
	
	for (i = 0; i < qty.length; i++) {
		// Full sets contain 9 books
		if (-1 != qty[i].name.indexOf('FSET-ED2')) {
			book_count += Number(qty[i].value) * 9;
			subtotal += Number(qty[i].value) * 126.00;
			$(rows[i+1].getElementsByTagName('td')[3]).update('$' + (Number(qty[i].value) * 126.00).toFixed(2));
		
		// Full sets contain 9 books
		} else if (-1 != qty[i].name.indexOf('FSET-ED1')) {
			book_count += Number(qty[i].value) * 9;
			subtotal += Number(qty[i].value) * 117.00;
			$(rows[i+1].getElementsByTagName('td')[3]).update('$' + (Number(qty[i].value) * 117.00).toFixed(2));
		
		// Gift pack A has 5 books
		} else if (-1 != qty[i].name.indexOf('05B1')) {
			book_count += Number(qty[i].value) * 5;
			subtotal += Number(qty[i].value) * 69.00;
			$(rows[i+1].getElementsByTagName('td')[3]).update('$' + (Number(qty[i].value) * 69.00).toFixed(2));
			f_spc = f_spc || Number(qty[i].value) != 0;

		// Gift pack B has 13 books
		} else if (-1 != qty[i].name.indexOf('13B1')) {
			book_count += Number(qty[i].value) * 13;
			subtotal += Number(qty[i].value) * 149.50;
			$(rows[i+1].getElementsByTagName('td')[3]).update('$' + (Number(qty[i].value) * 149.50).toFixed(2));
			f_spc = f_spc || Number(qty[i].value) != 0;

		// Full set of the 6 DVDs 
		} else if (-1 != qty[i].name.indexOf('D6PK')) {
			dvd_count += Number(qty[i].value) * 6;
			subtotal += Number(qty[i].value) * 99.00;
			$(rows[i+1].getElementsByTagName('td')[3]).update('$' + (Number(qty[i].value) * 99.00).toFixed(2));
			
		// Single DVD
		} else if (-1 != qty[i].name.indexOf('9842512') || -1 != qty[i].name.indexOf('9842873')) {
			dvd_count += Number(qty[i].value);
			subtotal += Number(qty[i].value) * 24.95;
			$(rows[i+1].getElementsByTagName('td')[3]).update('$' + (Number(qty[i].value) * 24.95).toFixed(2));	

		// Otherwise single book edition 1
		} else if (-1 != qty[i].name.indexOf('9763333')) {
			book_count += Number(qty[i].value);
			subtotal += Number(qty[i].value) * 14.95;
			$(rows[i+1].getElementsByTagName('td')[3]).update('$' + (Number(qty[i].value) * 14.95).toFixed(2));

		// Otherwise single book edition 2
		} else if (-1 != qty[i].name.indexOf('9801812')) {
			book_count += Number(qty[i].value);
			subtotal += Number(qty[i].value) * 15.95;
			$(rows[i+1].getElementsByTagName('td')[3]).update('$' + (Number(qty[i].value) * 15.95).toFixed(2));
		}


	}
	
	if (book_count >= 0 || dvd_count >= 0) {
		$('book_total').innerHTML = '$' + subtotal.toFixed(2);
	}
	
	var prefix = ($('shipping_check').checked ? 'billing_' : 'shipping_');
	var f_country = $F(prefix + 'country');
	var f_state = $F(prefix + 'state');
	var f_zip = $F(prefix + 'zip');
	var f_method = $F('shipping_method');
	var f_coupon = $F('coupon');

	new Ajax.Request('/shipping/', {
		method: 'post',
		parameters: {ajax: 'yes', count: book_count, d_count: dvd_count, country: f_country, state: f_state, method: f_method, zip: f_zip, coupon: f_coupon, spc: f_spc},
		onSuccess: function(transport) {
			var result = transport.responseText.evalJSON();
			var shipping_total = result.shipping_total;

			var sales_tax = 0;
			var sales_tax_amount = 0;
			
			// Sales Tax Calculation
			if (['CA','CALIFORNIA'].indexOf(f_state.toUpperCase()) != -1) {
				sales_tax = 8.25;
				sales_tax_amount = (subtotal * sales_tax) / 100;
				subtotal += sales_tax_amount;
				
				$('inc_sales_tax').show();
				$('inc_sales_tax').innerHTML = '(Includes Sales Tax of: $' + sales_tax_amount.toFixed(2) + ' (' + sales_tax.toFixed(2) + '%))';			
			} else {
				$('inc_sales_tax').hide();
			}
			
			$('shipping_total').update('$' + shipping_total.toFixed(2));
			$('total').update('Total: $' + (subtotal + shipping_total).toFixed(2));
			$('inc_shipping').innerHTML = '(Includes Shipping &amp; Handling: $' + shipping_total.toFixed(2) + ')';

			var methods = '';
			
			Object.values(result.shipping_methods).each(function(s) {
				methods += '<option value="' + s.id + '"' + (s.id == result.method ? 'selected="selected"' : '') + '>' + s.carrier + '</option>\n';
			});

			$('shipping_method').update(methods);
		},
		onFailure: function(transport) {
			alert('Error getting shipping rate.');
		}
	});
}

function checkForInt(evt) {
	evt = ( evt ) ? evt : window.event;
	var charCode = ( evt.which ) ? evt.which : evt.keyCode;
	return (charCode <= 32 || (charCode >= 48 && charCode <= 57) || charCode == 45);
}

function maxlength(field, size, counter) {
    if (field.value.length > size) {
        field.value = field.value.substring(0, size);
    }

	$(counter).update(size - field.value.length);
}

function do_stuff() {
	if ($('s1'))
		$('s1').disabled = true;
	if ($('s2'))
		$('s2').disabled = true;
	if ($('s3'))
		$('s3').disabled = true;
	if ($('s4'))
		$('s4').disabled = true;
	if ($('s2'))
		$('s2').value = 'Processing...';
	
	document.order.action.value = 'process';
	document.order.submit();

	if (navigator.userAgent.indexOf("Firefox") == -1)
		document.order.reset();
}

function check_state(field, popup) {
	if (!field || !popup)
		return false;
	
	states = [
		'ALBERTA',
		'BRITISH COLUMBIA',
		'MANITOBA',
		'NEW BRUNSWICK',
		'NEW FOUNDLAND AND LABRADOR',
		'NORTHWEST TERRITORIES',
		'NORTHWEST TERRITORY',
		'NOVA SCOTIA',
		'NUNAVUT',
		'ONTARIO',
		'PRINCE EDWARD ISLAND',
		'PRINCE EDWARD',
		'QUEBEC',
		'SASKATCHEWAN',
		'YUKON',
		'AB',
		'BC',
		'MB',
		'NB',
		'NF',
		'NL',
		'NS',
		'NT',
		'NU',
		'ON',
		'PE',
		'PQ',
		'QC',
		'SK',
		'YK',
		'YT',
		'B.C.',
		'P.E.',
		'N.B.',
		'N.S.',
		'N.T.'
		];

	if (states.indexOf(field.value.toUpperCase().strip()) != -1) {
		popup.value = 'CA';
		// popup.disabled = true;
	} else {
		// popup.disabled = false;
	}
	
	update_shipping();
}