	$(document).ready(function(){
		
		$('#indemnityItems input.pound').blur(function(){
			calculate_cover_charge();
		});
	
	});

	function calculate_cover_charge() {

		// Calculate total value of goods i.e. the sum of the value column 
		//var values = document.getElementsByName('value');
		var total_value = 0;
		var numRows = 5;
		
		var curr_value;
		for ( i = 1; i <= numRows; i++ ) {
			input = document.getElementById('value_' + i);
			if(input) {
				curr_value = parseFloat(input.value);
				if (curr_value > 10000) {
					alert('Maximum item value covered is £10000.');
				}
				total_value = (isNaN(curr_value)) ? total_value : total_value + curr_value;
				if (total_value > 10000) {
					alert('Maximum value for total value of goods covered is £10000.');
					break
				}
			}
		}
		document.getElementById('totalvalue').value = total_value;
 		
		// Cover charge equals minimum £20.00 or 2% of the total value, 
		// whichever is the greater, to a maximum of £10,000.00
		var cover_charge = 0.02*(total_value);
		cover_charge = Math.min(10000, Math.max(20.0, cover_charge));
		document.getElementById('covercharge').value = cover_charge;
 	}
	
 	function check_cover_charge(form) {
 		if (document.getElementById('totalvalue').value > 10000) {
			alert('Maximum value for total value of goods covered is £10000.');
			return false;
		}
		else {
			return validate(document.getElementById('contactForm'));
		}
	}

