function change_states(country_element_id, state_element_id, preserve_state_value) {
    var country_element = document.getElementById(country_element_id);
    var state_element = document.getElementById(state_element_id);
    var new_states = new Array();
    var state_container_id = state_element_id + "_container";
    var state_element_value = state_element.value;

    var y = 0;
    // for all states in states array
    for (x = 0; x < states.length; x++) {
        // if this state is in the country that was selected, store this state
        if (states[x][2] == country_element.value) {
            new_states[y] = x;
            y++;
        }
    }

    // if there is at least one state for selected country, prepare selection drop-down field for state
    if (new_states.length > 0) {
        document.getElementById(state_container_id).innerHTML = '<select name="' + state_element_id + '" id="' + state_element_id + '" class="software_select"></select>';

        var new_state_element = document.getElementById(state_element_id);

        // set "-Select-" to be the first option of state selection drop-down field
        new_state_element.options[new_state_element.length] = new Option('-Select-', '');

        // loop through all states
        for (x = 0; x < new_states.length; x++) {
            new_state_element.options[new_state_element.length] = new Option(states[new_states[x]][0], states[new_states[x]][1]);
        }

    // else there is not a state for selected country, so prepare text field for state
    } else {
        document.getElementById(state_container_id).innerHTML = '<input type="text" name="' + state_element_id + '" id="' + state_element_id + '" class="software_input_text" />';
    }
    
    if ((preserve_state_value == true) && (state_element_value != '')) {
        document.getElementById(state_element_id).value = state_element_value;
    }
}

function change_quick_add_product_id(product_id)
{
    var selection_type;
    var default_quantity;
    var recipient_required;
    
    if (product_id) {
        selection_type = quick_add_products[product_id][0];
        default_quantity = quick_add_products[product_id][1];
        recipient_required = quick_add_products[product_id][2];        
    }
    
    // hide all quick add rows until we figure out which rows to show
    
    if (document.getElementById('quick_add_quantity_row')) {
        document.getElementById('quick_add_quantity_row').style.display = 'none';        
    }
    
    if (document.getElementById('quick_add_amount_row')) {
        document.getElementById('quick_add_amount_row').style.display = 'none';        
    }
    
    if (document.getElementById('quick_add_ship_to_row')) {
        document.getElementById('quick_add_ship_to_row').style.display = 'none';        
    }
    
    if (document.getElementById('quick_add_add_name_row')) {
        document.getElementById('quick_add_add_name_row').style.display = 'none';        
    }
    
    // if product has a quantity selection type, then prefill default quantity and show quantity row
    if (selection_type == 'quantity') {
        document.getElementById('quick_add_quantity').value = default_quantity;
        document.getElementById('quick_add_quantity_row').style.display = '';
    }
    
    // if product has a donation selection type, then show amount row
    if (selection_type == 'donation') {
        document.getElementById('quick_add_amount_row').style.display = '';
    }
    
    // if a recipient is required to be selected, then show ship to and add name rows
    if (recipient_required == true) {
        document.getElementById('quick_add_ship_to_row').style.display = '';
        document.getElementById('quick_add_add_name_row').style.display = '';
    }
}

function software_show_popup_menu(parent_element, position)
{
    parent_element.className = 'on';

    // loop through all nodes under li, in order to find a menu
    for (var i = 0; i < parent_element.childNodes.length; i++) {
        var node = parent_element.childNodes[i];
        
        // if node is a ul, then position menu and show menu
        if (node.nodeName == 'UL') {
            switch (position) {
                case 'Top':
                    node.style.left = '0';
                    node.style.top = '-' + node.offsetHeight + 'px';
                    break
                        
                case 'Bottom':
                    node.style.left = '0';
                    node.style.top = node.parentNode.offsetHeight + 'px';
                    break
                        
                case 'Left':
                    node.style.left = '-' + node.offsetWidth + 'px';
                    node.style.top = '0';
                    break;
                    
                case 'Right':
                    node.style.left = node.parentNode.offsetWidth + 'px';
                    node.style.top = '0';
                    break;
            }
            
            node.style.visibility = 'visible';
        }
    }
}

function software_hide_popup_menu(parent_element)
{
    parent_element.className = '';
    
    // loop through all nodes under li, in order to find a menu
    for (var i = 0; i < parent_element.childNodes.length; i++) {
        var node = parent_element.childNodes[i];
        
        // if node is a ul, then hide menu
        if (node.nodeName == 'UL') {
            node.style.visibility = 'hidden';
        }
    }
}

