var onmouseover_event = 'onmouseover="normal_mouseover(this)" onmouseout="normal_mouseout(this)"';

var get_conflict_timer
var get_conflict_timeout
var conflict_check_in_use = false;

var show_override_conflicts_checkbox = true;

var conflict_status = 0;
var conflict_error_status = 0;

function handleGetConflicts()
{
        if(http_conflicts.readyState == 4)
        {
                var response = http_conflicts.responseText;
                clearTimeout(get_conflict_timeout);
                
                var conflicts_checked = false;
                
                var all_errors = "";
                
                if(response)
                {
                if(response.length > 0)
                {
                        conflicts_checked = true;
                        
                        var conflict_output = "";
                        var cur_index = 0;
                        var cur_sub_index = 0;
                        
                        var start_error_index = 0;
                        var end_error_index = 0;
                        
                        var start_section_index = 0;
                        var end_section_index = 0;
                        
                        var cur_title = "";
                        var start_title_index = 0;
                        var end_title_index = 0;
                        
                        var cur_line = "";
                        var start_line_index = 0;
                        var end_line_index = 0;
                        
                        var cur_num = 0;
                        
                        while(response.indexOf("<error>",cur_index) != -1)
                        {
                                start_error_index = response.indexOf("<error>",cur_index) + 7;
                                end_error_index = response.indexOf("</error>",start_error_index);
                                
                                all_errors += response.substring(start_error_index, end_error_index) + "<br />\n";
                                
                                cur_index = end_error_index + 7;
                        }
                        
                        cur_index = 0;
                        
                        while(response.indexOf("<section",cur_index) != -1)
                        {
                                start_section_index = response.indexOf("<section",cur_index);
                                end_section_index = response.indexOf("</section>",start_section_index);
                                
                                start_title_index = response.indexOf("title=\"",start_section_index) + 7;
                                end_title_index = response.indexOf("\"",start_title_index);
                                
                                cur_title = response.substring(start_title_index, end_title_index);
                                
                                cur_sub_index = start_section_index;
                                
                                cur_num++;
                                
                                while(cur_sub_index < (end_section_index-1))
                                {
                                        start_line_index = response.indexOf("<line>",cur_sub_index) + 6;
                                        end_line_index = response.indexOf("</line>",start_line_index);
                                        
                                        cur_line = response.substring(start_line_index, end_line_index);
                                        
                                        conflict_output += cur_line + "\n";
                                        
                                        cur_sub_index = end_line_index + 6;
                                        
                                        cur_num++;
                                }
                                
                                cur_index = end_section_index + 10;
                                
                                if(cur_title != 'hash')
                                {
                                        conflict_output += '<div class="sm_spacer">&nbsp;</div>';
                                }
                                
                        }
                        
                        if(show_override_conflicts_checkbox && conflict_output.length > 0)
                        {
                                conflict_output += '<div class="new_wizard_override_button">';
                                conflict_output += '<span class="override">Override</span> <span class="lblack">Conflict(s)?</span><input type="checkbox" id="override_conflicts" name="override_conflicts" value="1" />';
                                conflict_output += "</div>\n";
                        }
                        
                        if(conflict_output.length > 0)
                        {
                                var prev_conflict_hash = '';
                                var prev_conflict_checked = false;
                                
                                var new_conflict_hash = '';
                                var new_conflict_checked = false;
                                
                                if(document.getElementById("conflict_hash")) prev_conflict_hash = document.getElementById("conflict_hash").value;
                                if(document.getElementById("override_conflicts")) prev_conflict_checked = document.getElementById("override_conflicts").checked;
                                
                                document.getElementById("new_wizard_conflict_display").innerHTML = conflict_output;
                                
                                if(document.getElementById("conflict_hash")) new_conflict_hash = document.getElementById("conflict_hash").value;
                                if(document.getElementById("override_conflicts")) new_conflict_checked = document.getElementById("override_conflicts").checked;
                                
                                conflict_status = 3;
                                
                                document.getElementById("conflict_container").style.display = "";
                                document.getElementById("new_wizard_conflict_notification").style.display = "";
                                        
                                if(new_conflict_hash == prev_conflict_hash && prev_conflict_checked)
                                {
                                        document.getElementById("override_conflicts").checked = true;
                                }
                                else
                                {
                                        show_section(13);
                                }
                        }
                        else
                        {
                                conflicts_checked = false;
                        }
                }
                }
                
                document.getElementById("new_wizard_conflict_error_display").innerHTML = all_errors;
                
                if(all_errors.length > 0)
                {
                        if(conflict_error_status == 1) conflict_error_status = 3;
                        
                        document.getElementById("new_wizard_conflict_error_display").style.display = "";
                }
                else
                {
                        if(conflict_error_status == 1) conflict_error_status = 2;
                        
                        document.getElementById("new_wizard_conflict_error_display").style.display = "none";
                }
                
                //document.getElementById("debug_display").innerHTML += "from handleGetConflicts()<br />\n";
                handle_status_change();
                
                conflict_check_in_use = false;
                
                if(!conflicts_checked)
                {
                        conflict_status = 2;
                        
                        document.getElementById("new_wizard_conflict_display").innerHTML = "[ No Conflicts ]";
                        document.getElementById("new_wizard_conflict_notification").style.display = "none";
                }
        }
}

