/**
  * General multi-purpose getters
  */

function hideExistingFields() {
   // hide the two fields that would otherwise be duplicated
   
   document.getElementById('existing_fullname_cell1').style.display = 'none';
   document.getElementById('existing_fullname_cell2').style.display = 'none';
   document.getElementById('existing_emailaddress_cell1').style.display = 'none';
   document.getElementById('existing_emailaddress_cell2').style.display = 'none'; 
}

function showExistingFields() {
   // show the two fields that would otherwise be duplicated
   
   document.getElementById('existing_fullname_cell1').style.display = '';
   document.getElementById('existing_fullname_cell2').style.display = '';
   document.getElementById('existing_emailaddress_cell1').style.display = '';
   document.getElementById('existing_emailaddress_cell2').style.display = '';    
}

function getIsDog(animal) {
   
   animaldetails = Array(6);

   animaldetails[1] = ''; if(document.orderform.animaltype1 != null) { animaldetails[1] = document.orderform.animaltype1[0]; }
   animaldetails[2] = ''; if(document.orderform.animaltype2 != null) { animaldetails[2] = document.orderform.animaltype2[0]; }
   animaldetails[3] = ''; if(document.orderform.animaltype3 != null) { animaldetails[3] = document.orderform.animaltype3[0]; }
   animaldetails[4] = ''; if(document.orderform.animaltype4 != null) { animaldetails[4] = document.orderform.animaltype4[0]; }
   animaldetails[5] = ''; if(document.orderform.animaltype5 != null) { animaldetails[5] = document.orderform.animaltype5[0]; }
   
   if(animaldetails[animal].checked == true) { return true; }
   else { return false; }
}

function getNewIsDog(animal) {
   
   animaldetails = Array(6);

   animaldetails[1] = ''; if(document.orderform.newanimaltype1 != null) { animaldetails[1] = document.orderform.newanimaltype1[0]; }
   animaldetails[2] = ''; if(document.orderform.newanimaltype2 != null) { animaldetails[2] = document.orderform.newanimaltype2[0]; }
   animaldetails[3] = ''; if(document.orderform.newanimaltype3 != null) { animaldetails[3] = document.orderform.newanimaltype3[0]; }
   animaldetails[4] = ''; if(document.orderform.newanimaltype4 != null) { animaldetails[4] = document.orderform.newanimaltype4[0]; }
   animaldetails[5] = ''; if(document.orderform.newanimaltype5 != null) { animaldetails[5] = document.orderform.newanimaltype5[0]; }
   
   if(animaldetails[animal].checked == true) { return true; }
   else { return false; }
}
 
function getDropOffDate() {
   if(document.orderform.dropoff1.value == '') { return 'NaN'; }
   else {
      return new Date (document.orderform.dropoff1.value);
   }
}
 
function getPickUpDate() {
   if(document.orderform.pickup1.value == '') { return 'NaN'; }
   else {
      return new Date (document.orderform.pickup1.value);
   }
}
 
function getDropOffTime() {
   return document.orderform.dropoff2.value;
}
 
function getPickUpTime() {
   return document.orderform.pickup2.value;
} 
 
function getBoardedBefore() {
    if(document.orderform.radioDiv[0].checked == true) { return true; }
    else { return false; }
}

function getNumPets() {
   return document.orderform.pets.value;
}

/**
  *Calculation of costs
  */

function calculateNumberOfDays() { 
   return ((getPickUpDate() - getDropOffDate()) / 86400000);
}

function updateTotalCost(totalcost) {

   if(totalcost == 'NaN') { totalcost = ' Invalid Entry '; }
   document.getElementById('estimatedtotal').innerHTML = '$'+totalcost;
   document.orderform.estimatedcost.value = totalcost;

   //document.orderform.hiddenplaytimescost.value = costofplaytimes;
   //document.orderform.hiddennumberplaytimes.value = numberplaytimes;
   //document.orderform.hiddencostperplaytime.value = getPlaytimePrice().toFixed(2);

}

function updateCostPerDay() {
   
   if(getBoardedBefore()!=true) { return updateCostPerDayNew(); }
   
   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();
   dayscost = dayscost.toFixed(2);

   if(dayscost == 'NaN') { dayscost = ' Invalid Entry '; }

   document.getElementById('costperdaytext').innerHTML = ' Cost per day for all animals: ';
   document.getElementById('costperday').innerHTML = '$' + dayscost;         
}

function checkDatesAndTimes() {   
   if( getDropOffDate() == 'Invalid Date' || 
       getPickUpDate()  == 'Invalid Date' || 
       getDropOffDate() == 'NaN' || 
       getPickUpDate()  == 'Nan' ||       
       getPickUpTime()  == '' || 
       getDropOffTime() == ''
     ) { 
      return false;
   }
   else {
      return true;   
   }
}

function checkValidExistingOwnerInfo() {
   var email         = document.orderform.emailAddress.value;
   var fullname      = document.orderform.fullName.value;    
   
   if( fullname    == '' ||
       email       == '' || !checkEmailAddress(email)
      ) { 
       return false;
   }
   else {
      return true;
   }
}

function checkEmailAddress(email) {
   if (!(email.indexOf(' ')==-1 && 0<email.indexOf('@') && email.indexOf('@')+1 < email.length)) {
      return false;         
   }   
   return true;
}

function checkForm() {
   if(getBoardedBefore()) { 
      return isValidForm(); 
   }
   else {
      return isValidNewForm();
   }
}

function markErrors() {
   if(!getBoardedBefore()) { 
      markErrorsInNewForm(); 
   }
   else {
      markErrorsInForm();
   }   
}

function markErrorInForm(fieldname) {
   if(document.getElementById(fieldname)!=null) {
      document.getElementById(fieldname).style.color = 'red'; 
      document.getElementById(fieldname).style.fontWeight = 'bold';      
   }
}

function clearErrorInForm(fieldname) {
   if(document.getElementById(fieldname)!=null) {
      document.getElementById(fieldname).style.color = 'black';
      document.getElementById(fieldname).style.fontWeight = 'normal';  
   }       
}

function markUpDates()  {
   
   if(getDropOffDate() == 'NaN') { markErrorInForm('dropoffdatetext'); }
   else { clearErrorInForm('dropoffdatetext'); }
   if(getPickUpDate() == 'NaN') { markErrorInForm('pickupdatetext'); }
   else { clearErrorInForm('pickupdatetext'); }
   if(getDropOffTime() == '') { markErrorInForm('dropofftimetext'); }
   else { clearErrorInForm('dropofftimetext'); }
   if(getPickUpTime() == '') { markErrorInForm('pickuptimetext'); }
   else { clearErrorInForm('pickuptimetext'); }   
}