// rooms
var rooms = new Array();
rooms[0] = 'Family Room / Lounge';
rooms[1] = 'LAUNDRY';
rooms[2] = 'OUTDOORS';
rooms[3] = 'KITCHEN';
rooms[4] = 'BEDROOM';
rooms[5] = 'STUDY / OTHER';

var items = new Array();
items[0] = new Array(
	new Array(
		'Book Carton',
		0.06,
		10.00
	),
	new Array(
		'Large/Tea Chest carton',
		0.15,
		10.00
	),
	new Array(
		'2 seater couch',
		1.00,
		10.00
	),
	new Array(
		'3 seater couch',
		1.30,
		10.00
	),
        new Array(
		'Armchair',
		0.48,
		10.00
	),
         new Array(
		'Bookcase',
		0.60,
		10.00
	),
        new Array(
		'Buffet/Sideboard',
		1.00,
		10.00
	),
        new Array(
		'China Cabinet',
		0.70,
		10.00
	),
        new Array(
		'Coffee table',
		0.20,
		10.00
	),
        new Array(
		'Desk',
		0.85,
		10.00
	),
        new Array(
		'Dining Chair',
		0.15,
		10.00
	),
        new Array(
		'Entertainment Centre',
		0.85,
		10.00
	),
        new Array(
		'Heater',
		0.10,
		10.00
	),
        new Array(
		'Lampshade',
		0.20,
		10.00
	),
        new Array(
		'Piano',
		2.00,
		10.00
	),
        new Array(
		'Stereo',
		0.40,
		10.00
	),
        new Array(
		'Table',
		0.98,
		10.00
	),
        new Array(
		'TV',
		0.40,
		10.00
	),
        new Array(
		'Video Recorder',
		0.10,
		10.00
	),
        new Array(
		'Wall Unit',
		1.00,
		10.00
	)
);


items[1] = new Array(
	new Array(
		'Book Carton',
		0.06,
		10.00
	),
	new Array(
		'Large/Tea Chest carton',
		0.15,
		10.00
	),
	new Array(
		'Drier',
		0.56,
		10.00
	),
	new Array(
		'Ironing Board',
		0.10,
		10.00
	),
        new Array(
		'Laundry Basket',
		0.10,
		10.00
	),
        new Array(
		'Wash Machine',
		0.60,
		10.00
	),
        new Array(
		'Vaccum Cleaner',
		0.10,
		10.00
	)
);


items[2] = new Array(
	new Array(
		'Book Carton',
		0.06,
		10.00
	),
	new Array(
		'Large/Tea Chest carton',
		0.15,
		10.00
	),
	new Array(
		'BBQ',
		0.75,
		10.00
	),
	new Array(
		'Bicycle',
		0.40,
		10.00
	),
        new Array(
		'Outdoor Table',
		2.50,
		10.00
	),
        new Array(
		'Ladder',
		0.40,
		10.00
	),
        new Array(
		'Lawn Mover',
		0.30,
		10.00
	),
        new Array(
		'Wheel Barrow',
		0.48,
		10.00
	)
);

items[3] = new Array(
	new Array(
		'Book Carton',
		0.06,
		10.00
	),
	new Array(
		'Large/Tea Chest carton',
		0.15,
		10.00
	),
	new Array(
		'Dishwasher',
		0.60,
		10.00
	),
	new Array(
		'Freezer',
		1.00,
		10.00
	),
        new Array(
		'Fridge',
		1.00,
		10.00
	),
        new Array(
		'High Chair',
		0.15,
		10.00
	),
        new Array(
		'Microwave',
		0.15,
		10.00
	),
        new Array(
		'Stool',
		0.15,
		10.00
	)
);

items[4] = new Array(
	new Array(
		'Book Carton',
		0.06,
		10.00
	),
	new Array(
		'Large/Tea Chest carton',
		0.15,
		10.00
	),
	new Array(
		'SB & Mattress',
		1.10,
		10.00
	),
	new Array(
		'Freezer',
		1.00,
		10.00
	),
        new Array(
		'DB & Mattress',
		1.70,
		10.00
	),
        new Array(
		'QB & Mattress',
		1.80,
		10.00
	),
        new Array(
		'KB & Matttress',
		1.90,
		10.00
	),
        new Array(
		'Bedhead',
		0.30,
		10.00
	),
        new Array(
		'Bedside Table',
		0.15,
		10.00
	),
        new Array(
		'Cot',
		0.70,
		10.00
	),
        new Array(
		'Dressing Table',
		0.80,
		10.00
	),
        new Array(
		'Folding Bed',
		0.28,
		10.00
	),
        new Array(
		'Scotch Chest',
		0.90,
		10.00
	),
        new Array(
		'Wardrobe',
		1.20,
		10.00
	)
         
);