function get_conflicts_timeout()
{
        conflict_status = 2;
        
        document.getElementById("new_wizard_conflict_display").innerHTML = "An error occured while retrieving the conflict data";
        
        document.getElementById("conflict_container").style.display = "";
        document.getElementById("new_wizard_conflict_notification").style.display = "";
}

function optional_container_object(container,title)
{
        this.container = container;
        this.title = title;
}

function removeDiv(element_obj,id)
{
        if(document.getElementById(id))
        {
                element_obj.removeChild( document.getElementById(id) );
        }
}

function add_class_name(class_name,add_text)
{
        if(class_name.indexOf(add_text) == -1)
        {
                class_name += add_text;
        }
        
        return class_name;
}

function remove_class_name(class_name,remove_text)
{
        if(class_name.indexOf(remove_text) != -1)
        {
                class_name = class_name.substr( 0 , (class_name.length - remove_text.length) );
        }
        
        return class_name;
}

function get_clicked_class_name(class_name)
{
        return add_class_name(class_name,"_clicked");
}

function get_unclicked_class_name(class_name)
{
        return remove_class_name(class_name,"_clicked");
}

function normal_mouseover(ref_obj)
{
        ref_obj.className = add_class_name(ref_obj.className,"_hover");
}

function normal_mouseout(ref_obj)
{
        ref_obj.className = remove_class_name(ref_obj.className,"_hover");
}

var current_optional_container = -1;

function show_hide_section(section_number)
{
        var code_loc = document.getElementById('code_loc').value;
        
        for( loop in optional_containers )
        {
                if(document.getElementById(optional_containers[loop].container))
                {
                        if(loop != section_number || (loop == section_number && document.getElementById(optional_containers[loop].container).style.display != "none"))
                        {
                                if(loop == section_number) current_optional_container = -1;
                                
                                document.getElementById(optional_containers[loop].container).style.display = "none";
                                document.getElementById(optional_containers[loop].title+"_img").src = code_loc + "images/events/section_icon.gif";
                                
                                if(document.getElementById(optional_containers[loop].title).className == "new_wizard_sub_section_link_clicked")
                                {
                                        document.getElementById(optional_containers[loop].title).className = "new_wizard_sub_section_link";
                                }
                                else if(document.getElementById(optional_containers[loop].title).className == "new_wizard_sub_section_link_clicked_hover")
                                {
                                        document.getElementById(optional_containers[loop].title).className = "new_wizard_sub_section_link_hover";
                                }
                        }
                        else
                        {
                                if(loop == section_number) current_optional_container = section_number;
                                
                                document.getElementById(optional_containers[loop].container).style.display = "";
                                document.getElementById(optional_containers[loop].title+"_img").src = code_loc + "images/events/section_icon_open.gif";
                                
                                if(document.getElementById(optional_containers[loop].title).className == "new_wizard_sub_section_link")
                                {
                                        document.getElementById(optional_containers[loop].title).className = "new_wizard_sub_section_link_clicked";
                                }
                                else if(document.getElementById(optional_containers[loop].title).className == "new_wizard_sub_section_link_hover")
                                {
                                        document.getElementById(optional_containers[loop].title).className = "new_wizard_sub_section_link_clicked_hover";
                                }
                        }
                }
        }
}


