function update_variety_menu(el) {
	var f = el.form;
	var kind_menu = f.kind_names;
	var kind = kind_menu.value;
    var var_menu = f.variety_names;
	var l = 0;			// array length
	var a = 0;			// loop variable
	var v = '';			// menu value
	var t = '';			// menu text
	var unsetVariety = false;
	// Selected a kind.
	if (kind_menu.selectedIndex >= 0) {
		// Might have a variety list.
		if (typeof(variety_menu_array[kind]) != 'undefined') {
			l = variety_menu_array[kind].length;
			// Have a variety list.
			if (l > 0) {
				var_menu.length = (l / 2);
				for (a = 0; a < l; a += 2) {
					v = variety_menu_array[kind][a];
					t = variety_menu_array[kind][a + 1];
					var_menu.options[(a / 2)].value = v;
					// A variety.
					if (t.substring(0, 1) == '#') {
						t = String.fromCharCode(32) + '    ' + t;		// IE6 hack - &nbsp; won't work!?
						var_menu.options[(a / 2)].className = 'varietyOptions';
					} else {
						var_menu.options[(a / 2)].className = '';
					}
					var_menu.options[(a / 2)].text = t;
					var_menu.options[(a / 2)].selected = false;
				}
			// Don't have a variety list.
			} else {
				unsetVariety = true;
			}
		// Don't have a variety list.
		} else {
			unsetVariety = true;
		}
	// De-selected a kind.
	} else {
		unsetVariety = true;
	}

	// Unset variety list.
	if (unsetVariety) {
		var_menu.length = 0;
	}
}

function synch_controls(e) {
	var f = e.form;
	// Read about this kind.
	if (f.kind_names.selectedIndex >= 0) {
		div_control('read_about_kind_off', false);
		div_control('read_about_kind_on', true);
	} else {
		div_control('read_about_kind_on', false);
		div_control('read_about_kind_off', true);
	}
	// Read about this subkind/variety.
	if (f.variety_names.selectedIndex >= 0) {
		// Read about this variety.
		if (f.variety_names.value.length > 1) {
			div_control('read_about_variety_off', false);
			div_control('read_about_subkind_on', false);
			div_control('read_about_variety_on', true);
		// Read about this subkind.
		} else {
			div_control('read_about_variety_off', false);
			div_control('read_about_variety_on', false);
			div_control('read_about_subkind_on', true);
		}
	} else {
		div_control('read_about_subkind_on', false);
		div_control('read_about_variety_on', false);
		div_control('read_about_variety_off', true);
	}
	// Add to Seed List button.
	if (variety_is_selected(f)) {
		// Activate.
		div_control('add_seed_off', false);
		div_control('add_seed_on', true);
	} else {
		// Deactivate.
		div_control('add_seed_on', false);
		div_control('add_seed_off', true);
	}
}

function variety_is_selected(f) {
	// A kind is selected.
	if (f.kind_names.selectedIndex >= 0) {
		// A variety is selected.
		if (f.variety_names.value.length > 1) {
			return true;
		}
	}
	return false;
}

function add_seed_to_list(f) {
	// We must check if both menus have valid selections.
	if (variety_is_selected(f)) {
		var kind_menu = f.kind_names;
		var kind_text = trim(kind_menu[kind_menu.selectedIndex].text);
		var var_menu = f.variety_names;
		var var_text = trim(var_menu[var_menu.selectedIndex].text);
		// The seed name.
		var seed_name = kind_text + ', ' + var_text;
		// There was an available empty line and we added seed.
		var added_seed = false;
		var last_row = 0;
		for (var i = 0; i < f.length; i++) {
			var el = f.elements[i];
			if ((el.type == 'text') && (el.name.substring(0, 5) == 'seed_')) {
				// Store last row number.
				last_row = el.name.substring(5);
				if (isBlank(el.value)) {
					// Add it here.
					added_seed = true;
					el.value = seed_name;
					el = document.getElementById('quantity_' + last_row);
					el.value = '1';
					el.focus();
					el.select();
					window.scrollBy(0, -100);
					break;
				}
			}
		}
		// Seed wasn't added, must add a row.
		if (added_seed != true) {
			add_row_to_seed_list();
			el = document.getElementById('seed_' + ((last_row - 0) + 1));
			el.value = seed_name;
			el = document.getElementById('quantity_' + ((last_row - 0) + 1));
			el.value = '1';
			el.focus();
			el.select();
			window.scrollBy(0, -100);
		}
	}
}