function software_photo_gallery_update_photo(photo_id)
{
    // If there is photo gallery information for the photo_id
    if (software_photo_gallery_photos[photo_id] === undefined) {
        photo_id = 0;
    }
    
    if (software_photo_gallery_photos[photo_id] !== undefined) {    
        if (software_photo_gallery_number_of_thumbnails > 0) {
            // If the specified photo_id is displayed in a thumbnail view higher than the one currently visible, slide it to the right.
            while (photo_id >= (software_photo_gallery_first_thumbnail_id + software_photo_gallery_number_of_thumbnails)) {
                software_photo_gallery_update_thumbnails('forward');
            }
            
            // If the specified photo_id is displayed in a thumbnail view lower than the one currently visible, slide it to the left.
            while (photo_id < software_photo_gallery_first_thumbnail_id) {
                software_photo_gallery_update_thumbnails('back');
            }
        }
        
        // If the main photo gallery image tag exists
        if (document.getElementById('software_photo_gallery_photo')) {
            // Set the image src to the specified photo_id
            document.getElementById('software_photo_gallery_photo').src = '/files/' + software_photo_gallery_photos[photo_id][0];
            
            // If there is al ink around the main photo gallery photo, update its href to link to the new picture.
            if (document.getElementById('software_photo_gallery_photo_link')) {
                document.getElementById('software_photo_gallery_photo_link').setAttribute("href", "/files/" + software_photo_gallery_photos[photo_id][0]);
            }
            
            // If the caption area exists, update it with the specified photos caption.
            if (document.getElementById('software_photo_gallery_caption')) {
                document.getElementById('software_photo_gallery_caption').innerHTML = software_photo_gallery_photos[photo_id][1];
            }
            
            // store the previous selected thumbnail id in a variable, so that we can update its font weight.
            var software_photo_gallery_current_thumbnail_id = software_photo_gallery_current_photo_id - software_photo_gallery_first_thumbnail_id + 1;
            
            // If the first thumbnail exists, remove the class. (We could have moved to a thumbnail row that does not have the maximum amount of columns, so this could error)
            if (document.getElementById('software_photo_gallery_thumbnail_' + software_photo_gallery_current_thumbnail_id)) {            
                document.getElementById('software_photo_gallery_thumbnail_' + software_photo_gallery_current_thumbnail_id).className = 'thumbnail';
            }
            
            // If the first thumbnail label exists, remove the class. (We could have moved to a thumbnail row that does not have the maximum amount of columns, so this could error)
            if (document.getElementById('software_photo_gallery_thumbnail_label_' + software_photo_gallery_current_thumbnail_id)) {            
                document.getElementById('software_photo_gallery_thumbnail_label_' + software_photo_gallery_current_thumbnail_id).className = 'thumbnail_label';
            }
            
            // Update the global variable so software knows which photo is currently being shows (For the slideshow)
            software_photo_gallery_current_photo_id = photo_id;
            
            // store the selected thumbnail id in a variable, so that we can update its class to the active class
            var software_photo_gallery_current_thumbnail_id = photo_id - software_photo_gallery_first_thumbnail_id + 1;
            
            // If the selected thumbnail exists, assign it the active thumbnail class!
            if (document.getElementById('software_photo_gallery_thumbnail_' + software_photo_gallery_current_thumbnail_id)) {
                document.getElementById('software_photo_gallery_thumbnail_' + software_photo_gallery_current_thumbnail_id).className = 'current_thumbnail';
            }
            
            // If the selected thumbnail has a label, assign it the active thumbnail label class!
            if (document.getElementById('software_photo_gallery_thumbnail_label_' + software_photo_gallery_current_thumbnail_id)) {
                document.getElementById('software_photo_gallery_thumbnail_label_' + software_photo_gallery_current_thumbnail_id).className = 'current_thumbnail_label';
            }
        }
    }
}

