Subversion Repositories munaweb

Rev

Rev 6 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Relist on eBay</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="icon" href="favicon.ico" type="image/x-icon">

    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/tablesorter.theme.blue.css">
    <script src="js/XMLWriter.js"></script>
    <script src="js/vkbeautify.js"></script>
    <script src="js/lodash.min.js"></script>
    <script src="js/XMLparse.js"></script>
    <script src="js/muna-tools.js"></script>
    <script src="js/jquery.js"></script>
    <script src="js/jquery.tablesorter.min.js"></script>
    <script src="js/jquery.tablesorter.widgets.js"></script>
    <script src="js/jquery.parser-input-select.js"></script>
</head>

<body onload="return initConfig();">
    <div class="w3-main">
        <div class="w3-container w3-padding w3-margin w3-card-4">
            <div class="w3-container w3-gray" style="height:100px">
                <img class="w3-image" src="images/MUNA%20-%20Logo%20100x100.png" alt="MUNA Trading Logo" style="height:100px">
                <p id="connected" class="w3-xxlarge w3-right">Relist on eBay
                    <input id="login" type="button" class="w3-btn w3-large w3-green w3-margin w3-round-large w3-ripple w3-right w3-hide" onclick="eBayLogin();" value="Login" />
                </p>
            </div>

            <div class="w3-row">
                <div>
                    <form id="searchForm" class="w3-container w3-light-grey w3-padding w3-small" onsubmit="return getListings();">
                        <div id="createForm" class="w3-container w3-card-2 w3-cell w3-padding">
                            <input id="includeExported" name="includeExported" class="w3-check" style="height:1em;vertical-align:super;" type="checkbox">
                            <label for="includeExported">Include Exported Listing</label>
                            <p>Listing Type:</p>
                            <input type="radio" name="ltype" id="ltFixed" checked /> Fixed Price<br>
                            <input type="radio" name="ltype" id="ltAuction" /> Auction<br>
                            <input type="radio" name="ltype" id="ltBoth" /> Both<br>
                            <input id="startButton" type="button" class="w3-btn w3-large w3-red w3-margin w3-round-large w3-ripple" onclick="getListings();" value="Start" />
                        </div>
                        <div id="relistForm" class="w3-container w3-card-2 w3-cell w3-padding w3-hide">
                            <input id="relistButton" type="button" class="w3-btn w3-large w3-black w3-margin w3-round-large w3-ripple" onclick="putListings();" value="Relist" />
                        </div>
                    </form>
                </div>
                <div id="progressBarDiv" class="w3-container w3-padding w3-margin w3-card-4 w3-hide">
                    <h2 id="progressBarHeader"></h2>
                    <div class="w3-light-grey">
                        <div id="progressBar" class="w3-container w3-green w3-center" style="width:0%">0%</div>
                    </div>
                </div>
                <div id="results" class="w3-container w3-padding w3-card-4 w3-hide"></div>
                <div id="logging" class="w3-container w3-padding"></div>
            </div>
        </div>

        <footer class="w3-container w3-center w3-border-top w3-margin">
            Copyright &#169; 2018 MUNA Trading. All rights reserved.
        </footer>

    </div>

    <script>
// Globals
var tableName = "unsoldTable";
var maxPagesToProcess = 0;
var pagesToProcess = 0;
var pagesProcessed = 0;
var listingsToFill = 0;
var maxListingsToFill = 0;
var listingsFilled = 0;
var listingsToRelist = 0;
var maxListingsToRelist = 0;
var listingsRelisted = 0;
var html = [];

// Initialize Configuration Variables
function initConfig() {
    eBayAuthToken = readCookie();
    if (eBayAuthToken.length > 0) {
        connected();
    }

    if (eBayAuthTokenFlag === false) {
        var x = document.getElementById("login");
        if (x.className.indexOf("w3-show") == -1) {
            x.className += " w3-show";
        }
    }
}

function connected() {
    var x;

    eBayAuthTokenFlag = true;
    document.getElementById("connected").innerHTML += " (Connected)";

    x = document.getElementById("startButton");
    x.className = x.className.replace(" w3-red", " w3-black");

    x = document.getElementById("login");
    x.className = x.className.replace(" w3-show", "");

    x = document.getElementById("results");
    x.innerHTML = "";
    x.className = x.className.replace(" w3-show", "");
    x.className = x.className.replace(" process-errors", "");
}

function requireNewLogin() {
    var x = document.getElementById("startButton");
    x.className = x.className.replace(" w3-black", " w3-red ");
}

