function bCalcShrinkage_onclick(which){
	switch (which){
		case 'length':
			if (document.frmTools.txtWovenLen.value == "" || document.frmTools.txtFinishedLen.value == ""){
				alert ("You must enter both woven and finished lengths if you" +
					"\n want length shrinkage calculated.");
			}else{	 
				var lpct = (document.frmTools.txtWovenLen.value - document.frmTools.txtFinishedLen.value)/document.frmTools.txtWovenLen.value;
				lpct = Math.round(lpct * 100);
				document.getElementById('lblLenPctResult').innerHTML = lpct + "%";
				
				document.frmTools.txtLenShrinkPct.value = lpct;
				
			}
			break;
		case 'width':
			if (document.frmTools.txtWovenWidth.value == "" || document.frmTools.txtFinishedWidth.value == ""){
				alert ("You must enter both woven and finished widths if you" +
					"\n want width shrinkage calculated.");
			}else{
				var wpct = (document.frmTools.txtWovenWidth.value - document.frmTools.txtFinishedWidth.value)/document.frmTools.txtWovenWidth.value;
				wpct = Math.round(wpct * 100);
				document.getElementById('lblWidthPctResult').innerHTML = wpct + "%";				
				document.frmTools.txtWidthShrinkPct.value = wpct;
			}
			break;
	}
 }
 
 
 function bCalcDimen_onclick(which){

  	switch (which){
		case 'length':
			if (document.frmTools.txtPlannedLength.value == "" || document.frmTools.txtLenShrinkPct.value == ""){
				alert ("You must enter your planned length (in inches) and the expected shrinkage value.");
			}else{	 
				var pctY = 1 - (document.frmTools.txtLenShrinkPct.value/100);
				var Y = Math.ceil(document.frmTools.txtPlannedLength.value/pctY);
				document.getElementById('lblPlannedLengthResult').innerHTML = Y + " inches";
				document.frmTools.txtItemLength.value = Y;				
			}
			break;
		case 'width':
			if (document.frmTools.txtPlannedWidth.value == "" || document.frmTools.txtWidthShrinkPct.value == ""){
				alert ("You must enter your planned width (in inches) and the expected shrinkage value.");
			}else{
				var pctX = 1-(document.frmTools.txtWidthShrinkPct.value/100);
				var X = Math.ceil(document.frmTools.txtPlannedWidth.value/pctX);
				document.getElementById('lblPlannedWidthResult').innerHTML = X + " inches";
				document.frmTools.txtItemWidth.value = X;
			}
			break;
	}
  
 }
 
 function bCalcWarp_onclick(){
	if (document.frmTools.txtItemLength.value < 1 ||
		document.frmTools.txtNumItems.value < 1 ||
		document.frmTools.txtLoomWaste.value < 1 ||
		document.frmTools.txtItemWidth.value < 1 ||
		document.frmTools.txtEpi.value < 1){
		alert ("Fields marked with an * must contain a nonzero value");
		return false;
	}else{
		if (document.frmTools.txtSample.value == ""){
			document.frmTools.txtSample.value = 0;
		}
		var len = 0;
		len = len + (document.frmTools.txtItemLength.value * document.frmTools.txtNumItems.value)+ 
			parseInt(document.frmTools.txtLoomWaste.value,10) + parseInt(document.frmTools.txtSample.value,10);	
		var total=0;
		total = Math.ceil(len * (parseInt(document.frmTools.txtItemWidth.value) * parseInt(document.frmTools.txtEpi.value)));
		if (navigator.platform == "Win32"){
			gTotalWarp = (total/36).toFixed(2);
		}else{
			//gTotalWarp = Math.ceil(total/36);
			gTotalWarp = macTrim(total/36);
		}
		document.getElementById('lblTotalWarp').innerHTML = "Total amount of warp required is " + total + " inches or " + gTotalWarp + " yards. " ;
	}
 }
 function macTrim(value){
	var total = value.toString(10);
	var i = total.indexOf(".");
	if (i > 0){
		total = total.slice(0,i+3);
	}
	return total;
 }
 
 function rdoConvert(me){
	if (gTotalWarp != 0){
		var pounds;
		if (navigator.platform == "Win32"){
			pounds = (gTotalWarp/parseInt(me.value)).toFixed(2);
		}else{
			pounds = macTrim(gTotalWarp/parseInt(me.value));
		}
		alert (gTotalWarp + " yards @" + me.value + " ypp = " + pounds + " pounds of warp are needed");
	}else{
		alert ("I'm " + me.value + " yards per pound.");
	}
}

function btnReset_onclick(){

	for (i=0;i < document.frmTools.elements.length;i++){		
		if (document.frmTools.elements[i].type == "text"){
			document.frmTools.elements[i].value = "";
		}
		
	}
	document.getElementById('lblLenPctResult').innerHTML = "";
	document.getElementById('lblWidthPctResult').innerHTML = "";
	document.getElementById('lblPlannedLengthResult').innerHTML = "";
	document.getElementById('lblPlannedWidthResult').innerHTML = "";
	document.getElementById('lblTotalWarp').innerHTML = "";
	document.frmTools.txtSample.value = "0";
    gTotalWarp=0;
}
