// Calculate the price of a event
function calculate(iEventId, iConfiguration)
{
    // Get subtotal
	iSubTotal = Math.abs(document.getElementById('subtotal').innerHTML);
	
	// if the event being looked at is checked
	if (document.getElementById('check_' + iEventId).checked)
	{
	    // increment the number of events selected
		iCountEvents++;
		
		if (document.getElementById('cout_' + iEventId) != null)
		{
		    // Get the price of the selected element
		    document.getElementById('cout_' + iEventId).innerHTML = document.getElementById('price_' + iEventId).innerHTML;
    		
		    // add the price to the subtotal
		    iSubTotal += Math.abs(document.getElementById('price_' + iEventId).innerHTML);
		}
	}
	// if not checked
	else
	{
	    // decrement the number of events selected
		iCountEvents--;
		
		if (document.getElementById('cout_' + iEventId) != null)
		{
		    // set the price to zero
		    document.getElementById('cout_' + iEventId).innerHTML = '0.00';
    		
		    // update the subtotal
		    iSubTotal -= Math.abs(document.getElementById('price_' + iEventId).innerHTML);
		}
	}
	// put the subtotal into the html zone
	document.getElementById('subtotal').innerHTML = iSubTotal.toFixed(2);
	
	// calculate the price for persons
	calculate_persons(document.event_registration.personnes.value);
	
	// check the configurations to disable all the remaining unchecked boxes
	check_configuration_events(iConfiguration);
	
	// if the count of events is bigger than the minimum
	if (iCountEvents >= iLimitEvents)
	{
        // set button to be enablled
        bDisabled = false;
	}
	else
	{
	    // set button to be disabled
	    bDisabled = true;
	}
	// enable or disable the submit button as needed
	document.getElementById('submit_button').disabled = bDisabled;
}

// calculate the price with the number of people in consideration
function calculate_persons(iPersons)
{
    // Get the initial subtotal
	iSubTotal = Math.abs(document.getElementById('subtotal').innerHTML);
	
	// multiply by the amount of people
	iTotal = iSubTotal * Math.abs(iPersons);
	
	// set the total inside the total zone
	document.getElementById('total').innerHTML = iTotal.toFixed(2);
	
	// calculate big total
	calculate_big_total();	
}

// check the configurations
function check_configuration_events(iConfiguration)
{
    // get the list of events as an array
    list_events = events[iConfiguration].split(',');
    
    // set count to zero by default
    iCount = 0;
    
    // for each item inside the list of events
    for (a = 0; a < list_events.length; a++)
	{
	    // if the event is ticked
		if (document.getElementById('check_' + list_events[a]).checked)
		{
		    // increment the count
			iCount++;
		}
	}
	
	// if the count is equal to the limit
	if (iCount == maximum[iConfiguration])
	{
	    // loop through all the events
		for (a = 0; a < list_events.length; a++)
		{
		    // if the control is not ticked
			if (!document.getElementById('check_' + list_events[a]).checked)
			{
			    // disable it.
				document.getElementById('check_' + list_events[a]).disabled = true;
			}
		}
	}
	// if different
	else
	{
	    // loop through all the elements
		for (a = 0; a < list_events.length; a++)
		{  
		    // set the element as enabled
			document.getElementById('check_' + list_events[a]).disabled = false;
		}
	}
}

// calculate pre-loaded data
function calculate_preloaded(iConfiguration)
{
    // if the freebies is empty, retuurn false
    if (sFreebies != '')
    {
        // get the list of freebies
        aFreebies = sFreebies.split(',');
        
        // for each freebie
        for (a = 0; a < aFreebies.length; a++)
        {
            // if the value is different than zero
            if (document.getElementById('personnes_' + aFreebies[a]).value != '')
            {
                // calculate the freebie
                calculate_freebies(aFreebies[a], document.getElementById('personnes_' + aFreebies[a]).value);
            }
        }
    }
    // get the subtotal
    iSubTotal = Math.abs(document.getElementById('subtotal').innerHTML);
    
    // get the list of events
    list_events = events[iConfiguration].split(',');
    
    // for each item inside the list of events
    for (a = 0; a < list_events.length; a++)
    {    
        if (document.getElementById('check_' + list_events[a]).checked)
        {
            // increment the number of events selected
            iCountEvents++;
            
            if (document.getElementById('cout_' + list_events[a]) != null)
            {
                // get the price of the selected event
                document.getElementById('cout_' + list_events[a]).innerHTML = document.getElementById('price_' + list_events[a]).innerHTML;
            
                // add price to sub total
                iSubTotal += Math.abs(document.getElementById('price_' + list_events[a]).innerHTML);
            }
        }
    }
    // put the total into the html zone
    document.getElementById('subtotal').innerHTML = iSubTotal.toFixed(2);
    
    // calculate the price for persons
    calculate_persons(document.event_registration.personnes.value);
    
    // check the configurations to disable all remaining unchecked boxes
    check_configuration_events(iConfiguration);
    
    // enable or disable the submit button as needed
    document.getElementById('submit_button').disabled = (iCountEvents != iLimitEvents) ? true : false;
}

