/*******************/
/* Search & Browse */
/*******************/
                                     
var search_browse_list_div
                               
var ie = navigator.userAgent.indexOf("MSIE");
var ie7 = navigator.userAgent.indexOf("MSIE 7.0");
var ie8 = navigator.userAgent.indexOf("MSIE 8.0");

function createRequestObject()
{
        var httpRequest;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject('MSXML2.XMLHTTP.3.0');
            } catch (e) {
                try {
                    httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try {
                        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch(e) {}
                }
            }
        }
        return httpRequest;
}

var http_search = createRequestObject();
var http_browse = createRequestObject();

var search_timer_obj, search_timeout_obj;
var browse_timer_obj, browse_timeout_obj;
var browse_in_use = false;
var search_in_use = false;
var cancel_search = false;

var search_and_browse_used_once = false;

var remove_timer;

var ul_search_browse_list = '<ul id="search_browse_list" class="search_browse_list">';

function search_browse_object(section,subsections,display_div,hidden_ids,title,title_id,search_browse_container,section_containers,restriction_section)
{       
        this.section = section;                                 // the id/name of this section (usually the title - but all lowercase and underscores for spaces)
        this.subsections = subsections.split(",");              // all of the subsections contained in this section (separated by commas ',') *** must have at least 1 ***
        this.display_div = display_div;                         // the id of the div that displays the current selections for this section
        this.hidden_ids = hidden_ids;                           // the id of the hidden input that stores the selected objects
        this.title = title;                                     // the title that gets displayed to the right of the Search & Browse lists
        this.title_singular = make_singular(title);
        this.title_id = title_id;                               // the id of the <a> that directly above the 'display_div'
        this.search_browse_container = search_browse_container; // the id of the div that will hold the Search & Browse lists for this section
        this.section_containers = section_containers;           // an array of ids of the elements that contain this whole section
        this.restriction_section = restriction_section;         // the section to which the object is associated 
        if(this.subsections.length > 1) this.use_id_prefixes = true; // if this is true - it will put text prefixes on all ids - for example if you choose a school w/ id '1' it would record 's_1'
        else this.use_id_prefixes = false;
        
        this.section_containers_unselected_class = "section_container_unselected";  // the css class to set all section containers to when this section is unselected
        this.section_containers_selected_class = "section_container_selected";  // "                                                                    " selected
        
        this.display_div_unselected_class = "display_div";      // the css class that the display_div will have when this section is NOT currently selected
        this.display_div_selected_class = "display_div_selected";// "                                                           " IS currently selected
        
        this.in_optional_container = false;                     // true if the current section is an optional section 
        this.optional_container = 0;                            // this is the key in the 'optional_containers' array that corresponds to the optional_container this section is in 
        
        this.single_selection = false;                          // true if the section cannot not have more than 1 object selected
        this.do_not_sort = false;                               // true if the selections should not be ordered alphabetically
        
        this.replace_empty = false;                             // true if the section should display "Unknown" if no object is selected
        this.replace_empty_id = '0';
        this.replace_empty_name = 'UNKNOWN';
        this.replace_empty_class_name = 'selection_display';
        
        this.grouped_by_types = false;                          // true if the section is grouped by its types ( like buses )
        this.selections_can_be_reordered = false;               // true if the selections can be moved up & down in the list ( like the locations in trips )
        
        this.enabled = true;                                    // false if you do not want the user to be able to interact w/ this section ( like Charters in Trips when the "use charter" checkbox is not checked ) 
        
        this.get_recommendations = false;                       // true if you recommended selections to appear at the top of the browse list
        this.show_most_used_group = false;                      // true if you want the "Most Used" group to appear in the browse sub-groups list ( the one w/ All, A, B, C, D, etc... )
        
        this.show_add_links = true;                             // false if you do not want the Add links to appear for each subsection when this section is active
        this.auto_open_next_section = true;                     // false if you do NOT want the next section to automatically open up after hitting the 'ok' button on the current section
        
        this.add_link_text = "";
        this.add_more_link_text = "";
        
        this.search_browse_list_width = 364;
        
        this.additional_fields = "";
        
        this.override_navigation_title = false;
        this.navigation_title = '';
}

function search_browse_object_simple(section,subsections,title,select_all_option)
{       
        this.section = section;
        this.subsections = subsections.split(",");
        this.display_div = section+"_display";
        this.hidden_ids = "hidden_"+section;
        this.title = title;
        this.title_singular = make_singular(title);
        this.title_id = section+"_title";
        this.search_browse_container = section + "_search_browse_list";
        this.section_containers = new Array(section + "_container");
        
        if(this.subsections.length > 1) this.use_id_prefixes = true;
        else this.use_id_prefixes = false;
        
        this.section_containers_unselected_class = "section_container_unselected";
        this.section_containers_selected_class = "section_container_selected";
        
        this.display_div_unselected_class = "display_div";
        this.display_div_selected_class = "display_div_selected";
        
        this.in_optional_container = false;
        this.optional_container = 0;
        
        this.single_selection = false;
        this.do_not_sort = false;
        
        this.replace_empty = false;
        this.replace_empty_id = '0';
        this.replace_empty_name = 'UNKNOWN';
        this.replace_empty_class_name = 'selection_display';
        
        this.grouped_by_types = false;
        this.selections_can_be_reordered = false;
        
        this.enabled = true;
        this.restriction_section = "";
        
        this.get_recommendations = false;
        this.show_most_used_group = false;
        
        this.show_add_links = true;
        this.auto_open_next_section = true;
        
        this.add_link_text = "";
        this.add_more_link_text = "";
        
        this.search_browse_list_width = 358;
        
        this.additional_fields = "";
        
        this.override_navigation_title = false;
        this.navigation_title = '';
        this.select_all_option = select_all_option;
}

function show_options()
{
        clearTimeout(remove_timer);
        
        if( (document.getElementById(search_input_field).value == "" || document.getElementById(search_input_field).value == "[Search here]" ) && document.getElementById(search_browse_list_div_id).innerHTML == "" && !browse_in_use)
        {
                var i_section = current_search_browse_section_obj.section;
               
                get_section_subsections(i_section);
        }
        else if(document.getElementById(search_browse_list_div_id).innerHTML != "")
        {
                document.getElementById(search_browse_list_div_id).style.display = "";
        }
        else
        {
                getSearchData(500);
        }
}

function focus_on_me(ref_obj)
{
        if(document.getElementById(ref_obj))
        {
                document.getElementById(ref_obj).focus();
        }
}

function search_scroll()
{
        clearTimeout(remove_timer);
        document.getElementById(search_input_field).focus();
}

function remove_options()
{
        clearTimeout(remove_timer);
        remove_timer = setTimeout("actual_remove_options()",200);
}

function actual_remove_options()
{
        removeSearchBrowseList( current_search_browse_section_obj );
}

function finishCurrentSection()
{
        clearTimeout(remove_timer);
        var go_to_next_section = false;
        
        if(current_search_browse_section_obj.auto_open_next_section)
        {
                for( i in search_browse_sections )
                {
                        if( typeof search_browse_sections[i] == 'string' )
                        {
                                if( go_to_next_section && search_browse_sections[i].enabled )
                                {
                                        if( ( search_browse_sections[i].in_optional_container && current_search_browse_section_obj.in_optional_container && search_browse_sections[i].optional_container == current_search_browse_section_obj.optional_container ) || ( !search_browse_sections[i].in_optional_container && !current_search_browse_section_obj.in_optional_container ) )
                                        {                
                                                if( document.getElementById( search_browse_sections[i].hidden_ids ).value == "" )
                                                {
                                                        set_section( search_browse_sections[i].section );
                                                }
                                                else removeSearchBrowseList( current_search_browse_section_obj );
                                        }
                                        else removeSearchBrowseList( current_search_browse_section_obj );
                
                                        return;
                                }
                                else if( i == current_search_browse_section_obj.section )
                                {
                                        go_to_next_section = true;
                                }
                        }
                }
        }
        
        removeSearchBrowseList( current_search_browse_section_obj );
}

function parse_subsection(prefix)
{                                      
        if(prefix == "s") return "school";
        else if(prefix == "sr") return "school_restricted";
        else if(prefix == "ds") return "district_school";
        else if(prefix == "ts") return "tandem_school";
        else if(prefix == "fl") return "local_facility";
        else if(prefix == "fr") return "remote_facility";
        else if(prefix == "us") return "user";
        else if(prefix == "aus") return "admin_user";
        else if(prefix == "pus") return "private_user";
        else if(prefix == "tus") return "transport_user";
        else if(prefix == "ct") return "contact";
        else if(prefix == "et") return "event_type";
        else if(prefix == "at") return "activity";
        else if(prefix == "dp") return "department";
        else if(prefix == "gp") return "group";
        else if(prefix == "gpr") return "group_restricted";
        else if(prefix == "gps") return "school_group";
        else if(prefix == "gpc") return "community_group";
        else if(prefix == "gpmc") return "my_community_group";
        else if(prefix == "gt") return "group_type";
        else if(prefix == "gst") return "group_sub_type";
        else if(prefix == "gstc") return "community_group_sub_type";
        else if(prefix == "eq") return "equipment";
        else if(prefix == "dr") return "driver";
        else if(prefix == "bs") return "bus";
        else if(prefix == "ch") return "charter";
        else if(prefix == "pr") return "primary_group";
        else if(prefix == "upr") return "user_primary_group";
        else if(prefix == "pe") return "permission_group";
        else if(prefix == "od") return "odometer_rate";
        else if(prefix == "mth") return "minimum_trip_hour";
        else if(prefix == "v") return "vehicle";
        else if(prefix == "vt") return "vehicle_type";
        else if(prefix == "bg") return "budget";
        else if(prefix == "bt") return "bill_to";
        else if(prefix == "pr") return "pay_rate";
        else if(prefix == "rp") return "report";
        else if(prefix == "tr") return "trans_report";
        else if(prefix == "mt") return "menu_type";
        else if(prefix == "evt") return "event_template";
        else if(prefix == "rpt") return "report_template";
        else if(prefix == "cp") return "color_profile";
        else if(prefix == "ma") return "merchant_account"; 
        else if(prefix == "f") return "form";     
        else if(prefix == "li") return "line_item";
        else if(prefix == "adr") return "address";
        else if(prefix == "madr") return "my_address";
        else if(prefix == "usr") return "user_role";
        else if(prefix == "ft") return "facility_type";
        else if(prefix == "flr") return "local_facility_rentable";
        else if(prefix == "avt") return "available_time";
        else if(prefix == "aps") return "approval_step";
        else if(prefix == "apr") return "approval_role";
        else if(prefix == "apd") return "approval_decision";
        else if(prefix == "aprl") return "approval_rule";
        else return "";
}

