function formatISBN(isbn)
{
    if (isbn.length == 10)
    {
        return isbn.charAt(0) + '-' + 
               isbn.substr(1, 3) + '-' + 
               isbn.substr(4, 5) + '-' + 
               isbn.charAt(9);
    }
    else if (isbn.length == 13)
    {
        return isbn.substr(0, 3) + '-' + 
               isbn.charAt(3) + '-' + 
               isbn.substr(4, 3) + '-' + 
               isbn.substr(7, 5) + '-' + 
               isbn.charAt(12);
    }
    else return isbn;
}

function parseBook(xmlDoc)
{
    var popup = getPopupWindow();
    var i, node, link, fields, sect, fields;

    sect = createDiv();
    sect.className = 'sect';
    popup.appendChild(sect);

    // Title

    node = createDiv('title');
    node.innerHTML = xmlDoc.getElementsByTagName('title')[0].firstChild.data;
    sect.appendChild(node);

    // Authors

    var authors = xmlDoc.getElementsByTagName('author');
    var n = authors.length;
    var a;
    var role;

    for (var i = 0; i < n; i++)
    {
        role = authors[i].attributes.getNamedItem("role").value;
        node = createDiv();
        node.className = 'author';

        a = createAnchor("javascript:find('" + authors[i].firstChild.data.replace("'", "\'") + "');", authors[i].firstChild.data);
        node.appendChild(a);

        if (role.length > 0)
            node.appendChild(document.createTextNode(" (" + role + ")"));

        sect.appendChild(node);
    }

    // Added

    node = createDiv('added', 'Added ' + formatDate(xmlDoc.getElementsByTagName('added')[0].firstChild.data));
    popup.appendChild(node);

    sect = createDiv();
    sect.className = 'sect';
    popup.appendChild(sect);

    // Pub, format, isbn-10, isbn-13

    if (xmlDoc.getElementsByTagName('pub')[0].firstChild)
    {
        node = createDiv('pub', 'Published by ' + xmlDoc.getElementsByTagName('pub')[0].firstChild.data);
        sect.appendChild(node);
    }

    if (xmlDoc.getElementsByTagName('format')[0].firstChild)
    {
        node = createDiv('format', xmlDoc.getElementsByTagName('format')[0].firstChild.data);
        sect.appendChild(node);
    }

    if (xmlDoc.getElementsByTagName('isbn')[0].firstChild)
    {
        node = createDiv('isbn', 'ISBN-10 ' + formatISBN(xmlDoc.getElementsByTagName('isbn')[0].firstChild.data));
        sect.appendChild(node);
    }

    if (xmlDoc.getElementsByTagName('isbn13')[0].firstChild)
    {
        node = createDiv('isbn', 'ISBN-13 ' + formatISBN(xmlDoc.getElementsByTagName('isbn13')[0].firstChild.data));
        sect.appendChild(node);
    }

    // Comments

    if (xmlDoc.getElementsByTagName('comment')[0].firstChild)
    {
        node = createDiv('isbn', xmlDoc.getElementsByTagName('comment')[0].firstChild.data);
        popup.appendChild(node);
    }

    // Keywords

    if (sect = getKeywordList(xmlDoc))
        popup.appendChild(sect);

    // AMZ link

    if (xmlDoc.getElementsByTagName('isbn')[0].firstChild)
    {
        // init amazon link

        link = document.createElement('a');
        link.href = 'http://www.amazon.com/gp/product/' +
            xmlDoc.getElementsByTagName('isbn')[0].firstChild.data;
        link.target = '_blank';
        link.innerHTML = xmlDoc.getElementsByTagName('title')[0].firstChild.data;

        node = createDiv('link');
        node.appendChild(document.createTextNode('Find '));
        node.appendChild(link);
        node.appendChild(document.createTextNode(' at '));

        popup.appendChild(node);

        link = document.createElement('a');
        link.href = 'http://www.amazon.com/';
        link.target = '_blank';
        link.innerHTML = 'Amazon.com';

        node.appendChild(link);
    }
}

function parsePub(xmlDoc)
{
    var node, div, span;
    var popup = getPopupWindow();

    // title

    node = createDiv('title');
    node.innerHTML = xmlDoc.getElementsByTagName('name')[0].firstChild.data;
    popup.appendChild(node);

    // count

    var titles, count;
    titles = xmlDoc.getElementsByTagName('book');
    count = xmlDoc.getElementsByTagName('count')[0].firstChild.data;

    node = createDiv();
    node.className = 'count_rgn';
    span = createSpan();
    span.className = 'count';
    span.innerHTML = count;
    node.appendChild(span);
    node.appendChild(document.createTextNode(' title(s) in the collection:'));
    popup.appendChild(node);

    node = createDiv('books');
    popup.appendChild(node);

    var i;
    for (i = 0; i < titles.length; i++)
    {
        div = createDiv();
        div.className = 'title_row';
        span = createSpan(null);
        span.className = 'title';
        span.innerHTML = titles[i].firstChild.data;
        div.appendChild(span);

        span = createSpan(null);
        span.className = 'author';
        span.innerHTML = ' ' + titles[i].getAttribute('author');
        div.appendChild(span);

        node.appendChild(div);
    }

    if (count > titles.length)
    {
        node = createDiv();
        node.className = 'count_rgn';
        span = createSpan();
        span.className = 'count';
        span.innerHTML = count - titles.length;
        node.appendChild(document.createTextNode('Plus '));
        var a = document.createElement('a');
        a.href = 'javascript:find(\'' + xmlDoc.getElementsByTagName('name')[0].firstChild.data.replace('\'', '\\\'') + '\');';
        node.appendChild(a);
        a.appendChild(span);
        a.appendChild(document.createTextNode(' other(s)'));

        popup.appendChild(node);
    }
}

