// TANK VERSION...
var tankRowArray = new Array();
var incompleteString = '<span class="incomplete">Incomplete</span>';
var equalSignString = '<span class="equalSign">=</span>';
var customTotalString = 'Custom Total';

function tankRow(standardGal,customGalSelected,customGPM,customMinutesOrTotal,timesUsed,rowTotalObj, rowTotalDivName){ //create tankRow OBJECT
   this.standardGal = standardGal;
   this.customGalSelected = customGalSelected;
   
   if(customGPM.value == 'singleValue'){
      this.userEnteredPerUseTotal = customMinutesOrTotal;
	  this.customGPM = 'singleValue'; 				
      this.customMinutes = 'singleValue';	
   }
   else{
      this.customGPM = customGPM; 				
      this.customMinutes = customMinutesOrTotal;	
	  this.userEnteredPerUseTotal = 'multiValue';	
   }				   
   
   this.timesUsed = timesUsed;
   this.rowTotalObj = rowTotalObj;
   this.rowTotalDivName = rowTotalDivName;
   return this;
} // end tankRow


function tankRecalc(){  
   var galsPerUse = 0;
   var rowTotal = 0;
   var calcTotal = 0;

   for(index=0;index<tankRowArray.length;index++){ //loop through each tankRow obj in the tankRowArray
      
	//compute gals used
	 //custom
	  if(tankRowArray[index].customGalSelected.checked){ 
	     if(tankRowArray[index].customGPM == 'singleValue'){ // single user-entered value entry for useage
		    galsPerUse = tankRowArray[index].userEnteredPerUseTotal.value;
		 }
		 else{ galsPerUse = tankRowArray[index].customGPM.value * tankRowArray[index].customMinutes.value; } //User entered GPM & Minutes
      }   
	 //standard
	  else { galsPerUse = tankRowArray[index].standardGal.value; }
	
	//compute row total & display
	  rowTotal = galsPerUse * tankRowArray[index].timesUsed.options[tankRowArray[index].timesUsed.selectedIndex].value;
	  if(isNaN(rowTotal)){ rowTotal = incompleteString } // check if row total isn't a number
	  tankRowArray[index].rowTotalObj.value = rowTotal;
	  calcTotal += rowTotal;   
	  
	 //create & write string to row total div 
	  var rowTotalString = rowTotal;	  
	  if(rowTotal != incompleteString) { rowTotalString = Math.round(rowTotal * 100) / 100; rowTotalString = equalSignString + rowTotalString; }
	  document.getElementById(tankRowArray[index].rowTotalDivName).innerHTML = rowTotalString;
	  
	  
   } //end for
   if(isNaN(calcTotal)){ calcTotal = incompleteString; } // check if calc total isn't a number   
   document.getElementById('calcTotal').value = calcTotal;
  //create & write string to calc total div 
   var calcTotalString = calcTotal;	  
   if(calcTotal != incompleteString) { calcTotalString = Math.round(calcTotal * 100) / 100; calcTotalString = '<span>' + calcTotalString + '</span>'; }
   document.getElementById('calcTotalDiv').innerHTML = calcTotalString;
   
   

} // end tankRecalc





function tankCalcSetup(){
   var tankFormElements = document.getElementById('hotWaterCalculatorTankForm').elements; // put all form elements into an array   

   var fieldsPerRow = 6;   
   
   for(index=0; index<tankFormElements.length; index+=fieldsPerRow){ //loop through each row & put each row into a rankRow Object
      if(tankFormElements[index].value == 'endOfCalc'){break;} // look for end of calculator indicator field & break out of loop
      //get objs from row in form 
	  var standardGal = tankFormElements[index];
	  var customGalSelected = tankFormElements[index+1];
	  var customGPM = tankFormElements[index+2];
	  var customMinutesOrTotal = tankFormElements[index+3];
	  var timesUsed = tankFormElements[index+4];	
	  var rowTotalObj = tankFormElements[index+5];  	  	  
	  var rowTotalDivName = tankFormElements[index+5].id + 'Div'; //extrapalte the name of the total Div from the id of the hidden total field
	  //create new tankRowArray Object & put at end of array
      tankRowArray[tankRowArray.length] = new tankRow(standardGal,customGalSelected,customGPM,customMinutesOrTotal,timesUsed,rowTotalObj, rowTotalDivName);
   } // end for
   
     
   

/// -----


   var inputFieldArrayTank =  document.getElementById('hotWaterCalculatorTankForm').getElementsByTagName('input'); // make an array of all input fields
   var selectFieldArrayTank =  document.getElementById('hotWaterCalculatorTankForm').getElementsByTagName('select'); // make an array of all select fields
   
   
   //LOOP THROUGH INPUT FIELDS
   for(var index=0; index<inputFieldArrayTank.length; index++){ // loop through all input fields... 
      if(inputFieldArrayTank[index].type == 'text'){
	     inputFieldArrayTank[index].onkeyup = function(){ tankRecalc(); }
		 inputFieldArrayTank[index].onblur = function(){ tankRecalc(); }
		 inputFieldArrayTank[index].onfocus = function(){ 
														//build ID of custom checkbox and times pulldown depending on type of text field... 
														 var rowIDRoot = this.id; 
														 if(rowIDRoot.indexOf('GPM') != -1){ rowIDRoot = rowIDRoot.substring(0,rowIDRoot.indexOf('GPM')); }
														 if(rowIDRoot.indexOf('Minutes') != -1){ rowIDRoot = rowIDRoot.substring(0,rowIDRoot.indexOf('Minutes')); }
														 if(rowIDRoot.indexOf('TotalPerUse') != -1){ rowIDRoot = rowIDRoot.substring(0,rowIDRoot.indexOf('TotalPerUse')); }
														 var customCBName = rowIDRoot +  'EntryTypeCustom';
														 var timesPulldownName = rowIDRoot + 'TimesUsed';
														 document.getElementById(customCBName).checked = true; //...check the custom checkbox
														 														 
														//select value of 1 time in pulldown if none is currently selected
														 if(document.getElementById(timesPulldownName).options[0].selected == true){
														    document.getElementById(timesPulldownName).options[1].selected = true;
														 }
														 
														//clear field if nothing has already been entered
														 if((this.value == 'MM') || (this.value == 'gpm') || (this.value == customTotalString)) {
														    this.value = '';														    
													     }
														 
														 tankRecalc();
													 }  // end anon function
	  }	
	  if(inputFieldArrayTank[index].type == 'radio'){
	     	     inputFieldArrayTank[index].onchange = function(){tankRecalc(); }
				 inputFieldArrayTank[index].onclick = function(){tankRecalc(); } // this is needed for ie
	  }	    	  

   } // end for
   
   //LOOP THROUGH SELECT FIELDS
   for(var index=0; index<selectFieldArrayTank.length; index++){ // loop through all input fields...    
      selectFieldArrayTank[index].onchange = function(){ this.className = "entryField"; tankRecalc(); }
	  selectFieldArrayTank[index].onfocus = function(){ this.className = "entryFieldSelected"; };
   }  
   
} // end tankCalcSetup


onload = function(){ tankCalcSetup(); tankRecalc();};

// END TANK VERSION
