// Existing Pets

function updatePlaytimesCost() {
   
   if(getBoardedBefore()!=true) { return updatePlaytimesNew(); }
   
   totalcost = calculateCostOfPlayTimes(getTotalExistingPlaytimes())*1;
   totalcost = totalcost.toFixed(2);   
   
   if(getTotalExistingPlaytimes() == '0' || totalcost == 'NaN') { 
      document.getElementById('playtimecostpersession').innerHTML = ''; 
      document.getElementById('playtimecostpersessiontext').innerHTML = ''; 
      return true;
   }
   
   cost = getPlaytimePrice().toFixed(2);
   
   if(totalcost == 'NaN') { totalcost = ' Invalid Entry '; }
   document.getElementById('playtimecostpersession').innerHTML = '$' + cost + ' x ' + getTotalExistingPlaytimes() + ' = $' + totalcost;
   document.getElementById('playtimecostpersessiontext').innerHTML = 'Total Playtimes';

	//alert('updating now...');
   document.orderform.hiddenplaytimescost.value = totalcost;
   document.orderform.hiddennumberplaytimes.value = getTotalExistingPlaytimes();
   document.orderform.hiddencostperplaytime.value = cost;
}

// Just set up the array with all the playtimes values in. Then we can pick which we need.
function getTotalExistingPlaytimes() {
   var oform = document.orderform;
   existing_playtimes1 = 0; if(oform.existing_playtimes1 != null) { existing_playtimes1 = oform.existing_playtimes1.value; }
   existing_playtimes2 = 0; if(oform.existing_playtimes2 != null) { existing_playtimes2 = oform.existing_playtimes2.value; }
   existing_playtimes3 = 0; if(oform.existing_playtimes3 != null) { existing_playtimes3 = oform.existing_playtimes3.value; }
   existing_playtimes4 = 0; if(oform.existing_playtimes4 != null) { existing_playtimes4 = oform.existing_playtimes4.value; } 
   existing_playtimes5 = 0; if(oform.existing_playtimes5 != null) { existing_playtimes5 = oform.existing_playtimes5.value; }
   jointplaytimes1 = 0; if(oform.jointplaytimes1 != null) { jointplaytimes1 = oform.jointplaytimes1.value;}
   
   return existing_playtimes1*1 + existing_playtimes2*1 + existing_playtimes3*1 + existing_playtimes4*1 + existing_playtimes5*1 + jointplaytimes1*1;
}


// New Pets

function updatePlaytimesNew() {
   var costofplaytimes = getCostOfNewPlaytimes().toFixed(2);
   var numberplaytimes = getTotalNewPlaytimes();
   var cost = getPlaytimePrice().toFixed(2);
   
   if(numberplaytimes==0 || costofplaytimes=='' || costofplaytimes=='NaN') {
     document.getElementById('playtimecostpersession').innerHTML = ''; 
     document.getElementById('playtimecostpersessiontext').innerHTML = ''; 
   }
   else {
      document.getElementById('playtimecostpersession').innerHTML = '$' + cost+' x '+numberplaytimes+' = $' + costofplaytimes;
      document.getElementById('playtimecostpersessiontext').innerHTML = 'Total Playtimes';     
   }

}


function getTotalNewPlaytimes() {
   var oform = document.orderform;
  
   playtime1 = 0; if(oform.playtime1 != null) { playtime1 = oform.playtime1.value; }
   playtime2 = 0; if(oform.playtime2 != null) { playtime2 = oform.playtime2.value; }
   playtime3 = 0; if(oform.playtime3 != null) { playtime3 = oform.playtime3.value; }
   playtime4 = 0; if(oform.playtime4 != null) { playtime4 = oform.playtime4.value; } 
   playtime5 = 0; if(oform.playtime5 != null) { playtime5 = oform.playtime5.value; }
   jointplaytimes2 = 0; if(oform.jointplaytimes2 != null) { jointplaytimes2 = oform.jointplaytimes2.value;}	
	
   return playtime1*1 + playtime2*1 + playtime3*1 + playtime4*1 + playtime5*1 + jointplaytimes2*1;
}


 
function getCostOfNewPlaytimes() {
   return calculateCostOfPlayTimes(getTotalNewPlaytimes()); 
 } 

 
 function checkAllExistingPlaytimes() {
   var numberofpets = document.getElementById('existingpets').value;
   var oform = document.orderform;
     
   if(numberofpets >= 1 && oform.existing_playtimes1 != null && oform.existing_playtimes1.value != '' && !isNumeric(oform.existing_playtimes1.value) ) { return false; } 
   if(numberofpets >= 2 && oform.existing_playtimes2 != null && oform.existing_playtimes2.value != '' && !isNumeric(oform.existing_playtimes2.value) ) { return false; } 
   if(numberofpets >= 3 && oform.existing_playtimes3 != null && oform.existing_playtimes3.value != '' && !isNumeric(oform.existing_playtimes3.value) ) { return false; } 
   if(numberofpets >= 4 && oform.existing_playtimes4 != null && oform.existing_playtimes4.value != '' && !isNumeric(oform.existing_playtimes4.value) ) { return false; } 
   if(numberofpets >= 5 && oform.existing_playtimes5 != null && oform.existing_playtimes5.value != '' && !isNumeric(oform.existing_playtimes5.value) ) { return false; } 
   
   return true;
}

function calculateCostOfPlayTimes(playtimes) {
   return playtimes * getPlaytimePrice();
}
