// 80326 RKR added canSizeInches(cansize) and inchDec(num,denom,[decs])
function tofloat(str) {
	var numstr = str;
	var i = numstr.indexOf(",");
	while (i > 0) {
		numstr = numstr.substr(0,i) + numstr.substr(i+1);
		i = numstr.indexOf(",",i);
	};
	return parseFloat(numstr);
};

function convert(dbl,show1,curUnits,actVal1,actUnits,show2,actVal2,toRatio,decs,sci) {
	var origRatio = actUnits.value;
	curUnits.value = toRatio;
	var inval = 0;
	if (origRatio.length > 0)
	{	
		if (toRatio==origRatio) {
			if (actVal1.value.length > 0) 
				show1.value = actVal1.value;
			if (dbl>0 && actVal2.value.length > 0) 
					show2.value = actVal2.value;
		} else {
			commapos = toRatio.indexOf(",");
			commapos2 = origRatio.indexOf(",");
			var toRatioVal = parseFloat(toRatio.substring(0,commapos));
			var toAdd = parseFloat(toRatio.substring(commapos+1,99));
			var origRatioVal = parseFloat(origRatio.substring(0,commapos2));
			var origAdd = parseFloat(origRatio.substring(commapos2+1,99));
			if (actVal1.value.length > 0) {
				var inval = tofloat(actVal1.value);
				if (sci) {
					show1.value = showSci(((inval - origAdd) * origRatioVal / toRatioVal + toAdd),decs)
				} else {
					show1.value = Math.round(Math.pow(10,decs) *
 ((inval - origAdd) * origRatioVal / toRatioVal + toAdd))/Math.pow(10,decs)
				};
			};
			if (dbl>0 && actVal2.value.length > 0) {
				var inval = tofloat(actVal2.value);
				if (sci) {
					show2.value = showSci(((inval - origAdd) * origRatioVal / toRatioVal + toAdd),decs)
				} else {
					show2.value = Math.round(Math.pow(10,decs) *
 ((inval - origAdd) * origRatioVal / toRatioVal + toAdd))/Math.pow(10,decs)
				};
			};
		};
	} else actUnits.value = toRatio;
};

function showSci(Input,decs) {
if (isNaN(Input)) return Input;
var AbsInput = Math.abs(parseFloat(Input));
var precision = parseInt(decs)+1;
var Negative = "";
if (AbsInput>Input) Negative="-";
if (AbsInput!=0)  {
	var Exponent = 0;
	var PowerofTen = 1;
	var Prefix = 0;
	var Rounded = 0;
	Exponent = Math.floor(Math.log(AbsInput)/Math.LN10);
	PowerofTen= Math.pow(10,Exponent);
	Prefix = AbsInput/PowerofTen;
	Rounded = (Math.round(Prefix * Math.pow(10,(precision-1))) / Math.pow(10,(precision-1)));

	return Negative + Rounded + "E" + Exponent;
} else return "0"
};

function chgMeas(dbl,show1,curUnits,actVal1,actUnits,show2,actVal2) {
	actUnits.value=curUnits.value;
	if (dbl==0) {
		if (actVal1.value.length==0 || actUnits.value.length>0) actVal1.value=show1.value;
	} else {
		if (actVal2.value.length==0 || actUnits.value.length>0) actVal2.value=show2.value;
	};
};


/*********************************************************** 
for default dir=0, convert fraction [num]/[denom] to decimal (whole treated outside of function)
for dir=1, convert decimal num to string as [whole] [n/m] for m being exp of 2 up to denom

Params:
 (0) num - numerator of inch fraction provided or decimal for dir=1
 (1) denom - denominator of inch fraction, provided for dir=0, or 16 or 64 as max power-of-2 denominator in inch text generated for dir=1
 (2) decs - decimal places for resulting decimal if dir=0, or placed read from supplied decimal if dir =1
 (3) dir - 0 means inches to decimal; 1 means decimal to inches

**********************************************************/
function inchDec(num,denom) {
	var decs = 4;
	var dir = 0;
	//grab the parameter list
	var args=inchDec.arguments;
	// if reverse direction, convert decimal to whole/inch format
	if (args[3]) dir=args[3];
	// if decimals specified, respecify precision for either direction
	if (args[2]) decs=args[2];

	if (dir==0) {
		if (!isNaN(num) && !isNaN(denom))
			return Math.round(parseFloat(num)/parseFloat(denom)*Math.pow(10,decs))/Math.pow(10,decs)
		else if ((''+num).length==0 || denom==0) 
			return ''
		else 
			return 0.0;
	} else {
		// round to specified decimal places (default of 4)
		var numdec = Math.round(parseFloat(num)*Math.pow(10,decs))/Math.pow(10,decs);
		// strip out integer component of decimal number num
		if(numdec>=1.0) {
			whole = ''+Math.floor(parseFloat(numdec));
			numdec = numdec-Math.floor(parseFloat(numdec));
		} else {
			whole = '';
		};
		// split between 16ths and 64ths as max denominator of resulting fraction
		if (denom == 16 ) {
			var numdenom = Math.round(numdec*16.0);
			if (numdenom-numdec*16.0 != 0.0) ast="*"
			else ast="";
			if (numdenom==0.0) return whole;
		} else { // 64ths is only alternative to 16ths
			var numdenom = Math.round(numdec*64.0);
			if (numdenom-numdec*64.0 != 0.0) ast="*"
			else ast="";
			if (numdenom==0.0) return whole;
			if (numdenom%2!=0) return whole +' '+ numdenom+'/64'+ast
			else numdenom=numdenom/2.0;
			if (numdenom%2!=0) return whole +' '+ numdenom+'/32'+ast
			else numdenom=numdenom/2.0;
		};
		if (numdenom%2!=0) return whole +' '+ numdenom+'/16'+ast
		else numdenom=numdenom/2.0;
		if (numdenom%2!=0) return whole +' '+ numdenom+'/8'+ast
		else numdenom=numdenom/2.0;
		if (numdenom%2!=0) return whole +' '+ numdenom+'/4'+ast
		return whole + ' 1/2'+ast;
	};
};

