window.onload = function() {
	calculate();
}
function calculate() {
	
	
	
	
	var form = document.getElementById('quantity-calc');
	
	
	if(!form) { return; }

	
	form.onsubmit = function() {
	
	
		
		
		var inputs = form.getElementsByTagName('input');
		
		
		if(!inputs) { return; }
	
		for(var i = 0; i < inputs.length; i++) {
		
			if(/(^| )required( |$)/.test(inputs[i].className) && (inputs[i].value == '' || isNaN(inputs[i].value))) {
				
				alert('Please enter a numerical value for ' + inputs[i].id);
				inputs[i].focus();
				return false;
							
			} 
			
			
			switch(inputs[i].id) {
				
				case 'depth':
				var depth = inputs[i].value / 1000;
				break;
				
				case 'width':
				var width = inputs[i].value;
				break;
				
				case 'length_m':
				var length = inputs[i].value;
				break;
			}
		}
		
		doCalcs(depth,width,length);
		
		return false;
		
	} // onsubmit
	
};

function doCalcs(depth, width, length) {
	
	var cubic = depth * width * length;
	var stoneType = document.getElementById('itemcode').value;
	
	
	var quantities = {
		'1019': cubic * 1.5,
		'1021': cubic * 1.5,
		'1050': cubic * 1.5,
		'1015': cubic * 1.7,
		'100110': cubic * 1.7,
		'1016': cubic * 1.7,
		'1029': cubic * 1.7,
		'1009': cubic * 1.7,
		'1025': cubic * 1.7,
		'1026': cubic * 1.7,
		'1032': cubic * 1.7,
		'1034': cubic * 1.7,
		'1035': cubic * 1.7,
		'1036': cubic * 1.7,
		'5001': cubic * 1.7,
		'5002': cubic * 1.7,
		'1003': cubic * 1.8,
		'1005': cubic * 1.8,
		'1006': cubic * 1.8,
		'1007': cubic * 1.8,
		'1008': cubic * 1.8,
		'1009': cubic * 1.8,
		'1010': cubic * 1.8,
		'1018': cubic * 1.8,
		'1023': cubic * 1.8,
		'1024': cubic * 1.8,
		'1011': cubic * 2.0,
		'1013': cubic * 2.0,
		'1020': cubic * 2.0,	
		'1012': cubic * 2.2
	}
	
	if(stoneType in quantities) {
		var quantity = document.getElementById('quantity'); //= quantities[stonetype];
		if(!quantity) { return; }
		
		var exactQuantity = document.getElementById('exact-quantity');
		if(!exactQuantity) { return; }
		
		exactQuantity.value = quantities[stoneType].toFixed(2);
		quantity.value = Math.ceil(quantities[stoneType]);
	}
	
	

}