function getListings() {
    var x;

    if (eBayAuthTokenFlag === false) {
        return;
    }

    document.getElementById("logging").innerHTML = '';

    x = document.getElementById("results");
    if (x.className.indexOf("w3-show") == -1) {
        x.className += " w3-show";
    }
    x.className = x.className.replace("process-errors", "");
    x.innerHTML = '<p><strong>Retrieving Unsold Listings...</strong></p>';

    initProgressBar('Get Unsold and Not Relisted Items (1/2)');

    tableStart();
    maxPagesToProcess = 1;
    pagesToProcess = 1;
    pagesProcessed = 0;
    eBaySearch(1);

    function checkpagesToProcess() {
        if (pagesToProcess > 0) {
            window.setTimeout(checkpagesToProcess, 100); // wait 100 milliseconds
        } else {
            tableEnd();
            endProgressBar();
            // fillListings(); is now in tableEnd()
        }
    }

    checkpagesToProcess();
}

function tableStart() {
    document.getElementById("logging").innerHTML = '';
    html = [];

    html.push('<h3>Unsold and Not Relisted</h3>');
    html.push('<p>Showing <span id="filtered-rows">0</span> of <span id="total-rows">0</span> / <span id="selected-rows">0</span> selected.</p>');
    html.push('<div class="w3-responsive">');
    html.push('<table id="' + tableName + '" class="tablesorter">');
    html.push('<thead>');
    html.push('<tr>');

    html.push(tableHeaderCheckbox(tableName));
    html.push(tableHeader('Image'));
    html.push(tableHeader('Title'));
    html.push(tableHeader('Item Id'));
    html.push(tableHeader('SKU'));
    html.push(tableHeader('Category'));
    html.push(tableHeader('Format'));
    html.push(tableHeader('Price'));
    html.push(tableHeader('Quantity'));
    html.push(tableHeader('Views'));
    html.push(tableHeader('Watchers'));
    html.push(tableHeader('End Date'));
    html.push(tableHeader('Note'));

    html.push('</tr>');
    html.push('</thead>');
    html.push('<tbody>');
}

function tableEntry(JsonObj) {
    var i;
    var image;
    var title;
    var itemId;
    var sku;
    var format;
    var price;
    var quantity;
    var views;
    var watchers;
    var endDate;
    var privateNote;
    var ebayNote;
    var relisted;

    for (i = 0; i < JsonObj.length; i++) {
        image = getJsonValue(JsonObj[i].PictureDetails).replace('http:', 'https:');
        title = getJsonValue(JsonObj[i].Title);
        itemId = getJsonValue(JsonObj[i].ItemID);
        sku = getJsonValue(JsonObj[i].SKU);
        format = getJsonValue(JsonObj[i].ListingType);
        if (format == 'Chinese') {
            format = 'Auction';
            price = getJsonValue(JsonObj[i].StartPrice.text);
        } else {
            format = 'Fixed Price';
            price = getJsonValue(JsonObj[i].BuyItNowPrice.text);
        }
        quantity = getJsonValue(JsonObj[i].QuantityAvailable);
        views = '';
        watchers = getJsonValue(JsonObj[i].WatchCount);
        watchers = (watchers.length === 0 ? "0" : watchers);
        endDate = getJsonValue(JsonObj[i].ListingDetails).substr(0, 10);
        privateNote = getJsonValue(JsonObj[i].PrivateNotes);
        ebayNote = getJsonValue(JsonObj[i].eBayNotes);
        relisted = getJsonValue(JsonObj[i].Relisted);

        if ((privateNote.startsWith("Exported to ") && !document.getElementById('includeExported').checked) || relisted == 'true' || privateNote.startsWith("Sold via ") || privateNote.startsWith("Relisted As New Item")) {
            continue;
        }

        if (format == 'Auction' && document.getElementById('ltFixed').checked) {
            continue;
        } else if (format == 'Fixed Price' && document.getElementById('ltAuction').checked) {
            continue;
        }

        html.push('<tr>');

        html.push(tableCellCheckbox());
        html.push(tableCell('<img class="w3-image" src="' + image + '" alt="Gallery Image" style="max-height:100px;max-width:100px;">'));
        html.push(tableCell(title));
        html.push(tableCell(itemId));
        html.push(tableCell(sku));
        html.push(tableCellLabel('Category' + itemId));
        html.push(tableCell(format));
        html.push(tableCell('$' + price));
        html.push(tableCell(quantity));
        html.push(tableCellLabel('Views' + itemId));
        html.push(tableCell(watchers));
        html.push(tableCell(endDate));
        html.push(tableCell(privateNote + ' ' + ebayNote));

        html.push('</tr>');
    }
}

