// price array
var priceArray = Array();

/**
 * called on initial load of calculator page
 */
 function initLoad() {
	  document.calculateLaminate.sqft.readOnly = true;
	  document.calculateLaminate.box.readOnly = true;
	  document.calculateLaminate.waste.readOnly = true;
	  document.calculateLaminate.ckWaste.readOnly = true;
	  document.calculateLaminate.sqft.style.background = "#dbdbdb";
	  document.calculateLaminate.box.style.background = "#dbdbdb";
	  document.calculateLaminate.waste.style.background = "#dbdbdb";
 }

/**
 * Preps the fields based on which radio button was selected
 */
 function prepFields(element) {
	if(element.value == 'byBox') {
		document.calculateLaminate.box.readOnly = false;
		document.calculateLaminate.sqft.readOnly = true;
		document.calculateLaminate.sqft.value = '';
		document.calculateLaminate.box.value = '';
		document.calculateLaminate.box.style.background = "#FFFF54";
		document.calculateLaminate.sqft.style.background = "#ffffff";
		document.getElementById('adjField').innerHTML = '';
		document.getElementById('costPerBoxField').innerHTML = '';
		document.calculateLaminate.ckWaste.checked = false;
		document.calculateLaminate.waste.value = '';
		document.getElementById('totalCostField').innerHTML = '';
	} else if(element.value == 'bySqFt') {
		document.calculateLaminate.sqft.readOnly = false;
		document.calculateLaminate.box.readOnly = true;
		document.calculateLaminate.box.value = '';
		document.calculateLaminate.sqft.value = '';
		document.calculateLaminate.sqft.style.background = "#FFFF54";
		document.calculateLaminate.box.style.background = "#ffffff";
		document.getElementById('adjField').innerHTML = '';
		document.getElementById('costPerBoxField').innerHTML = '';
		document.calculateLaminate.ckWaste.checked = false;
		document.calculateLaminate.waste.value = '';
		document.getElementById('totalCostField').innerHTML = '';
	}
 }
  
/**
 * Calculates the other fields based on sq ft
 */
 function calculateBySqFt(frm,element,sqFtPerBox) {
	 if(!isNumeric(element.value) || !isNumeric(sqFtPerBox)) {
		 alert("Please enter a valid Square Footage Number.");
		 return false;
	 }
	 if(element.value != '' && element.value.length > 0) {
		 var numBoxes = Math.ceil(parseFloat(element.value)/parseFloat(sqFtPerBox));
		 var adjCoverage = parseFloat(numBoxes * sqFtPerBox);
		 var boxCost = getCostByBoxNum(numBoxes);
		 var totalBoxCost = boxCost * numBoxes;
		 frm.box.value = numBoxes;
		 frm.qty.value = numBoxes;
		 frm.adjSqFt.value = adjCoverage;
		 document.getElementById('adjField').innerHTML = adjCoverage.toFixed(2);
		 document.getElementById('costPerBoxField').innerHTML = '$'+boxCost.toFixed(2);
		 document.getElementById('totalCostField').innerHTML = '$'+totalBoxCost.toFixed(2);
		 frm.ckWaste.readOnly = false;
	 } else {
		 frm.box.value = '';
		 document.getElementById('adjField').innerHTML = '';
		 document.getElementById('costPerBoxField').innerHTML = '';
		 frm.ckWaste.checked = false;
		 frm.waste.value = '';
		 frm.adjSqFt.value = '';
		 document.getElementById('totalCostField').innerHTML = '';
		 frm.ckWaste.readOnly = true;
		 frm.qty.value = '';
	 }
 }

