// $Id: ticket_quantity_limiter.js,v 1.5 2008-06-24 23:23:57 mlee Exp $

function TMTicketQuantityLimiter ( max_dollar_amount ) {

    this.max_dollar_amount = max_dollar_amount;

    this.ticket_levels = new Array();

    this.add_limit = function( combo_box_id, dollar_amount ) {
        var ticket_level = new Object();
        ticket_level.dollar_amount = dollar_amount;
        ticket_level.combo_box_id = combo_box_id;
        ticket_level.combo_box = document.getElementById( combo_box_id );

        // clear out combo_box to start clean
        with( ticket_level.combo_box ) {
            while( firstChild ) {
                removeChild( firstChild );
            }

            // create zero option.  TODO: move this to separate function.
            var option = document.createElement( "option" );
            option.value = 0;
            option.appendChild( document.createTextNode( 0 ) );
            appendChild( option );
        }

        this.ticket_levels.push( ticket_level );
    };

    this.enforce_limit = function() {
        var amount_remaining = this.max_dollar_amount - this.get_total_amount();

        for( var i = 0 ; i < this.ticket_levels.length ; i ++ ) {
            this.set_limit( i, amount_remaining );
        }
    };

    this.get_total_amount = function() {
        var total = 0;

        for( var i = 0 ; i < this.ticket_levels.length ; i ++ ) {
            total = total + this.get_current_amount( i );
        }

        return total;
    };

    this.get_current_amount = function( level_index ) {
        with( this.ticket_levels[level_index] ) {
            return combo_box.selectedIndex * dollar_amount;
        }
    };

    this.set_limit = function( level_index, amount_remaining ) {
        // add in any amount currenly selected for this ticket level to get the real limit.
        amount_remaining = amount_remaining + this.get_current_amount( level_index );

        with( this.ticket_levels[level_index] ) {
            // calculate the maximum tickets allowed for this level
            var max_tickets = Math.floor( amount_remaining / dollar_amount );

            // update the combo box quantities
            if( combo_box.lastChild.value > max_tickets ) {
                while( combo_box.lastChild.value > max_tickets ) {
                    combo_box.removeChild( combo_box.lastChild );
                }
            }

            if ( combo_box.lastChild.value < max_tickets ) {
                while( combo_box.lastChild.value < max_tickets ) {
                    var option = document.createElement( "option" );
                    var value = parseInt( combo_box.lastChild.value ) + 1;
                    option.value = value;
                    option.appendChild( document.createTextNode( value ) );
                    combo_box.appendChild( option );
                }
            }
        }
    };
};

function TicketCountLimiter() {
    this.max_tickets = 0 ;

    this.ticket_levels = [];

    this.add_level = function( combo_box_id ) {
        var ticket_level = {};
        ticket_level.combo_box_id = combo_box_id;
        ticket_level.combo_box = document.getElementById( combo_box_id );
        ticket_level.original_limit = ticket_level.combo_box.options.length - 1;

        this.ticket_levels.push( ticket_level );

        return this.ticket_levels.length - 1;
    };

    this.reset_limits = function() {
        this.clear_levels();

        for( var i = 0 ; i < this.ticket_levels.length ; i ++ ) {
            with( this.ticket_levels[i] ) {
                with( combo_box ) {
                    while( firstChild ) {
                        removeChild( firstChild );
                    }

                    for( var val = 0 ; val <= this.ticket_levels[i].original_limit ; val ++ ) {
                        var option = document.createElement( "option" );
                        option.value = val;
                        option.appendChild( document.createTextNode( val ) );
                        appendChild( option );
                    }
                }
            }
        }
    }

    this.clear_levels = function() {
        for( var i = 0 ; i < this.ticket_levels.length ; i ++ ) {
            with( this.ticket_levels[i].combo_box ) {
                while( firstChild ) {
                    removeChild( firstChild );
                }

                var option = document.createElement( "option" );
                option.value = 0;
                option.appendChild( document.createTextNode( 0 ) );
                appendChild( option );
            }
        }
    };

    this.set_level_quantity = function( level, quantity ) {
        this.ticket_levels[level].combo_box.selectedIndex = quantity;
        this.set_limits();
    };

    this.set_max_tickets = function( max_tickets ) {
        this.max_tickets = max_tickets;

        if( max_tickets ) {
            this.clear_levels();
            this.set_limits();
        }
        else
            this.reset_limits();
    };

    this.get_total_selected = function() {
        var total = 0;

        for( var i = 0 ; i < this.ticket_levels.length ; i ++ ) {
            with( this.ticket_levels[i] ) {
                total += combo_box.selectedIndex;
            }
        }

        return total;
    };

    this.set_limits = function() {
        if( !this.max_tickets )
            return;

        var total_remaining = this.max_tickets - this.get_total_selected();

        for( var i = 0 ; i < this.ticket_levels.length ; i ++ ) {
            with( this.ticket_levels[i] ) {
                while( (combo_box.options.length - 1) > (combo_box.selectedIndex + total_remaining) ) {
                    combo_box.removeChild( combo_box.lastChild );
                }

                while( (combo_box.options.length - 1) < (combo_box.selectedIndex + total_remaining) ) {
                    var next_value = combo_box.options.length;
                    var option = document.createElement( "option" );
                    option.value = next_value;
                    option.appendChild( document.createTextNode( next_value ) );
                    combo_box.appendChild( option );
                }
            }
        }
    };
};

