function getCostOfExistingPets() {
   
   // for each pet that we've selected and filled in information for, calculate its cost.
   var numexistingpets = document.getElementById('existingpets').value;

   if(!checkValidExistingFormFields()) {
      document.getElementById('estimatedtotal').innerHTML = 'Please complete all fields.';
   }
   else {
      
      var dayscost = 1*getCostPerDayForExistingAnimal(1);
      dayscost += 1*getCostPerDayForExistingAnimal(2);
      dayscost += 1*getCostPerDayForExistingAnimal(3);
      dayscost += 1*getCostPerDayForExistingAnimal(4);
      dayscost += 1*getCostPerDayForExistingAnimal(5);
	  dayscost -= getBoardDogsTogetherDiscount();
	  dayscost -= getBoardCatsTogetherDiscount();
      
      var bathsdipscost = 1*getCostOfBathdipsByValue(1);
      bathsdipscost += 1*getCostOfBathdipsByValue(2);
      bathsdipscost += 1*getCostOfBathdipsByValue(3);
      bathsdipscost += 1*getCostOfBathdipsByValue(4);
      bathsdipscost += 1*getCostOfBathdipsByValue(5);        
      
      var numdays = calculateNumberOfDays()*1 + getChargeForExtraDays()*1;
      var dayscost = dayscost*numdays;
      var totalcost = calculateCostOfPlayTimes(getTotalExistingPlaytimes())*1 + bathsdipscost*1 + dayscost*1; 

      updateTotalCost(totalcost.toFixed(2));
   }         
}

function getCostPerDayForExistingAnimal(animal) {
   if(document.getElementById('existingpets').value > (animal-1)) { 
      if(getIsDog(animal)) {
         return getCostPerDay(getExistingDogWeight(animal)).toFixed(2);
      }
      else {
         return getCatPerDayCost().toFixed(2);
      }
   }
   else {
      return 0;   
   }
}

function getExistingDogWeight(index) {
     
   weights = getAllWeights(); 
   weight = weights[index];

   if(isNumeric(weight) && weight != 0) { 
     return weight;
   } else {
      return 0;
   }
} 

// Just set up the array with all the weights. Then we can pick which we need.
function getAllWeights() {
     
   var weights = new Array(5);
   var oform = document.orderform;
    
   weights[1] = 0; if(oform.existing_dogweight1 != null) { weights[1] = oform.existing_dogweight1.value; }
   weights[2] = 0; if(oform.existing_dogweight2 != null) { weights[2] = oform.existing_dogweight2.value; }
   weights[3] = 0; if(oform.existing_dogweight3 != null) { weights[3] = oform.existing_dogweight3.value; }
   weights[4] = 0; if(oform.existing_dogweight4 != null) { weights[4] = oform.existing_dogweight4.value; }
   weights[5] = 0; if(oform.existing_dogweight5 != null) { weights[5] = oform.existing_dogweight5.value; }   
   
   return weights;
}




function checkAllExistingPetNames() {
   var numberofpets = document.getElementById('existingpets').value;
   var oform = document.orderform; 
   
   if(oform.boardedPets1 != null && (numberofpets >= 1 && oform.boardedPets1.value == '')) { return false; } 
   if(oform.boardedPets2 != null && (numberofpets >= 2 && oform.boardedPets2.value == '')) { return false; } 
   if(oform.boardedPets3 != null && (numberofpets >= 3 && oform.boardedPets3.value == '')) { return false; } 
   if(oform.boardedPets4 != null && (numberofpets >= 4 && oform.boardedPets4.value == '')) { return false; } 
   if(oform.boardedPets5 != null && (numberofpets >= 5 && oform.boardedPets5.value == '')) { return false; } 
   return true;
}

function checkAllExistingDogWeights() {
   var numberofpets = document.getElementById('existingpets').value;
   var oform = document.orderform; 
   
   if(numberofpets >= 1 && getExistingDogWeight(1)=='' && getIsDog(1)) { return false; } 
   if(numberofpets >= 2 && getExistingDogWeight(2)=='' && getIsDog(2)) { return false; } 
   if(numberofpets >= 3 && getExistingDogWeight(3)=='' && getIsDog(3)) { return false; } 
   if(numberofpets >= 4 && getExistingDogWeight(4)=='' && getIsDog(4)) { return false; } 
   if(numberofpets >= 5 && getExistingDogWeight(5)=='' && getIsDog(5)) { return false; } 

   return true;  
}

