// A couple of ajax HTTP constants

var AG_FINISHED_LOADING = 4;
var AG_LOADING_COMPLETE = 'complete';
var AG_STATUS_OK = 200;

// global variable to store our attack graphic selection
// so it will not be automatically updated when the
// attack list drop-down box changes

var AG_graphic_selected_GLOBAL;
var PATH;

// function to load additional content for the selected attack

function AG_load_content (preference) {

        // find the selection (event_id) and load the image/player divs that match it
        // preference is either "prefer_old" or "prefer_new"
        // prefer_old checks for an old value and uses that if it exists
        // prefer_new only uses the new default value.

        var event_id;
	var list_select = document.getElementById("AG_list_select");
        if(list_select==null) return;

        // first check to see if there is a stored value
        if (AG_graphic_selected_GLOBAL && preference == "prefer_old") {
                // use the existing value & set the form selection to it
                event_id = AG_graphic_selected_GLOBAL;
		ii=0
                while(i<list_select.length){
			if(list_select[i].value==AG_graphic_selected_GLOBAL){
				list_select[i].selected=true; // had a typo 'list_selected'
			}
		i++;
		}
        } else {
		if(list_select.length){
                // use the new value & store it for later in the global var.
                event_id = list_select[list_select.selectedIndex].value;
                AG_graphic_selected_GLOBAL = event_id;
	   }
        };
if(event_id){
        AG_load_image(event_id);

        AG_load_players(event_id);
}
        return;
};



// function to make the key and player numbers visible or invisi

function AG_toggle_key() {

    var key_span = document.getElementById("AG_key");
    var current_display = key_span.style.visibility;
    if (current_display == "visible") {
        key_span.style.visibility = "hidden";
    } else {
        key_span.style.visibility = "visible";
    };

    var toggle_key_click_span = document.getElementById("AG_toggle_key_click");
    if (toggle_key_click_span.innerHTML == 'Hide&nbsp;Key') {
        toggle_key_click_span.innerHTML = 'Show&nbsp;Key';
    } else if (toggle_key_click_span.innerHTML == 'Show&nbsp;Key') {
        toggle_key_click_span.innerHTML = 'Hide&nbsp;Key';
    };
    return;
};

// function to load the AG_attack_list DIV with the latest shot_list.html

function AG_load_attack_list(response) {
        // look to put the response in response_string
        var response_string;
        var response_ok = 0; // set 1 to indicate success

        if (response.readyState == AG_FINISHED_LOADING || response.readyState == AG_LOADING_COMPLETE) {
                if (response.status == AG_STATUS_OK) {
                        response_string = response.responseText
                        response_ok = 1;
                };
        };

        if (response_ok) {
                var attack_list_obj = document.getElementById("AG_attack_list");
                attack_list_obj.innerHTML = response_string;
        };
        return;
};

// function to load the correct image

function AG_load_image(event_id) {

        // get image object, which has id AG_attack_image
        // event_id is the attack list selection & determines the image src name
        var image_src;
        var image_object = document.getElementById("AG_attack_image")

        // change src to the correct image
        // image names are in the form: shot_$event_id.png
        image_src = PATH + 'shot_' + event_id + '.png';
        image_object.src = image_src+"?"+Math.random()*99999;

        return;
};

// function to load the correct player to numbers translations
function AG_load_players(event_id) {

        // call the ajax library to get
        // the file "player_id_translations_$event_id.html"
        // and put in the div id="AG_attack_player_numbers"
        // event_id is the attack list selection & determines the filename to load

        var filename = PATH  +'player_id_translations_' + event_id + '.html';

        // Perform an asynchronous HTTP GET request and let the function
        // AG_process_players handle the result.
        AJAXRequest( 'GET', filename, '', AG_process_players);

        return;
};

// function to handle the result of the ajax request for the player_id_translations_nnn.html file

function AG_process_players(response) {

        // look to put the response in response_string
        var response_string;
        var response_ok = 0; // set 1 to indicate success

        if (response.readyState == AG_FINISHED_LOADING || response.readyState == AG_LOADING_COMPLETE) {
	try {
 	 mystat= response.status;
	}catch(e){
	 return;
	}         
        if (response.status == AG_STATUS_OK) {
                        response_string = response.responseText
                        response_ok = 1;
                };
        };

        if (response_ok) {
                var players_obj = getObj("AG_attack_player_numbers");
                players_obj.innerHTML = response_string;
        };
        return;
};