function close_section(close_section_id)
{
        document.getElementById(close_section_id).style.display = "none";
}

function hide_section(section_number)
{
        document.getElementById(default_containers[section_number]).style.display = "none";
        document.getElementById(default_containers[section_number]+"_hide").style.display = "none";
        document.getElementById(default_containers[section_number]+"_show").style.display = "";
        
        if(parseInt(section_number) == 13 && document.getElementById("new_wizard_conflict_display").innerHTML == "[ No Conflicts ]")
        {
                document.getElementById("conflict_container").style.display = "none";
                document.getElementById("new_wizard_conflict_notification").style.display = "none";
        }
}

function show_section(section_number)
{
        document.getElementById(default_containers[section_number]).style.display = "";
        document.getElementById(default_containers[section_number]+"_hide").style.display = "";
        document.getElementById(default_containers[section_number]+"_show").style.display = "none";
}

function show_div(i_div_id)
{
        if(document.getElementById(i_div_id))
        {
                if(document.getElementById(i_div_id).style.display == "none")
                {
                        document.getElementById(i_div_id).style.display = "";
                        document.getElementById(i_div_id+"_show").style.display = "none";
                        document.getElementById(i_div_id+"_hide").style.display = "";
                }
                else
                {
                        document.getElementById(i_div_id).style.display = "none";
                        document.getElementById(i_div_id+"_show").style.display = "";
                        document.getElementById(i_div_id+"_hide").style.display = "none";
                }
        }
}

function hide_div(i_div_id)
{
        if(document.getElementById(i_div_id))
        {
                document.getElementById(i_div_id).style.display = "none";
                if(document.getElementById(i_div_id+"_show")) document.getElementById(i_div_id+"_show").style.display = "";
                if(document.getElementById(i_div_id+"_hide")) document.getElementById(i_div_id+"_hide").style.display = "none";
        }
}

function select_show_hide_div(select_obj,value_div_ar)
{
        if( typeof value_div_ar != 'undefined' )
        {
                for( var i = 0; i < value_div_ar.length; i++ )
                {
                        if(value_div_ar[i][0] == select_obj.selectedIndex)
                        {
                                document.getElementById( value_div_ar[i][1] ).style.display = '';
                                
                                highlight_div_id(value_div_ar[i][1],'selection_display_new','');
                        }
                        else
                        {
                                document.getElementById( value_div_ar[i][1] ).style.display = 'none';
                        }
                }
        }
}

function checkbox_show_hide_div(checkbox_obj,div_id,uncheck_ids)
{
        if(checkbox_obj.checked)
        {
                document.getElementById(div_id).style.display = '';
        }
        else
        {
                document.getElementById(div_id).style.display = 'none';
                
                if( typeof uncheck_ids != 'undefined' )
                {
                        uncheck_ids_ar = uncheck_ids.split(',');
                        
                        for( var i = 0; i < uncheck_ids_ar.length; i++ )
                        {
                                document.getElementById( uncheck_ids_ar[i] ).checked = false;
                        }
                }
        }
}

function checkbox_hide_div(checkbox_obj,div_id,uncheck_ids)
{
        if(!checkbox_obj.checked)
        {
                document.getElementById(div_id).style.display = 'none';
                
                if( typeof uncheck_ids != 'undefined' )
                {
                        uncheck_ids_ar = uncheck_ids.split(',');
                        
                        for( var i = 0; i < uncheck_ids_ar.length; i++ )
                        {
                                document.getElementById( uncheck_ids_ar[i] ).checked = false;
                        }
                }
        }
}