function tableEnd() {
    html.push('</tbody>');
    html.push('</table>');
    html.push('</div>');

    document.getElementById("logging").innerHTML = html.join('');

    $(function() {
        $("#" + tableName).on('tablesorter-initialized', function() {

            // class name to add on tr when checkbox is checked
            var highlightClass = 'checked',
                // resort the table after the checkbox is modified?
                resort = true,
                $table = $(this),
                c = this.config,
                wo = c && c.widgetOptions,
                // include sticky header checkbox; if installed
                $sticky = c && wo.$sticky || '',
                doChecky = function(c, col) {
                    $table
                        .children('tbody')
                        .children('tr:visible')
                        .children('td:nth-child( ' + (parseInt(col, 10) + 1) + ' )')
                        .find('input[type=checkbox]')
                        .each(function() {
                            this.checked = c;
                            $(this).trigger('change');
                        });
                };

            $table
                .children('tbody')
                .on('change', 'input[type=checkbox]', function() {
                    // ignore change if updating all rows
                    if ($table[0].ignoreChange) {
                        return;
                    }
                    var $this = $(this);
                    $this.closest('tr').toggleClass(highlightClass, this.checked);
                    $this.trigger('updateCell', [$this.closest('td'), resort]);

                    // handle header
                    var rowCount = $('#' + tableName + ' tbody tr').length + $('#' + tableName + ' tbody tr:hidden').length;
                    var checkedCount = $('#' + tableName + ' tbody .checked').length;
                    var ua = window.navigator.userAgent;
                    if (checkedCount === 0) {
                        $table.add($sticky).find('thead input[type=checkbox]').prop('checked', false);
                        $table.add($sticky).find('thead input[type=checkbox]').prop('indeterminate', false);
                    } else if (checkedCount === rowCount) {
                        $table.add($sticky).find('thead input[type=checkbox]').prop('checked', true);
                        $table.add($sticky).find('thead input[type=checkbox]').prop('indeterminate', false);
                    } else {
                        $table.add($sticky).find('thead input[type=checkbox]').prop('checked', !(ua.indexOf('Trident/') > -1 || ua.indexOf('Edge/') > -1));
                        $table.add($sticky).find('thead input[type=checkbox]').prop('indeterminate', true);
                    }
                    $('#selected-rows').html(checkedCount);
                })
                .end()
                .add($sticky)
                .find('thead input[type=checkbox]')
                // Click on checkbox in table header to toggle all inputs
                .on('change', function() {
                    // prevent updateCell for every cell
                    $table[0].ignoreChange = true;
                    var c = this.checked,
                        col = $(this).closest('th').attr('data-column');
                    doChecky(c, col);
                    // update main & sticky header

                    $table.children('tbody').children('tr:visible').toggleClass(highlightClass, c);
                    // update all at once
                    $table[0].ignoreChange = false;
                    $table.trigger('update', [resort]);

                    // handle header
                    var rowCount = $('#' + tableName + ' tbody tr').length + $('#' + tableName + ' tbody tr:hidden').length;
                    var checkedCount = $('#' + tableName + ' tbody .checked').length;
                    var ua = window.navigator.userAgent;
                    if (checkedCount === 0) {
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('checked', false);
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('indeterminate', false);
                    } else if (checkedCount === rowCount) {
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('checked', true);
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('indeterminate', false);
                    } else {
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('checked', !(ua.indexOf('Trident/') > -1 || ua.indexOf('Edge/') > -1));
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('indeterminate', true);
                    }
                    $('#selected-rows').html(checkedCount);
                })
                .on('mouseup', function() {
                    return false;
                });

        });

        $("#" + tableName).tablesorter({
            theme: "blue",
            widgets: ["zebra", "stickyHeaders", "filter"],
            headers: {
                0: {
                    sorter: "checkbox"
                },
                1: {
                    sorter: false,
                    filter: false
                }
            },
            initialized: function() {
                fillListings();
            }
        });

        $("#" + tableName).bind('filterInit filterEnd', function(event, data) {
            $('#filtered-rows').html(data.filteredRows);
            $('#total-rows').html(data.totalRows);
        });
    });
}