items[5] = new Array(
	new Array(
		'Book Carton',
		0.06,
		10.00
	),
	new Array(
		'Large/Tea Chest carton',
		0.15,
		10.00
	),
	new Array(
		'Coat Stand',
		0.60,
		10.00
	),
	new Array(
		'Computer',
		0.40,
		10.00
	),
        new Array(
		'Computer Desk',
		0.85,
		10.00
	),
        new Array(
		'Cupboard',
		0.85,
		10.00
	),
        new Array(
		'Exercise Bike',
		0.40,
		10.00
	),
        new Array(
		'Filing Cabinet',
		0.30,
		10.00
	),
        new Array(
		'Folding Chairs',
		0.10,
		10.00
	),
        new Array(
		'Golf Bag',
		0.20,
		10.00
	),
        new Array(
		'Hall Table',
		0.25,
		10.00
	),
        new Array(
		'Mats & Rug',
		0.15,
		10.00
	),
        new Array(
		'Medium Cartons',
		0.15,
		10.00
	),
        new Array(
		'Large Cartons',
		0.25,
		10.00
	),
        new Array(
		'Sewing Cabinet',
		0.60,
		10.00
	),
        new Array(
		'Sewing Machine',
		0.20,
		10.00
	),
        new Array(
		'Suitcase',
		0.20,
		10.00
	)
        
);

$(document).ready(function(){
    
	var table;
	var tbody;
	var row;
	var row_total;
	
	for (i=0;i<rooms.length;i++)
	{

	    if (items[i]) {
		
			table =
			   $.TABLE( { Class:"room", CellPadding:"0", Cellspacing:"0" },
			       $.THEAD({},
			           $.TR({},
			               $.TD({ Class:"title", colSpan:"4" }, rooms[i] )
			           ),
			           $.TR({},
			               $.TH({ Class:"col_1" }, 'Qty' ),
			               $.TH({ Class:"col_2" }, 'Item' ),
			               $.TH({ Class:"col_3" }, 'Cubic Metres' ),
			               $.TH({ Class:"col_4" }, 'Total' )
			           )
			       )
			   );
		
			tbody = $.TBODY({});
			
			for (j=0;j<items[i].length;j++)
			{
				row =
			       $.TR({},
			           $.TD({ Class:"qty" },
			               $.INPUT({ Class:"item", type:"text", name:i+"_"+j, value:"0", size:"1" })
			           ),
			           $.TD({}, items[i][j][0] ),
			           $.TD({}, items[i][j][1].toFixed(2) ),
			           $.TD({ Class:"item_total" },
			               $.INPUT({ Class:i+"_itemtotal", type:"text", name:i+"_"+j+"_itemtotal", id:i+"_"+j+"_itemtotal", value:"0", size:"1", disabled:"true" })
			           )
			       );
			       
			    tbody.appendChild(row);
			    
		    }
		    
			row_total =
		       $.TR({},
		           $.TD({ Class:"blank", colSpan:"2" }, $.NBSP),
		           $.TD({ Class:"room_total" }, 'Total' ),
		           $.TD({ Class:"room_total", align:"center" },
		               $.INPUT({ Class:"roomtotal", type:"text", name:i+"_roomtotal", id:i+"_roomtotal", value:"0", size:"1", disabled:"true" })
		           )
		       );

		    tbody.appendChild(row_total);
		    table.appendChild(tbody);
		    document.getElementById('calc').appendChild(table);
		    
	    }
	
	}
    
    $(".item").calc();


});

$.fn.calc = function()
{

	this.click(
		function(e)
		{	
			if (this.value == 0) this.value='';

		}
	);

	this.blur(
		function(e)
		{	
			// determine room id and item id
			var params = this.name.split('_');
			
			// grab the value
			var qty = parseInt(this.value);
			if (isNaN(qty) == true) qty = 0;
			
			// update the item total with new calculated value
			var itemtotal = document.getElementById(this.name + '_itemtotal');
			if (params[0] && params[1]) {
				
				// parse id's
				var room_id = parseInt(params[0]);
				var item_id = parseInt(params[1]);
				
				// if the value is not 0 add an element to the form to be submitted
				resultform = document.getElementById('fm-result');
				var input_exists = document.getElementById(this.name);
				if (qty > 0) {
				    if (input_exists) {
				    	input_exists.value = qty;
				    } else {
					    var input_new = $.INPUT({ type:"hidden", id:this.name, name:rooms[room_id]+": "+items[room_id][item_id][0], value:qty });
					    resultform.appendChild(input_new);
				    }
				} else {
					if (input_exists) resultform.removeChild(input_exists);
				}
				
				// grab space total
				var spacetotal = document.getElementById('spacetotal');
				var spacetotal_view = document.getElementById('spacetotal_view');
				spacetotal_value = 0.00;
				
				// update modified room value
				//itemtotal.value = qty * items[room_id][item_id][1] * items[room_id][item_id][2]; // metres by price
				var itemtotal_value = qty * items[room_id][item_id][1]; // just cubic metres
				itemtotal.value = itemtotal_value.toFixed(2);
				
				// cycle through the totals for this item
				for (i=0;i<rooms.length;i++)
				{
					// grab room total
					var roomtotal = document.getElementById(i + '_roomtotal');

					// check the room exists
					if (!roomtotal) continue;
					
					// update the value of this room total
					if (i == room_id) {
						roomtotal_value = 0.00;
						
						// cycle through the totals for this item
						for (j=0;j<items[room_id].length;j++)
						{
							itemtotal_value = parseFloat(document.getElementById(room_id + '_' + j + '_itemtotal').value);
							roomtotal_value = roomtotal_value + itemtotal_value;
		
						}
						
						roomtotal.value = roomtotal_value;
					}
					
					// add up space total
					spacetotal_value = spacetotal_value + parseFloat(roomtotal.value);
				}
				
				// update space total
				spacetotal.value = spacetotal_value;
				spacetotal_view.value = spacetotal_value;
				
			}

		}
	);
	return this;

}