function checkPaperType() {
	var flag = false;
	for (g = 1; g <= 15; g++) {
		if (document.getElementById('cbPT_' + g).checked == true) {
			flag = true
		}
	}
	if (flag == false) {
		alert('You must select a paper type from the options provided to the left!');
		return false;
	}
}

function hideall(skip) {
	for (i = 1; i <= 15; i++) {
		if (i != skip){
			document.getElementById('papertype' + i).style.display = 'none';
			document.getElementById('cbPT_' + i).checked = false;
		}
	}
}

function unmarkAll() {
	for (a = 1; a <= 14; a++) {
		var rbGroupL = document.getElementsByName('PaperType_' + a + '_Options_L');
		for (L=0; L < rbGroupL.length; L++) { 
		    if (rbGroupL[L].checked == true) { 
    				rbGroupL[L].checked = false 
    			}
		} 
		if (a > 1) {
			var rbGroupR = document.getElementsByName('PaperType_' + a + '_Options_R');
			for (R=0; R < rbGroupR.length; R++) { 
		    		if (rbGroupR[R].checked == true) { 
    					rbGroupR[R].checked = false 
    				}
			} 
		}
	}
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the 'getSelectedRadio' function

function redoFromOther() {
	if (	document.forms[0].BasisWeight_Text.value == 'Other') {
		document.forms[0].BasisWeight_Text[0].selected = true;
		tsHideObject("basisweightsOther");	
	}
	if (	document.forms[0].BasisWeight_Cover.value == 'Other') {
		document.forms[0].BasisWeight_Cover[0].selected = true;
		tsHideObject("basisweightsOther");	
	}
	if (	document.forms[0].BasisWeight_Writing.value == 'Other') {
		document.forms[0].BasisWeight_Writing[0].selected = true;
		tsHideObject("basisweightsOther");	
	}
}

function showWTC(){
	if (document.forms[0].SelectCalculator[0].checked == false && document.forms[0].SelectCalculator[1].checked == false) {
		document.forms[0].SelectCalculator[0].checked = true;
		tsShowObject("recperc");
		tsShowObject("pounds");	
		tsShowObject("calcpounds");
		tsHideObject("sheets");
	}
	if (document.forms[0].SelectCalculator[1].checked == true) {
		tsShowObject("papertypesWTC");	
		tsHideObject("papertypesTC");	
		tsHideObject("papertypesC");	
	}
}

function showTC(){
	if (document.forms[0].SelectCalculator[0].checked == false && document.forms[0].SelectCalculator[1].checked == false) {
		document.forms[0].SelectCalculator[0].checked = true;
		tsShowObject("recperc");
		tsShowObject("pounds");	
		tsShowObject("calcpounds");
		tsHideObject("sheets");
	}
	if (document.forms[0].SelectCalculator[1].checked == true) {
		tsShowObject("papertypesTC");	
		tsHideObject("papertypesWTC");	
		tsHideObject("papertypesC");	
	}
}

function showC(){
	if (document.forms[0].SelectCalculator[0].checked == false && document.forms[0].SelectCalculator[1].checked == false) {
		document.forms[0].SelectCalculator[0].checked = true;
		tsShowObject("recperc");
		tsShowObject("pounds");	
		tsShowObject("calcpounds");
		tsHideObject("sheets");
	}
	if (document.forms[0].SelectCalculator[1].checked == true) {
		tsShowObject("papertypesC");	
		tsHideObject("papertypesWTC");	
		tsHideObject("papertypesTC");	
	}
}

function punctuated(n) {
	var s=(''+n).split(',');
	s[0]=s[0].split('').reverse().join('').match(/\d{1,3}/gi).join(',').split('').reverse().join('');
	return(s.join('.'));
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function calcResults() {
	if (document.forms[0].SelectCalculator[0].checked == true) {
		var nLBS = document.forms[0].Pounds.value;
		nLBS = nLBS.replace(",","");
		nLBS = nLBS.replace(",","");
		nLBS = nLBS.replace(",","");
		var nRP = document.forms[0].RecycledContent.value / 100;
		var nSWRes = Math.round(nLBS * nRP * document.forms[0].SolidWaste.value);
		var nEURes = Math.round(nLBS * nRP * document.forms[0].EnergyUse.value);
		var nGHGRes = Math.round(nLBS * nRP * document.forms[0].NetGreenGasEmitted.value);
		var nWWRes = Math.round(nLBS * nRP * document.forms[0].WaterWastes.value);
		var nWURes = Math.round(nLBS * nRP * document.forms[0].WaterUsage.value);
		var nTSRes = Math.round(nLBS * nRP * document.forms[0].TreesSaved.value);

		//document.forms[0].resSolidWaste.value = nSWRes;
		//document.forms[0].resEnergyUse.value = nEURes;
		//document.forms[0].resNetGreenGasEmitted.value = nGHGRes;
		//document.forms[0].resWaterWastes.value = nWWRes;
		//document.forms[0].resWaterUsage.value = nWURes;
		//document.forms[0].resTreesSaved.value = nTSRes;
		
		document.getElementById("sp_resTreesSaved").innerHTML = addCommas(nTSRes);
		document.getElementById("sp_resWaterUsage").innerHTML = addCommas(nWURes);
		document.getElementById("sp_resNetGreenGasEmitted").innerHTML = addCommas(nGHGRes);
		document.getElementById("sp_resEnergyUse").innerHTML = addCommas(nEURes);
		document.getElementById("sp_resSolidWaste").innerHTML = addCommas(nSWRes);
		document.getElementById("sp_resWaterWastes").innerHTML = addCommas(nWWRes);
				
		document.getElementById("TREES").innerHTML = addCommas(nTSRes);
		document.getElementById("WATERS").innerHTML = addCommas(nWURes);
		document.getElementById("BTUS").innerHTML = addCommas((nEURes * 1) / 1000000);
		document.getElementById("GHGS").innerHTML = addCommas(nGHGRes);
		document.getElementById("SOLIDS").innerHTML = addCommas(nSWRes);
		document.getElementById("WATERBW").innerHTML = addCommas(nWWRes);
	}
}

function calcResults2() {
	if (document.forms[0].SelectCalculator[1].checked == true) {
		//sheets * 
		//(height / 12) * 
		//(width / 12) * 
		//(basisweight / papertype value)  OR (basisweightother / papertype value)
		//  ====  POUNDS;
		var nSHEETS = document.forms[0].Sheets.value;

		nSHEETS = nSHEETS.replace(",","");
		nSHEETS = nSHEETS.replace(",","");
		nSHEETS = nSHEETS.replace(",","");
		//
		var nHEIGHT = document.forms[0].Height.value / 12;
		//
		var nWIDTH = document.forms[0].Width.value / 12;
		//
		var nBASIS = 0;
		if (document.forms[0].PaperType_2.checked == true || document.forms[0].PaperType_4.checked == true || document.forms[0].PaperType_6.checked == true || document.forms[0].PaperType_9.checked == true || document.forms[0].PaperType_11.checked == true) {
			if (document.forms[0].SelPaperType_2.value == 'C' || document.forms[0].SelPaperType_3.value == 'C') {
				if (document.forms[0].BasisWeight_Cover.value == 'Other') {
					nBASIS = document.forms[0].BasisWeight_Other.value / document.forms[0].BasisWeightCover.value;
				}else{
					nBASIS = document.forms[0].BasisWeight_Cover.value / document.forms[0].BasisWeightCover.value;
				}
			}
			if (document.forms[0].SelPaperType_2.value == 'W') {
				if (document.forms[0].BasisWeight_Writing.value == 'Other') {
					nBASIS = document.forms[0].BasisWeight_Other.value / document.forms[0].BasisWeightWriting.value;
				}else{
					nBASIS = document.forms[0].BasisWeight_Writing.value / document.forms[0].BasisWeightWriting.value;
				}
			}
		}else{
			if (document.forms[0].SelPaperType_1.value == 'C' || document.forms[0].SelPaperType_3.value == 'C') {
				if (document.forms[0].BasisWeight_Cover.value == 'Other') {
					nBASIS = document.forms[0].BasisWeight_Other.value / document.forms[0].BasisWeightCover.value;
				}else{
					nBASIS = document.forms[0].BasisWeight_Cover.value / document.forms[0].BasisWeightCover.value;
				}
			}
			if (document.forms[0].SelPaperType_1.value == 'W') {
				if (document.forms[0].BasisWeight_Writing.value == 'Other') {
					nBASIS = document.forms[0].BasisWeight_Other.value / document.forms[0].BasisWeightWriting.value;
				}else{
					nBASIS = document.forms[0].BasisWeight_Writing.value / document.forms[0].BasisWeightWriting.value;
				}
			}
			if (document.forms[0].SelPaperType_1.value == 'T') {
				if (document.forms[0].BasisWeight_Text.value == 'Other') {
					nBASIS = document.forms[0].BasisWeight_Other.value / document.forms[0].BasisWeightText.value;
				}else{
					nBASIS = document.forms[0].BasisWeight_Text.value / document.forms[0].BasisWeightText.value;
				}
			}
		}
		var lbsSHEETS = Math.round(nBASIS * nHEIGHT * nWIDTH * nSHEETS);
		//alert('pounds =: ' + lbsSHEETS + ' basis: ' + nBASIS + ' height: ' + nHEIGHT + ' width: ' + nWIDTH + ' sheets: ' + nSHEETS);
		var shnRP = document.forms[0].RecycledContent.value / 100;
		var shnSWRes = Math.round(lbsSHEETS * shnRP * document.forms[0].SolidWaste.value);
		var shnEURes = Math.round(lbsSHEETS * shnRP * document.forms[0].EnergyUse.value);
		var shnGHGRes = Math.round(lbsSHEETS * shnRP * document.forms[0].NetGreenGasEmitted.value);
		var shnWWRes = Math.round(lbsSHEETS * shnRP * document.forms[0].WaterWastes.value);
		var shnWURes = Math.round(lbsSHEETS * shnRP * document.forms[0].WaterUsage.value);
		var shnTSRes = Math.round(lbsSHEETS * shnRP * document.forms[0].TreesSaved.value);
		 
		document.forms[0].SheetsPounds.value = lbsSHEETS;
		document.getElementById("ResPounds_1").innerHTML = lbsSHEETS;
		//document.forms[0].resSolidWaste.value = shnSWRes;
		//document.forms[0].resEnergyUse.value = shnEURes;
		//document.forms[0].resNetGreenGasEmitted.value = shnGHGRes;
		//document.forms[0].resWaterWastes.value = shnWWRes;
		//document.forms[0].resWaterUsage.value = shnWURes;
		//document.forms[0].resTreesSaved.value = shnTSRes;
		
		document.getElementById("sp_resTreesSaved").innerHTML = addCommas(shnTSRes);
		document.getElementById("sp_resWaterUsage").innerHTML = addCommas(shnWURes);
		document.getElementById("sp_resNetGreenGasEmitted").innerHTML = addCommas(shnGHGRes);
		document.getElementById("sp_resEnergyUse").innerHTML = addCommas(shnEURes);
		document.getElementById("sp_resSolidWaste").innerHTML = addCommas(shnSWRes);
		document.getElementById("sp_resWaterWastes").innerHTML = addCommas(shnWWRes);
		
		document.getElementById("TREES").innerHTML = addCommas(shnTSRes);
		document.getElementById("WATERS").innerHTML = addCommas(shnWURes);
		document.getElementById("BTUS").innerHTML = addCommas((shnEURes * 1) / 1000000);
		document.getElementById("GHGS").innerHTML = addCommas(shnGHGRes);
		document.getElementById("SOLIDS").innerHTML = addCommas(shnSWRes);
		document.getElementById("WATERBW").innerHTML = addCommas(shnWWRes);
	}
}

function tsHideObject(objNameStr, documentObj){
	// hides a specified object (such as a <span>). documentObj is optional
	if (documentObj) {
		if (typeof documentObj == "object"){
			documentObj.getElementById(objNameStr).style.display = "none";
		} else {
			alert("CODE ERROR: The second parameter of tsHideObject must be an object representing the document on which you want to unhide an element. For the current document, you may leave it blank.");
		}
	} else {
		if (document.getElementById(objNameStr)){
			document.getElementById(objNameStr).style.display = "none";
		}
	}
}

function tsShowObject(objNameStr, documentObj){
	// unhides a specified object (such as a <span>). documentObj is optional
	if (documentObj) {
		if (typeof documentObj == "object"){
			documentObj.getElementById(objNameStr).style.display = "block";
		} else {
			alert("CODE ERROR: The second parameter of tsShowObject must be an object representing the document on which you want to unhide an element. For the current document, you may leave it blank.");
		}
	} else {
		if (document.getElementById(objNameStr)){
			document.getElementById(objNameStr).style.display = "block";
		}
	}
}

function showCalc() {
	document.getElementById('intro').style.display = "none";
	document.getElementById('calc').style.display = 'inline';
	document.getElementById('results').style.display = 'none'
}

function IsNumeric(strString) {
   //  check for valid numeric strings	
 
   var strValidChars = "0123456789.-, ";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++) {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1) {
         blnResult = false;
       }
    }
   return blnResult;
}