function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function densityUnits() {
	var unit = $("select#watch-density").val();
	var agent = $("select#agent").val();
	$("select#density-mirror").val(unit);
}

function checkAgent(tag) {
	var i = $( tag + " option:selected").attr("class");
	var target = ".conc";
	if (i == "solution") {
		var units = $( tag + " option:selected").text();
		$(tag).parent(".naked").addClass("shim");
		$(target + " #units").empty();
		$(target).fadeIn("fast");
		$(target + " #units").text(units);
	} else {
		$(target).hide();
		$(tag).parent(".naked").removeClass("shim");
	}
}

function copperAdvise() {
	var copperText = {
		'CuSO4'			: 'This is the dry blue powder Copper Sulfate Pentahydrate<br> (CuSO<sub>4</sub> &bull; 5H<sub>2</sub>O).',
		'Cu+2 Solution'	: 'This is a solution expressed in terms of <em>Copper ion</em> (Cu<sup>+2</sup>) rather than Copper Sulfate Pentahydrate.',
		'CuSO4 Solution': 'This is a solution expressed in terms of <em>Copper Sulfate Pentahydrate</em> (CuSO<sub>4</sub> &bull; 5H<sub>2</sub>O), rather than Copper ion.'
	}
	var replaceText = copperText[$('select.watch').val()];
	if (replaceText) { 
		$("#copper-info").html(replaceText);
		$("#copper-info").effect("highlight", null, 2000);
	}
}

function checkMustVol() {
	var value = $("#must_vol_units option:selected").val();
	if ( (value == "Gals") || (value == "L") ) {
		$("#must-vol").hide();
		$("#must_vol_units").parent().removeClass("top");
	} else {
		$("#must-vol").show();
		$("#must_vol_units").parent().addClass("top");
	}
}

/* Put focus on first input element found */
$(document).ready(function(){
	$("input:visible:enabled:first").focus();
});

/* only allow numeric and decimal input to text input fields */
$(document).ready(function(){
	$("input.numeric").numeric({ allow: "." } );
});

/* show meta info on add pages */
$(document).ready(function() {
	$("a.meta_show").click(function() {
		try {
			$(".meta").effect("slide");
		} catch(err) {
			$(".meta").show();
			window.location.hash = "meta";
		}
		$('.context-banner').show();
		$(this).hide();		
		$(".meta_hide").show();
		// showMeta(this);
		return false;
	});
});

/* hide meta info on add pages */
$(document).ready(function() {
	$("a.meta_hide").click(function() {
		try {
			$(".meta").effect("blind");
		} catch(err) {
			$(".meta").hide();
			window.location.hash = "";
		}
		$(this).hide();
		$(".meta_show").show();
		return false;
	});	
});

$(document).ready(function(){
	checkMustVol();
	$("#must_vol_units").change(function(){
		checkMustVol();
	});
});

/* reveal solution conc input if option is solution */
$(document).ready(function() {
	copperAdvise();
	$("select.watch").change(function() {
		copperAdvise();
		var target = ".conc";
		if ($(this).val().match(/[Ss]olution/)) {
			$(target).show();
			$(this).parent().addClass("shim");
		} else {
			$(target).hide();
			$(this).parent().removeClass("shim");
		}
	});
});

/* Add the text 'nevermind' to the meta-hide link, keep away from googlebot */
$(document).ready(function(){
	$(".meta_hide").text("nevermind");
});

/* mirror selected units to sample list for lab trials */
$(document).ready(function() {
	$("select.watch-units").change(function() {
		var unit = $("select.watch-units option:selected").text();
		$("span#replace").empty();
		$("span#replace").text(unit);
		$("span#units").empty();
		$("span#units").text(unit);
	});
});

$(document).ready(function() {
	checkAgent("select.watch-conc");
	$("select.watch-conc").change(function() {
		densityUnits();
		checkAgent("select.watch-conc")
	});
});

$(document).ready(function() {
	var num = $("select#target_numerator option:selected").text();
	var den = $("select#target_denominator option:selected").text();
	var output = num + "/" + den;
	if ( (num == 'ppm')  || (num == '%') ) {
		$(".trial-units").text(num);
	} else {
		$(".trial-units").text(output);
	}
	$("select").change(function() {
		var num = $("select#target_numerator option:selected").text();
		var den = $("select#target_denominator option:selected").text();
		var output = num + "/" + den;
		if ( (num == 'ppm')  || (num == '%') ) {
			$(".trial-units").text(num);
		} else {
			$(".trial-units").text(output);
		}
	});
});

