function setConv(type) {											// Change the conversion routine based on which radio button is clicked
	if (type == 'w') {													// Units of measure for conversion by weight 
		var details = [ 1, "grams (g)",
						1000, "kilograms (kg)",
						28.349523, "ounces (oz)",
						453.59237, "pounds (lb)" ];
		document.form.type.value = 'w';									// Set hidden value for reference later by javascript
	}
	else if (type == 'v') {												// Units of measure for conversion by volume
		var details = [ 1000, "liters (l)",
						100, "deciliters (dl)",
						10, "centiliters (cl)",
						1, "milliliters (ml)",
						3785.412, "gallons (gal)",
						946.353, "quarts (qt)",
						473.1765, "pints (pt)",
						236.58825, "cups (c)",
						29.573531, "fluid ounces (fl oz)",
						14.7867647, "tablespoons (tb)",
						4.928921593, "teaspoons (t)" ];
		document.form.type.value = 'v';									// Set hidden value for reference later by javascript
	}
	else if (type == 'l') {												// Units of measure for conversion by length
		var details = [ 1, "millimeters (mm)",
						10, "centimeters (cm)",
						1000, "meters (m)",
						25.4, "inches (in)",
						304.8, "feet (ft)" ];
		document.form.type.value = 'l';									// Set hidden value for reference later by javascript
	}
	else if (type == 't') {												// Units of measure for conversion by temperature
		var details = [ 1, "Fahrenheit (F)",
						-1, "centigrade (C)",
						0, "thermostat" ];
		document.form.type.value = 't';									// Set hidden value for reference later by javascript
	}
	else if (type == 'x') {												// Units of measure for approximation of weight to volume conversion
		var detailX = [ .005, "grams (g) of granulated sugar",
						.1417476150, "ounces (oz) of granulated sugar",
						.0083333333, "grams (g) of confectioner's sugar",
						.2362460250, "ounces (oz) of confectioner's sugar",
						.0071428571, "grams (g) of all-purpose flour",
						.2024965929, "ounces (oz) of all-purpose flour",
						.008, "grams (g) of cake flour",
						.226796184, "ounces (oz) of cake flour",
						.0074074074, "grams (g) of cornstarch",
						.2099964667, "ounces (oz) of cornstarch",
						.0076923077, "grams (g) of yellow cornmeal",
						.2180732538, "ounces (oz) of yellow cornmeal",
						.0095238095, "grams (g) of cocoa powder",
						.2699954571, "ounces (oz) of cocoa powder",
						.0052083333, "grams (g) of baking powder",
						.1476537656, "ounces (oz) of baking powder",
						.00390625, "grams (g) of baking soda",
						.1107403242, "ounces (oz) of baking soda",
						.0065789474, "grams (g) of salt (fine)",
						.1865100197, "ounces (oz) of salt (fine)",
						.0044247788, "grams (g) of butter",
						.1254403673, "ounces (oz) of butter",
						.00625, "grams (g) of honey",
						.1771845188, "ounces (oz) of honey",
						.0066666667, "grams (g) of almonds (whole)",
						.18899682, "ounces (oz) of almonds (whole)",
						.0071428571, "grams (g) of hazelnuts (whole)",
						.2024965929, "ounces (oz) of hazelnuts (whole)",
						.0071428571, "grams (g) of pistachios (whole)",
						.2024965929, "ounces (oz) of pistachios (whole)",
						.0090909091, "grams (g) of walnuts (halves)",
						.2577229364, "ounces (oz) of walnuts (halves)" ];
		var detailY = [ 1, "cups (c)",
						.0625, "tablespoons (tb)",
						.020833333, "teaspoons (t)" ];

		document.form.in_units.options.length = 0;						// Clear pull-down menus
		for (n = 0; n < detailX.length/2; n++) {						// Populate pull-down menus based on type of conversion
			x = 2*n;
			y = x+1;
			document.form.in_units.options.length = n + 1;					// Increase pull-down size by 1
			document.form.in_units.options[n].text = detailX[y];			// Plug in text information in pull-down menu
			document.form.in_units.options[n].value = detailX[x];			// Plug in conversion information in pull-down menu
		}
		document.form.in_units.selectedIndex = 0;							// Indicate that the first item in this pull-down is selected
		document.form.input.value = '';									// Clear text input field on form
		
		document.form.out_units.options.length = 0;						// Clear pull-down menus
		for (n = 0; n < detailY.length/2; n++) {						// Populate pull-down menus based on type of conversion
			x = 2*n;
			y = x+1;
			document.form.out_units.options.length = n + 1;					// Increase pull-down size by 1
			document.form.out_units.options[n].text = detailY[y];			// Plug in text information in pull-down menu
			document.form.out_units.options[n].value = detailY[x];			// Plug in conversion information in pull-down menu
		}
		document.form.out_units.selectedIndex = 0;							// Indicate that the first item in this pull-down is selected
		document.form.output.value = '';									// Clear text output field on form
		
		document.form.type.value = 'x';									// Set hidden value for reference later by javascript

		alert('Caution: converting dry ingredients from \rweight to volume is not precise. Use \rthe result as an estimate only.'); // Warn about using this converter
		return;
	}

	document.form.in_units.options.length = 0;							// Clear pull-down menus
	for (n = 0; n < details.length/2; n++) {							// Populate pull-down menus based on type of conversion
		x = 2*n;
		y = x+1;
		document.form.in_units.options.length = n + 1;						// Increase pull-down size by 1
		document.form.out_units.options.length = n + 1;
		document.form.in_units.options[n].text = details[y];				// Plug in text information in pull-down menu
		document.form.out_units.options[n].text = details[y];
		document.form.in_units.options[n].value = details[x];				// Plug in conversion information in pull-down menu
		document.form.out_units.options[n].value = details[x];
	}
	document.form.in_units.selectedIndex = 0;								// Indicate that the first item in each pull-down is selected
	document.form.out_units.selectedIndex = 0;
	document.form.input.value = '';										// Clear text input fields on form
	document.form.output.value = '';

}