function software_photo_gallery_update_thumbnails(direction)
{
    function update_onclick(thumbnail_id, photo_id)
    {
        document.getElementById('software_photo_gallery_thumbnail_' + thumbnail_id).onclick = function() { software_photo_gallery_update_photo(photo_id); };
    }

    switch (direction) {
        case 'back':
            // If this is not the first thumbnail view, allow it to move backwards
            if (software_photo_gallery_first_thumbnail_id != 0) {
                software_photo_gallery_first_thumbnail_id = software_photo_gallery_first_thumbnail_id - software_photo_gallery_number_of_thumbnails;
                
                // If we traveled into the negatives, set it to 0 instead.
                if (software_photo_gallery_first_thumbnail_id < 0) {
                    software_photo_gallery_first_thumbnail_id = 0;
                }
                
                // For each thumbnail
                for (var i = 0; i < software_photo_gallery_number_of_thumbnails; i++) {
                    var current_image_index = software_photo_gallery_first_thumbnail_id + i;
                    
                    // If the thumbnail exists
                    if (document.getElementById('software_photo_gallery_thumbnail_' + (i + 1))) {
                        // If the thumbnail photo does not exists
                        if (software_photo_gallery_photos[current_image_index] === undefined) {
                            // Hide the thumbnails table cell.
                            document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).parentNode.style.display = 'none';
                            document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).parentNode.style.display = 'none';
                            // Remove the previous photo, onclick, and label if any.
                            document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).src = '';
                            document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).onclick = '';
                            document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).innerHTML = '';
                            
                        // Else, there is a photo for the current thumbnail
                        } else {
                            // Update the thumbnails img src, label and onclick
                            document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).src = '/files/' + software_photo_gallery_photos[current_image_index][0];
                            document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).innerHTML = (current_image_index + 1);
                            update_onclick(i + 1, current_image_index);
                            
                            // If the current thumbnail is the current main photo gallery image, apply the active class
                            if (document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).src == document.getElementById('software_photo_gallery_photo').src) {
                                document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).className = 'current_thumbnail_label';
                            	document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).className = 'current_thumbnail';
                            // Else, remove the active class just in case there is some previously selected image with the active class applied.
                            } else {
                                document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).className = 'thumbnail_label';
                                document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).className = 'thumbnail';
                            }
                            
                            // Display the thumbnail table cell.
                            document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).parentNode.style.display = '';
                            document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).parentNode.style.display = '';
                        }
                    }
                }
            }
            break;
                
        case 'forward':
            // If there are more photos in the photo array, update the first thumbnail id.
            if (software_photo_gallery_photos[software_photo_gallery_first_thumbnail_id + software_photo_gallery_number_of_thumbnails] !== undefined) {
                software_photo_gallery_first_thumbnail_id = software_photo_gallery_first_thumbnail_id + software_photo_gallery_number_of_thumbnails;
            }
            
            // For each thumbnail
            for (var i = 0; i < software_photo_gallery_number_of_thumbnails; i++) {
                var current_image_index = software_photo_gallery_first_thumbnail_id + i;
                
                // If the thumbnail container exists
                if (document.getElementById('software_photo_gallery_thumbnail_' + (i + 1))) {
                    // If there is not a photo for this thumbnail
                    if (software_photo_gallery_photos[current_image_index] === undefined) {
                        // Hide its container, remove its photo, its label and its onclick event
                        document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).parentNode.style.display = 'none';
                        document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).parentNode.style.display = 'none';
                        document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).src = '';
                        document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).onclick = '';
                        document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).innerHTML = '';
                    
                    // Else, there was a photo that we can use
                    } else {
                        // Update its photo and label
                        document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).src = '/files/' + software_photo_gallery_photos[current_image_index][0];
                        document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).innerHTML = (current_image_index + 1);
                        
                        // Assign it an onclick.
                        update_onclick(i + 1, current_image_index);
                        
                        // If the thumbnail image is the same as the main photo gallery photo, apply the active class
                        if (document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).src == document.getElementById('software_photo_gallery_photo').src) {
							document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).className = 'current_thumbnail_label';
                            document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).className = 'current_thumbnail';
                        // Else, remove the active class
                        } else {
                            document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).className = 'thumbnail_label';
                            document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).className = 'thumbnail';
                        }
                        
                        // Display the thumbnail container
                        document.getElementById('software_photo_gallery_thumbnail_' + (i + 1)).parentNode.style.display = '';
                        document.getElementById('software_photo_gallery_thumbnail_label_' + (i + 1)).parentNode.style.display = '';
                    }
                }
            }
            
            break;
    }
}

function software_photo_gallery_start_slideshow()
{
    // If the slideshow is not currently enabled.
    if (software_photo_gallery_slideshow === false) {
        // Start the slideshow
        window.setTimeout("software_photo_gallery_update_slideshow()", software_photo_gallery_slideshow_interval);
        
        // Switch the font weight
        document.getElementById('software_photo_gallery_slideshow_on').className = 'active';
        document.getElementById('software_photo_gallery_slideshow_off').className = '';
        
        // Set the global variable so we now know that the slideshow is enabled.
        software_photo_gallery_slideshow = true;
        
    }
}

function software_photo_gallery_stop_slideshow()
{
    // Disable the slideshow so the photo will stop changing.
    software_photo_gallery_slideshow = false;
    
    // Switch the font weight.
    document.getElementById('software_photo_gallery_slideshow_on').className = '';
    document.getElementById('software_photo_gallery_slideshow_off').className = 'active';
}

function software_photo_gallery_update_slideshow()
{
    // If the slideshow is enabled.
    if (software_photo_gallery_slideshow === true) {
        // Update the current photo gallery main photo!
        software_photo_gallery_update_photo(software_photo_gallery_current_photo_id + 1);
        
        // And then set up for recursion.
        window.setTimeout("software_photo_gallery_update_slideshow()", software_photo_gallery_slideshow_interval);
    }
}
