var ns4 = (navigator.appName.indexOf("Netscape")>=0 && !document.getElementById)? true : false;
var ie4 = (document.all && !document.getElementById)? true : false;
var ie5 = (document.getElementById && document.all)? true : false;
var ns6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
var w3c = (document.getElementById)? true : false;

function convert(whichOne) {
	var cf = document.convertForm;
	
	if (whichOne == "temp1") {
		var fTemp = cf.f.value;

		if ((fTemp == null) || (fTemp == "") || (isNaN(fTemp))) {
			alert("WARNING: Please enter a Farenheit temperature.");
			cf.f.focus();
			cf.f.select();
			return false
		}

		var fTemp = Number(fTemp);
		cTemp = (fTemp - 32) * 5 / 9;
		cTemp = roundNum(cTemp,1);

		cf.c.value = cTemp;
		return;
	}

	if (whichOne == "temp2") {
		var cTemp = cf.c.value;

		//  Ensure that a numerical value has been entered
		if ((cTemp == null) || (cTemp == "") || (isNaN(cTemp))) {
			alert("WARNING: Please enter a Celsius temperature.");
			cf.c.focus();
			cf.c.select();
			return false
		}

		var cTemp = Number(cTemp);
		fTemp = (cTemp * 9 / 5) + 32;
		fTemp = roundNum(fTemp,1);

		cf.f.value = fTemp;
		return;
	}

	if (whichOne == "weight1") {
		var weight1Text = cf.weight1Text.value;
		var weight1 = cf.weight1.selectedIndex;
		var weight2 = cf.weight2.selectedIndex;

		//  Ensure that a numerical value has been entered
		if ((weight1Text == null) || (weight1Text == "") || (isNaN(weight1Text)) || (weight1Text < 0)) {
			var whatUnit = cf.weight1.options[weight1].text;
			alert("WARNING: Please enter a weight in " + whatUnit + ".");
			cf.weight1Text.focus();
			cf.weight1Text.select();
			return false
		}
		if (weight1 == 5) {		//  If "pounds", find ounces
			var ounce1 = cf.ounce1.value;
			if (!checkError(ounce1, "ounce1", "correct ounces")) {return}
			cf.hideOunces.value = cf.ounce1.value;
		}
		
		var weight2Text = convertWeight(weight1Text,weight1,weight2,"no");
		cf.weight2Text.value = weight2Text;
		cf.hideOunces.value = 0;
		if (weight2 == 5) {		//  If "pounds", display ounces
			cf.ounce2.value = cf.finalOunces.value
			var ounceRow2 = eval(getLabel("ounceRow2"));
			ounceRow2.style.display = "";
		}
		var wtConvert = "1 " + wtAbbr(weight1) + " = " + convertWeight(1,weight1,weight2,"convert") + " " + wtAbbr(weight2);
		cf.wtConvert.value = wtConvert;
		return;
	}

	if (whichOne == "weight2") {
		var weight2Text = cf.weight2Text.value;
		var weight1 = cf.weight1.selectedIndex;
		var weight2 = cf.weight2.selectedIndex;

		//  Ensure that a numerical value has been entered
		if ((weight2Text == "") || (isNaN(weight2Text)) || (weight2Text == null) || (weight2Text < 0)) {
			var whatUnit = cf.weight2.options[weight2].text;
			alert("WARNING: Please enter a weight in " + whatUnit + ".");
			cf.weight2Text.focus();
			cf.weight2Text.select();
			return false
		}
		if (weight2 == 5) {		//  If "pounds", find ounces
			var ounce2 = cf.ounce2.value;
			if (!checkError(ounce2, "ounce2", "correct ounces")) {return}
			cf.hideOunces.value = cf.ounce2.value;
		}
		var weight1Text = convertWeight(weight2Text,weight2,weight1,"no");
		cf.weight1Text.value = weight1Text;
		cf.hideOunces.value = 0;
		if (weight1 == 5) {		//  If "pounds", display ounces
			cf.ounce1.value = cf.finalOunces.value
			var ounceRow1 = eval(getLabel("ounceRow1"));
			ounceRow1.style.display = "";
		}
		var wtConvert = "1 " + wtAbbr(weight2) + " = " + convertWeight(1,weight2,weight1,"convert") + " " + wtAbbr(weight1);
		cf.wtConvert.value = wtConvert;
		return;
	}

	if (whichOne == "vol1") {
		var vol1Text = cf.vol1Text.value;
		var vol1 = cf.vol1.selectedIndex;
		var vol2 = cf.vol2.selectedIndex;

		//  Ensure that a numerical value has been entered
		if ((vol1Text == null) || (vol1Text == "") || (isNaN(vol1Text)) || (vol1Text < 0)) {
			var whatUnit = cf.vol1.options[vol1].text;
			alert("WARNING: Please enter a volume in " + whatUnit + ".");
			cf.vol1Text.focus();
			cf.vol1Text.select();
			return false
		}

		var vol2Text = convertVol(vol1Text,vol1,vol2);
		cf.vol2Text.value = vol2Text;
		var volConvert = "1 " + volAbbr(vol1) + " = " + convertVol(1,vol1,vol2) + " " + volAbbr(vol2);
		cf.volConvert.value = volConvert;
		return;
	}

	if (whichOne == "vol2") {
		var vol2Text = cf.vol2Text.value;
		var vol1 = cf.vol1.selectedIndex;
		var vol2 = cf.vol2.selectedIndex;

		//  Ensure that a numerical value has been entered
		if ((vol2Text == "") || (isNaN(vol2Text)) || (vol2Text == null) || (vol2Text < 0)) {
			var whatUnit = cf.vol2.options[vol2].text;
			alert("WARNING: Please enter a volume in " + whatUnit + ".");
			cf.vol2Text.focus();
			cf.vol2Text.select();
			return false
		}
		var vol1Text = convertVol(vol2Text,vol2,vol1);
		cf.vol1Text.value = vol1Text;
		var volConvert = "1 " + volAbbr(vol2) + " = " + convertVol(1,vol2,vol1) + " " + volAbbr(vol1);
		cf.volConvert.value = volConvert;
		return;
	}

	if (whichOne == "len1") {
		var len1Text = cf.len1Text.value;
		var len1 = cf.len1.selectedIndex;
		var len2 = cf.len2.selectedIndex;

		//  Ensure that a numerical value has been entered
		if ((len1Text == null) || (len1Text == "") || (isNaN(len1Text)) || (len1Text < 0)) {
			var whatUnit = cf.len1.options[len1].text;
			alert("WARNING: Please enter a length in " + whatUnit + ".");
			cf.len1Text.focus();
			cf.len1Text.select();
			return false
		}
		if (len1 == 6) {		//  If "feet", find inches
			var inch1 = cf.inch1.value;
			if (!checkError(inch1, "inch1", "correct inches")) {return}
			cf.hideInches.value = cf.inch1.value;
		}

		var len2Text = convertLen(len1Text,len1,len2,"no");
		cf.len2Text.value = len2Text;
		cf.hideInches.value = 0;
		if (len2 == 6) {
			cf.inch2.value = cf.finalInches.value
			var inchRow2 = eval(getLabel("inchRow2"));
			inchRow2.style.display = "";
		}
		var lenConvert = "1 " + lenAbbr(len1) + " = " + convertLen(1,len1,len2,"convert") + " " + lenAbbr(len2);
		cf.lenConvert.value = lenConvert;
		return;
	}

	if (whichOne == "len2") {
		var len2Text = cf.len2Text.value;
		var len1 = cf.len1.selectedIndex;
		var len2 = cf.len2.selectedIndex;

		//  Ensure that a numerical value has been entered
		if ((len2Text == "") || (isNaN(len2Text)) || (len2Text == null) || (len2Text < 0)) {
			var whatUnit = cf.len2.options[len2].text;
			alert("WARNING: Please enter a length in " + whatUnit + ".");
			cf.len2Text.focus();
			cf.len2Text.select();
			return false
		}
		if (len2 == 6) {		//  If "feet", find inches
			var inch2 = cf.inch2.value;
			if (!checkError(inch2, "inch2", "correct inches")) {return}
			cf.hideInches.value = cf.inch2.value;
		}

		var len1Text = convertLen(len2Text,len2,len1,"no");
		cf.len1Text.value = len1Text;
		cf.hideInches.value = 0;
		if (len1 == 6) {
			cf.inch1.value = cf.finalInches.value
			var inchRow1 = eval(getLabel("inchRow1"));
			inchRow1.style.display = "";
		}
		var lenConvert = "1 " + lenAbbr(len2) + " = " + convertLen(1,len2,len1,"convert") + " " + lenAbbr(len1);
		cf.lenConvert.value = lenConvert;
		return;
	}
}

