//
// Press Stroke Speeds Calculator
//

	// Build an array so furmulas can loop
	//
	press = new makePressArray(3);
    //                           label     adv  pwr retract
	press[1] = new pressDetails("C-15-C",  780, 25, 1000)
	press[2] = new pressDetails("C-20-C",  465, 55, 1000)
	press[3] = new pressDetails("C-20-C+", 780, 80, 1000)

	function makePressArray(n) {
		this.length=n
		for (var i=1; i<=n; i++) {
			this[i]=new pressDetails()
			}
		return this
		}

	function pressDetails(label, advance, power, retract) {
		this.label = label
		this.advance = advance
		this.power = power
		this.retract = retract
		}

	// Set these variables to an integer type
	//
	var userAdv = 0
	var UserPwr = 0
	var UserRet = 0

	// Actual calculation function
	//
	function calculateSpeedValues() {
		userAdv = Math.abs(Number(document.pressform.advance.value))
		userPwr = Math.abs(Number(document.pressform.power.value))
		userRet = userAdv + userPwr
		if ((userRet <= 4) && (userRet > 0)) {
			document.pressform.advance.value = roundToPlaces(userAdv,3)
			document.pressform.power.value = roundToPlaces(userPwr,3)
			document.pressform.retract.value = roundToPlaces(userRet,3)
	
			for (var i=1; i<4; i++) {
				pressAdvSec = (userAdv * 60) / press[i].advance
				pressPwrSec = (userPwr * 60) / press[i].power
				pressRetSec = (userRet * 60) / press[i].retract
				// Added .25 second correction to pressTotSec below per Dan's request
				pressTotSec = pressAdvSec + pressPwrSec + pressRetSec + .25
				pressTotMin = 60 / pressTotSec
				//document.pressform["advanceSec" + i].value = roundToPlaces(pressAdvSec,3)
				//document.pressform["powerSec" + i].value = roundToPlaces(pressPwrSec,3)
				//document.pressform["retractSec" + i].value = roundToPlaces(pressRetSec,3)
				//document.pressform["totalSec" + i].value = roundToPlaces(pressTotSec,3)
				document.pressform["totalMin" + i].value = roundToPlaces(pressTotMin,2)
				}
			}
		else {
			alert("The Retract stroke must be between 0 and 4.00.\r\rPlease choose different values for the\rAdvance and Power strokes and try again.");
			}
		}

		
//
// Punch Tonnage Calculator
//
function calculatePunchValues() {

	// Get the HoleSize Value from the array or "other" box:
	// 	- the values in a form list are in an array - requires the array index to get the value
	// 	- we know "other" is selected if the value is 0
	//	- id an alpha character is inteh Other box then the form returns NaN
	holeSizeValue = Number(document.punchform.holesize[document.punchform.holesize.selectedIndex].value) * 3.1416
	holeSize = (holeSizeValue == 0) ? Number(document.punchform.holesizeOther.value) : holeSizeValue
	if (isNaN(holeSize)) {	// NaN indicates the value is not in decimal form
		holeSize = 0
		document.punchform.holesizeOther.value = 0
		alert ("Please enter a decimal\rvalue for the Hole Size.")
		}

	// Get the MaterialSize Value from the array or "other" box:
	// 	- the values in a form list are in an array - requires the array index to get the value
	// 	- if "other" is selected then get the value from the respective input field
	//	- id an alpha character is inteh Other box then the form returns NaN
	materialSizeValue = Number(document.punchform.materialsize[document.punchform.materialsize.selectedIndex].value)
	materialSize = (materialSizeValue == 0) ? Number(document.punchform.materialsizeOther.value) : materialSizeValue
	if (isNaN(materialSize)) {	// NaN indicates theh value is not in decimal form
		materialSize = 0
		document.punchform.materialsizeOther.value = 0
		alert ("Please enter a decimal value\rfor the Material Thickness.")
		}
		
	// Get the Material Type from the array
	// 	- the values in a form list are in an array - requires the array index to get the value
	materialType = Number(document.punchform.materialtype[document.punchform.materialtype.selectedIndex].value)

	// Final Calculation
	//	 - Hole Perimeter * Material Thickness * shear (T/inē)
	tonnageReq  = holeSize * materialSize * 25 * materialType
	document.punchform.tonnageReq.value = roundToPlaces(tonnageReq,3) + " Tons"
	}

//
// Round a number to x decimal places and
//   - return in Engineering Notation to indicate accuracy
//
function roundToPlaces(N,places) {
	S = new String(Math.round(N*Math.pow(10,places)))	// move decimals to integer and convert to a string ((N >= 1) ? "" : "0") +
	S = "0000".substr(0, (places-S.length+1)) + S		// add zeros to the left if digits < {places}
	result =  (S.substr(0, T=(S.length-places))			// move the digits back into place with a "."
			  + '.' + S.substr(T, places))
	return result;
	// original code: return (S=new String(Math.round(N*Math.pow(10,places)))).substr(0, T=(S.length-places)) + '.' + S.substr(T, places) + "0000".substr(0, (T*-1))
	}