// converts measurement VV1Sn and V1Sn form elements to default units content and checks default radio button
function defaultUnit(seq) {
	var radio=eval("document.dataform.MVal"+seq);

	if (!radio[0].checked) {
		var v = eval("document.dataform.V1S"+seq);
		var vv = eval("document.dataform.VV1S"+seq);
		var uc = eval("document.dataform.VUC"+seq);
		var ua =eval("document.dataform.VUA"+seq);
		if (radio[1].checked) {
			radio[1].checked=false;radio[0].checked=true;
			if (vv.value.length>0) convert(0,vv,uc,v,ua,vv,v,radio[0].value,4,0);
		} else if (radio[2]) {
			radio.options[2].checked=false;radio.options[0].checked=true;
			if (vv.value.length>0) convert(0,vv,uc,v,ua,vv,v,radio[0].value,4,0);		
		};
	};
};

// obj is reference to form within document; base is basename of span id and hidden field designed into page;
// seqsys=0 means use seqno of unit for each column (indexed @ jscript base 0); seqsys=1 means use unit for [opt] (mssysno) measurement system
// if seqsys=1, opt=mssysno (either metric=0 or US=1) of measurement system to use if present;
// jscript var mssys[col] contains 2-element array of seqNos for SI and American units in jscript var unit[col] array painted on listings page

var tUnits = '<span class="units^">%</span>';
function showUnits (obj,base,cols,seqsys,opt) {
	//alert(obj+','+base+','+cols+','+seqsys+','+opt); return;
	var aCols = cols.split(',');  // set up array of column numbers to be processed on page
	if (aCols.length==0) return;
	var ix=0; // steps thru aCols list of table column numbers where rows in table are all to be handled
	var ixCol = aCols[ix];  // column number for a type of measurement data
	var ixRow=1;  // steps thru rows of each column processed
	// loop over all rows (at least row "1") for each column in list
	while (ix<aCols.length) {  // && eval('document.'+obj+'.'+base+ixCol+'1X')
		ixRow=1;  // (re)initialized row counter, starting at 1
		swDo=1;   // switch to skip row changes for a column if seqsys=1 and there is no unit specified for myssysno=[opt]
		// do indirect selection of display value if seqsys is by mssysno, not simple seqno
		// nSeq is jscript 0-based index of desired unit represented by [opt] mssysno within [unit] array for column [ixCol]
		if (seqsys>0) {
			nSeq = mssys[ixCol].split(',')[opt]; // look up in mssys[ixCol] position of [opt] mssysno for column
			// if there is on the page the standard "msUnits" icon, set its source to reflectr the [opt] mssysno
			if (nSeq<2) {
				if(document.images.msUnits!=undefined) document.images.msUnits.src='/dpimages/msUnits'+opt+'.gif';
			} else {
				swDo=0;
			};
		} else {
			nSeq = opt;  // simple seqno to pick data value; reduce by one for jscript indexing starting with zero
		};
		if (swDo==1) {
			temp = '';  // initialize display data value for definition via loops doing concatenations
			colUnits = unit[ixCol].split('|');  // set up array of units alternatives for the column being processed, to aid picking by index ixu
			// build colhead:  first, cycle thru alternative unit measurements to create parenthsized list of links to switch view to other units
			for (ixu=0;ixu<colUnits.length;ixu++) {
				// build parenthesized list of links to switch unit displayed
				if(ixu!=nSeq) temp = temp + (temp.length==0?'(':'&middot;') + '<a href="javascript:showUnits(\'msform\',\'dtxt\',\''+ixCol+'\',0,'+ixu+')" class="unit">'+colUnits[ixu]+'</a>';
			};
			// complete colhead as parenthesized list plus clear text of chosen unit
			temp = temp + (temp.length>0?') ':'') + '<span class="unith">'+colUnits[nSeq]+'</span>';
			// drop colhead into reserved position via pre-defined span on page named "uCol[ixCol]
			document.getElementById('uCol'+ixCol).innerHTML = temp;
			// loop over rows selecting for display in pre-defined span on each row the selected measurement from the hidden field for that row/column
			//while (eval('document.'+obj+'.'+base+ixCol.toString()+ixRow.toString()+'X')!=undefined) {
							//document.getElementById(base+ixCol+ixRow).innerHTML = eval('document.'+obj+'.'+base+ixCol.toString()+ixRow.toString()+'X.value').split('|')[nSeq];
			while (dtxt[ixCol][ixRow]!=undefined) {
				document.getElementById(base+ixCol+ixRow).innerHTML = dtxt[ixCol][ixRow].split('|')[nSeq];
				ixRow++; // loop over rows
			};
		};  //swDo==1
		ix++;
		ixCol = aCols[ix];  // loop over list of columns
	};
};
	
