// check the button to define whether it should be disabled or not
function check_button()
{
    // set total to zero by default
	iTotal = 0;
	
	// for each item inside the series
	for (a = 0; a < aSeries.length; a++)
	{
	    // add to the total
		iTotal += Math.abs(document.getElementById('subtotal_' + aSeries[a]).innerHTML);
	}
	
	// set proper status to button
	document.getElementById('submit_button').disabled = (iTotal != 0) ? false : true;
}

// calculate the section to be enabled or not
function calculate(iSection)
{
    // get the amount of people
    iPersonnes = document.getElementById('personnes_' + iSection).value;
    
    // get the subtotal from the section
	document.getElementById('subtotal_' + iSection).innerHTML = (aPrices[iSection] * iPersonnes).toFixed(2);
	
	// get the big total
	calculate_big_total();
	
	// check the button
	check_button();
}

// calculate pre-loaded information
function calculate_preloaded()
{
    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);
            }
        }
    }

    // for all the items inside the series
    for (a = 0; a < aSeries.length; a++)
    {
        // get the number of persons
        iPersonnes = document.getElementById('personnes_' + aSeries[a]).value;
        
        // get the subtotal inside the html zone
        document.getElementById('subtotal_' + aSeries[a]).innerHTML = (aPrices[aSeries[a]] * iPersonnes).toFixed(2);
    }
    
    check_button();
}

// 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);
	
	// get the big total
	calculate_big_total();
}

// reset prices in extra tickets prefilled
function reset_extra_prices()
{
    iTotal = 0;
    
    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 (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);
            }
        }
    }
    else
    {
        iTotalFreebies = 0;
    }

    // get the extra tickets subtotal
    iExtraTickets = Math.abs(document.getElementById('total_extra_tickets').innerHTML);
    
    // set total of normal events
    iTotal = 0;
    
    // loop through each series
   	for (a = 0; a < aSeries.length; a++)
	{
	    // add to the total
		iTotal += Math.abs(document.getElementById('subtotal_' + aSeries[a]).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;
}