function addNote(itemId) {
    var i;
    var xml;

    var xw = new XMLWriter('UTF-8', '1.0');
    var xhr = new XMLHttpRequest();

    if (!createAddXMLNote(xw, xhr, 'SetUserNotes', itemId)) {
        return;
    }

    xml = xw.flush();
    xw.close();
    //window.alert(vkbeautify.xml(xml));

    xhr.onload = function() {
        var obj = XMLparse(xhr.responseXML);
        //window.alert(JSON.stringify(obj));
        //window.alert(vkbeautify.xml(xhr.responseText));

        var returnCode = obj.Ack;

        var x = document.getElementById("results");

        if (returnCode == 'Success' || (returnCode == 'Warning' && obj.Errors.ErrorCode == '21917108')) {
            if (returnCode == 'Warning') {
                x.innerHTML += '<p>' + obj.CorrelationID + ': <strong>' + returnCode + '</strong></p>';
                x.innerHTML += "<p>" + obj.Errors.SeverityCode + " (" + obj.Errors.ErrorCode + "): " + escapeHtml(obj.Errors.LongMessage) + "</p>";
            }
        } else {
            x.className += " process-errors";
            x.innerHTML += '<p class="w3-red">' + obj.CorrelationID + ': <strong>' + returnCode + ':</strong></p>';
            var errors = obj.Errors;
            x.innerHTML += "<p>";
            if (errors.length > 0) {
                for (i = 0; i < errors.length; i++) {
                    x.innerHTML += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
                }
            } else {
                x.innerHTML += obj.Errors.SeverityCode + " (" + obj.Errors.ErrorCode + "): " + escapeHtml(obj.Errors.LongMessage) + "<br/>";
            }
            x.innerHTML += "</p>";
        }

        if (obj.Message) {
            x.innerHTML += obj.Message;
        }

        --listingsToRelist;
        ++listingsRelisted;
        updateProgressBar(maxListingsToRelist, listingsRelisted);
    };

    xhr.send(xml);
}

function createAddXMLNote(xw, xhr, callname, itemId) {
    var today = new Date();
    var todayYYYYMMDD = today.toISOString().slice(0, 10);

    xw.writeStartDocument();
    xw.writeStartElement(callname + "Request");
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');

    xw.writeStartElement('RequesterCredentials');
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
    xw.writeEndElement(); /* RequesterCredentials */

    xw.writeElementString('ItemID', itemId);
    xw.writeElementString('MessageID', itemId);
    xw.writeElementString('Action', 'AddOrUpdate');
    xw.writeElementString('NoteText', 'Relisted As New Item on ' + todayYYYYMMDD);

    xw.writeElementString('ErrorLanguage', 'en_US');
    xw.writeElementString('Version', configeBayTradingVersion);
    xw.writeElementString('WarningLevel', configWarningLevel);

    xw.writeEndElement(); /* xmlrequest */
    xw.writeEndDocument();

    xhr.open('POST', configProxyUrl, true);
    xhr.setRequestHeader('Content-Type', 'text/xml');
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);

    return true;
}

function eBaySearch(pageNo) {
    var i;
    var xml;

    if (eBayAuthTokenFlag === false) {
        return;
    }

    var xw = new XMLWriter('UTF-8', '1.0');
    var xhr = new XMLHttpRequest();

    if (!createAddXMLSearch(xw, xhr, 'GetMyeBaySelling', pageNo)) {
        return;
    }

    xml = xw.flush();
    xw.close();
    //window.alert(vkbeautify.xml(xml));

    xhr.onload = function() {
        var obj = XMLparse(xhr.responseXML);
        //window.alert(JSON.stringify(obj));
        //window.alert(vkbeautify.xml(xhr.responseText));

        var returnCode = obj.Ack;

        var x = document.getElementById("results");

        if (returnCode == 'Success') {
            tableEntry(obj.UnsoldList.ItemArray);

            if (Number(obj.UnsoldList.PaginationResult.TotalNumberOfPages) > pageNo) {
                maxPagesToProcess = Number(obj.UnsoldList.PaginationResult.TotalNumberOfPages);
                ++pagesToProcess;
                eBaySearch(pageNo + 1);
            }

            updateProgressBar(maxPagesToProcess, pagesProcessed);
        } else {
            x.className += " process-errors";
            x.innerHTML += '<p class="w3-red">' + obj.CorrelationID + ': <strong>' + returnCode + ':</strong></p>';
            var errors = obj.Errors;
            x.innerHTML += "<p>";
            if (errors.length > 0) {
                for (i = 0; i < errors.length; i++) {
                    x.innerHTML += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
                }
            } else {
                x.innerHTML += obj.Errors.SeverityCode + " (" + obj.Errors.ErrorCode + "): " + escapeHtml(obj.Errors.LongMessage) + "<br/>";
            }
            x.innerHTML += "</p>";
        }

        --pagesToProcess;
        ++pagesProcessed;

        updateProgressBar(maxPagesToProcess, pagesProcessed);
    };

    xhr.send(xml);
}

