var papers = {
	'telegraph':{
		'monday-friday':1.20,
		'saturday':2.00,
		'sunday':2.00
	},
	'times':{
		'monday-friday':1.00,
		'saturday':1.50,
		'sunday':2.20
	},
	'independent':{
		'monday-friday':1.00,
		'saturday':1.60,
		'sunday':1.80
	},
	'guardian':{
		'monday-friday':1.20,
		'saturday':2.10,
		'sunday':2.20
	},
	'mail':{
		'monday-friday':0.55,
		'saturday':0.90,
		'sunday':1.50
	},
	'financial_times':{
		'monday-friday':2.50,
		'saturday':3.00
	}
};

var id;

function toCurrency(subject, inte) {
	var result = (isNaN(parseFloat(subject))) ? 0 : parseFloat(subject);
	result = (inte === true) ? parseInt(result) : result;
	result = (inte === true) ? result : result.toFixed(2);
	return result;
}

function getNumber(id, inte) {
	var result = toCurrency($('#' + id).val(), inte);
	$('#' + id).val(result);
	return result;
}

$(function() {
	$('#print').click(
		function() {
			window.print();
			return false;
		}
	);
	$('.ms').click(
		function() {
			$('.s').attr('checked', '').hide();
			id = $(this).attr('id').replace('_ms', '');
			var id_s = id + '_s';
			$('#' + id).show();
			$('#label_' + id_s).show();
			$('#p_' + id_s).show();
		}
	);
	$('#calculate').click(
		function() {
			if (id) {
				var value = ((getNumber('monday-friday', true) * papers[id]['monday-friday']) + (getNumber('saturday', true) * papers[id]['saturday']));
				if ($('#' + id + '_s').length) {
					value += (getNumber('sunday', true) * papers[id]['sunday']);
				}
				var balance = Number(getNumber('paperbill') - value);
				balance = Math.max(0, balance);
				balance = balance.toFixed(2);
				$('#balance').val(balance);
			}
			return false;
		}
	);
	jQuery.validator.addMethod('currency', function(value, element) {
	    return this.optional(element) || /^(\d{1,3})(\.\d{1,2})?$/.test(value);
	}, 'Must be in currency format 0.99');
	$('#pay_form').validate(
		{
			rules: {
				account_number: {
					required: true
				},
				date_of_bill: {
					required: true
				},
				amount: {
					required: true,
					currency: true
				}
			},
			messages: {
				account_number: {
					required:'Please enter your account number shown at the top of your bill'
				},
				date_of_bill: {
					required:'Please enter the date of the bill'
				},
				amount: {
					required: 'Please enter the amount shown on your bill',
					currency: 'Amount must be in currency format eg 0.99. Please omit the pound sign'
				}
			},
			errorLabelContainer: '#errors',
			wrapper: 'dd'
		}
	);
	$('#company').keyup(
		function() {
			$('#invoice').val(escape($(this).val()) + '|||' + escape($('#account_number').val()) + '|||' + escape($('#date_of_bill').val()));
		}
	);
	$('#account_number').keyup(
		function() {
			$('#invoice').val(escape($('#company').val()) + '|||' + escape($(this).val()) + '|||' + escape($('#date_of_bill').val()));
		}
	);
	$('#date_of_bill').keyup(
		function() {
			$('#invoice').val(escape($('#company').val()) + '|||' + escape($('#account_number').val()) + '|||' + escape($(this).val()));
		}
	);
	$('#amount').keyup(
		function() {
			var charge = parseFloat($('#charge').val());
			var amount = parseFloat($('#amount').val());
			if (!isNaN(amount)) {
				$('#total').val(toCurrency(charge + amount));
				$('#subtotal').val(toCurrency(charge + amount));
			}
		}
	);
});