function parse_id(i_section, i_subsection, i_actual_id)
{       
        if(search_browse_sections[i_section].use_id_prefixes)
        {
                if(i_subsection == "school") return "s_"+i_actual_id;
                else if(i_subsection == "school_restricted") return "sr_"+i_actual_id;
                else if(i_subsection == "district_school") return "ds_"+i_actual_id;
                else if(i_subsection == "tandem_school") return "ts_"+i_actual_id;
                else if(i_subsection == "local_facility") return "fl_"+i_actual_id;
                else if(i_subsection == "remote_facility") return "fr_"+i_actual_id;
                else if(i_subsection == "user") return "us_"+i_actual_id;
                else if(i_subsection == "admin_user") return "aus_"+i_actual_id;
                else if(i_subsection == "private_user") return "pus_"+i_actual_id;
                else if(i_subsection == "transport_user") return "tus_"+i_actual_id;
                else if(i_subsection == "contact") return "ct_"+i_actual_id;
                else if(i_subsection == "event_type") return "et_"+i_actual_id;
                else if(i_subsection == "activity") return "at_"+i_actual_id;
                else if(i_subsection == "department") return "dp_"+i_actual_id;
                else if(i_subsection == "group") return "gp_"+i_actual_id;
                else if(i_subsection == "group_restricted") return "gpr_"+i_actual_id;
                else if(i_subsection == "school_group") return "gps_"+i_actual_id;
                else if(i_subsection == "community_group") return "gpc_"+i_actual_id;
                else if(i_subsection == "my_community_group") return "gpmc_"+i_actual_id;
                else if(i_subsection == "credit_card") return "cc_"+i_actual_id;
                else if(i_subsection == "group_type") return "gt_"+i_actual_id;
                else if(i_subsection == "group_sub_type") return "gst_"+i_actual_id;
                else if(i_subsection == "community_group_sub_type") return "gstc_"+i_actual_id;
                else if(i_subsection == "equipment") return "eq_"+i_actual_id;
                else if(i_subsection == "driver") return "dr_"+i_actual_id;
                else if(i_subsection == "bus") return "bs_"+i_actual_id;
                else if(i_subsection == "charter") return "ch_"+i_actual_id;
                else if(i_subsection == "primary_group") return "pr_"+i_actual_id;
                else if(i_subsection == "user_primary_group") return "upr_"+i_actual_id;
                else if(i_subsection == "permission_group") return "pe_"+i_actual_id;
                else if(i_subsection == "odometer_rate") return "od_"+i_actual_id;
                else if(i_subsection == "minimum_trip_hour") return "mth_"+i_actual_id;
                else if(i_subsection == "vehicle") return "v_"+i_actual_id;
                else if(i_subsection == "vehicle_type") return "vt_"+i_actual_id;
                else if(i_subsection == "budget") return "bg_"+i_actual_id;
                else if(i_subsection == "bill_to") return "bt_"+i_actual_id;
                else if(i_subsection == "pay_rate") return "pr_"+i_actual_id;
                else if(i_subsection == "report") return "rp_"+i_actual_id;
                else if(i_subsection == "trans_report") return "tr_"+i_actual_id;
                else if(i_subsection == "menu_type") return "mt_"+i_actual_id;
                else if(i_subsection == "event_template") return "evt_"+i_actual_id;
                else if(i_subsection == "report_template") return "rpt_"+i_actual_id;
                else if(i_subsection == "color_profile") return "cp_"+i_actual_id;
                else if(i_subsection == "merchant_account") return "ma"+i_actual_id;
                else if(i_subsection == "form") return "f"+i_actual_id;
                else if(i_subsection == "line_item") return "li"+i_actual_id;
                else if(i_subsection == "address") return "adr"+i_actual_id;
                else if(i_subsection == "my_address") return "madr"+i_actual_id;
                else if(i_subsection == "my_address") return "madr"+i_actual_id;
                else if(i_subsection == "user_role") return "usr"+i_actual_id;
                else if(i_subsection == "facility_type") return "ft"+i_actual_id;
                else if(i_subsection == "local_facility_rentable") return "flr"+i_actual_id;
                else if(i_subsection == "available_time") return "avt"+i_actual_id;
                else if(i_subsection == "approval_step") return "aps"+i_actual_id;
                else if(i_subsection == "approval_role") return "apr"+i_actual_id;
                else if(i_subsection == "approval_decision") return "apd"+i_actual_id;
                else if(i_subsection == "approval_rule") return "aprl"+i_actual_id;
                else return i_actual_id;
        }
        else return i_actual_id;
}

function get_subsection_add_link(i_subsection)
{
        if(i_subsection == "school") return "type=schools&action=add";
        else if(i_subsection == "school_restricted") return "type=schools&action=add";
        else if(i_subsection == "district_school") return "type=schools&action=add";
        else if(i_subsection == "tandem_school") return "type=schools&action=add";
        else if(i_subsection == "local_facility") return "type=local_facilities&action=add";
        else if(i_subsection == "remote_facility") return "type=remote_facilities&action=add";
        else if(i_subsection == "user") return "type=user_accounts&action=add";
        else if(i_subsection == "admin_user") return "type=user_accounts&action=add";
        else if(i_subsection == "private_user") return "type=user_accounts&action=add";
        else if(i_subsection == "transport_user") return "type=user_accounts&action=add";
        else if(i_subsection == "contact") return "type=contacts&action=add";
        else if(i_subsection == "event_type") return "type=event_types&action=add";
        else if(i_subsection == "activity") return "type=activity&action=add";
        else if(i_subsection == "department") return "type=departments&action=add";
        else if(i_subsection == "group") return "type=groups&action=add";
        else if(i_subsection == "group_restricted") return "type=groups&action=add";
        else if(i_subsection == "school_group") return "type=groups&action=add";
        else if(i_subsection == "community_group") return "type=groups&action=add_community";
        else if(i_subsection == "my_community_group") return "type=groups&action=add_community";
        else if(i_subsection == "group_sub_type") return "type=group_sub_type&action=add";
        else if(i_subsection == "community_group_sub_type") return "type=group_sub_type&action=add";
        else if(i_subsection == "equipment") return "type=equipment&action=add";
        else if(i_subsection == "driver") return "type=user_accounts&action=add&driver=1";
        else if(i_subsection == "bus") return "type=vehicles&action=add";
        else if(i_subsection == "charter") return "type=charters&action=add";
        else if(i_subsection == "primary_group") return "type=primary_groups&action=add";
        else if(i_subsection == "user_primary_group") return "type=primary_groups&action=add";
        else if(i_subsection == "credit_card") return "type=user_account_card&action=add";
        else if(i_subsection == "permission_group") return "type=permission_groups&action=add";
        else if(i_subsection == "odometer_rate") return "type=odometer_rates&action=add";
        else if(i_subsection == "minimum_trip_hour") return "type=minimum_trip_hour&action=add";
        else if(i_subsection == "vehicle") return "type=vehicles&action=add";
        else if(i_subsection == "vehicle_type") return "type=vehicle_types&action=add";
        else if(i_subsection == "budget") return "type=budgets&action=add";
        else if(i_subsection == "bill_to") return "type=bill_tos&action=add";
        else if(i_subsection == "pay_rate") return "type=pay_rates&action=add";
        else if(i_subsection == "report") return "type=reports&action=add";
        else if(i_subsection == "trans_report") return "type=trans_reports&action=add";
        else if(i_subsection == "menu_type") return "type=menu_types&action=add";
        else if(i_subsection == "event_template") return "type=event&action=add";
        else if(i_subsection == "report_template") return "type=reports&action=add";
        else if(i_subsection == "color_profile") return "type=color_profiles&action=add";
        else if(i_subsection == "merchant_account") return "type=merchant_accounts&action=add";
        else if(i_subsection == "line_item") return "type=line_items&action=add";
        else if(i_subsection == "form") return "type=forms&action=add";
        else if(i_subsection == "address") return "type=addresses&action=add";
        else if(i_subsection == "my_address") return "type=my_addresses&action=add";
        else if(i_subsection == "facility_type") return "type=local_facilities_types&action=add";
        else if(i_subsection == "local_facility_rentable") return "type=local_facilities&action=add";
        else if(i_subsection == "available_time") return "type=available_times&action=add";
        else if(i_subsection == "approval_step") return "type=approval_step&action=add";
        else if(i_subsection == "approval_role") return "type=approval_role&action=add";
        else if(i_subsection == "approval_decision") return "type=approval_decision&action=add";
        else if(i_subsection == "approval_rule") return "type=approval_rule&action=add";
        else return "";
}