function convertWeight(weight,weight1,weight2,convertText) {
	parseFloat(weight);

	//  Convert current weight to kilograms
	if (weight1 == 0) {weight = weight / (1000 * 1000 * 1000);}
	if (weight1 == 1) {weight = weight / (1000 * 1000);}
	if (weight1 == 2) {weight = weight / 1000;}
	if (weight1 == 3) {weight = weight;}
	if (weight1 == 4) {weight *= 0.02834952;}
	if (weight1 == 5) {		//  Pounds & ounces
		var ounce = Number(cf.hideOunces.value);
		weight = Number(weight) + ounce / 16;
		weight *= 0.45359237;
	}
	
	//  Convert kilograms to desired weight
	if (weight2 == 0) {weight *= 1000000000;}
	if (weight2 == 1) {weight *= 1000000;}
	if (weight2 == 2) {weight *= 1000;}
	if (weight2 == 3) {weight = weight;}
	if (weight2 == 4) {weight *= 35.273966;}
	if (weight2 == 5) {		//  Pounds & ounces
		weight *= 2.2046226;
		if (convertText != "convert") {	// calculate ounces, only if not part of conversion text
			if (roundNum(weight,3) == Math.round(weight)) {weight = Math.round(weight)}
			var newOunce = (weight*16) % 16;
			weight = Math.floor(weight);
			cf.finalOunces.value = roundNum(newOunce,1);
		}
	}
	
	if (weight < 1)	{weight = roundNum(weight,5);}
		else {weight = roundNum(weight,2);}
	return weight;
}