function createAddXMLSearch(xw, xhr, callname, pageNo) {

    xw.writeStartDocument();
    xw.writeStartElement(callname + "Request");
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');

    xw.writeStartElement('RequesterCredentials');
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
    xw.writeEndElement(); /* RequesterCredentials */

    xw.writeElementString('DetailLevel', 'ReturnAll');
    xw.writeStartElement('ActiveList');
    xw.writeElementString('Include', 'false');
    xw.writeEndElement(); /* ActiveList */
    xw.writeStartElement('DeletedFromSoldList');
    xw.writeElementString('Include', 'false');
    xw.writeEndElement(); /* DeletedFromSoldList */
    xw.writeStartElement('DeletedFromUnsoldList');
    xw.writeElementString('Include', 'false');
    xw.writeEndElement(); /* DeletedFromUnsoldList */
    xw.writeStartElement('ScheduledList');
    xw.writeElementString('Include', 'false');
    xw.writeEndElement(); /* ScheduledList */
    xw.writeStartElement('SellingSummary');
    xw.writeElementString('Include', 'false');
    xw.writeEndElement(); /* SellingSummary */
    xw.writeStartElement('SoldList');
    xw.writeElementString('Include', 'false');
    xw.writeEndElement(); /* SoldList */

    xw.writeStartElement('UnsoldList');
    xw.writeElementString('Include', 'true');
    xw.writeElementString('IncludeNotes', 'true');
    xw.writeStartElement('Pagination');
    xw.writeElementString('EntriesPerPage', '200');
    xw.writeElementString('PageNumber', '' + pageNo);
    xw.writeEndElement(); /* Pagination*/
    xw.writeEndElement(); /* UnsoldList> */
    xw.writeElementString('OutputSelector', 'BuyItNowPrice');
    xw.writeElementString('OutputSelector', 'eBayNotes');
    xw.writeElementString('OutputSelector', 'ItemID');
    xw.writeElementString('OutputSelector', 'EndTime');
    xw.writeElementString('OutputSelector', 'ListingType');
    xw.writeElementString('OutputSelector', 'PrivateNotes');
    xw.writeElementString('OutputSelector', 'QuantityAvailable');
    xw.writeElementString('OutputSelector', 'PictureDetails');
    xw.writeElementString('OutputSelector', 'Relisted');
    xw.writeElementString('OutputSelector', 'SKU');
    xw.writeElementString('OutputSelector', 'Title');
    xw.writeElementString('OutputSelector', 'StartPrice');
    xw.writeElementString('OutputSelector', 'WatchCount');
    xw.writeElementString('OutputSelector', 'PaginationResult');

    xw.writeElementString('ErrorLanguage', 'en_US');
    xw.writeElementString('Version', configeBayTradingVersion);
    xw.writeElementString('WarningLevel', configWarningLevel);

    xw.writeEndElement(); /* xmlrequest */
    xw.writeEndDocument();

    xhr.open('POST', configProxyUrl, true);
    xhr.setRequestHeader('Content-Type', 'text/xml');
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);

    return true;
}

function fillListings() {
    var i;
    var x = document.getElementById(tableName);

    listingsToFill = 0;
    maxListingsToFill = 0;
    listingsFilled = 0;

    initProgressBar('Fill Listing Information (2/2)');

    for (i = 2; i < x.rows.length; i++) {
        ++listingsToFill;
        ++maxListingsToFill;
        fillListing(x.rows[i].cells[3].innerHTML);
    }

    function checkListingsToFill() {
        if (listingsToFill > 0) {
            window.setTimeout(checkListingsToFill, 100); // wait 100 milliseconds
        } else {
            $("#" + tableName).trigger("update");

            endProgressBar();

            document.getElementById("createForm").className += " w3-hide";
            document.getElementById("relistForm").className += " w3-show";

            x = document.getElementById("results");
            x.innerHTML += '<p><strong>Report Finished!</strong></p>';
            if (!x.className.includes("process-errors")) {
                setTimeout(function() {
                    x.className = x.className.replace(" w3-show", "");
                }, 3000);
            }
        }
    }

    checkListingsToFill();
}

function fillListing(itemId) {
    var i;
    var xml;

    var xw = new XMLWriter('UTF-8', '1.0');
    var xhr = new XMLHttpRequest();

    if (!createFillXMLSearch(xw, xhr, 'GetItem', itemId)) {
        return;
    }

    xml = xw.flush();
    xw.close();
    //window.alert(vkbeautify.xml(xml));

    xhr.onload = function() {
        var obj = XMLparse(xhr.responseXML);
        //window.alert(JSON.stringify(obj));
        //window.alert(vkbeautify.xml(xhr.responseText));

        var returnCode = obj.Ack;

        var x = document.getElementById("results");

        if (returnCode == 'Success') {
            var itemId = getJsonValue(obj.Item.ItemID);
            document.getElementById("Views" + itemId).innerHTML = getJsonValue(obj.Item.HitCount);
            document.getElementById("Category" + itemId).innerHTML = getJsonValue(obj.Item.PrimaryCategory);
        } else {
            x.className += " process-errors";
            x.innerHTML += '<p class="w3-red">' + obj.CorrelationID + ': <strong>' + returnCode + ':</strong></p>';

            var errors = obj.Errors;
            x.innerHTML += "<p>";
            if (errors.length > 0) {
                for (i = 0; i < errors.length; i++) {
                    x.innerHTML += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
                }
            } else {
                x.innerHTML += obj.Errors.SeverityCode + " (" + obj.Errors.ErrorCode + "): " + escapeHtml(obj.Errors.LongMessage) + "<br/>";
            }
            x.innerHTML += "</p>";
        }

        --listingsToFill;
        ++listingsFilled;
        updateProgressBar(maxListingsToFill, listingsFilled);
    };

    xhr.send(xml);
}