function get_subsection_titles(i_subsection,singular)
{
        var title = "";
        
        if(i_subsection == "school") title = "Schools";
        else if(i_subsection == "school_restricted") title = "Schools";
        else if(i_subsection == "district_school") title = "District Schools";
        else if(i_subsection == "tandem_school") title = "Tandem Schools";
        else if(i_subsection == "local_facility") title = "Facilities";
        else if(i_subsection == "remote_facility") title = "Places";
        else if(i_subsection == "user") title = "Users";
        else if(i_subsection == "admin_user") title = "Administrators";
        else if(i_subsection == "private_user") title = "Private Users";
        else if(i_subsection == "transport_user") title = "Transportation Users";
        else if(i_subsection == "contact") title = "Contacts";
        else if(i_subsection == "event_type") title = "Event Types";
        else if(i_subsection == "activity") title = "Activities";
        else if(i_subsection == "department") title = "Departments";
        else if(i_subsection == "group") title = "Groups";
        else if(i_subsection == "group_restricted") title = "Groups";
        else if(i_subsection == "school_group") title = "School Groups";
        else if(i_subsection == "community_group") title = "Community Groups";
        else if(i_subsection == "my_community_group") title = "My Community Groups";
        else if(i_subsection == "group_type") title = "Group Types";
        else if(i_subsection == "group_sub_type") title = "Group Sub-Types";
        else if(i_subsection == "community_group_sub_type") title = "Group Sub-Types";
        else if(i_subsection == "equipment") title = "Equipment";
        else if(i_subsection == "driver") title = "Drivers";
        else if(i_subsection == "bus") title = "Buses";
        else if(i_subsection == "charter") title = "Charters";
        else if(i_subsection == "primary_group") title = "Primary Groups";
        else if(i_subsection == "user_primary_group") title = "Your Primary Groups";
        else if(i_subsection == "permission_group") title = "Permission Groups";
        else if(i_subsection == "odometer_rate") title = "Odometer Rates";
        else if(i_subsection == "minimum_trip_hour") title = "Minimum Trip Hours";
        else if(i_subsection == "vehicle") title = "Vehicles";
        else if(i_subsection == "vehicle_type") title = "Vehicle Types";
        else if(i_subsection == "budget") title = "Budgets";
        else if(i_subsection == "bill_to") title = "Bill Tos";
        else if(i_subsection == "pay_rate") title = "Pay Rates";
        else if(i_subsection == "report") title = "Reports";
        else if(i_subsection == "trans_report") title = "Transportation Reports";
        else if(i_subsection == "menu_type") title = "Menu Types";
        else if(i_subsection == "event_template") title = "Event Templates";
        else if(i_subsection == "report_template") title = "Schedule Templates";
        else if(i_subsection == "color_profile") title = "Color Profiles";
        else if(i_subsection == "merchant_account") title = "Merchant Accounts";
        else if(i_subsection == "address") title = "Addresses";
        else if(i_subsection == "my_address") title = "My Addresses";
        else if(i_subsection == "user_role") title = "User Roles";
        else if(i_subsection == "facility_type") title = "Facility Types";
        else if(i_subsection == "local_facility_rentable") title = "Facilities";
        else if(i_subsection == "available_time") title = "Available Times";
        else if(i_subsection == "approval_step") title = "Approval Step";
        else if(i_subsection == "approval_role") title = "Approval Role";
        else if(i_subsection == "approval_decision") title = "Approval Step";
        else if(i_subsection == "approval_rule") title = "Approval Rule";

        if(singular) title = make_singular(title);
        
        return title;
}

function make_singular(title)
{
        var end = title.substr( (title.length - 3) );
        
        if(end == 'ies') end = 'y';
        else if(end == 'ses') end = end.charAt(0);
        else if(end.charAt(2) == 's') end = end.charAt(0) + end.charAt(1);
        
        title = title.substr(0, (title.length - 3)) + end;
        
        return title;
}

function get_a_class(i_class_int)
{
        if(i_class_int == 0) return "li_browse_item";
        else if(i_class_int == 1) return "li_browse_item_selected";
        else if(i_class_int == 2) return "li_browse_item_recommended";
        else if(i_class_int == 3) return "li_browse_item_recommended_selected";
        else if(i_class_int == 4) return "li_browse_item_recommended_last";
        else if(i_class_int == 5) return "li_browse_item_recommended_last_selected";
        else return "li_browse_item";
}

function get_browse_section()
{
        if(browse_timer_obj) clearTimeout(browse_timer_obj);
        
        focus_on_me(search_input_field);
        
        var output = "";
        for( i in search_browse_sections)
        {
                if( typeof search_browse_sections[i] == 'string' )
                {
                        var temp_display_div = search_browse_sections[i].display_div;
                        if(document.getElementById(temp_display_div) && search_browse_sections[i].enabled)
                        {
                                output += "<li class=\"browse_item\"><a class=\"li_browse_section\" href=\"javascript:set_section('" + search_browse_sections[i].section + "')\">" + search_browse_sections[i].title + "</a></li>";
                        }
                }
        } 
        
        browse_list_ul.innerHTML = output;
        browse_list_div.scrollTop = 0;
}

function get_event_name(get_recommendations)
{
        if(document.getElementById('name') && document.getElementById('type') && get_recommendations == 1)
        {
                if(document.getElementById('type').value == 'event')
                {
                        var event_name = document.getElementById('name').value;
                        return event_name.replace(/&/gi,"%26");
                }
                else
                {
                        return "";
                }
        }
        else
        {
                return "";
        }
}

function get_host_id_link()
{
        if(document.getElementById('host_id'))
        {
                return "host_id=" + document.getElementById('host_id').value + "&"; 
        }
        else
        {
                return "";
        }
}