$(document).ready(function() {
	$("a#add-trial").click(function() {
		var size = $("ol#trial li").size();
		var num = $("select#target_numerator option:selected").text();
		var den = $("select#target_denominator option:selected").text();
		var output = num + "/" + den;
		if ( ( num == "ppm" ) || ( num == "%" ) ) {
			var output = num
		}
		span = "<li><input type='number' name='add[samples][]' id='sample-'" + size + 1 +  " size='5' /> <span id='units-'" + size + 1 + " class='trial-units'> " + output + "</span></li>"
		$("ol#trial").append(span);
		$(".trial-units").text(output);
		return false;
	});
});

$(document).ready(function() {
	$(document).keyup(function() {
		var xph 		= $("input#xph").val();
		xph				= parseFloat(xph);
		var targetMSO2 	= $("input#target-mso2").val();
		targetMSO2		= parseFloat(targetMSO2);
		var currentFSO2 = $("input#target-fso2").val();
		currentFSO2		= parseFloat(currentFSO2);
		var exp 		= parseFloat(xph - 1.81); //changed pKa value from 1.83 to 1.81 to match ETS, Ribereau-Gayon and Zoecklein
		var currentMSO2 = currentFSO2 / ( 1 + Math.pow(10, exp) );
		var reqFSO2 = targetMSO2 * ( 1 + Math.pow(10, exp) );
		if (currentMSO2 > 0) { 
			$("span#output-mso2").text(roundNumber(currentMSO2,2) + " ppm");
		} else {
			$("span#output-mso2").text("");	
		}
		if (reqFSO2 > 0 ) {
			$("span#output-fso2").text(roundNumber(reqFSO2, 0) + " ppm");
		} else {
			$("span#output-fso2").text("");
		}
	});
});

$(document).ready(function() {
	$("select[id$='_numerator']").change(function() {
		var option 	= $(this).val();
		var type	= $(this).attr("id").match(/^[a-z]*/)
		if ( ( option == "ppm" ) || ( option == "%" ) ) {
			$("#" + type + "_per").hide();
			$("#" + type + "_denominator").hide();
		} else {
			$("#" + type + "_per").show();
			$("#" + type + "_denominator").show();
		}
	});
});

$(document).ready(function() {
	densityUnits();
	$("select#watch-density").change(function() {
		densityUnits();
	});
});

$(document).ready(function(){
	$("body.iphone ul li.group a").click(function(){
		var target = $(this).attr("id");
		$("." + target).toggle();
		window.location.hash = target;
		return false;
	});
});

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('(2($){$.c.f=2(p){p=$.d({g:"!@#$%^&*()+=[]\\\\\\\';,/{}|\\":<>?~`.- ",4:"",9:""},p);7 3.b(2(){5(p.G)p.4+="Q";5(p.w)p.4+="n";s=p.9.z(\'\');x(i=0;i<s.y;i++)5(p.g.h(s[i])!=-1)s[i]="\\\\"+s[i];p.9=s.O(\'|\');6 l=N M(p.9,\'E\');6 a=p.g+p.4;a=a.H(l,\'\');$(3).J(2(e){5(!e.r)k=o.q(e.K);L k=o.q(e.r);5(a.h(k)!=-1)e.j();5(e.u&&k==\'v\')e.j()});$(3).B(\'D\',2(){7 F})})};$.c.I=2(p){6 8="n";8+=8.P();p=$.d({4:8},p);7 3.b(2(){$(3).f(p)})};$.c.t=2(p){6 m="A";p=$.d({4:m},p);7 3.b(2(){$(3).f(p)})}})(C);',53,53,'||function|this|nchars|if|var|return|az|allow|ch|each|fn|extend||alphanumeric|ichars|indexOf||preventDefault||reg|nm|abcdefghijklmnopqrstuvwxyz|String||fromCharCode|charCode||alpha|ctrlKey||allcaps|for|length|split|1234567890|bind|jQuery|contextmenu|gi|false|nocaps|replace|numeric|keypress|which|else|RegExp|new|join|toUpperCase|ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('|'),0,{}));