/**
 * Calculates the other fields based on number of boxes
 */
 function calculateByBox(frm,element,sqFtPerBox) {
	 if(!isNumeric(element.value) || !isNumeric(sqFtPerBox)) {
		 alert("Please enter a valid Square Footage Number.");
		 return false;
	 }
	 if(element.value != '' && element.value.length > 0) {
		 var adjCoverage = parseFloat(parseFloat(element.value) * sqFtPerBox);
		 var boxCost = getCostByBoxNum(element.value);
		 var totalBoxCost = boxCost * element.value;
		 frm.adjSqFt.value = adjCoverage;
		 document.getElementById('adjField').innerHTML = adjCoverage.toFixed(2);
		 document.getElementById('costPerBoxField').innerHTML = '$'+boxCost.toFixed(2);
		 document.getElementById('totalCostField').innerHTML = '$'+totalBoxCost.toFixed(2);
		 frm.qty.value = element.value;
		 frm.ckWaste.readOnly = false;
	 } else {
		 document.getElementById('adjField').innerHTML = '';
		 document.getElementById('costPerBoxField').innerHTML = '';
		 frm.ckWaste.checked = false;
		 frm.waste.value = '';
		 frm.adjSqFt.value = '';
		 document.getElementById('totalCostField').innerHTML = '';
		 frm.qty.value = '';
		 frm.ckWaste.readOnly = true;
	 }
 }
 
 /**
  * Returns the cost by box number
  */
 function getCostByBoxNum(boxNum) {
	 for(var i=0;i<priceArray.length;i++) {
		 obj = priceArray[i];
		 if(boxNum >= obj.fromQty && boxNum <= obj.toQty) {
		    document.getElementById('costPerBoxField').innerHTML = '$'+obj.boxPrice;
			return obj.boxPrice; 
		 }
	 }
	 
 }
 
 /**
  * Builds a price object
  */
 function buildPriceObject(fromQty,toQty,boxPrice,sqftPrice) {
	objPrice = new Object();
	objPrice.fromQty = fromQty;
	objPrice.toQty = toQty;
	objPrice.boxPrice = boxPrice;
	objPrice.sqftPrice = sqftPrice;
	curr = priceArray.length;
	priceArray[curr++] = objPrice;
 }
 
 /**
  * Caculates the amount needed for waste
  */
  function calculateWaste(frm,element,sqFtPerBox) {
	  var squareFt;
	  var totalBoxes;
	  var boxCost;
	  var grandTotal;
	  var totalBoxCost;
	  if(element.checked) {
		  if(frm.sqft.value == '' || frm.sqft.value.length == 0) {
			squareFt =  frm.adjSqFt.value; 
		  } else {
			squareFt = frm.sqft.value;
		  }
		  frm.waste.value = Math.ceil((squareFt * .10)/sqFtPerBox);
		  totalBoxes = parseInt(frm.box.value) + parseInt(frm.waste.value);
		  // recalc box price and total price
		  boxCost = getCostByBoxNum(totalBoxes);
		  grandTotal = boxCost * totalBoxes;
		  document.getElementById('costPerBoxField').innerHTML = '$'+boxCost.toFixed(2);
		  document.getElementById('totalCostField').innerHTML = '$'+grandTotal.toFixed(2);
		  frm.qty.value = totalBoxes;
	  } else {
		  totalBoxes = parseInt(frm.box.value) - parseInt(frm.waste.value);
		  // recalc box price and total price
		  boxCost = getCostByBoxNum(totalBoxes);
		  grandTotal = boxCost * totalBoxes;
		  document.getElementById('costPerBoxField').innerHTML = '$'+boxCost.toFixed(2);
		  document.getElementById('totalCostField').innerHTML = '$'+grandTotal.toFixed(2);
		  frm.qty.value = totalBoxes;
		  frm.waste.value = '';
	  }
  }
  
  /**
   * Calculates the number of rolls of padding needed based on square feet
   */
   function caculateRolls(frm) {
	   var rollSize = parseInt(frm.rollsize.options[frm.rollsize.selectedIndex].value);
	   var sqft = frm.sqft.value;
	   if(!isNumeric(sqft)) {
		   alert("Please enter a valid square footage number.");
		   return false;
	   } else {
		   sqft = parseFloat(sqft);
	   }
	   if(sqft == '' || sqft.length == 0) {
		   document.getElementById('totRollsField').innerHTML = '';
	   } else {
		   var totRolls = Math.ceil(sqft/rollSize);
		   if(rollSize > 0) {
			   document.getElementById('totRollsField').innerHTML = totRolls+' rolls';
		   }
	   }
   }
 