function get_browse_categories(total_objects, other_objects, i_section, i_subsection, i_data)
{
        if(browse_timer_obj) clearTimeout(browse_timer_obj);
        
        total_objects = parseInt(total_objects);
        
        var output = "";
        var temp = "";
        
        var search_browse_section_obj = search_browse_sections[i_section];
        
        var hidden_id = search_browse_sections[i_section].hidden_ids;
        
        if(total_objects <= group_1)
        {
                //get all of them
                
                if(i_data.length > 0)
                {
                        var output = processGetDataGroup(i_section, i_subsection, total_objects, other_objects, 'all', i_data);
                        
                        populateSearchBrowseList(output);
                }
                else
                {
                        var selected_ids = document.getElementById(hidden_id).value;
                        
                        focus_on_me(search_input_field);
                        
                        if(browse_in_use) 
                        {
                                browse_timer_obj = setTimeout("get_browse_categories('"+total_objects+"', '"+other_objects+"', '"+i_section+"', '"+i_subsection+"', '"+i_data+"')",100);
                                return;
                        }
                        
                        browse_timeout_obj = setTimeout("browse_timeout()",15000);
                        
                        var get_recommendations = 0;
                        if(search_browse_sections[i_section].get_recommendations) get_recommendations = 1;
                        
                        var event_name = get_event_name(get_recommendations);
                        
                        var host_id_link = get_host_id_link();
                        
                        http_browse = createRequestObject();
                        
                        http_browse.open('get', 'index.php?' + host_id_link + 'type=ajax&action=get_data_group&section=' + i_section + "&subsection=" + i_subsection + "&total_objects=" + total_objects + "&other_objects=" + other_objects + "&group=all&selected_ids=" + selected_ids + '&event_name=' + event_name + '&get_recommendations=' + get_recommendations, true);
                        
                        http_browse.onreadystatechange = handleGetDataGroup;
                
                        if(http_browse.readyState > 0)
                        {
                                browse_in_use = true;
                                http_browse.send(null);
                        }
                }
                
                return;
        }
        else if(search_browse_sections[i_section].grouped_by_types)
        {
                other_objects_array = other_objects.split("|");
                
                for(var loop = 0; loop < other_objects_array.length; loop++)
                {
                        var type_data = other_objects_array[loop].split("~");
                        var type_id = type_data[0];
                        var type_text = type_data[1];
                        
                        output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_"+type_id+"\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','"+type_id+"')\">"+type_text+"</a></li>";
                }
        }
        else if(total_objects > group_1 && total_objects <= group_2)
        {
                output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_a-h\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','a-h')\">A - H</a></li>";
                output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_i-r\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','i-r')\">I - R</a></li>";
                output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_s-z\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','s-z')\">S - Z</a></li>";
                
                other_objects_array = other_objects.split("|");
                
                var temp = other_objects_array[other_objects_array.length-1].split("~");
                var group = temp[0];
                var num_group = parseInt(temp[1]);
                
                if(num_group > 0 && group == "others")
                {
                        output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_"+group+"\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','"+group+"')\">Others</a></li>";
                }
        }
        else if(total_objects > group_2 && total_objects <= group_3)
        {
                output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_a-f\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','a-f')\">A - F</a></li>";
                output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_g-l\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','g-l')\">G - L</a></li>";
                output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_m-r\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','m-r')\">M - R</a></li>";
                output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_s-z\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','s-z')\">S - Z</a></li>";
                
                other_objects_array = other_objects.split("|");
                
                var temp = other_objects_array[other_objects_array.length-1].split("~");
                var group = temp[0];
                var num_group = parseInt(temp[1]);
                
                if(num_group > 0 && group == "others")
                {
                        output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_"+group+"\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','"+group+"')\">Others</a></li>";
                }
        }
        else if(total_objects > group_3)
        {
                other_objects_array = other_objects.split("|");
                
                for(var loop = 0; loop < other_objects_array.length; loop++)
                {
                        var temp = other_objects_array[loop].split("~");
                        var group = temp[0];
                        var num_group = parseInt(temp[1]);
                        
                        if(num_group > 0 && group != "others")
                        {
                                output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_"+group+"\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','"+group+"')\">"+group.toUpperCase()+"</a></li>";        
                        }
                        else if(num_group > 0)
                        {
                                output += "<li id=\""+i_section+"_"+i_subsection+"_browse_section_"+group+"\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','"+group+"')\">Others</a></li>";
                        }
                }
        }
        
        var all_style = "";
        
        if(search_browse_sections[i_section].grouped_by_types)
        {
                all_style = "style=\"border-bottom: 1px dashed black\" ";
        }
        
        output = "<li id=\""+i_section+"_"+i_subsection+"_browse_section_all\" "+all_style+"class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','all')\">All</a></li>" + output;
        
        if(search_browse_sections[i_section].show_most_used_group)
        {
                output = "<li id=\""+i_section+"_"+i_subsection+"_browse_section_most_used\" class=\"browse_item\"><a class=\"li_browse_group\" href=\"javascript:get_browse_sub_category('"+i_section+"','"+i_subsection+"','"+total_objects+"','"+other_objects+"','most_used')\">Most Used</a></li>" + output;
        }
        
        if(total_objects > group_1 && i_data)
        {
                i_data = i_data.split("<separator>");
                
                var data_output = "";
                
                var a_class = "";
                
                for(var loop = 0; loop < i_data.length; loop++)
                {
                        var temp = i_data[loop].split("|");
                        
                        a_class = get_a_class(temp[ 2 ]);
                        
                        if(search_browse_sections[i_section].additional_fields != "")
                        {
                                var additional_fields_array = search_browse_sections[i_section].additional_fields.split(",");
                                
                                var additional_fields_data = "";
                                
                                for( i in additional_fields_array )
                                {
                                        additional_fields_data += temp[ ( parseInt(i) + 3) ] + "|";
                                }
                                
                                additional_fields_data = additional_fields_data.substr(0 , (additional_fields_data.length - 1) );
                        }
                        else
                        {
                                var additional_fields_data = "";
                        }
                         
                        data_output += "<li id=\"browse:"+i_section+":"+i_subsection+":"+temp[0]+"\" class=\"browse_item\" additional_fields_data=\""+additional_fields_data+"\"><a id=\"browse:"+i_section+":"+i_subsection+":"+temp[0]+"a\" class=\"" + a_class + "\" href=\"javascript:select_option('browse:"+i_section+":"+i_subsection+":"+temp[0]+"')\">"+temp[1]+"</a></li>";
                         
                }
                
                output = data_output + output;
        }
        
        if(search_browse_sections[i_section].select_all_option)
        {
                select_all_output = "<li id=\"browse:"+i_section+":"+i_subsection+"\" class=\"browse_item\"><a id=\"browse:"+i_section+":"+i_subsection+"\" class=\"li_browse_group\" href=\"javascript:select_all_options('"+i_section+"','"+i_subsection+"','"+total_objects+"')\">Select All</a></li>";
                
                select_all_output += "<li id=\"browse:"+i_section+":"+i_subsection+"\" class=\"browse_item\"><a id=\"browse:"+i_section+":"+i_subsection+"\" class=\"li_browse_group\" href=\"javascript:deselect_all_options('"+i_section+"','"+i_subsection+"','"+total_objects+"')\">Deselect All</a></li>";
                
                output = select_all_output + output;
        }

        if(total_objects > group_1)
        {

                if( search_browse_sections[i_section].subsections.length > 1 )
                {
                        if(search_browse_sections[i_section].override_navigation_title) var section_title = search_browse_sections[i_section].navigation_title;
                        else var section_title = search_browse_sections[i_section].title;
                        
                        var section_link = "<a class=\"li_section_nav\" href=\"javascript:get_section_subsections('"+i_section+"')\">"+section_title+"</a>";
                        var sub_section_link = "<span class=\"section_nav_separator\"> &gt; </span><a class=\"li_section_nav_current\" href=\"javascript:get_subsection_number('"+i_section+"','"+i_subsection+"')\">"+ get_subsection_titles(i_subsection,false) +"</a>";
                        
                        var back_button = "<li class=\"li_section_nav\">" + section_link + sub_section_link + "</li>";
                        
                        output = back_button + output;
                }
                else
                {
                        var back_button = "";
                        output = "<li class=\"search_item\"><a id=\"browse_section_title:"+i_section+"\" class=\"li_search_section_title\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Please choose a category below</a></li>" + output;
                        
                        output = back_button + output + back_button;
                }
        }
        else if( search_browse_sections[i_section].subsections.length > 1 )
        {
        
        }
        
        output = ul_search_browse_list + output + '</ul>';
        
        populateSearchBrowseList(output);
}

function checkHiddenIds(search_browse_section_obj)
{
        if(document.getElementById( search_browse_section_obj.hidden_ids ).value != "")
        {
                var temp_hidden_value = document.getElementById( search_browse_section_obj.hidden_ids ).value.split(",");
                
                var i_section = search_browse_section_obj.section;
                var subsection = "";
                var temp_id = "";
                
                for(var loop = 0; loop < temp_hidden_value.length; loop++)
                {
                        if(temp_hidden_value[loop].length > 0)
                        {
                                if(temp_hidden_value[loop].indexOf("_") != -1)
                                {
                                        temp_id = temp_hidden_value[loop].split("_");
                                        
                                        subsection = parse_subsection(temp_id[0]);
                                        
                                        temp_id = temp_id[1];
                                }
                                else
                                {
                                        temp_id = temp_hidden_value[loop];
                                        
                                        subsection = search_browse_section_obj.subsections[0];
                                }
                                        
                                if(!document.getElementById("disp:" + i_section + ":" + subsection + ":" + temp_id))
                                {
                                        document.getElementById( search_browse_section_obj.hidden_ids ).value = "";
                                        break;
                                }
                        }
                } 
        }
}

function set_section(i_section)
{        
        var search_browse_section_obj = search_browse_sections[i_section];
        var restriction_section_id = search_browse_section_obj.restriction_section;
        if(restriction_section_id)
        {
                var restriction_section_data = search_browse_sections[restriction_section_name];
                var restriction_section_hidden_id = restriction_section_data.hidden_ids;
                var restriction_selected_ids = document.getElementById( restriction_section_hidden_id ).value;
                if(restriction_selected_ids == '')
                {
                        var tip = "You must select at least 1 " + restriction_section_data.title_singular + " before selecting a " + search_browse_section_obj.title_singular + "."; 
                        alert(tip);
                        return;
                }
        }
        
        if(remove_timer) clearTimeout(remove_timer);
        if(browse_timer_obj) clearTimeout(browse_timer_obj);
        
        if(!search_browse_sections[i_section]) return; //error
        
        if(browse_in_use)
        {
                browse_timer_obj = setTimeout("set_section('"+i_section+"')",100);
                return;
        }
        
        var search_browse_section_obj = search_browse_sections[i_section];
           
        //loop through hidden ids and make sure each one has a corresponding 'disp:' div
        checkHiddenIds(search_browse_section_obj); 
         
        if(current_search_browse_section_obj.section != search_browse_section_obj.section)
        {
                moveSearchBrowseHtml(current_search_browse_section_obj, search_browse_section_obj);
                
                current_search_browse_section_obj = search_browse_section_obj;
        }
        else
        {
                if(document.getElementById( search_input_field ).value == "") set_search_input_defaults();
                
                var cur_search_browse_container = document.getElementById( current_search_browse_section_obj.search_browse_container );
                
                if(search_and_browse_used_once && cur_search_browse_container.style.display == '')
                {
                        focus_on_search_field();
                        return;
                }
        }
          
        search_and_browse_used_once = true;
                            
        prepareSection(search_browse_section_obj);
        
        showSection(search_browse_section_obj);
}

function moveSearchBrowseHtml(from_section_obj, to_section_obj)
{
        var from_search_browse_container = document.getElementById( from_section_obj.search_browse_container );
        var to_search_browse_container = document.getElementById( to_section_obj.search_browse_container );

        //if new section is in a different optional container than the current one is - show the new optional container
        if( to_section_obj.in_optional_container && current_optional_container != to_section_obj.optional_container)
        {
                show_hide_section( to_section_obj.optional_container );
        }
        
        // the following moves the search input box, search and browse result box, 'ok' & 'close' buttons, and the 'Add Links' (if they are shown)
        var search_browse_html = from_search_browse_container.innerHTML;
        
        from_search_browse_container.innerHTML = "";
        to_search_browse_container.innerHTML = search_browse_html;
        
        removeSearchBrowseList( from_section_obj );
}

function prepareSection(search_browse_section_obj)
{
        var display_div = document.getElementById( search_browse_section_obj.display_div );
        
        // the following removes the "Add", "Add more" or "Change" link
        removeAddLink(display_div,search_browse_section_obj.section);
        
        // the following removes the "[No [Section Title] Selected]" text
        removeNoSelectedText(display_div,search_browse_section_obj.section);
        
        if(document.getElementById( search_browse_section_obj.hidden_ids ).value == "")
        {
                var none_selected_link = '<div id="' + search_browse_section_obj.section + '_no_selected" class="selection_display_faded">[No \''+ search_browse_section_obj.title +'\' Selected]</div>';
                
                display_div.innerHTML += none_selected_link;
        }
        
        for( i in search_browse_section_obj.section_containers )
        {
                if( typeof search_browse_section_obj.section_containers[i] == 'string' )
                {
                        document.getElementById( search_browse_section_obj.section_containers[i] ).className = search_browse_section_obj.section_containers_selected_class;
                } 
        }
        
        display_div.className = search_browse_section_obj.display_div_selected_class;
        
        if(search_browse_section_obj.title_id != '')
        {
                document.getElementById( search_browse_section_obj.title_id ).className = get_clicked_class_name( document.getElementById( search_browse_section_obj.title_id ).className );
        }
        
        createdAddLinks(search_browse_section_obj);
        
        set_search_input_defaults();
}

