var fields = new Array("<a target='_blank' href='"+HTTP_PATH+"LTL-info.php'>Class</a>", "Weight", "Pieces", "Length", "Width",
		"Height", "Cubic Feet", "Cubic Meters");
var options = new Array("0", "50", "55", "60", "65", "70", "77.5", "85",
		"92.5", "100", "110", "125", "150", "175", "200", "250", "300", "400",
		"500");
var RowCount = new Array();
RowCount['Imp'] = 0;
RowCount['Exp'] = 0;

function deleteTable(id) {
	$('#' + id + 'DestinationForm').hide("slow", function() {
		$('#' + id + 'DestinationForm').html('');
		$('#ExpInsur').hide();
	});
}

function initTable(id, insurance) {
	RowCount[id] = 0;
	$('#' + id + 'DestinationForm').hide('slow', function(){
		var mainTable = '<table width="100%" style="float:left;" id="' + id + 'UserTable"></table>';
		var resultTable = '<table width="100%" style="float:left;" id="' + id + 'ResultUserTable"></table>';
		var action = '<div id="'
				+ id
				+ '-action-link" style="float:right;"><a onclick="javascript:addRow(\''
				+ id
				+ '\');" id="'
				+ id
				+ '-add-row-link" href="javascript:void(0);">Add row</a>&nbsp;&nbsp;&nbsp;<a onclick="delRow(\''
				+ id
				+ '\');" id="'
				+ id
				+ '-del-row-link" href="javascript:void(0);">Delete Last row</a></div>';
		$('#' + id + 'DestinationForm').html(mainTable + resultTable + action);
		_TableBuidder(id);
		$('#' + id + 'DestinationForm').show("slow", function(){
			if(id='Exp' && insurance == 1){
				$('#ExpInsur').show('slow');
			}else $('#ExpInsur').hide();
		});
	});
}

function _TableBuidder(id) {
	var obj = $('#' + id + 'UserTable');
	var head = '<thead>';
	var rowResult = '<tr>';

	head += '<tr class="rowhead"><td class="lcl_cell"></td><td class="lcl_cell"><select class="textBox" name="' + id + '-weight_unit" id="' + id + '-weight_unit"><option value="kg">Kilogram</option><option selected="selected" value="pound">Pound</option></select></td><td class="lcl_cell"></td><td colspan="3" class="lcl_cell"><select class="textBox" onclick="javascript:calculate(\''+ id + '\');" name="' + id + '-dimension_unit" id="' + id + '-dimension_unit"><option value="inch">Inches</option><option value="cm">Centimeters</option></select></td><td class="lcl_cell"> </td><td class="lcl_cell"> </td></tr>';
	head += '<tr class="rowhead">';
	for (f in fields) {
		head += '<td class="lcl_cell">' + fields[f] + '</td>';
		rowResult += '<td align="center" class="lcl" id="tabResult' + id + '-'
				+ f + '">&nbsp;</td>';
	}
	rowResult += '</tr>';
	head += '</tr></thead>';
	head += '</tr></thead>';
	obj.html(head);
	addRow(id);
	$('#' + id + 'ResultUserTable').html(rowResult);
	$('#tabResult' + id + '-0').html('Totals:');
}

function addRow(id) {
	RowCount[id]++;
	var rclass = (RowCount[id] % 2 == 0) ? 'rowA' : 'rowB';
	var row = '<tr class="' + rclass + ' row' + RowCount[id] + '">';
	for (k in fields) {
		TDclass = 'lcl ' + id + '-lcl' + RowCount[id] + '-' + k;
		if (k >= 6) {
			row += '<td class="' + TDclass + '">&nbsp;</td>';
		} else if (k == 0) {//this class selector
			row += '<td class="' + TDclass + '">' + _getClassSelector(id)
					+ '</td>';
		} else {
			row += '<td class="' + TDclass + '">' + _getInput(k, id) + '</td>';
		}
	}
	row += '</tr>';
	$('#' + id + 'UserTable').append(row);
}

function delRow(id) {
	if (RowCount[id] == 1)
		return false;
	RowCount[id]--;
	$('#' + id + 'UserTable tr:last').remove();
	calculate(id);
}

function _getInput(cellIndex, id) {
	var suffix = RowCount[id] + '-' + cellIndex;
	return '<input type="text" class="textBox lcl_txt cell' + cellIndex + ' row'
			+ RowCount[id] + '" onblur="javascript:calculate(\'' + id
			+ '\');" name="lcl_' + suffix + '" id="lcl_' + suffix + '"/>';
}

function _getClassSelector(id) {
	var s = '<select id="lcl_class_' + RowCount[id]
			+ '" class="lcl_cmb textBox" name="lcl_' + RowCount[id] + '">';
	for (k in options) {
		s += '<option value="' + options[k] + '">' + options[k] + '</option>';
	}
	s += '</select>';
	return s;
}