// -------------------------------------------------(
// Comparison Configurations and  Global Variables
//

    // How many possible contestants can we compare at any one given time?
    // NOTE: If this is changed, be sure to add an element to the compare__current_checked object
    var COMPARE__TOTAL_CHECKABLE = 2;

    // The minimum number of contestants to compare
    var COMPARE__MIN_NUM_TO_COMPARE = 2;

    // The base URL for the redirect
    //var COMPARE__BASE_URL = 'http://contest.localhost.com/compare/';
    var COMPARE__BASE_URL = 'http://contest.bodybuilding.com/compare/';



    // How many are currently checked?
    var compare__total_checked = 0;

    // What is the current event?
    var compare__current_event = '';

    // The id's of the currently checked elements
    var compare__current_checked = {1 : '', 2 : ''/*, 3 : ''*/};

//
// )-------------------------------------------------




/**
 * compare__box_click 
 *
 * onclick function for when the user checks a box
 * 
 * @param string elem_id 
 * @param string cur_event 
 * @access public
 * @return void
 */
function compare__box_click(elem_id, cur_event)
{
    // Update the current event
    compare__current_event = cur_event;

    // Update the counter that keeps track of how many are checked
    compare__update_total_checked(elem_id);

    // Update the "array" of currently checked elements
    compare__update_current_checked(elem_id);


    // ----------------------------------------------------------------(
    // Update which checkboxes are disabled
    //
        // If none are checked, none should be disabled
        if ( compare__total_checked == 0 )
            compare__ensure_none_disabled();

        // If one is checked, then the OTHER events should be disabled
        else if ( compare__total_checked > 0 && compare__total_checked < COMPARE__TOTAL_CHECKABLE )
            compare__ensure_events_disabled();

        // If two are checked, we should not let them select any more
        else if ( compare__total_checked == COMPARE__TOTAL_CHECKABLE )
            compare__ensure_all_disabled();
    //
    // )----------------------------------------------------------------
}


/**
 * compare__update_current_checked 
 * 
 * @param string elem_id 
 * @access public
 * @return void
 */
function compare__update_current_checked(elem_id)
{
    // -------------------------------------------------(
    //  If the checkbox has been checked,
    //  ADD it to the list of those checked
    //
        if ( document.getElementById(elem_id).checked ) {
            for(var i=1; i<=COMPARE__TOTAL_CHECKABLE; i++) {
                if ( compare__current_checked[i] == '' ) {
                    compare__current_checked[i] = elem_id;
                    break;
                }
            }
        }
    //
    //  If the checkbox has been UNchecked,
    //  REMOVE it from the list of those checked
    //
        else {
            for(var i=1; i<=COMPARE__TOTAL_CHECKABLE; i++) {
                if ( compare__current_checked[i] == elem_id ){
                    compare__current_checked[i] = '';
                    break;
                }
            }
        }
    //
    // )-------------------------------------------------
}




/**
 * compare__ensure_none_disabled 
 * 
 * @access public
 * @return void
 */
function compare__ensure_none_disabled()
{
    all_boxes = document.getElementsByName('compare__check');

    for (var i=0; i<all_boxes.length; i++) {
        all_boxes[i].className = '';
        all_boxes[i].disabled  = false;
    }
}

/**
 * compare__ensure_events_disabled 
 * 
 * @access public
 * @return void
 */
function compare__ensure_events_disabled()
{
    all_boxes = document.getElementsByName('compare__check');

    for (var i=0; i<all_boxes.length; i++) {
        if ( !all_boxes[i].checked ) {
            if ( all_boxes[i].getAttribute('event') != compare__current_event ) {
                all_boxes[i].className = 'disabled';
                all_boxes[i].disabled  = true;
            } else {
                all_boxes[i].className = '';
                all_boxes[i].disabled  = false;
            }
        }
    }
}

/**
 * compare__ensure_all_disabled 
 * 
 * @access public
 * @return void
 */
function compare__ensure_all_disabled()
{
    all_boxes = document.getElementsByName('compare__check');

    for (var i=0; i<all_boxes.length; i++) {
        if ( !all_boxes[i].checked ) {
            all_boxes[i].className = 'disabled';
            all_boxes[i].disabled  = true;
        }
    }
}



/**
 * compare__update_total_checked 
 *
 * updates the total number of checked boxes
 * 
 * @param string elem_id 
 * @access public
 * @return void
 */
function compare__update_total_checked(elem_id)
{
    if ( document.getElementById(elem_id).checked )
        compare__total_checked++;
    else compare__total_checked--;
}



/**
 * compare__submit 
 * 
 * @access public
 * @return void
 */
function compare__submit()
{
    if ( compare__total_checked < COMPARE__MIN_NUM_TO_COMPARE ) {
        alert('Please select at least '+COMPARE__MIN_NUM_TO_COMPARE.toString()+' contestants to compare.');
        return;
    }

    var url = compare__build_url();
    document.location.href = url;
}


/**
 * compare__build_url 
 * 
 * @access public
 * @return void
 */
function compare__build_url()
{
    // Have to start somewhere...
    var url = COMPARE__BASE_URL;

    var attribs_to_add = new Array(
        'contest'    ,
        'event'      ,
        'division'   ,
        'contestant'
    );

    for (var idx in compare__current_checked) {
        elem_check = document.getElementById(compare__current_checked[idx]);
        for (var i in attribs_to_add) {
            var attrib = attribs_to_add[i];
            url += attrib + idx.toString() + '/' + elem_check.getAttribute(attrib) + '/';
        }
    }
    return url;
}