function showSection(search_browse_section_obj)
{
        var search_browse_container = document.getElementById( search_browse_section_obj.search_browse_container );
        
        search_browse_container.style.display = "";
        
        document.getElementById(search_browse_list_div_id).innerHTML = "";
        document.getElementById(search_browse_list_div_id).style.display = "";
        
        focus_on_search_field();
        
        get_section_subsections(search_browse_section_obj.section);
}

function createdAddLinks(search_browse_section_obj)
{
        if(document.getElementById('search_browse_list_add_links_div'))
        {
                if(search_browse_section_obj.show_add_links)
                {
                        var host_id_link = get_host_id_link();
                        
                        var add_links_html = "Is the '" + search_browse_section_obj.title_singular + "' you are looking for not there?<br /><br />";
                        
                        for(i in search_browse_section_obj.subsections)
                        {
                                if( typeof search_browse_section_obj.subsections[i] == 'string' )
                                {
                                        add_links_html += '<a target="_blank" href="index.php?' + host_id_link + get_subsection_add_link( search_browse_section_obj.subsections[i] ) + '">Add ' + get_subsection_titles( search_browse_section_obj.subsections[i], true ) + '</a>&nbsp;<span class="add_link_desc">(opens in new window)</a><br />';
                                } 
                        }
                        
                        document.getElementById('search_browse_list_add_links_div').innerHTML = add_links_html;
                }
                else
                {
                        document.getElementById('search_browse_list_add_links_div').innerHTML = '';
                }
        }
}

function set_search_input_defaults()
{
        document.getElementById( search_input_field ).value = "[Search here]";
        document.getElementById( search_input_field ).className = "search_browse_search_text";
}

function focus_on_search_field()
{
        document.getElementById(search_input_field).blur();
        document.getElementById(search_input_field).focus();
        if(ie != -1)
        {
                document.getElementById(search_input_field).focus();
        }
}

function addAddText(search_browse_section_obj)
{
        var cur_section = search_browse_section_obj.section;
        var display_div = document.getElementById( search_browse_section_obj.display_div );
        
        if(!document.getElementById(cur_section + "_add"))
        {
                var cur_hidden_ids = document.getElementById( search_browse_section_obj.hidden_ids ).value;
               
                if(search_browse_section_obj.add_link_text == "")
                {
                        if( search_browse_section_obj.single_selection )
                        {
                                if(cur_hidden_ids.length > 0) var add_text = "Change";
                                else var add_text = "Add...";
                        }
                        else
                        {
                                if(cur_hidden_ids.length > 0) var add_text = "Add more";
                                else var add_text = "Add...";
                        }
                }
                else
                {
                        if(cur_hidden_ids.length > 0) var add_text = search_browse_section_obj.add_more_link_text;
                        else var add_text = search_browse_section_obj.add_link_text;
                }
                
                var add_link = document.createElement("div");
                add_link.id = cur_section + '_add';
                add_link.innerHTML = '<a id="' + cur_section + '_modify" class="new_wizard_action_link" href="javascript:set_section(\'' + cur_section + '\')" >' + add_text + '</a>';
                
                display_div.appendChild(add_link);
        }
}

function addNoSelectedText(search_browse_section_obj)
{
        var cur_section = search_browse_section_obj.section;
        var display_div = document.getElementById( search_browse_section_obj.display_div );
        
        var no_selected_text = document.createElement("div");
        no_selected_text.id = cur_section + '_no_selected';
        no_selected_text.className = 'selection_display_faded';
        no_selected_text.innerHTML = '[No \''+ search_browse_section_obj.title +'\' Selected]';
        
        display_div.appendChild(no_selected_text);       
}

function removeAddLink(display_div,section)
{
        removeDiv(display_div,section + "_add");
}

function removeNoSelectedText(display_div,section)
{
        removeDiv(display_div,section + "_no_selected");
}

function removeSearchBrowseList(search_browse_section_obj)
{
        var cur_section = search_browse_section_obj.section;
        
        var display_div = document.getElementById( search_browse_section_obj.display_div );
        
        if(search_browse_section_obj.title_id != '')
        {
                document.getElementById( search_browse_section_obj.title_id ).className = get_unclicked_class_name( document.getElementById( search_browse_section_obj.title_id ).className );
        }
        
        display_div.className = search_browse_section_obj.display_div_unselected_class;
        
        for( i in search_browse_section_obj.section_containers )
        {
                if( typeof search_browse_section_obj.section_containers[i] == 'string' )
                {
                        document.getElementById( search_browse_section_obj.section_containers[i] ).className = search_browse_section_obj.section_containers_unselected_class;
                } 
        }
        
        removeNoSelectedText(display_div, cur_section);
        
        addAddText(search_browse_section_obj);
        
        document.getElementById( search_browse_section_obj.search_browse_container ).style.display = "none";
        //display_div.style.height = "auto";
}

function get_section_subsections(i_section)
{
        var output = ul_search_browse_list + "<li class=\"browse_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Getting " + search_browse_sections[i_section].title + " list...</a></li></ul>";
        populateSearchBrowseList(output);
        
        if(search_browse_sections[i_section].subsections.length == 1)
        {
                get_subsection_number(i_section, search_browse_sections[i_section].subsections[0] );
        }
        else
        {
                focus_on_me(search_input_field);
                
                output = "<li class=\"search_item\"><a id=\"browse_section_title:"+i_section+"\" class=\"li_search_section_title\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Please choose a category below</a></li>";
                var subsection = "";
                var title = "";

                for( i in search_browse_sections[i_section].subsections )
                {
                        if( typeof search_browse_sections[i_section].subsections[i] == 'string' )
                        {
                                subsection = search_browse_sections[i_section].subsections[i];
                                title = get_subsection_titles(subsection, false); 
                                output += "<li id=\""+i_section+"_"+subsection+"_browse_section\" class=\"browse_item\"><a class=\"li_browse_subsection\" href=\"javascript:get_subsection_number('"+i_section+"', '"+subsection+"')\">"+title+"</a></li>";
                        }
                }
                
                output = ul_search_browse_list + output + '</ul>';
            
                populateSearchBrowseList(output);
        }
}

function get_subsection_number(i_section, i_subsection)
{
        clearTimeout(browse_timer_obj);
        
        focus_on_me(search_input_field);
        
        if(browse_in_use) 
        {
                browse_timer_obj = setTimeout("get_subsection_number('"+i_section+"', '"+i_subsection+"')",100);
                return;
        }
        
        var output = ul_search_browse_list + "<li class=\"browse_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Getting " + search_browse_sections[i_section].title;
        if(search_browse_sections[i_section].subsections.length > 1) output += " - " + i_subsection;
        output += " list...</a></li></ul>";
        
        populateSearchBrowseList(output);
        
        var get_recommendations = 0;
        if(search_browse_sections[i_section].get_recommendations) get_recommendations = 1;
        
        var event_name = get_event_name(get_recommendations);
        
        var selected_ids = document.getElementById( search_browse_sections[i_section].hidden_ids ).value;
        var additional_fields = search_browse_sections[i_section].additional_fields;
        
        browse_timeout_obj = setTimeout("browse_timeout()",15000);
                
        var host_id_link = get_host_id_link();
        
        http_browse = createRequestObject();
        
        http_browse.open('get', 'index.php?' + host_id_link + 'type=ajax&action=get_subsection_number&section=' + i_section + '&subsection=' + i_subsection + '&selected_ids=' + selected_ids + '&additional_fields=' + additional_fields + '&event_name=' + event_name, true);
        
        http_browse.onreadystatechange = handleGetSubsectionNumber;

        if(http_browse.readyState > 0)
        {
                browse_in_use = true;
                http_browse.send(null);
        }
}

function get_browse_sub_category(i_section, i_subsection, i_total_objects, i_other_objects, i_group)
{
        clearTimeout(browse_timer_obj);

        focus_on_me(search_input_field);
        
        if(browse_in_use) 
        {
                browse_timer_obj = setTimeout("get_browse_sub_category('"+i_section+"', '"+i_subsection+"', '"+i_total_objects+"', '"+i_other_objects+"', '"+i_group+"')",100);
                return;
        }
        
        var output = ul_search_browse_list + "<li class=\"browse_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Getting " + search_browse_sections[i_section].title;
        if(search_browse_sections[i_section].subsections.length > 1) output += " - " + get_subsection_titles(i_subsection, false);
        output += " list...</a></li></ul>";
        
        populateSearchBrowseList(output);
        
        var get_recommendations = 0;
        if(search_browse_sections[i_section].get_recommendations) get_recommendations = 1;
        
        var event_name = get_event_name(get_recommendations);
                
        var selected_ids = document.getElementById( search_browse_sections[i_section].hidden_ids ).value;
        var additional_fields = search_browse_sections[i_section].additional_fields;
        
        browse_timeout_obj = setTimeout("browse_timeout()",15000);
        
        var host_id_link = get_host_id_link();
        
        http_browse = createRequestObject();
        
        http_browse.open('get', 'index.php?' + host_id_link + 'type=ajax&action=get_data_group&section=' + i_section + "&subsection=" + i_subsection + "&total_objects=" + i_total_objects + "&other_objects=" + i_other_objects + "&group=" + i_group + "&selected_ids=" + selected_ids + '&additional_fields=' + additional_fields + '&event_name=' + event_name + '&get_recommendations=0', true);
        
        http_browse.onreadystatechange = handleGetDataGroup;
        
        if(http_browse.readyState > 0)
        {
                browse_in_use = true;
                http_browse.send(null);
        }
}

function browse_timeout()
{
        var output = ul_search_browse_list + "<li class=\"browse_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Request timed out before returning the browse list.</a></li></ul>";
        populateSearchBrowseList(output);
}

