// JavaScript Document
// create a "fish" object
function fish (name, level, info) {
	this.name = name; // name of species
	this.level = level; // mercury concentration level in ppm
	this.info = info; // species info text which will appear at bottom of calculator
}
//generic message. put something between the quotes if you want to add generic message
gm = "";
//for fish with no mercury info
noinfo = "There is no information regarding mercury contamination available for this species at this time. This does not mean that this species contains no mercury!";
// create an array of fish objects
var fish_array = [
	new fish("Click to Select", "",gm), // gm is for generic message (see above--currently it is blank)
	new fish("Ahi Tuna (Bigeye, 0.639 ppm)", 0.639,gm),
	new fish("Ahi Tuna (Yellowfin, 0.325 ppm)", 0.325,gm),
	new fish("Albacore (Canned, 0.353 ppm)", 0.353, "Albacore tuna is caught using longline fishing methods that capture or kill millions of whales, dolphins, sharks and sea turtles per year."),
	new fish("Albacore (Fresh/Frozen, 0.357 ppm)", 0.357, "Albacore tuna is caught using longline fishing methods that capture or kill millions of whales, dolphins, sharks and sea turtles per year."),
	new fish("Albacore (Troll-Caught, 0.14 ppm)", 0.14, "Troll-caught albacore are younger, smaller fish and therefore have lower levels of mercury than adults."),
	new fish("Anchovies (0.043 ppm)", 0.043,gm),
	new fish("Bass (Black, Striped, 0.219 ppm)", 0.219,gm),
	new fish("Bass (Chilean, 0.386 ppm)", 0.386,gm),
	new fish("Bluefish (0.337 ppm)", 0.337,gm),
	new fish("Butterfish (0.058 ppm)", 0.058,gm),
	new fish("Buffalofish (0.19 ppm)", 0.19,gm),
	new fish("Carp (0.14 ppm)", 0.14,gm),
	new fish("Catfish  (0.049 ppm)", 0.049,gm),
	new fish("Clams (Not Detectable)", 0.00, "Mercury levels in clams are below the .01 ppm FDA mercury detection limit."),
	new fish("Cod (Atlantic, 0.095 ppm)", 0.095,gm),
	new fish("Crab (0.060 ppm)", 0.06, "Includes blue, king and snow crab."),
	new fish("Crawfish (0.033 ppm)", 0.033,gm),
	new fish("Croaker (Atlantic, 0.072 ppm)", 0.072,gm),
	new fish("Croaker (White, Pacific, 0.287 ppm)", 0.287,gm),
	new fish("Flatfish (0.045 ppm)", 0.045,gm),
	new fish("Flounder (0.045 ppm)", 0.045,gm),
	new fish("Grouper (All, 0.465 ppm)", 0.465,gm),
	new fish("Haddock (Atlantic, 0.031 ppm)", 0.031,gm),
	new fish("Hake (0.014 ppm)", 0.014,gm),
	new fish("Halibut (0.252 ppm)", 0.252,gm),
	new fish("Herring (0.044 ppm)", 0.044,gm),
	new fish("Jacksmelt (0.108 ppm)", 0.108,gm),
	new fish("King Mackerel (0.730 ppm) ", 0.73,gm),
	new fish("Lobster (All, 0.149 ppm)", 0.149,gm),
	new fish("Lobster (Northern, US, 0.310 ppm)", 0.310,gm),
	new fish("Lobster (Spiny, 0.090 ppm)", 0.09,gm),
	new fish("Mackerel (N. Atlantic, 0.050 ppm)", 0.05,gm),
	new fish("Mackerel (Spanish, Gulf, 0.454 ppm)", 0.454,gm),
	new fish("Mackerel (Spanish, Atlantic, 0.18 ppm)", 0.182,gm),
	new fish("Mackerel chub (0.088 ppm)", 0.088,gm),
	new fish("Mahi Mahi (0.16 ppm)", 0.16,"This data is an average value from FDA data published at: http://www.cfsan.fda.gov/~frf/seamehg2.html"),
	new fish("Marlin (0.485 ppm)", 0.485,gm),
	new fish("Monkfish (0.180 ppm)", 0.180,gm),
	new fish("Mullet (0.046 ppm)", 0.046,gm),
	new fish("Orange Roughy (0.554 ppm)", 0.554,"In addition to being high in mercury, orange roughy has seen dramatic declines in many fisheries. From a sustainablility perspective it is best to avoid eating orange roughy."),
	new fish("Oysters (0.013 ppm)", 0.013,"Oysters tested by the FDA were found to be below 0.01 ppm, which is considered very low in mercury."),
	new fish("Plaice (0.045 ppm)", 0.045,gm),
	new fish("Perch (Freshwater, 0.14 ppm)", 0.14,gm),
	new fish("Perch (Ocean, Not Detectable)", 0.00,"FDA data show ocean perch to have mercury levels below detection limit of 0.01 ppm. This is a very low mercury fish."),
	new fish("Pollock (0.041 ppm)", 0.041,gm),
	new fish("Sablefish (0.220 ppm)", 0.22,gm),
	new fish("Salmon (Canned, Not Detectable)", 0.00,gm),
	new fish("Salmon (Fresh/Frozen, 0.014 ppm)", 0.014, "While salmon tends to be low in mercury, farmed salmon contain higher levels of PCB's.  Salmon farms are extremely harmful to coastal ocean ecosystems- Avoid farmed salmon."),
	new fish("Sardine (0.016 ppm)", 0.016,gm),
	new fish("Scallop (0.050 ppm)", 0.050,"Scallp dredges causes severe damage to the seafloor habitat. In addition, endangered sea turtles and other species can be caught incidentally  and are discarded, often dead or dying, as bycatch"),
	new fish("Scorpionfish (0.286 ppm)", 0.286,gm),
	new fish("Shad (American, 0.065 ppm)", 0.065,gm),	
	new fish("Shark (0.988 ppm)", 0.988, "In addition to being high in mercury, many species of shark are being decimated by longline fishing fleets."),
	new fish("Sheepshead (0.128 ppm)", 0.128,gm),
	new fish("Shrimp (Not Detectable, other issues)", 0.00, "Mercury levels in shrimp are less than the FDA 0.01 ppm detection limit.  The majority of shrimp sold in the US comes from environmentally destructive aquaculture facilities in Asia."),
	new fish("Skate (0.137 ppm)", 0.137,gm),
	new fish("Snapper (0.189 ppm)", 0.189,gm),
	new fish("Sole (0.045 ppm)", 0.045,gm),
	new fish("Squid (0.070 ppm)", 0.070,gm),
	new fish("Swordfish (0.976 ppm)", 0.976, "Swordfishing longline fleets are one of the worlds greatest threats to sea turtles, putting some species at risk of extinction over the next 10 years."),
	new fish("Tilapia (0.010 ppm)", 0.010,gm),
	new fish("Tilefish (Atlantic, 0.144 ppm)", 0.144,gm),
	new fish("Tilefish (Gulf of Mexico, 1.450 ppm)", 1.45,gm),
	new fish("Trout (Freshwater, 0.072 ppm)", 0.072,gm),
	new fish("Trout (Ocean, 0.256 ppm)", 0.256,"Sea Trout is sometimes called weakfish"),
	new fish("Tuna (Canned Light, 0.118 ppm)", 0.118,gm),
	new fish("Tuna (Canned Albacore, 0.353 ppm)", 0.353,gm),
	new fish("Tuna (Troll-Caught, 0.14 ppm)", 0.14,"Troll-caught albacore are younger fish and have lower mercury levels. Data above is from the University of Oregon."),
	new fish("Tuna (Albacore, 0.357 ppm)", 0.357,gm),
	new fish("Tuna (All, 0.383 ppm)", 0.383,gm),
	new fish("Tuna (Bigeye, 0.639 ppm)", 0.639,gm),
	new fish("Tuna (Yellowfin, 0.325 ppm)", 0.325,gm),
	new fish("Weakfish (Sea Trout, 0.256 ppm)", 0.256,gm),
	new fish("Whitefish (0.069 ppm)", 0.069,gm),
	new fish("Whiting (Not Detectable)", 0.000,gm)
];

