/********************************************************
 *                                                      *
 * Fills the list of books related to selected category *
 * Copyright (c), Intelrate, 2008                       *
 *                                                      *
 ********************************************************/

/**
 * Book list form containing all the boks belonging to the given category.
 */
var Books =
{
    componentId : ''
};

/// asynchronous request to the server to search for books
var httpObject = getXmlHttpObject();

/**
 * New category from the list has been selected.
 *
 * @param selectId id of select HTML element
 * @param bookListId id of div to place the list of books into
 */
function categoryChanged(selectId, bookListId)
{
    Books.componentId = bookListId;

    var selectElement = document.getElementById(selectId);
    var categoryUid = selectElement.options[selectElement.selectedIndex].value;
    httpObject.open('GET', '/books/BooksByCategoryHandler.php?value=' + categoryUid);
    httpObject.onreadystatechange = showSelectCategoryStatus;
    httpObject.send(null);
}

/**
 * Handles asynchronous response from the server and fills the list of books.
 * TODO: refactor this to use standard update web form function from common.js
 */
function showSelectCategoryStatus()
{
    if (httpObject.readyState == 4)
    {
        var response = httpObject.responseText;
        document.getElementById(Books.componentId).innerHTML = response;
    }
}