function browse_timeout_status()
{
        var status_code = http_browse.status;
        var output = ul_search_browse_list + "<li class=\"browse_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">An error occured while retrieving the browse list.</a></li><li class=\"browse_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Error Code: "+status_code+"</a></li></ul>";
        populateSearchBrowseList(output);
}

function handleGetSubsectionNumber()
{
        if(http_browse.readyState == 4)
        {
                if (http_browse.status == 200)
                {
                        var response = http_browse.responseText;

                        response = response.split(":");
                        
                        clearTimeout(browse_timeout_obj);

                        get_browse_categories(parseInt(response[0]), response[1], response[2], response[3], response[4]);
                }
                else browse_timeout_status();
                
                browse_in_use = false;
        }
}

function handleGetDataGroup()
{
        if(http_browse.readyState == 4)
        {
                if (http_browse.status == 200)
                {
                        var response = http_browse.responseText;
                        response = response.split(":");
                        
                        clearTimeout(browse_timeout_obj);
                        
                        var r_section = response[0];
                        var r_subsection = response[1];
                        var section_total_number = response[2];
                        var section_others_number = response[3];
                        var group = response[4];
                        if(response.length >= 6)
                        {
                                var data = response[5];
                        }
                        else var data = "";
                        
                        var output = processGetDataGroup(r_section, r_subsection, section_total_number, section_others_number, group, data);
                        
                        populateSearchBrowseList(output);
                }
                else browse_timeout_status();
                
                browse_in_use = false;
        }
}

function processGetDataGroup(r_section, r_subsection, section_total_number, section_others_number, group, data)
{
        var output = "";
        var bottom_button_js = "";
        var bottom_button = "";
        var top_button = "";
           
        var group_name = "";
        var search_browse_section_obj = search_browse_sections[r_section];
           
        if(group == 'all') group_name = "All";
        else if(group == 'most_used') group_name = "Most Used";
        else
        {
                group_name = group.replace(/-/gi," - ");
                group_name = group_name.toUpperCase();
        }
        
        if(section_total_number <= group_1)
        {
                if( search_browse_sections[r_section].subsections.length > 1 )
                {
                        if(search_browse_sections[r_section].override_navigation_title) var section_title = search_browse_sections[r_section].navigation_title;
                        else var section_title = search_browse_sections[r_section].title;

                        var section_link = "<a class=\"li_section_nav\" href=\"javascript:get_section_subsections('"+r_section+"')\">"+section_title+"</a>";
                        var sub_section_link = "<span class=\"section_nav_separator\"> &gt; </span><a class=\"li_section_nav_current\" href=\"javascript:get_subsection_number('"+r_section+"','"+r_subsection+"')\">"+ get_subsection_titles(r_subsection,false) +"</a>";
                        
                        top_button = "<li class=\"li_section_nav\">" + section_link + sub_section_link + "</li>";
                        
                        bottom_button_js = "href=\"javascript:get_section_subsections('"+r_section+"')\"";
                }
        }
        else
        {
                if(search_browse_sections[r_section].override_navigation_title) var section_title = search_browse_sections[r_section].navigation_title;
                else var section_title = search_browse_sections[r_section].title;
                
                var section_link = "<a class=\"li_section_nav\" href=\"javascript:get_section_subsections('"+r_section+"')\">"+section_title+"</a>";
                var sub_section_link = "";
                if( search_browse_sections[r_section].subsections.length > 1 )
                {
                        sub_section_link = "<span class=\"section_nav_separator\"> &gt; </span><a class=\"li_section_nav\" href=\"javascript:get_subsection_number('"+r_section+"','"+r_subsection+"')\">"+ get_subsection_titles(r_subsection,false) +"</a>";
                }
                
                var group_link = "<span class=\"section_nav_separator\"> &gt; </span><a class=\"li_section_nav_current\" href=\"javascript:get_browse_sub_category('"+r_section+"','"+r_subsection+"','"+section_total_number+"','"+section_others_number+"','"+group+"');\">"+ group_name +"</a>";
                
                top_button = "<li class=\"li_section_nav\">" + section_link + sub_section_link + group_link + "</li>";
                
                bottom_button_js = "href=\"javascript:get_subsection_number('"+r_section+"', '"+r_subsection+"')\"";
        }
        
        if(bottom_button_js != "")
        {
                bottom_button = "<li class=\"browse_item\"><a class=\"li_browse_back\" " + bottom_button_js + ">"+back_button_text+"</a></li>";
        }
        
        if(data.length > 0)
        {
                data = data.split("<separator>");
                
                for(var loop = 0; loop < data.length; loop++)
                {
                        var temp = data[loop].split("|");
                        var item_type = temp[0];
                        var item_title = temp[1];
                        
                        var a_class = get_a_class( temp[2] );
                        
                        if(search_browse_sections[r_section].additional_fields != "")
                        {
                                var additional_fields_array = search_browse_sections[r_section].additional_fields.split(",");
                                
                                var additional_fields_data = "";
                                
                                for( i in additional_fields_array )
                                {
                                        additional_fields_data += temp[ ( parseInt(i) + 3) ] + "|";
                                }
                                
                                additional_fields_data = additional_fields_data.substr(0 , (additional_fields_data.length - 1) );
                        }
                        else
                        {
                                var additional_fields_data = "";
                        }
                       
                         output += "<li id=\"browse:"+r_section+":"+r_subsection+":"+item_type+"\" class=\"browse_item\" additional_fields_data=\""+additional_fields_data+"\"><a id=\"browse:"+r_section+":"+r_subsection+":"+item_type+"a\" class=\"" + a_class + "\" href=\"javascript:select_option('browse:"+r_section+":"+r_subsection+":"+item_type+"')\">"+item_title+"</a></li>";	
                        
                        
                }
        }
        else
        {
                output += "<li class=\"browse_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">[No Results]</a></li>";
        }
        
        output = ul_search_browse_list + top_button + output + bottom_button + '</ul>';
        
        return output;
}

function populateSearchBrowseList(output)
{
        document.getElementById(search_browse_list_div_id).innerHTML = output;
        document.getElementById(search_browse_list_div_id).scrollTop = 0;
        document.getElementById(search_browse_list_div_id).style.display = "";
        
        if(ie != -1 && document.getElementById("search_browse_list"))
        {
                if(document.getElementById("search_browse_list").offsetHeight > 120)
                {
                        document.getElementById("search_browse_list").style.width = (current_search_browse_section_obj.search_browse_list_width - 21) + "px";
                }
                else
                {
                        if(ie7 != -1 || ie8 != -1)
                        {
                                document.getElementById("search_browse_list").style.width = (current_search_browse_section_obj.search_browse_list_width - 21) + "px";
                        }
                        else
                        {
                                document.getElementById("search_browse_list").style.width = "99%";
                        }
                }
        }
}

/* Function called to get the search data */
function getBrowseSearchData()
{
        if(search_in_use) cancel_search = true;
        else cancel_search = false;

        if(document.getElementById(search_input_field).value != "" && document.getElementById(search_input_field).value != "[Search here]")
        {
                getSearchData(500);
        }
        else
        {
                clearTimeout(search_timer_obj);
                
                get_section_subsections(current_search_browse_section_obj.section);
        }
}

function getSearchData(wait_time)
{
        clearTimeout(search_timer_obj);
        if(isNaN(wait_time))
        {
                wait_time = 500;
        } 
        search_timer_obj = setTimeout("getSearchData2()",wait_time);
}

function getSearchData2()
{
        if(document.getElementById(search_input_field).value == "[Search here]") return;
        
        if(document.getElementById(search_input_field).value == "")
        {
                document.getElementById(search_browse_list_div_id).innerHTML = "";
                document.getElementById(search_browse_list_div_id).style.display = "none";
                return;
        }
        
        clearTimeout(search_timer_obj);
        
        if(search_in_use) 
        {
                search_timer_obj = setTimeout("getSearchData2()",100);
                return;
        }
        
        //var output = ul_search_browse_list + "<li class=\"browse_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Searching " + search_browse_sections[i_section].title + " for '" + document.getElementById(search_input_field).value + "'...</a></li></ul>";
        //populateSearchBrowseList(output);

        var selected_ids = document.getElementById( current_search_browse_section_obj.hidden_ids ).value;
        var additional_fields = current_search_browse_section_obj.additional_fields;
        var subsection_text = '';
        
        for( i in current_search_browse_section_obj.subsections )
        {
                if( typeof current_search_browse_section_obj.subsections[i] == 'string' )
                {
                        subsection_text += current_search_browse_section_obj.subsections[i] + ",";
                } 
        }
        
        subsection_text = subsection_text.substr(0, (subsection_text.length - 1) );
        
        var host_id_link = get_host_id_link();
        
        search_timeout_obj = setTimeout("search_timeout()",15000);
        
        http_search = createRequestObject();
        
        http_search.open('get', 'index.php?' + host_id_link + 'type=ajax&action=get_search_data&section=' + current_search_browse_section_obj.section + '&subsections=' + subsection_text + '&data=' + document.getElementById(search_input_field).value + '&selected_ids=' + selected_ids + '&additional_fields=' + additional_fields , true);
        
        http_search.onreadystatechange = handleSearch;
        
        if(http_search.readyState > 0)
        {
                search_in_use = true;
                cancel_search = false;
                http_search.send(null);
        }
}

function search_timeout()
{
        var output = ul_search_browse_list + "<li class=\"search_item\"><a class=\"li_search_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Request timed out before returning the search list</a></li></ul>";
        populateSearchBrowseList(output);
}
function search_timeout_status()
{
        var status_code = http_browse.status;
        var output = ul_search_browse_list + "<li class=\"search_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">An error occured while retrieving the search list.</a></li><li class=\"browse_item\"><a class=\"li_browse_item\" href=\"javascript:focus_on_me('"+search_input_field+"')\">Error Code: "+status_code+"</a></li></ul>";
        populateSearchBrowseList(output);
}