function createFillXMLSearch(xw, xhr, callname, itemId) {
    xw.writeStartDocument();
    xw.writeStartElement(callname + "Request");
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');

    xw.writeStartElement('RequesterCredentials');
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
    xw.writeEndElement(); /* RequesterCredentials */

    xw.writeElementString('ItemID', itemId);
    xw.writeElementString('MessageID', itemId);
    xw.writeElementString('OutputSelector', 'ItemID');
    xw.writeElementString('OutputSelector', 'HitCount');
    xw.writeElementString('OutputSelector', 'CategoryName');

    xw.writeElementString('ErrorLanguage', 'en_US');
    xw.writeElementString('Version', configeBayTradingVersion);
    xw.writeElementString('WarningLevel', configWarningLevel);

    xw.writeEndElement(); /* xmlrequest */
    xw.writeEndDocument();

    xhr.open('POST', configProxyUrl, true);
    xhr.setRequestHeader('Content-Type', 'text/xml');
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);

    return true;
}

function putListings() {
    var table = document.getElementById(tableName);

    listingsToRelist = 0;
    maxListingsToRelist = 0;
    listingsRelisted = 0;

    initProgressBar("Relist selected Items");

    var x = document.getElementById("results");
    x.innerHTML = '';
    x.className = x.className.replace("process-errors", "");
    if (x.className.indexOf("w3-show") == -1) {
        x.className += " w3-show";
    }

    for (var i = 2; i < table.rows.length; i++) {
        if (table.rows[i].cells[0].children[0].checked) {
            ++listingsToRelist;
            ++maxListingsToRelist;

            if (Number(table.rows[i].cells[10].innerHTML) > 0 || table.rows[i].cells[12].innerHTML.startsWith("Exported to ")) { // watchers or previously exported
                relistItem(table.rows[i].cells[3].innerHTML, (table.rows[i].cells[3].innerHTML == 'Auction'));
            } else {
                handleAddItem(table.rows[i].cells[3].innerHTML);
            }

            table.deleteRow(i--);
        }
    }

    function checkListingsToRelist() {
        if (listingsToRelist > 0) {
            window.setTimeout(checkListingsToRelist, 100); // wait 100 milliseconds
        } else {
            $("#" + tableName).trigger("update");
            $('#selected-rows').html('0');
            $("#" + tableName).find('thead input[type=checkbox]').prop('checked', false);
            $("#" + tableName).find('thead input[type=checkbox]').prop('indeterminate', false);

            endProgressBar();

            x = document.getElementById("results");
            x.innerHTML += '<p><strong>Relist Finished!</strong></p>';
            if (!x.className.includes("process-errors")) {
                setTimeout(function() {
                    x.className = x.className.replace(" w3-show", "");
                }, 3000);
            }
        }
    }

    checkListingsToRelist();
}

function relistItem(itemId, auctionFlag) {
    var i;
    var xml;

    var xw = new XMLWriter('UTF-8', '1.0');
    var xhr = new XMLHttpRequest();

    if (!createRelistXML(xw, xhr, (auctionFlag ? 'RelistItem' : "RelistFixedPriceItem"), itemId)) {
        return;
    }

    xml = xw.flush();
    xw.close();
    //window.alert(vkbeautify.xml(xml));

    xhr.onload = function() {
        var obj = XMLparse(xhr.responseXML);
        //window.alert(JSON.stringify(obj));
        //window.alert(vkbeautify.xml(xhr.responseText));

        var returnCode = obj.Ack;

        var x = document.getElementById("results");

        if (returnCode != 'Success') {
            x.className += " process-errors";
            x.innerHTML += '<p class="w3-red">' + obj.CorrelationID + ': <strong>' + returnCode + ':</strong></p>';
            var errors = obj.Errors;
            x.innerHTML += "<p>";
            if (errors.length > 0) {
                for (i = 0; i < errors.length; i++) {
                    x.innerHTML += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
                }
            } else {
                x.innerHTML += obj.Errors.SeverityCode + " (" + obj.Errors.ErrorCode + "): " + escapeHtml(obj.Errors.LongMessage) + "<br/>";
            }
            x.innerHTML += "</p>";
        }

        --listingsToRelist;
        ++listingsRelisted;
        updateProgressBar(maxListingsToRelist, listingsRelisted);
    };

    xhr.send(xml);
}