function wtAbbr(wtType) {
	if (wtType == 0) {wtType = "mcg"}
	if (wtType == 1) {wtType = "mg"}
	if (wtType == 2) {wtType = "gm"}
	if (wtType == 3) {wtType = "kg"}
	if (wtType == 4) {wtType = "oz"}
	if (wtType == 5) {wtType = "lb"}
	
	return wtType;
}


function convertVol(vol,vol1,vol2) {
	parseFloat(vol);

	//  Convert current volume to liters
	if (vol1 == 0) {vol = vol / 1000;}
	if (vol1 == 1) {vol = vol / 1000;}
	if (vol1 == 2) {vol = vol;}
	if (vol1 == 3) {vol *= 0.02957303;}
	if (vol1 == 4) {vol *= 0.2365882;}
	if (vol1 == 5) {vol *= 0.4731765;}
	if (vol1 == 6) {vol *= 0.9463529;}
	if (vol1 == 7) {vol *= 3.785412;}
	if (vol1 == 8) {vol *= 0.004928922;}
	if (vol1 == 9) {vol *= 0.01478676;}
	
	//  Convert liters to desired volume
	if (vol2 == 0) {vol *= 1000;}
	if (vol2 == 1) {vol *= 1000;}
	if (vol2 == 2) {vol = vol;}
	if (vol2 == 3) {vol *= 33.814594;}
	if (vol2 == 4) {vol *= 4.2267535;}
	if (vol2 == 5) {vol *= 2.1133763;}
	if (vol2 == 6) {vol *= 1.0566883;}
	if (vol2 == 7) {vol *= 0.26417204;}
	if (vol2 == 8) {vol *= 202.88412;}
	if (vol2 == 9) {vol *= 67.628067;}
	
	if (vol < 2) {vol = roundNum(vol,5);}
		else {vol = roundNum(vol,2);}
	return vol;
}

function volAbbr(volType) {
	if (volType == 0) {volType = "cc"}
	if (volType == 1) {volType = "ml"}
	if (volType == 2) {volType = "L"}
	if (volType == 3) {volType = "oz"}
	if (volType == 4) {volType = "cups"}
	if (volType == 5) {volType = "pint"}
	if (volType == 6) {volType = "quarts"}
	if (volType == 7) {volType = "gal"}
	if (volType == 8) {volType = "tsp"}
	if (volType == 9) {volType = "tbsp"}
		
	return volType;
}


function convertLen(len,len1,len2,convertText) {
	var cf = document.convertForm;
	parseFloat(len);

	//  Convert current length to meters
	if (len1 == 0) {len = len / 1000000;}
	if (len1 == 1) {len = len / 1000;}
	if (len1 == 2) {len = len / 100;}
	if (len1 == 3) {len = len;}
	if (len1 == 4) {len *= 1000;}
	if (len1 == 5) {len *= 0.0254;}
	if (len1 == 6) {		//  Feet and inches
		var inch = Number(cf.hideInches.value);
		len = Number(len) + inch / 12;
		len *= 0.3048;
	}
	if (len1 == 7) {len *= 0.9144;}
	if (len1 == 8) {len *= 1609.347;}
	
	//  Convert meters to desired length
	if (len2 == 0) {len *= 1000000;}
	if (len2 == 1) {len *= 1000;}
	if (len2 == 2) {len *= 100;}
	if (len2 == 3) {len = len;}
	if (len2 == 4) {len = len / 1000;}
	if (len2 == 5) {len *= 39.370079;}
	if (len2 == 6) {		//  Feet and inches
		len *= 3.2808399;
		if (convertText != "convert") {	// calculate inches, only if not part of conversion text
			var newInch = (len*12) % 12;
			len = Math.floor(len);
			cf.finalInches.value = roundNum(newInch,1);
		}
	}
	if (len2 == 7) {len *= 1.0936133;}
	if (len2 == 8) {len *= 0.00062137003;}
	
	if (len < 2) {len = roundNum(len,5);}
		else {len = roundNum(len,2);}
	return len;
}