function handleSearch()
{
        if(http_search)
        {
                if(http_search.readyState == 4)
                {
                        if (http_search.status == 200)
                        {
                                var response = http_search.responseText;
        
                                clearTimeout(search_timeout_obj);
                                
                                if(!cancel_search)
                                {
                                        populateSearchBrowseList(response);
                                }
                                else
                                {
                                        cancel_search = false;
                                }
                        } 
                        else search_timeout_status();
                        
                        search_in_use = false;
                }
        }
}

function get_selected_class_name(class_name)
{
        return add_class_name(class_name,"_selected");
}

function get_unselected_class_name(class_name)
{
        return remove_class_name(class_name,"_selected");
}

function force_select_option(section, subsection, actual_id, name)
{
        var id = parse_id(section, subsection, actual_id);
        
        var hidden_id = document.getElementById( search_browse_sections[section].hidden_ids );
        
        var already_in = false;
        
        var hidden_value = hidden_id.value;
        
        if(hidden_value.length > 0)
        {
                var temp_hidden = hidden_value.split(",");
                
                for(var loop = 0; loop < temp_hidden.length; loop++)
                {
                        if(id == temp_hidden[loop])
                        {
                                alert("That object is already selected.");
                                already_in = true;
                        }
                }
        }
        
        if(!already_in)
        {
                addOption(section, subsection, actual_id, name)
        }
}


function handleDeselectAllOptions()
{
        if(http_browse.readyState == 4)
        {
                if (http_browse.status == 200)
                {
                        var response = http_browse.responseText;
                        response = response.split(":");
                        
                        clearTimeout(browse_timeout_obj);
                        
                        var r_section = response[0];
                        var r_subsection = response[1];
                        var section_total_number = response[2];
                        var null_data = response[3];
                        var group = response[4];
                        var data = response[5];
                        
                        data = data.split("<separator>");
                        
                        //var hidden_id = document.getElementById( search_browse_sections[r_section].hidden_ids );
                        
                        //var hidden_value = hidden_id.value;
                
                        for(var loop = 0; loop < data.length; loop++)
                        {
                                var temp = data[loop].split("|");
                                var item_id = temp[0];
                                var item_title = temp[1];
                                
                                remove_selection(r_section+':'+r_subsection+':'+item_id);
                                //if(!check_for_hidden_id(hidden_value, item_id))
                                //{
                                //        addOption(r_section, r_subsection, item_id, item_title);
                                //}
                        }
                        
                }
                else browse_timeout_status();
                
                browse_in_use = false;
        }
}

function handleSelectAllOptions()
{
        if(http_browse.readyState == 4)
        {
                if (http_browse.status == 200)
                {
                        var response = http_browse.responseText;
                        response = response.split(":");
                        
                        clearTimeout(browse_timeout_obj);
                        
                        var r_section = response[0];
                        var r_subsection = response[1];
                        var section_total_number = response[2];
                        var null_data = response[3];
                        var group = response[4];
                        var data = response[5];
                        
                        data = data.split("<separator>");
                        
                        var hidden_id = document.getElementById( search_browse_sections[r_section].hidden_ids );
                        
                        var hidden_value = hidden_id.value;
                
                        for(var loop = 0; loop < data.length; loop++)
                        {
                                var temp = data[loop].split("|");
                                var item_id = temp[0];
                                var item_title = temp[1];
                                
                                //select_option('browse:'+r_section+':'+r_subsection+':'+item_id+'a');
                                if(!check_for_hidden_id(hidden_value, item_id))
                                {
                                        addOption(r_section, r_subsection, item_id, item_title);
                                }
                        }
                        
                }
                else browse_timeout_status();
                
                browse_in_use = false;
        }
}

function select_all_options(section,subsection,total_objects)
{

        browse_timeout_obj = setTimeout("browse_timeout()",15000);
        
        var host_id_link = get_host_id_link();

        http_browse = createRequestObject();
        
        http_browse.open('get', 'index.php?' + host_id_link + 'type=ajax&action=get_data_group&section=' + section + "&subsection=" + subsection + "&total_objects=" + total_objects + "&group=all", true);// + '&additional_fields=' + additional_fields + '&event_name=' + event_name + '&get_recommendations=0
        
        http_browse.onreadystatechange = handleSelectAllOptions;
        
        if(http_browse.readyState > 0)
        {
                browse_in_use = true;
                http_browse.send(null);
        }
        
}

function deselect_all_options(section,subsection,total_objects)
{

        browse_timeout_obj = setTimeout("browse_timeout()",15000);
        
        var host_id_link = get_host_id_link();

        http_browse = createRequestObject();
        
        http_browse.open('get', 'index.php?' + host_id_link + 'type=ajax&action=get_data_group&section=' + section + "&subsection=" + subsection + "&total_objects=" + total_objects + "&group=all", true);// + '&additional_fields=' + additional_fields + '&event_name=' + event_name + '&get_recommendations=0
        
        http_browse.onreadystatechange = handleDeselectAllOptions;
        
        if(http_browse.readyState > 0)
        {
                browse_in_use = true;
                http_browse.send(null);
        }
        
}

function select_option(option_id)
{
        var temp = option_id.split(":");
        
        var click_section = temp[0];
        
        if(click_section == "search") clearTimeout(remove_timer);
        document.getElementById(option_id+'a').focus();
        
        var section = temp[1];
        var subsection = temp[2];
        var actual_id = temp[3];
        
        var search_browse_section_obj = search_browse_sections[section];
        
        var id = parse_id(section, subsection, actual_id);
        
        var hidden_id = document.getElementById( search_browse_sections[section].hidden_ids );
        
        var set_selected = false;
        
        focus_on_me(search_input_field);

        if(click_section == "search")
        {
                var search_link_id = option_id+'a';
                var search_class_name = document.getElementById(search_link_id).className;
                
                if(search_class_name == "li_search_item")
                {
                        document.getElementById(search_link_id).className = get_selected_class_name(document.getElementById(search_link_id).className);
                        
                        var browse_link_id = "browse:"+section+":"+subsection+":"+actual_id+"a";
                        
                        if(document.getElementById(browse_link_id))
                        {
                                document.getElementById(browse_link_id).className = get_selected_class_name(document.getElementById(browse_link_id).className);
                        }
        
                        set_selected = true;
                }
                else
                {
                        document.getElementById(search_link_id).className = "li_search_item";
                        
                        remove_selection(section+":"+subsection+":"+actual_id);
                        
                        return;
                }
        }
        else if(click_section == "browse")
        {
                var browse_link_id = option_id+'a';
                var browse_class_name = document.getElementById(browse_link_id).className;
                
                if(browse_class_name.indexOf("_selected") != -1)
                {
                        remove_selection(section+":"+subsection+":"+actual_id);
                        
                        return;
                }
                else
                {
                        document.getElementById(browse_link_id).className = get_selected_class_name(document.getElementById(browse_link_id).className);
                        
                        var search_link_id = "search:"+section+":"+subsection+":"+actual_id+"a";
                        
                        if(document.getElementById(search_link_id))
                        {
                                document.getElementById(search_link_id).className = get_selected_class_name(document.getElementById(search_link_id).className);
                        }
        
                        set_selected = true;
                }
        }
        
        var hidden_value = hidden_id.value;
        
        var already_in = check_for_hidden_id(hidden_value, id);
        
        if(already_in) alert("That object is already selected.");
        
        if(set_selected && !already_in)
        {
                var name_text = document.getElementById(option_id+'a').innerHTML;
                
                var strong_start_search = new RegExp("<strong>","gi")
                var strong_end_search = new RegExp("</strong>","gi")
                
                name_text = name_text.replace(strong_start_search,"");
                name_text = name_text.replace(strong_end_search,"");
                
                addOption(section, subsection, actual_id, name_text);
        }
}

function check_for_hidden_id(hidden_value, id)
{
        if(hidden_value.length > 0)
        {
                var temp_hidden = hidden_value.split(",");
                
                for(var loop = 0; loop < temp_hidden.length; loop++)
                {
                        if(id == temp_hidden[loop])
                        {
                                return true;
                        }
                }
        }
        
        return false;
}