function createRelistXML(xw, xhr, callname, itemId) {
    xw.writeStartDocument();
    xw.writeStartElement(callname + "Request");
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');

    xw.writeStartElement('RequesterCredentials');
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
    xw.writeEndElement(); /* RequesterCredentials */

    xw.writeStartElement('Item');
    xw.writeElementString('ItemID', itemId);
    xw.writeEndElement(); /* Item */

    xw.writeElementString('ErrorLanguage', 'en_US');
    xw.writeElementString('Version', configeBayTradingVersion);
    xw.writeElementString('WarningLevel', configWarningLevel);
    xw.writeElementString('MessageID', itemId);

    xw.writeEndElement(); /* xmlrequest */
    xw.writeEndDocument();

    xhr.open('POST', configProxyUrl, true);
    xhr.setRequestHeader('Content-Type', 'text/xml');
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);

    return true;
}

function handleAddItem(itemId) {
    var i;
    var xml;

    var xw = new XMLWriter('UTF-8', '1.0');
    var xhr = new XMLHttpRequest();

    if (!createGetItemAllXML(xw, xhr, "GetItem", itemId)) {
        return;
    }

    xml = xw.flush();
    xw.close();
    //window.alert(vkbeautify.xml(xml));

    xhr.onload = function() {
        var obj = XMLparse(xhr.responseXML);
        //window.alert(JSON.stringify(obj));
        //window.alert(vkbeautify.xml(xhr.responseText));

        var returnCode = obj.Ack;

        var x = document.getElementById("results");

        if (returnCode == 'Success') {
            var st = xhr.responseText.indexOf('<Item>') + 6;
            var len = xhr.responseText.indexOf('</Item>') - st;
            addItem(itemId, xhr.responseText.substr(st, len));
        } else {
            x.className += " process-errors";
            x.innerHTML += '<p class="w3-red">' + obj.CorrelationID + ': <strong>' + returnCode + ':</strong></p>';

            var errors = obj.Errors;
            x.innerHTML += "<p>";
            if (errors.length > 0) {
                for (i = 0; i < errors.length; i++) {
                    x.innerHTML += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
                }
            } else {
                x.innerHTML += obj.Errors.SeverityCode + " (" + obj.Errors.ErrorCode + "): " + escapeHtml(obj.Errors.LongMessage) + "<br/>";
            }
            x.innerHTML += "</p>";

            --listingsToRelist;
            ++listingsRelisted;
            updateProgressBar(maxListingsToRelist, listingsRelisted);
        }
    };

    xhr.send(xml);
}

function createGetItemAllXML(xw, xhr, callname, itemId) {
    xw.writeStartDocument();
    xw.writeStartElement(callname + "Request");
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');

    xw.writeStartElement('RequesterCredentials');
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
    xw.writeEndElement(); /* RequesterCredentials */

    xw.writeElementString('ItemID', itemId);
    xw.writeElementString('MessageID', itemId);
    xw.writeElementString('DetailLevel', 'ReturnAll');
    xw.writeElementString('IncludeItemSpecifics', 'true');
    xw.writeElementString('IncludeTaxTable', 'true');
    xw.writeElementString('OutputSelector', 'AutoPay');
    xw.writeElementString('OutputSelector', 'BuyItNowPrice');
    xw.writeElementString('OutputSelector', 'BuyerResponsibleForShipping');
    xw.writeElementString('OutputSelector', 'ConditionDescription');
    xw.writeElementString('OutputSelector', 'ConditionDisplayName');
    xw.writeElementString('OutputSelector', 'ConditionID');
    xw.writeElementString('OutputSelector', 'Country');
    xw.writeElementString('OutputSelector', 'Currency');
    xw.writeElementString('OutputSelector', 'Description');
    xw.writeElementString('OutputSelector', 'DispatchTimeMax');
    xw.writeElementString('OutputSelector', 'HitCounter');
    xw.writeElementString('OutputSelector', 'ItemSpecifics');
    xw.writeElementString('OutputSelector', 'ListingDetails');
    xw.writeElementString('OutputSelector', 'ListingDuration');
    xw.writeElementString('OutputSelector', 'ListingType');
    xw.writeElementString('OutputSelector', 'Location');
    xw.writeElementString('OutputSelector', 'PayPalEmailAddress');
    xw.writeElementString('OutputSelector', 'PaymentMethods');
    xw.writeElementString('OutputSelector', 'PictureDetails');
    xw.writeElementString('OutputSelector', 'PostalCode');
    xw.writeElementString('OutputSelector', 'PrimaryCategory');
    xw.writeElementString('OutputSelector', 'ProductListingDetails');
    xw.writeElementString('OutputSelector', 'Quantity');
    xw.writeElementString('OutputSelector', 'ReservePrice');
    xw.writeElementString('OutputSelector', 'ReturnPolicy');
    xw.writeElementString('OutputSelector', 'SellerProfiles');
    xw.writeElementString('OutputSelector', 'SKU');
    xw.writeElementString('OutputSelector', 'ShippingDetails');
    xw.writeElementString('OutputSelector', 'ShippingPackageDetails');
    xw.writeElementString('OutputSelector', 'Site');
    xw.writeElementString('OutputSelector', 'StartPrice');
    xw.writeElementString('OutputSelector', 'Storefront');
    xw.writeElementString('OutputSelector', 'Title');
    xw.writeElementString('OutputSelector', 'UseTaxTable');
    xw.writeElementString('ErrorLanguage', 'en_US');
    xw.writeElementString('Version', configeBayTradingVersion);
    xw.writeElementString('WarningLevel', configWarningLevel);

    xw.writeEndElement(); /* xmlrequest */
    xw.writeEndDocument();

    xhr.open('POST', configProxyUrl, true);
    xhr.setRequestHeader('Content-Type', 'text/xml');
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);

    return true;
}