function parseAuthor(xmlDoc)
{
    var node, div, span;
    var popup = getPopupWindow();

    // title

    node = createDiv('title');
    node.innerHTML = xmlDoc.getElementsByTagName('name')[0].firstChild.data;
    popup.appendChild(node);

    // count

    var titles, count;
    titles = xmlDoc.getElementsByTagName('book');
    count = xmlDoc.getElementsByTagName('count')[0].firstChild.data;

    node = createDiv();
    node.className = 'count_rgn';
    span = createSpan();
    span.className = 'count';
    span.innerHTML = count;
    node.appendChild(span);
    node.appendChild(document.createTextNode(' title(s) in the collection:'));
    popup.appendChild(node);

    node = createDiv('books');
    popup.appendChild(node);

    var i;
    for (i = 0; i < titles.length; i++)
    {
        div = createDiv();
        div.className = 'title_row';
        span = createSpan(null);
        span.className = 'title';
        span.innerHTML = titles[i].firstChild.data;
        div.appendChild(span);

        node.appendChild(div);
    }

    if (count > titles.length)
    {
        node = createDiv();
        node.className = 'count_rgn';
        span = createSpan();
        span.className = 'count';
        span.innerHTML = count - titles.length;
        node.appendChild(document.createTextNode('Plus '));
        var a = document.createElement('a');
        a.href = 'javascript:findBooks(\'' + xmlDoc.getElementsByTagName('name')[0].firstChild.data.replace('\'', '\\\'') + '\');';
        node.appendChild(a);
        a.appendChild(span);
        a.appendChild(document.createTextNode(' other(s)'));

        popup.appendChild(node);
    }
}

function parseBookData(xmlDoc)
{
    document.getElementsByName('title')[0].value = 
        xmlDoc.getElementsByTagName('title')[0].firstChild.data;
    document.getElementsByName('author')[0].value = 
        xmlDoc.getElementsByTagName('author')[0].firstChild.data;
    document.getElementsByName('pub')[0].value = 
        xmlDoc.getElementsByTagName('pub')[0].firstChild.data;
    document.getElementsByName('format')[0].value = 
        xmlDoc.getElementsByTagName('format')[0].firstChild.data;
    document.getElementsByName('isbn13')[0].value = 
        xmlDoc.getElementsByTagName('isbn13')[0].firstChild.data;
}

function lookup(isbn)
{
    if (!isbn.value.match(/^([0-9]{9}[0-9X]|[0-9]{13})$/))
        { displayError('Invalid ISBN'); return; }

    animGo(isbn.parentNode);
    sendRequest(getModParams('isbn_query', isbn.value));
}

function editTheme(table, xmlDoc)
{
    var cell, c, coll;

    c = new Object();

    // Title

    cell = createFormRow(table, 'Title');
    c['title'] = createTextbox('title');
    cell.appendChild(c['title']);

    // Author

    cell = createFormRow(table, 'Author');
    c['author'] = createTextbox('author');
    cell.appendChild(c['author']);

    // Format

    cell = createFormRow(table, 'Format');
    var formats = createDropList('format');
    formats.appendChild(createOption('Hardcover'));
    formats.appendChild(createOption('Paperback'));
    formats.appendChild(createOption('Boxed set'));
    formats.appendChild(createOption('Other'));
    cell.appendChild(formats);

    // Publisher

    cell = createFormRow(table, 'Publisher');
    c['pub'] = createTextbox('pub');
    cell.appendChild(c['pub']);

    // ISBN

    cell = createFormRow(table, 'ISBN');
    c['isbn'] = createTextbox('isbn');
    c['isbn'].className += ' isbn';
    cell.appendChild(c['isbn']);

    var btn = createButton('Look up');
    btn.onclick = function() { lookup(c['isbn']); };
    cell.appendChild(btn);

    cell = createFormRow(table, 'ISBN-13');
    c['isbn13'] = createTextbox('isbn13');
    c['isbn'].className += ' isbn13';
    cell.appendChild(c['isbn13']);

    // Comment

    cell = createFormRow(table, 'Comment');
    c['comment'] = createTextbox('comment');
    cell.appendChild(c['comment']);

    // Keywords

    cell = createFormRow(table, 'Keywords');
    c['keywords'] = createTextbox('keywords');
    cell.appendChild(c['keywords']);

    // Init fields

    var node, coll;
    for (var i = 0; i < xmlDoc.documentElement.childNodes.length; i++)
    {
        node = xmlDoc.documentElement.childNodes[i];
        if (c[node.tagName] && node.firstChild)
            c[node.tagName].value = node.firstChild.data;
    }

    if ((coll = xmlDoc.getElementsByTagName('format')).length > 0)
        for (i = 0; i < formats.options.length; i++)
            if (formats.options[i].value == coll[0].firstChild.data)
                { formats.selectedIndex = i; break; }

}

addPopupHandler('book', parseBook);
addPopupHandler('publisher', parsePub);
addPopupHandler('author', parseAuthor);

addHandler('book_data', parseBookData);