function addOption(section, subsection, actual_id, name_text)
{
        var id = parse_id(section, subsection, actual_id);
        var new_option_id = section + ":" + subsection + ":" + actual_id;
        
        var display_div = document.getElementById( search_browse_sections[section].display_div );
        var hidden_id = document.getElementById( search_browse_sections[section].hidden_ids );
        
        var hidden_value = hidden_id.value;
        
        var temp = new Array();
        
        if(search_browse_sections[section].replace_empty && hidden_value == search_browse_sections[section].replace_empty_id)
        {
                var unknown_option_id = section + ":" + search_browse_sections[section].subsections[0] + ":" + search_browse_sections[section].replace_empty_id;
                var unknown_div = document.getElementById( 'disp:' + unknown_option_id );
                
                if(unknown_div)
                {
                        display_div.removeChild( unknown_div );
                }
                
                hidden_id.value = "";
                hidden_value = "";
        }
        
        // the following removes the "Add", "Add more" or "Change" link
        removeAddLink(display_div,section);
        
        // the following removes the "[No [Section Title] Selected]" text
        removeNoSelectedText(display_div,section);

        var disp_id = 'disp:'+new_option_id;
        var disp_class = "selection_display_new";
        
        var new_div = document.createElement('div');
        new_div.id = disp_id;
        new_div.className = disp_class;
        
        var new_text = '';
         
        new_text += getNextNumText(section, hidden_value);
        
        new_text += name_text + "&nbsp;";
        
        if(search_browse_sections[section].subsections.length > 1)
        {
                new_text += '<span class="selection_display_type">(' + get_subsection_titles(subsection, true) + ')</span>&nbsp;';
        }
        
        new_text += getReorderHtml(section, new_option_id, hidden_value);
        
        new_text += '<a class="new_wizard_remove_link" href="javascript:remove_selection(\'' + new_option_id + '\')">X</a>';
        
        new_div.innerHTML = new_text;
        
        if(search_browse_sections[section].single_selection)
        {
                var cur_id = section+":"+subsection+":"+hidden_value;
                var browse_link_id = "browse:"+cur_id+"a";
                var search_link_id = "search:"+cur_id+"a";
                
                if(document.getElementById(browse_link_id))
                {
                        document.getElementById(browse_link_id).className = get_unselected_class_name(document.getElementById(browse_link_id).className);
                }
                
                if(document.getElementById(search_link_id))
                {
                        document.getElementById(search_link_id).className = get_unselected_class_name(document.getElementById(search_link_id).className);
                }
                
                if(document.getElementById("disp:"+cur_id)) display_div.removeChild( document.getElementById("disp:"+cur_id) );
                
                display_div.appendChild(new_div);
                
                hidden_id.value = id;
        }
        else
        {
                if(search_browse_sections[section].do_not_sort)
                {
                        display_div.appendChild(new_div);
                }
                else
                {
                        /*************************/
                        /*  Sorting of the List  */
                        /*************************/
                        
                        if(hidden_value.length > 0)
                        {
                                var sort_array = new Array();
                                
                                var divs = display_div.getElementsByTagName('div');
                                
                                for (var i = 0; i < divs.length; i++)
                                {
                                        if(divs[i].id != null && divs[i].id != "" && document.getElementById( divs[i].id ) )
                                        {
                                                var temp_display_html = divs[i].innerHTML;
                                                
                                                sort_array[i] = temp_display_html.substring(0, temp_display_html.indexOf("<")) + "|";
                                                
                                                sort_array[i] += divs[i].id + "|";
                                                
                                                sort_array[i] += i;
                                        }
                                }
                                
                                sort_array.push(name_text+"|disp:"+new_option_id+"|-1");
                                
                                sort_array.sort();
                                
                                var last_id = -1;
                                
                                for(var loop = 0; loop < sort_array.length; loop++)
                                {
                                        var temp = sort_array[loop].split("|");
                                        var div_array_index = temp[2];
                                        
                                        if(div_array_index == "-1")
                                        {
                                                if(loop == (sort_array.length - 1) )
                                                {
                                                        display_div.appendChild(new_div);
                                                }
                                                else
                                                {
                                                        var next_element = sort_array[ (loop+1) ].split("|");
                                                        var next_element_id = next_element[1];
                                                        
                                                        display_div.insertBefore(new_div, document.getElementById(next_element_id) );
                                                        break;
                                                }
                                        }
                                        
                                        last_id = temp[1];
                                }
                        }
                        else
                        {
                                display_div.appendChild(new_div);
                        }
                        
                        /*  End of Sorting  */
                }
                
                if(hidden_id.value.length > 0) hidden_id.value += ",";
                
                hidden_id.value += id;
        }
        
        if(section != current_search_browse_section_obj.section)
        {
                addAddText(search_browse_sections[section])
        }
        
        display_div.style.height = "auto";
        
        highlight_div( document.getElementById(disp_id) );

        handle_select_option(section,subsection,actual_id,id,name_text);
}

function getNextNumText(i_section, hidden_value)
{
        var new_text = "";
        
        if( search_browse_sections[i_section].selections_can_be_reordered )
        {
                if(hidden_value.length > 0)
                {
                        var temp_hidden = hidden_value.split(",");
                        var next_num = temp_hidden.length + 1;
                        
                        var num_text = get_num_text( next_num );
                        
                        new_text += next_num + num_text + ": ";
                }
                else
                {
                        new_text += "1st: ";
                }
        }
        
        return new_text;
}

function getReorderHtml(section, new_option_id, hidden_value)
{
        var new_text = "";
        
        if( search_browse_sections[section].selections_can_be_reordered )
        {
                var move_up_style = "";
                
                if(hidden_value.length == 0) move_up_style = ' style="display: none;"';
                
                var move_loc_up_img_mouseover = 'onmouseover="mouseover_move_up_img(this)" onmouseout="mouseout_move_up_img(this)"';
                var move_loc_down_img_mouseover = 'onmouseover="mouseover_move_down_img(this)" onmouseout="mouseout_move_down_img(this)"';
                
                var code_loc = document.getElementById('code_loc').value;
                
                var move_down_img = '<a id="' + new_option_id + ':a:img:down" style="display: none;" onClick="move_location_down(\'' + new_option_id + '\')"><img ' + move_loc_down_img_mouseover + ' src="'+code_loc+'images/navigation/down_small.png" alt="Move Down" border="0" /></a>';
                var move_up_img = '<a id="' + new_option_id + ':a:img:up"'+move_up_style+' onClick="move_location_up(\'' + new_option_id + '\')"><img ' + move_loc_up_img_mouseover + ' src="'+code_loc+'images/navigation/up_small.png" alt="Move Up" border="0" /></a>';
        
                new_text += move_up_img + move_down_img + "&nbsp;";
        }
           
        return new_text;
}

function remove_selection(selection_id)
{
        if( document.getElementById( current_search_browse_section_obj.search_browse_container ).style.display == "")
        {
                focus_on_me(search_input_field);
        }
        
        var temp = selection_id.split(":");

        var section = temp[0];
        var subsection = temp[1];
        var actual_id = temp[2];
        
        var id = parse_id(section, subsection, actual_id);
        var disp_id = 'disp:' + selection_id;
        
        var hidden_value = document.getElementById( search_browse_sections[section].hidden_ids ).value;
        var temp_hidden = hidden_value.split(",");
        
        var replace_with_empty = false;
        
        var complete_removal = false;
        
        if( search_browse_sections[section].replace_empty && temp_hidden.length == 1 )
        {
                hidden_value = search_browse_sections[section].replace_empty_id;
                complete_removal = true;
                replace_with_empty = true;
        }
        else if(hidden_value.length > 0)
        {
                var new_hidden = "";
                
                for(var loop = 0; loop < temp_hidden.length; loop++)
                {
                        if(id != temp_hidden[loop])
                        {
                                new_hidden += temp_hidden[loop] + ",";
                        }
                        else
                        {
                                complete_removal = true;
                        }
                }
                
                if(new_hidden.length > 0)
                {
                        hidden_value = new_hidden.substring(0,new_hidden.length-1);
                }
                else
                {
                        hidden_value = "";
                        if(document.getElementById( section+"_modify" ))
                        {
                                if(search_browse_sections[section].add_link_text == "")
                                {
                                        document.getElementById( section+"_modify" ).innerHTML = "Add";
                                }
                                else
                                {
                                        document.getElementById( section+"_modify" ).innerHTML = search_browse_sections[section].add_link_text;
                                }
                                
                        }
                }
        }
        
        var selection_div = document.getElementById( disp_id );
        var display_div = document.getElementById( search_browse_sections[section].display_div );
          
        if(complete_removal)
        {
                document.getElementById( search_browse_sections[section].hidden_ids ).value = hidden_value;
                
                display_div.removeChild( selection_div );
                
                if( search_browse_sections[section].selections_can_be_reordered )
                {
                        var divs = display_div.getElementsByTagName('div');
                        
                        for (var i = 0; i < divs.length; i++)
                        {
                                if( document.getElementById( divs[i].id ) && divs[i].id != section + "_add" && divs[i].id != section + "_no_selected" )
                                {
                                        var before_html = divs[i].innerHTML;
                                        
                                        divs[i].innerHTML = replace_num_text(divs[i].innerHTML, i + 1);
                                        
                                        if(divs[i].innerHTML != before_html)
                                        {
                                                highlight_div(divs[i]);
                                        }
                                }
                        }
                }
                
                if(replace_with_empty)
                {
                        removeAddLink(display_div, section);
                        
                        var unknown_option_id = section + ":" + search_browse_sections[section].subsections[0] + ":" + search_browse_sections[section].replace_empty_id;
                        
                        var unknown_link = document.createElement("div");
                        unknown_link.id = 'disp:' + unknown_option_id;
                        unknown_link.className = search_browse_sections[section].replace_empty_class_name;
                        unknown_link.innerHTML = search_browse_sections[section].replace_empty_name;
                        
                        display_div.appendChild(unknown_link);
                        
                        if(section != current_search_browse_section_obj.section)
                        {
                                addAddText(search_browse_sections[section]);
                        }
                }
                
                if(!document.getElementById( section+"_modify" ))
                {
                        var hidden_ids_text = document.getElementById( search_browse_sections[section].hidden_ids ).value;
                        
                        if(hidden_ids_text == "")
                        {
                                addNoSelectedText(search_browse_sections[section]);
                        }
                }
                
                var browse_link_id = "browse:"+section+":"+subsection+":"+actual_id+"a";
                var search_link_id = "search:"+section+":"+subsection+":"+actual_id+"a";
                
                if(document.getElementById(browse_link_id))
                {
                        document.getElementById(browse_link_id).className = get_unselected_class_name(document.getElementById(browse_link_id).className);
                }
                
                if(document.getElementById(search_link_id))
                {
                        document.getElementById(search_link_id).className = get_unselected_class_name(document.getElementById(search_link_id).className);
                }
                
                handle_remove_selection(section,subsection,actual_id,id, search_browse_sections[section].hidden_ids );
        }   
}

function checkForSearchText()
{
        if(document.getElementById(search_input_field).value == "[Search here]")
        {
                document.getElementById(search_input_field).value = "";
                document.getElementById(search_input_field).className = "new_wizard_input";
        }
}