function addItem(itemId, itemXml) {
    var i;

    var xhr = new XMLHttpRequest();
    var xml = createAddItemXML(xhr, "AddItem", itemId, itemXml);

    xhr.onload = function() {
        var obj = XMLparse(xhr.responseXML);
        //window.alert(JSON.stringify(obj));
        //window.alert(vkbeautify.xml(xhr.responseText));

        var returnCode = obj.Ack;
        var newItemId = obj.ItemID;
        var correlationID = obj.CorrelationID;

        var x = document.getElementById("results");

        if (returnCode == 'Success' || returnCode == 'Warning') {
            addNote(itemId);
            correlationID = newItemId;
        }

        if (returnCode != 'Success') {
            x.className += " process-errors";
            x.innerHTML += '<p class="w3-red">' + correlationID + ': <strong>' + returnCode + ':</strong></p>';
            var errors = obj.Errors;
            x.innerHTML += "<p>";
            if (errors.length > 0) {
                for (i = 0; i < errors.length; i++) {
                    x.innerHTML += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
                }
            } else {
                x.innerHTML += obj.Errors.SeverityCode + " (" + obj.Errors.ErrorCode + "): " + escapeHtml(obj.Errors.LongMessage) + "<br/>";
            }
            x.innerHTML += "</p>";
            --listingsToRelist;
            ++listingsRelisted;
            updateProgressBar(maxListingsToRelist, listingsRelisted);
        }
    };

    xhr.send(xml);

}

function createAddItemXML(xhr, callname, itemId, itemXml) {
    var n1, n2;

    if (itemXml.contains('<ShippingType>Flat</ShippingType>')) {
        n1 = itemXml.indexOf('<CalculatedShippingRate>');
        if (n1 > 0) {
            n2 = itemXml.indexOf('</CalculatedShippingRate>') + 25;
        }
        str = itemXml.substr(0, n1) + itemXml.substr(n2);
        itemXml = str;
    }

    if (itemXml.contains('<ShippingPackage>None</ShippingPackage>')) {
        n1 = itemXml.indexOf('<ShippingPackageDetails>');
        if (n1 > 0) {
            n2 = itemXml.indexOf('</ShippingPackageDetails>') + 25;
        }
        str = itemXml.substr(0, n1) + itemXml.substr(n2);
        itemXml = str;
    }

    var xml = '<?xml version="1.0" encoding="utf-8"?>';
    xml += '<' + callname + 'Request xmlns="urn:ebay:apis:eBLBaseComponents">';
    xml += '<RequesterCredentials><eBayAuthToken>' + eBayAuthToken + '</eBayAuthToken></RequesterCredentials>';
    xml += '<ErrorLanguage>en_US</ErrorLanguage>';
    xml += '<WarningLevel>' + configWarningLevel + '</WarningLevel>';
    xml += '<MessageID>' + itemId + '</MessageID>';
    xml += '<Item>';
    xml += itemXml;
    xml += '<UseTaxTable>true</UseTaxTable>';
    xml += '</Item>';
    xml += '</' + callname + 'Request>';

    xhr.open('POST', configProxyUrl, true);
    xhr.setRequestHeader('Content-Type', 'text/xml');
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);

    return xml;
}

    </script>

</body>
</html>