function calc() {													// Perform actual unit conversion
	var input = document.form.input.value;								// Substitute variable for input field property
	var output = document.form.output;									// Substitute variable for output field object
	var type = document.form.type.value;								// Substitute variable for conversion type: w, v, l, or t
	
	var n = document.form.in_units.selectedIndex;						// Determine "convert from" unit of measure conversion factor
	var in_unit = document.form.in_units.options[n].value;
	
	var m = document.form.out_units.selectedIndex;						// Determine "convert to" unit of measure conversion factor
	var out_unit = document.form.out_units.options[m].value;

	if ((input == '') || (input == ' ') || (!isFinite(input))) {		// Exit if no valid number to convert
		alert('The input field is either empty or contains \ra space character or a non-numeric character. \rPlease enter only numbers in the input field.');
		return

	} else if (type != 't') {											// Perform conversion if not for temperature
		var x = input * in_unit / out_unit;									// Do conversion
		if (x >= 100) {														// Reduce number of digits depending on size of number to the left of the decimal
			output.value = Math.round(x);
		} else if (x >=10) {
			output.value = Math.round(x * 10) / 10;
		} else if (x >=1) {
			output.value = Math.round(x * 100) / 100;
		} else {
			x = Math.round(x * 1000) / 1000;
			if ( x <= 0 ) {													// Add a leading zero if result is less than 0
				x = '0' + x;
			}
			output.value = x;
		}
		return
		
	} else if ((in_unit >= 1) && (out_unit <= -1)) {					// Perform conversion if Fahrenheit to centigrade and round to no more 1 digit right of the decimal
		var x = (input - 32) / 1.8
		output.value = Math.round(x * 10) / 10;
		return

	} else if ((in_unit <= -1) && (out_unit >= 1)) {					// Perform conversion if centigrade to Fahrenheit and round to no more 1 digit right of the decimal
		var x = (input * 1.8) + 32
		output.value = Math.round(x * 10) / 10;
		return

	} else if (in_unit == out_unit) {									// Perform "conversion" if "in" and "out" units are the same
		output.value = input;
		return
		
	} else if ((in_unit > -.5) && (in_unit < .5)) {						// Test for valid entry for conversion from a thermostat setting
		if (input != Math.round(input)) {									// Alert and exit if invalid
			alert('The value to be converted must be an integer from 1 to 10.');	// Non-integer entered
			return
		} else if (((input < 1) || (input > 10)) && ((in_unit < .5) || (in_unit > .5))) {
			alert('The value to be converted must be an integer from 1 to 10.');	// Number outside of range
			return
		}
		var x = input * 30;												// Convert to centigrade degrees
		if (out_unit >= 1) {												// Comvert to fahrenheit degrees if needed
			x = (x * 1.8) + 32;
		}
		output.value = Math.round(x * 10) / 10;							// Dipslay conversion
		return
		
	} else if (in_unit > .5) {											// Assume conversion from Fahrenheit degrees to thermostat reading
		var x = (input - 32) / 1.8											// Convert to centigrade

	} else {															// Assume conversion from centigtade degrees to thermostat reading
		var x = input;
	}
	if ((x < 15) || (x > 315)) {										// Test for range error, exit if error
		alert('The value to be converted is outside the range of thermostat settings. The input temperature must be between 30 and 300 degrees centigrade or 85 and 570 degrees Fahrenheit.');
		return
	}
	var t_stat = [ "1", "1 - 2", "2", "2 - 3", "3", "3 - 4", "4", "4 - 5", "5", "5 - 6", "6", "6 - 7", "7", "7 - 8", "8", "8 - 9", "9", "9 - 10", "10"];

	output.value = t_stat[ Math.round(x / 15) - 2 ];					// Output result from table of possible results (t_stat array)
	return
}

function clearOut() {												// Clear results field. Called by onFocus event handler of input field
	document.form.output.value = '';
}