function lenAbbr(lenType) {
	if (lenType == 0) {lenType = "µm"}	//  µ = alt+0181
	if (lenType == 1) {lenType = "mm"}
	if (lenType == 2) {lenType = "cm"}
	if (lenType == 3) {lenType = "m"}
	if (lenType == 4) {lenType = "km"}
	if (lenType == 5) {lenType = "in"}
	if (lenType == 6) {lenType = "ft"}
	if (lenType == 7) {lenType = "yd"}
	if (lenType == 8) {lenType = "mi"}
		
	return lenType;
}

function changeStatus(first,last,whichOne) {
	cf = document.convertForm;
	first = eval("cf." + whichOne + first + ".selectedIndex");
	last = eval("cf." + whichOne + last + ".selectedIndex");
	var statusText = "";
	
	for (var i=1; i <= 2; i++) {
		if (i == 1) {iNum = first} else {iNum = last}
		if (whichOne == "weight") {
			if (iNum == 0) {statusText += "Micrograms"}
			if (iNum == 1) {statusText += "Milligrams"}
			if (iNum == 2) {statusText += "Grams"}
			if (iNum == 3) {statusText += "Kilograms"}
			if (iNum == 4) {statusText += "Ounces"}
			if (iNum == 5) {statusText += "Pounds"}
		} else if (whichOne == "vol") {
			if (iNum == 0) {statusText += "Cubic Centimeters (cc)"}
			if (iNum == 1) {statusText += "Milliliters"}
			if (iNum == 2) {statusText += "Liters"}
			if (iNum == 3) {statusText += "Ounces"}
			if (iNum == 4) {statusText += "Cups"}
			if (iNum == 5) {statusText += "Pints"}
			if (iNum == 6) {statusText += "Quarts"}
			if (iNum == 7) {statusText += "Gallons"}
			if (iNum == 8) {statusText += "Teaspoons"}
			if (iNum == 9) {statusText += "Tablespoons"}
		} else if (whichOne == "len") {
			if (iNum == 0) {statusText += "Microns"}
			if (iNum == 1) {statusText += "Millimeters"}
			if (iNum == 2) {statusText += "Centimeters"}
			if (iNum == 3) {statusText += "Meters"}
			if (iNum == 4) {statusText += "Kilometers"}
			if (iNum == 5) {statusText += "Inches"}
			if (iNum == 6) {statusText += "Feet"}
			if (iNum == 7) {statusText += "Yards"}
			if (iNum == 8) {statusText += "Miles"}
		}
		if (i == 1) {statusText += " to "}
	}
	
	self.status = "Convert " + statusText;
	return true;
}


function isFeet(x) {
	var cf = document.convertForm;
	if (x == 1) {
		var lenNum = cf.len1.selectedIndex;
		var len = cf.len1.options[lenNum].text;
		var inchRow = eval(getLabel("inchRow1"));
	}
	if (x == 2) {
		var lenNum = cf.len2.selectedIndex;
		var len = cf.len2.options[lenNum].text;
		var inchRow = eval(getLabel("inchRow2"));
	}
	if (len == "feet") {inchRow.style.display = "";}
		else {inchRow.style.display = "none"}
	return true;
}

function isOunce(x) {
	var cf = document.convertForm;
	if (x == 1) {
		var wtNum = cf.weight1.selectedIndex;
		var wt = cf.weight1.options[wtNum].text;
		var ounceRow = eval(getLabel("ounceRow1"));
	}
	if (x == 2) {
		var wtNum = cf.weight2.selectedIndex;
		var wt = cf.weight2.options[wtNum].text;
		var ounceRow = eval(getLabel("ounceRow2"));
	}
	if (wt == "pounds") {ounceRow.style.display = "";}
		else {ounceRow.style.display = "none"}
	return true;
}

function checkError(whatVar, varName, varText) {
	if (whatVar < 0 || isNaN(whatVar) || ((varName == "inch1" || varName == "inch2") && whatVar >= 12) || ((varName == "ounce1" || varName == "ounce2") && whatVar >= 16)) {
		alert("Please enter the " + varText + ".");
		eval("document.convertForm." + varName + ".focus();");
		eval("document.convertForm." + varName + ".select();");
		return false;
	}
	return true;
}

function getLabel(nameID) {
	var whatTag;
	if (ie4 || w3c) {whatTag = "document.all[\"" + nameID + "\"]";}
	if (ns6) {whatTag = "document.getElementById(\"" + nameID + "\")";}
	return whatTag;	
}

function roundNum(number,decimal) {
	multiplier = Math.pow(10,decimal);
	number = Math.round(number * multiplier) / multiplier;
	return number
}