function get_num_text(i_number)
{
        i_number = parseInt(i_number);
        var i_number_string = String(i_number);
        var normal = false;
        var num_text = "";
        var i_number_remainder = 0;
        
        if(i_number_string.length > 1)
        {
                if(( i_number_string.substring( i_number_string.length-2 , i_number_string.length-1 ) ) != "1")
                {
                        normal = true;
                }
                else normal = false;
        }
        else normal = true;
        
        if(normal)
        {
                i_number_remainder = i_number % 10;
        
                if(i_number_remainder == 1) num_text = "st";
                else if(i_number_remainder == 2) num_text = "nd";
                else if(i_number_remainder == 3) num_text = "rd";
                else if(i_number_remainder > 1 || i_number_remainder == 0) num_text = "th";
        }
        else num_text = "th";
        
        return num_text;
}

function replace_num_text(div_html, new_num)
{
        var num_text = get_num_text( new_num );
        
        var new_num_text = new_num + num_text + ": ";
        
        if(div_html.indexOf("<a") != -1) var cur_option_name = div_html.substring(0, div_html.indexOf("<a"));
        else var cur_option_name = div_html.substring(0, div_html.indexOf("<A"));
        
        var cur_option_num_text = cur_option_name.substring(0, cur_option_name.indexOf(": ") + 2 );
        
        cur_option_name = new_num_text + cur_option_name.substring( cur_option_name.indexOf(": ") + 2 );
        
        if(div_html.indexOf("<a") != -1) div_html = cur_option_name + div_html.substring( div_html.indexOf("<a") );
        else div_html = cur_option_name + div_html.substring( div_html.indexOf("<A") );
        
        return div_html;

}

var unhighlight_selected_timer_obj = new Array();

function highlight_div_id(div_id,highlight_class,unhighlight_class)
{
        if( typeof highlight_class == 'undefined' ) highlight_class = 'selection_display_new';
        if( typeof unhighlight_class == 'undefined' ) unhighlight_class = 'selection_display';
        
        if(document.getElementById(div_id)) highlight_div( document.getElementById(div_id), highlight_class, unhighlight_class );
}

function highlight_div(div_obj,highlight_class,unhighlight_class)
{
        if( typeof highlight_class == 'undefined' ) highlight_class = 'selection_display_new';
        if( typeof unhighlight_class == 'undefined' ) unhighlight_class = 'selection_display';
        
        if(unhighlight_selected_timer_obj[ div_obj.id ]) clearTimeout(unhighlight_selected_timer_obj[ div_obj.id ]);
        
        unhighlight_selected_timer_obj[ div_obj.id ] = setTimeout("highlight_div2('"+div_obj.id+"','"+highlight_class+"','"+unhighlight_class+"')", 0);
}

function highlight_div2(selection_id,highlight_class,unhighlight_class)
{
        if( typeof highlight_class == 'undefined' ) highlight_class = 'selection_display_new';
        if( typeof unhighlight_class == 'undefined' ) unhighlight_class = 'selection_display';
        
        if(document.getElementById(selection_id))
        {
                document.getElementById(selection_id).className = highlight_class;
                
                if(unhighlight_selected_timer_obj[ selection_id ]) clearTimeout(unhighlight_selected_timer_obj[ selection_id ]);
                
                unhighlight_selected_timer_obj[ selection_id ] = setTimeout("unhighlight_selected('"+selection_id+"','"+unhighlight_class+"')", 750);
        }
}

function unhighlight_selected(selection_id,unhighlight_class)
{
        if( typeof unhighlight_class == 'undefined' ) unhighlight_class = 'selection_display';
        
        clearTimeout(unhighlight_selected_timer_obj[selection_id]);
        
        if(document.getElementById(selection_id))
        {
                document.getElementById(selection_id).className = unhighlight_class;
        }
}

var cur_title_id = "";

function set_cur_title(id)
{
        if(cur_title_id != "" && cur_title_id != id) unset_cur_title(cur_title_id);
        document.getElementById(id).className = 'new_wizard_group_current';
        cur_title_id = id;
}

function unset_cur_title(id)
{
        document.getElementById(id).className = 'new_wizard_group';
}
