var Materials = new Array();
Materials['Vertical Uprights'] = new Array(3.4,4.23);
Materials['Corner Joints'] = new Array(0.29,0.84);
Materials['Horizontal Bars'] = new Array(2.5,2.5);
Materials['Side out joints'] = new Array(0.36,0.99);
Materials['4 Way Joints'] = new Array(0.43,1.13);
Materials['Gate'] = new Array(9,12.5);
Materials['Cutting'] = new Array(2.2,2.2);
Materials['Side Netting'] = new Array(0.13,0.13);
Materials['Roof Netting'] = new Array(0.14,0.14);
Materials['S Hooks'] = new Array(0.05,0.05);
Materials['Zip Tie'] = new Array(0.02,0.02);
Materials['Ground Pegs'] = new Array(0.05,0.05);

function pad_with_zeros(rounded_value, decimal_places) {
    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
    if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return pad_with_zeros(result3, decimals)*1;
}
	
function calcCost(){

	if(document.dimensions.height.value == ''){
		alert('Please enter a Length.');
		return false;
	}
	
	if(document.dimensions.width.value == ''){
		alert('Please enter a Width.');
		return false;
	}
	
	if((document.dimensions.width.value*1) > 10 || (document.dimensions.height.value*1) > 10){
		alert('P.O.A, Please contact us for a quotation, tel 0845 260 4450');
		document.dimensions.width.value = '';
		document.dimensions.height.value = '';
		return false;
	}

	material_type = '';
	for(var i = 0; i < document.dimensions.material_type.length; i++) {
		if(document.dimensions.material_type[i].checked == true) {
			material_type = document.dimensions.material_type[i].value;
		}
	}
	
	if(material_type != ''){
		document.material_pic.src = 'images/fruit_'+material_type+'.jpg';
		if(material_type == 0){
			material_text = 'Galvanised';
			document.getElementById('material_name').innerHTML = 'Galvanised';
		}else{
			material_text = 'Black Plastic';
			document.getElementById('material_name').innerHTML = 'Black plastic coated steel';
		}
	}
	else{
		alert('Please select a material.');
		return false;
	}
	
	noofdoors = document.dimensions.noofdoors.value;						// NUMBER OF DOORS
	width = document.dimensions.width.value * 1;							// B25 VALUE
	height = document.dimensions.height.value * 1; 							// B28 VALUE
	width_plus_height = width + height;										// B30 VALUE
	width_plus_height_minus_ten = 10 - width_plus_height;					// B34 VALUE
	width_meters = (((Math.ceil(width/2.5))*2.5)/2.5); 						// C8 VALUE
	height_meters = (((Math.ceil(height/2.5))*2.5)/2.5); 					// C13 VALUE

	width_vertical_uprights = width_meters + 1;								// D8 VALUE
	length_vertical_uprights = width_vertical_uprights * height_meters;		// D13 VALUE
	
	width_horizontal_bars = width_meters + 1;
	extra_horizontal_bars = width_vertical_uprights * height_meters;
	
	width_sideout_joints = (width_meters - 1) * 2;
	extra_sideout_joints = height_meters * 2 - 2;
	
	count_vertical_uprights = width_vertical_uprights + length_vertical_uprights;
	count_corner_joints = 4;
	count_horizontal_bars = width_meters * height_meters + extra_horizontal_bars + width_meters;
	count_sideout_joints = width_sideout_joints + extra_sideout_joints;
	count_4way_joints = (height_meters - 1) * (width_sideout_joints / 2);
	count_side_netting_width = 2;
	count_side_netting_length = (height * 2) + (width * 2) + 2;
	count_roof_netting_width = width + 2;
	count_roof_netting_length = height + 2;
	count_s_hooks = ((width * 2) + (height * 2)) * 3;
	count_zip_tie = ((width * 2) + (height * 2)) * 3;
	count_ground_pegs = ((width * 2) + (height * 2)) * 3;

	// alert(count_vertical_uprights+' '+count_corner_joints+' '+count_horizontal_bars+' '+count_sideout_joints+' '+count_4way_joints);
	// alert(count_side_netting_width+' '+count_side_netting_length+' '+count_roof_netting_width+' '+count_roof_netting_length);
	// alert(count_s_hooks+' '+count_zip_tie+' '+count_ground_pegs);
	
	total_vertical_uprights = round_decimals(count_vertical_uprights * Materials['Vertical Uprights'][material_type], 3);
	total_corner_joints = round_decimals(count_corner_joints * Materials['Corner Joints'][material_type], 3);
	total_horizontal_bars = round_decimals(count_horizontal_bars * Materials['Horizontal Bars'][material_type], 3);
	total_sideout_joints = round_decimals(count_sideout_joints * Materials['Side out joints'][material_type], 3);
	total_4way_joints = round_decimals(count_4way_joints * Materials['4 Way Joints'][material_type], 3);
	total_gate = round_decimals(noofdoors * Materials['Gate'][material_type], 3);
	total_cutting = round_decimals(((height_meters + 1) + (width_meters + 1)) * Materials['Cutting'][material_type], 3);
	total_side_netting = round_decimals((count_side_netting_width * count_side_netting_length) * Materials['Side Netting'][material_type], 3);
	total_roof_netting = round_decimals((count_roof_netting_width * count_roof_netting_length) * Materials['Roof Netting'][material_type], 3);
	total_s_hooks = round_decimals(count_s_hooks * Materials['S Hooks'][material_type], 3);
	total_zip_tie = round_decimals(count_zip_tie * Materials['Zip Tie'][material_type], 3);
	total_ground_pegs = round_decimals(count_ground_pegs * Materials['Ground Pegs'][material_type], 3);
	
	// alert(total_vertical_uprights+' '+total_corner_joints+' '+total_horizontal_bars+' '+total_sideout_joints+' '+total_4way_joints);
	// alert(total_gate+' '+total_cutting+' '+total_side_netting+' '+total_roof_netting+' '+total_s_hooks+' '+total_zip_tie+' '+total_ground_pegs);
	
	total_parts = total_vertical_uprights+total_corner_joints+total_horizontal_bars+total_sideout_joints+total_4way_joints+total_gate+total_cutting+total_side_netting+total_roof_netting+total_s_hooks+total_zip_tie+total_ground_pegs;
	// alert(total_parts);
	
	/*
	if(width_plus_height > 55){
		if((width + height) < 61){
			total_no_vat = round_decimals(total_parts * 1.57, 2);
		}
		else{
			if(material_type == 0){ // Galvanised
				total_no_vat = round_decimals(((total_parts*1.53)*(100+(width_plus_height_minus_ten-7))/100), 2);
			}
			else{ // Black Plastic
				total_no_vat = round_decimals(((total_parts*1.53)*(100+(width_plus_height_minus_ten-5))/100), 2);
			}
		}
	}
	else{
	*/

	if((width + height) < 10){
		total_no_vat = round_decimals(total_parts * 3.4, 2);
	}
	else{
		if(material_type == 0){ // Galvanised
			total_no_vat = round_decimals(((total_parts*3.4)*(100+(width_plus_height_minus_ten-5))/100), 2);
		}
		else{ // Black Plastic
			total_no_vat = round_decimals(((total_parts*3.4)*(100+(width_plus_height_minus_ten-5))/100), 2);
		}
	}
	
	// alert(total_no_vat);

	// Caluculate the totals
	// if(noofdoors > 1) total_no_vat = total_no_vat + ((noofdoors-1)*45);
	total_vat = round_decimals(total_no_vat * 0.175, 2);
	sub_total = round_decimals(total_no_vat + total_vat, 2);
	
	// alert(total_vat);
	// alert(sub_total);
	
	// Set the ordre form variables :-
	document.orderform.sub_total.value = sub_total.toFixed(2);
	document.orderform.secuitems.value = '[||Agriframes '+material_text+' Fruit Cage (doors='+noofdoors+', length='+height+', width='+width+')|'+sub_total.toFixed(2)+'|1|'+sub_total.toFixed(2)+']';

	// Add the shipping charge
	shippingcharge = (document.dimensions.shippingcharge.options[document.dimensions.shippingcharge.selectedIndex].value*1);
	document.orderform.shippingcharge.value = shippingcharge;
	
	grand_total = sub_total + shippingcharge;
	
	document.getElementById('price').innerHTML = grand_total.toFixed(2);
	document.orderform.transactionamount.value = grand_total.toFixed(2);
	
	return false;
}