// show the supplement tickets section if needed
function show_supplement(sShow)
{
    // set visibility
    document.getElementById('supplement_row').style.display = (sShow == 'yes') ? '' : 'none';
    
    // change text in total cell
    document.getElementById('soustotal_label').innerHTML = (sShow == 'yes') ? 'Sous Total' : 'Total';
}

// calculate the price for the extra tickets
function calculate_extra_price(iExtraTicket)
{
    // get the selected ticket price
	iTicketPrice = document.getElementById('regular_price_' + iExtraTicket).innerHTML;
	
	// get the number of ticket
	iTicketNumber = Math.abs(document.getElementById('number_' + iExtraTicket).value);
	
	// set the total for this ticket
	iTotalTicket = iTicketPrice * iTicketNumber;
	
	// put the total into the HTML zone
	document.getElementById('total_ticket_' + iExtraTicket).innerHTML = iTotalTicket.toFixed(2);
	
	// set total of supplement tickets to zero
	iTotal = 0;
	
	// loop through all the extra tickets
	for (a = 1; a <= iSupplements; a++)
	{
	    // increment total
		iTotal += Math.abs(document.getElementById('total_ticket_' + a).innerHTML);
	}
	
	// set total into html zone
	document.getElementById('total_extra_tickets').innerHTML = iTotal.toFixed(2);

	// calculate big total
	calculate_big_total();
}

// reset prices in extra tickets prefilled
function reset_extra_prices()
{
    for (a = 1; a <= iSupplements; a++)
    {
        var list = document.getElementById('list_supplement_' + a);
        
        iEvent = list.options[list.selectedIndex].value;
        
        if (iEvent != '')
        {
            get_regular_price(iEvent, a, iSupplements);
        }
    }
}
// calculate the grand total
function calculate_big_total()
{
    // if the freebies is not empty
    if (sFreebies != '')
    {
        // get the list of freebies
        aFreebies = sFreebies.split(',');
        
        // get the total of freebies
        iTotalFreebies = 0;
        
        // for reach freebie
        for (a = 0; a < aFreebies.length; a++)
        {
            // if the total is different than zero
            if (Math.abs(document.getElementById('cout_' + aFreebies[a]).innerHTML) != 0)
            {   
                // add to total freebies
                iTotalFreebies += Math.abs(document.getElementById('cout_' + aFreebies[a]).innerHTML);
            }
        }
        
        // set the freebies sub total
        document.getElementById('soustotal_freebies').innerHTML = iTotalFreebies.toFixed(2);
    } 
    
    // if freebies is empty
    else
    {
        if (document.getElementById('soustotal_freebies'))
        {
            // set the freebies subtotal as empty
            document.getElementById('soustotal_freebies').innerHTML = '0.00';
        }
        
        // set the total freebies as empty
        iTotalFreebies = 0;
    }
    
    // get the extra tickets subtotal
    iExtraTickets = Math.abs(document.getElementById('total_extra_tickets').innerHTML);
    
    // set total of normal events
    iTotal = Math.abs(document.getElementById('total').innerHTML);
	
	// set the grand total into the html zone
	document.getElementById('grand_total').innerHTML = (iExtraTickets + iTotal + iTotalFreebies).toFixed(2);
}

// calculate the freebies
function calculate_freebies(iFreebie, iTickets)
{
    // if the check for freebie was false, get out
	if (!check_freebie(iFreebie))
	{
		return false;
	}
	
	// if the number of tickets was higher than 4, error
	if (iTickets > 4)
	{
		alert('Maximum de quatre billets autorisé');
		
		// reser the value to 4
		document.getElementById('personnes_' + iFreebie).value = 4;
	}
	
	// get the price for the freebie
	iPrice = Math.abs(document.getElementById('price_' + iFreebie).innerHTML);
	
	// get the count of people
	iPersonnes = document.getElementById('personnes_' + iFreebie).value;
	
	// calculate the sub total
	iSubTotal = iPrice * iPersonnes;
	
	// set the subtotal into the html zone
	document.getElementById('cout_' + iFreebie).innerHTML = iSubTotal.toFixed(2);
	
	// calculate the big total
	calculate_big_total();
}

// check for the freebie availability
function check_freebie(iFreebie)
{
    // get the list of freebies
	aFreebies = sFreebies.split(',');
	
	// default to valid
	bValid = true;
	
	// for each item inside the freebies
	for (a = 0; a < aFreebies.length; a++)
	{
	    // if the id checked is different than the one we look at
		if (aFreebies[a] != iFreebie)
		{
		    // if the value is filled
			if (document.getElementById('personnes_' + aFreebies[a]).value != '')
			{
			    // set to false
				bValid = false;
			}
		}
	}
	
	// if we had an invalid value
	if (!bValid)
	{
	    // set the value to zero
		document.getElementById('personnes_' + iFreebie).value = '';
		
		// send an error and get out
		alert('Vous ne pouvez pas choisir plus d\'un spectacle exclusif, et ce à un maximum de 4 billets');
		return false;
	}
	
	// we got there, all is ok
	return true;
}

