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

function init() {
	// if (document.order.process_me)
	// 	document.order.process_me.value = 'no';
		
	if ($('scroll'))
		document.body.scrollTop = $('scroll').value;
	
	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 ($('cc_s_date'))
		$('cc_s_date').style.display = ('2' == $('cc_type').value ? 'block' : 'none');

	if ($('cc_issue'))
		$('cc_issue').style.display = ('2' == $('cc_type').value ? 'block' : 'none');
		
	if ($('cc_number'))
		$('cc_number').value = '';
	
	if ($('cc_cvv2'))
		$('cc_cvv2').value = '';
}

function clearform() {
	if (document.order)
		document.order.reset();
}

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

function update_shipping() {
	var rows = $$('#your_order tr');
	var qty = $$('#your_order tr 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')) {
			book_count += Number(qty[i].value) * 9;
			subtotal   += Number(qty[i].value) * 79.29;
			rows[i+1].getElementsByTagName('td')[3].update('£' + (Number(qty[i].value) * 79.29).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) * 44.75;
			rows[i+1].getElementsByTagName('td')[3].update('£' + (Number(qty[i].value) * 44.75).toFixed(2));
			f_spc = f_spc || Number(qty[i].value) != 0;

			// Gift pack B has 10 books
		} else if (-1 != qty[i].name.indexOf('10B1')) {
			book_count += Number(qty[i].value) * 10;
			subtotal   += Number(qty[i].value) * 79.00;
			rows[i+1].getElementsByTagName('td')[3].update('£' + (Number(qty[i].value) * 79.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) * 94.90;
			rows[i+1].getElementsByTagName('td')[3].update('£' + (Number(qty[i].value) * 94.90).toFixed(2));
			f_spc = f_spc || Number(qty[i].value) != 0;
			
		// Single book
		} else if (-1 != qty[i].name.indexOf('BK')) {
			book_count += Number(qty[i].value);
			subtotal   += Number(qty[i].value) * 9.95;
			rows[i+1].getElementsByTagName('td')[3].update('£' + (Number(qty[i].value) * 9.95).toFixed(2));

		// Single DVD
		} else if (-1 != qty[i].name.indexOf('DVD')) {
			dvd_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 DVD 4 pack
		} else if (-1 != qty[i].name.indexOf('D4PK')) {
			dvd_count += Number(qty[i].value) * 4;
			subtotal  += Number(qty[i].value) * 49.00;
			rows[i+1].getElementsByTagName('td')[3].update('£' + (Number(qty[i].value) * 49.00).toFixed(2));
			
		// Otherwise DVD 6 pack
		} else if (-1 != qty[i].name.indexOf('D6PK')) {
			dvd_count += Number(qty[i].value) * 6;
			subtotal  += Number(qty[i].value) * 79.00;
			rows[i+1].getElementsByTagName('td')[3].update('£' + (Number(qty[i].value) * 79.00).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_coupon = $F('coupon');

	new Ajax.Request('/shipping/', {
		method: 'post',
		parameters: {ajax: 'yes', subtotal: (subtotal * 100), count: book_count, d_count: dvd_count, country: f_country, coupon: f_coupon, spc: f_spc},
		onSuccess: function(transport) {
			var result = transport.responseText.evalJSON();
			var shipping_total = result.shipping_total;
			
			$('shipping_total').update('£' + shipping_total.toFixed(2));
			$('total').update('Your Total: £' + (subtotal + shipping_total).toFixed(2));
			$('inc_shipping').innerHTML = '(including shipping: £' + shipping_total.toFixed(2) + ')';
		},
		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 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 update_totals() {
	
	if ($$('#order input[type="text"]').size() == 0)
		return false;
	
	var params = Form.serializeElements($$('#order input[type="text"]'), true);
	
	params['coupon'] = $('coupon').value;
	params['billing_country'] = $('billing_country').value;
	params['shipping_country'] = ($('shipping_check').checked ? $('billing_country').value : $('shipping_country').value);
		
	new Ajax.Request(
		'/ajax/',
		{
			method: 'post',
			parameters: params,
			onSuccess: function(transport) {
				var result = transport.responseText.evalJSON();

				$('subtotal_top').update(result.subtotal.toFixed(2));
				$('shipping_total').update((result.shipping + result.handling).toFixed(2));
				
				$('subtotal').update(result.subtotal.toFixed(2));				
				$('shipping').update((result.shipping + result.handling).toFixed(2));
				$('sales_tax').update(Object.isString(result.sales_tax) ? result.sales_tax : '£' + result.sales_tax.toFixed(2));
				$('total').update(result.total.toFixed(2));
			}
		});
}

