/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Jim Stiles :: www.jdstiles.com */

/* Modified by Stefan Langer, August 2007. 
   Customized it for ticket calculations 
   and added the div-layer update.         */ 

function startCalc() {
  ticketInterval = setInterval("calculateTickets()", 1);
} // end-function startCalc

function calculateTickets() {

   // set local variables. 
   var ticket_string = 'tickets'; 

   // assume that all properties referenced 
   // below are prefaced with this object. 
   with (window.document.forms[0]) { 

      // set the number of tickets and the 
      // price for each category as variables. 
      ticket1       = tickets_adults.value;
      ticket1_price = tickets_adult_price.value;
      ticket2       = tickets_students.value; 
      ticket2_price = tickets_student_price.value; 
      ticket3       = tickets_seniors.value; 
      ticket3_price = tickets_senior_price.value; 
      ticket4       = tickets_prepaid.value; 
      ticket4_price = tickets_prepaid_price.value; 

      // determine if the user input something 
      // besides numbers in the ticket fields. 
      if (isNaN(ticket1)) { 
         // replace the non-numeric value with a zero.
         ticket1 = 0; 
         tickets_adults.value   = 0; 
      } else if (isNaN(ticket2)) { 
         // replace the non-numeric value with a zero.
         ticket2 = 0; 
         tickets_students.value = 0; 
      } else if (isNaN(ticket3)) { 
         // replace the non-numeric value with a zero.
         ticket3 = 0; 
         tickets_seniors.value  = 0; 
      } else if (isNaN(ticket4)) { 
         // replace the non-numeric value with a zero.
         ticket4 = 0; 
         tickets_prepaid.value  = 0; 
      } // end-if statement 

      // total up the number of tickets. 
      ticketCount = (ticket1 * 1)  + 
                    (ticket2 * 1)  + 
                    (ticket3 * 1)  + 
                    (ticket4 * 1); 

      // add up the total price for the tickets. 
      priceTotal  = (ticket1 * ticket1_price) + 
                    (ticket2 * ticket2_price) + 
                    (ticket3 * ticket3_price) + 
                    (ticket4 * ticket4_price); 

      // update hidden form fields with the totals. 
      tickets_totalcount.value = ticketCount; 
      tickets_totalprice.value = '$' + priceTotal + '.00'; 

   } // end-with statement

   // determine if the user ordered only one ticket. 
   if (ticketCount == 1) { 
      // update the string. 
      ticket_string = 'ticket'; 
   } // end-if statement 

   // update the appropriate div layer with the totals.  
   document.getElementById('totalTickets').innerHTML = ticketCount + ' ' + ticket_string + '; ' + 
                                                       '$' + priceTotal  + '.00';

} // end-function calculateTickets

function stopCalc() { 
  clearInterval(ticketInterval);
} // end-function stopCalc