function clear_row(num) {
    // If number is zero, clear all rows.
	// Form.
	var f = document.bulk_quote_form;
	// Clear all rows.
	if (num == '0') {
		for (var i = 0; i < f.length; i++) {
			var el = f.elements[i];
			if ((el.type == 'text') && ((el.name.substring(0, 5) == 'seed_') || (el.name.substring(0, 9) == 'quantity_') || (el.name.substring(0, 5) == 'date_')) ) {
				el.value = '';
			} else if ((el.type == 'select-one') && (el.name.substring(0, 5) == 'unit_')) {
				el.selectedIndex = 0;
			}
		}
	// Clear one row.
	} else {
		var el = document.getElementById('seed_' + num); el.value = '';
		el = document.getElementById('quantity_' + num); el.value = '';
		el = document.getElementById('unit_' + num); el.selectedIndex = 0;
		el = document.getElementById('date_' + num); el.value = '';
	}
}

function validate_quantity(f, line) {
	// Quantity value must be >= 0.25 lb (or blank). We convert to pounds if necessary.
	// Quantity.
	var quant = document.getElementById('quantity_' + line);
	var val = trim(quant.value);
	quant.value = val;
	// Blank is okay.
	if (isBlank(val)) return true;
	// Check for valid number.
	var val_num = parseFloat(val);
	if (val.match(/[^0-9\.]/) || isNaN(val_num)) {
		alert("Please enter a decimal number only. For instance, enter \"0.25\" for a quarter-pound.");
		return false;
	}
	quant.value = val_num;
	// Unit.
	var un = document.getElementById('unit_' + line).value;
	// Unit multiplier to convert to pounds.
	var mult = 1;
	if (un == 'kg') {
		// kg to pounds
		mult = 2.205;
	} else if (un == 'tons') {
		// tons to pounds
		mult = 2000;
	}
	// Check minimum amount if not 1000-seed count.
	if ((un != 'M') && ((val_num * mult) < 0.25) ) {
		var msg = 'This quantity does not meet the minimum amount (0.25 lb per variety).'
		if (un != 'lbs') msg += "\n" + 'In ' + un + ', the minimum amount is ' + (Math.round(0.25 / mult * 100000) / 100000) + ' ' + un + '.';
		alert(msg);
		return false;
	}
	return true;
}

function add_row_to_seed_list() {
	var tbl = document.getElementById('bulk_seed_table');
	var lastRow = tbl.rows.length;
	// There are two extra header rows, so subtract two. Add one to start with next highest number.
	var iteration = lastRow - 2 + 1;
	// Maximum number of rows.
	if (iteration > 50) return;
	var row = tbl.insertRow(lastRow);

	// Number cell.
	var num_cell = row.insertCell(0);
	num_cell.className = 'smallText';
	var textNode = document.createTextNode(iteration + '.');
	num_cell.appendChild(textNode);

	// Seed name cell
	var seed_name_cell = row.insertCell(1);
	var el = document.createElement('input');
	el.type = 'text';
	el.size = 35;
	el.maxLength = 255;
	el.name = 'seed_' + iteration;
	el.id = 'seed_' + iteration;
	seed_name_cell.appendChild(el);

	// Quantity cell
	var quantity_cell = row.insertCell(2);
	el = document.createElement('input');
	el.type = 'text';
	el.size = 3;
	el.maxLength = 8;
	el.name = 'quantity_' + iteration;
	el.id = 'quantity_' + iteration;
	el.onblur = function() { validate_quantity(this.form, iteration); }
	quantity_cell.appendChild(el);

	// Unit cell
	var unit_cell = row.insertCell(3);
	var sel = document.createElement('select');
	sel.name = 'unit_' + iteration;
	sel.id = 'unit_' + iteration;
	sel.size = 1;
	sel.onchange = function() { validate_quantity(this.form, iteration); }
	sel.options[0] = new Option('lbs', 'lbs');
	sel.options[1] = new Option('kg', 'kg');
	sel.options[2] = new Option('M', 'M');
//	sel.options[3] = new Option('tons', 'tons');
	unit_cell.appendChild(sel);

	// Date cell
	var date_cell = row.insertCell(4);
	date_cell.align = 'center';
	el = document.createElement('input');
	el.type = 'text';
	el.size = 12;
	el.maxLength = 20;
	el.name = 'date_' + iteration;
	el.id = 'date_' + iteration;
	date_cell.appendChild(el);

	// Clear row cell.
	var clear_cell = row.insertCell(5);
	clear_cell.align = 'right';
	var anc = document.createElement('a');
	anc.href = 'Javascript:void(0);';
	anc.onclick = function() { clear_row(iteration); }
//	anc.setAttribute('href', 'Javascript:void(0);');
//	anc.setAttribute('onclick', "clear_row('" + iteration + "');");
	var new_image = document.createElement('img');
	new_image.src = 'graphics/box_delete_this.gif';
	new_image.width = 15;
	new_image.height = 9;
	new_image.border = 0;
	new_image.alt = '';
	anc.appendChild(new_image);
	clear_cell.appendChild(anc);
}