// calculate concentrations based on weight and diet
// var names were originally same as spreadsheet cells, but not anymore!
// the columns are actually like this:
//	b	c	d	f
// and rows with input boxes on them are 3, 8, 9, 10, 11, 22, 23
//
// the input boxes are named after the cell values D3, F22, and so on
function calculate () {
        d3 = document.form1.D3.value;
        d8 = document.form1.D8.value;
        d9 = document.form1.D9.value;
        d10 = document.form1.D10.value;
        c11 = document.form1.C11.value;
        d11 = document.form1.D11.value;
        f22 = document.form1.F22.value;
        f23 = document.form1.F23.value; 

	//weird js behavior means we've got to get index value first!
	b8idx = document.form1.B8.value;
	if (!isNaN(parseInt(b8idx)) && b8idx > 0) {
		c8 = fish_array[b8idx].level;
		document.form1.C8.value = c8 + " ppm";
	} else {
		c8 = 0;
		document.form1.C8.value = "";
	} 

	b9idx = document.form1.B9.value;
	if (!isNaN(parseInt(b9idx)) && b9idx > 0) {
		c9 = fish_array[b9idx].level;
		document.form1.C9.value = c9 + " ppm";
	} else {
		c9 = 0;
		document.form1.C9.value = "";
	} 

	b10idx = document.form1.B10.value;
	if (!isNaN(parseInt(b10idx)) && b10idx > 0) {
		c10 = fish_array[b10idx].level;
		document.form1.C10.value = c10 + " ppm";
	} else {
		c10 = 0;
		document.form1.C10.value = "";
	} 

	// make sure user enters numbers! 
	if (isNaN(parseFloat(c10))) c10 = 0;
	if (isNaN(parseFloat(c11))) c11 = 0;
	if (isNaN(parseFloat(d3))) d3 = 0;
	if (isNaN(parseFloat(d8))) d8 = 0;
	if (isNaN(parseFloat(d9))) d9 = 0;
	if (isNaN(parseFloat(d10))) d10 = 0;
	if (isNaN(parseFloat(d10))) d11 = 0;

	// make fields blank instead of 0
	if (d8 == 0 || c8 == 0) document.form1.D8.value = '';
	if (d9 == 0 || c9 == 0) document.form1.D9.value = '';
	if (d10 == 0 || c10 == 0) document.form1.D10.value = '';
	if (d11 == 0 || c11 == 0) document.form1.D11.value = '';

	if (d3 <= 0) {
		alert ("Please enter your weight on line 1.");
		return;
	}
	
	// convert lbs to kilos
	e3 = precision(d3 / 2.2, 2);
	
	// species 1
	e8 = precision((d8 / 7)* 28.35, 2);
	s1 = precision((e8 * c8) / e3, 2);
	if (s1 == 0) document.form1.F8.value = "";
	else document.form1.F8.value = s1;

	// species 2
	e9 = precision((d9 / 7) * 28.35, 2);
	s2 = precision((e9 * c9) / e3, 2)	;
	if (s2 == 0) document.form1.F9.value = "";
	else document.form1.F9.value = s2;

	// species 3
	e10 = precision((d10 / 7) * 28.35, 2);
	s3 = precision((e10 * c10) / e3, 2);
	if (s3 == 0) document.form1.F10.value = "";
	else document.form1.F10.value = s3;
	
	// species 4 (fill in the blank)
	e11 = precision((d11 / 7) * 28.35, 2);
	s4 = precision((e11 * c11) / e3, 2);
	if (s4 == 0) document.form1.F11.value = "";
	else document.form1.F11.value = s4;

	// total dosage in &mu;g/kg-day
	f22 = precision(s1 + s2 + s3 + s4, 2);
	if (f22 == 0) document.form1.F22.value = "";
	else document.form1.F22.value = f22;

	// total as percentage of EPA limit (which is 0.1)
	f23 = precision(100 * f22 / 0.1, 1);
	if (f23 == 0) document.form1.F23.value = "---";
	else document.form1.F23.value = f23 + "%";

}

// round off numbers to arg[1] decimal places
// usage: precision(n, decimal_places)
// sometimes small numbers get rounded to zero
function precision () {
	var str = arguments[0];
	var prec = arguments[1];
	str = Math.round(parseFloat(str) * Math.pow(10, prec)) / Math.pow(10, prec);
	return str;
}
//preload the meter images via the Image obj:
if(document.images){ 
	var metergreen = new Image();
	metergreen.src = "img/original/dose_meter3.gif";
}
if(document.images){ 
	var meteryello = new Image();
	meteryello.src = "img/original/dose_meter2.gif";
}
if(document.images){ 
	var meterred = new Image();
	meterred.src = "img/original/dose_meter1.gif";
}
//switch the meter image with an onClick of the Calculate button:
function metergo () {
	percent = f23;
	//meter control: swap images
	if (percent <= 50){
		document.meter.src = metergreen.src;
		return;
	}
	if (percent > 50 && percent <= 99){
		document.meter.src = meteryello.src;
		return;
	}
	if (percent > 99){
		document.meter.src = meterred.src;
		return;
	}
	
}

