Subversion Repositories munaweb

Rev

Rev 54 | Rev 68 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
<!DOCTYPE html>
2
<html lang="en">
3
 
4
<head>
5
    <title>Export eBay Listings to Shopify</title>
6
    <meta charset="UTF-8">
7
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
9
    <link rel="icon" href="favicon.ico" type="image/x-icon">
10
 
30 - 11
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
12
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
13
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
14
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
15
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
16
    <script src="js/XMLWriter.js"></script>
17
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
18
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
19
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>
2 - 20
    <link rel="stylesheet" href="css/tablesorter.theme.blue.css">
21
    <script src="js/jquery.tablesorter.min.js"></script>
22
    <script src="js/jquery.tablesorter.widgets.js"></script>
23
    <script src="js/jquery.parser-input-select.js"></script>
30 - 24
    <link rel="stylesheet" href="css/style.css">
25
    <script src="js/muna-tools.js"></script>
32 - 26
    <script src="js/stopcock.js"></script>
2 - 27
</head>
28
 
29
<body onload="return initConfig();">
30 - 30
    <div>
31
        <div">
32
            <div class="container-fluid bg-secondary">
33
                <div class="clearfix">
34
                    <img class="img-fluid float-right" src="images/MUNA%20-%20Logo%20100x100.png" alt="MUNA Trading Logo" />
35
                    <h1 id="connected">Export eBay Listings to Shopify
36
                    <input id="login" type="button" class="btn bg-success mb-2 w3-hide" onclick="eBayLogin();" value="Login" /></h1>
37
                </div>
2 - 38
            </div>
39
 
30 - 40
            <div class="col border">
2 - 41
                <div>
30 - 42
                    <form id="searchForm" class="container-fluid bg-light" onsubmit="return getListings();">
43
                        <div id="form1div" class="w3-hide">
44
                            <div class="form-check">
45
                                <label for="completedList">
46
                                    <input id="completedList" class="form-check-input" type="radio" name="rptType" value="completedList" checked>Completed Listings
47
                                </label>
48
                            </div>
49
                            <div class="form-check">
50
                                <label for="liveList">
51
                                    <input id="liveList" class="form-check-input" type="radio" name="rptType" value="liveList" checked>Live Listings
52
                                </label>
53
                            </div>
54
                            <input id="startButton" type="button" class="btn btn-danger" onclick="getListings();" value="Start" />
6 - 55
                        </div>
30 - 56
                        <div id="form2div" class="w3-hide">
57
                            <input id="downloadButton" type="button" class="btn btn-dark" onclick="downloadListings()" value="Download CSV" />
6 - 58
                        </div>
2 - 59
                    </form>
60
                </div>
30 - 61
                <div class="modal" id="progressBarDiv">
62
                    <div class="modal-dialog">
63
                        <div class="modal-content">
64
                            <div class="modal-header">
65
                                <h4 id="progressBarHeader"></h4>
66
                            </div>
67
                            <div class="modal-body">
68
                                <div class="progress">
69
                                    <div id="progressBar" class="progress-bar" style="width:0%">0%</div>
70
                                </div>
71
                            </div>
72
                        </div>
2 - 73
                    </div>
74
                </div>
30 - 75
                <div id="results" class="border bg-info w3-hide"></div>
76
                <div id="csv" class="w3-hide"></div>
77
                <div id="logging"></div>
2 - 78
            </div>
79
        </div>
80
 
30 - 81
        <footer class="container-fluid text-center border border-bottom-0 border-left-0 border-right-0">
17 - 82
            <div w3-include-html="php/footer.php"></div>
2 - 83
        </footer>
84
 
85
    </div>
86
 
87
    <script>
88
 
89
// Globals
90
var tableName = 'exportTable';
91
var tableSorterName = 'selectTable';
92
var liveListing;
93
var maxPagesToProcess = 0;
94
var pagesToProcess = 0;
95
var pagesProcessed = 0;
96
var maxNotesToAdd = 0;
97
var notesToAdd = 0;
98
var notesAdded = 0;
32 - 99
var listingsToFill = 0;
100
var maxListingsToFill = 0;
101
var listingsFilled = 0;
102
var listingsToFind = 0;
103
var maxListingsToFind = 0;
104
var listingsFound = 0;
2 - 105
var html = [];
106
 
107
// Initialize Configuration Variables
108
function initConfig() {
109
    var x = document.getElementById("form1div");
110
    if (x.className.indexOf("w3-show") == -1) {
111
        x.className += " w3-show";
112
    }
113
 
114
    eBayAuthToken = readCookie();
115
    if (eBayAuthToken.length > 0) {
116
        connected();
117
    }
118
 
119
    if (eBayAuthTokenFlag === false) {
120
        x = document.getElementById("login");
121
        if (x.className.indexOf("w3-show") == -1) {
122
            x.className += " w3-show";
123
        }
124
    }
125
}
126
 
127
function connected() {
128
    var x;
129
 
130
    eBayAuthTokenFlag = true;
131
    document.getElementById("connected").innerHTML += " (Connected)";
132
 
133
    x = document.getElementById("startButton");
30 - 134
    x.className = x.className.replace(" btn-danger", " btn-dark");
2 - 135
 
136
    x = document.getElementById("login");
137
    x.className = x.className.replace(" w3-show", "");
138
 
139
    x = document.getElementById("results");
140
    x.innerHTML = "";
141
    x.className = x.className.replace(" w3-show", "");
142
    x.className = x.className.replace(" process-errors", "");
143
}
144
 
145
function requireNewLogin() {
146
    var x = document.getElementById("startButton");
30 - 147
    x.className = x.className.replace(" btn-dark", " btn-danger ");
2 - 148
}
149
 
150
function downloadListings() {
151
    var noteList = [];
152
    var table = document.getElementById(tableSorterName);
153
 
154
    maxPagesToProcess = 0;
155
    pagesToProcess = 0;
156
    pagesProcessed = 0;
157
 
158
    initProgressBar("Downloading Selected Items");
159
 
160
    var x = document.getElementById("results");
161
    x.innerHTML = '';
162
    x.className = x.className.replace("process-errors", "");
163
    if (x.className.indexOf("w3-show") == -1) {
164
        x.className += " w3-show";
165
    }
166
    x.innerHTML = '<p><strong>Downloading Listings...</strong></p>';
167
 
168
    document.getElementById("csv").innerHTML = '';
169
 
170
    tableStart();
171
    for (var i = 2; i < table.rows.length; i++) {
172
        if (table.rows[i].cells[0].children[0].checked) {
173
            ++maxPagesToProcess;
174
            ++pagesToProcess;
175
            noteList.push(table.rows[i].cells[3].innerHTML);
32 - 176
            eBaySearch(table.rows[i].cells[3].innerHTML, (table.rows[i].cells[13].innerHTML == 'Yes' ? true : false));
2 - 177
 
178
            table.deleteRow(i--);
179
        }
180
    }
181
 
182
    function checkpagesToProcess() {
183
        if (pagesToProcess > 0) {
184
            window.setTimeout(checkpagesToProcess, 100); // wait 100 milliseconds
185
        } else {
186
            tableEnd();
7 - 187
            tableSorterUpdateCounters(tableSorterName);
2 - 188
            endProgressBar();
36 - 189
            exportTableToCSV(tableName, 'import.csv', true);
2 - 190
            addNotes(noteList);
191
        }
192
    }
193
 
194
    checkpagesToProcess();
195
}
196
 
197
function addNotes(noteList) {
198
    var i, x;
199
 
200
    maxNotesToAdd = 0;
201
    notesToAdd = 0;
202
    notesAdded = 0;
203
 
204
    if (!liveListing) {
205
        initProgressBar('Adding Notes to Ended Listings');
206
 
207
        for (i = 0; i < noteList.length; i++) {
208
            ++maxNotesToAdd;
209
            ++notesToAdd;
210
            addNote(noteList[i]);
211
        }
212
    }
213
 
214
    function checkNotesToAdd() {
215
        if (notesToAdd > 0) {
216
            window.setTimeout(checkNotesToAdd, 100); // wait 100 milliseconds
217
        } else {
218
            endProgressBar();
219
 
220
            x = document.getElementById("results");
221
            x.innerHTML += '<p><strong>Report Finished!</strong></p>';
222
            if (!x.className.includes("process-errors")) {
223
                setTimeout(function() {
224
                    x.className = x.className.replace(" w3-show", "");
225
                }, 3000);
226
            }
227
        }
228
    }
229
 
230
    checkNotesToAdd();
231
}
232
 
233
function tableStart() {
234
    document.getElementById("csv").innerHTML = '';
235
    html = [];
236
 
237
    html.push('<h3>Shopify Import</h3>');
30 - 238
    html.push('<div class="border table-responsive">');
2 - 239
    html.push('<table id="' + tableName + '">');
240
    html.push('<thead>');
241
    html.push('<tr>');
242
 
243
    html.push(tableHeader('Handle'));
244
    html.push(tableHeader('Title'));
245
    html.push(tableHeader('Body (HTML)'));
246
    html.push(tableHeader('Vendor'));
247
    html.push(tableHeader('Type'));
248
    html.push(tableHeader('Tags'));
249
    html.push(tableHeader('Published'));
25 - 250
    html.push(tableHeader('Option1 Name'));
251
    html.push(tableHeader('Option1 Value'));
252
    html.push(tableHeader('Option2 Name'));
253
    html.push(tableHeader('Option2 Value'));
254
    html.push(tableHeader('Option3 Name'));
255
    html.push(tableHeader('Option3 Value'));
2 - 256
    html.push(tableHeader('Variant SKU'));
25 - 257
    html.push(tableHeader('Variant Grams'));
2 - 258
    html.push(tableHeader('Variant Inventory Tracker'));
25 - 259
    html.push(tableHeader('Variant Inventory Qty'));
2 - 260
    html.push(tableHeader('Variant Inventory Policy'));
261
    html.push(tableHeader('Variant Fulfillment Service'));
262
    html.push(tableHeader('Variant Price'));
25 - 263
    html.push(tableHeader('Variant Compare At Price'));
2 - 264
    html.push(tableHeader('Variant Requires Shipping'));
265
    html.push(tableHeader('Variant Taxable'));
266
    html.push(tableHeader('Variant Barcode'));
267
    html.push(tableHeader('Image Src'));
268
    html.push(tableHeader('Image Position'));
269
    html.push(tableHeader('Image Alt Text'));
25 - 270
    html.push(tableHeader('Gift Card'));
271
    html.push(tableHeader('Google Shopping / MPN'));
272
    html.push(tableHeader('Google Shopping / Age Group'));
273
    html.push(tableHeader('Google Shopping / Gender'));
274
    html.push(tableHeader('Google Shopping / Google Product Category'));
275
    html.push(tableHeader('SEO Title'));
276
    html.push(tableHeader('SEO Description'));
277
    html.push(tableHeader('Google Shopping / AdWords Grouping'));
278
    html.push(tableHeader('Google Shopping / AdWords Labels'));
279
    html.push(tableHeader('Google Shopping / Condition'));
280
    html.push(tableHeader('Google Shopping / Custom Product'));
281
    html.push(tableHeader('Google Shopping / Custom Label 0'));
282
    html.push(tableHeader('Google Shopping / Custom Label 1'));
283
    html.push(tableHeader('Google Shopping / Custom Label 2'));
284
    html.push(tableHeader('Google Shopping / Custom Label 3'));
285
    html.push(tableHeader('Google Shopping / Custom Label 4'));
286
    html.push(tableHeader('Variant Image'));
2 - 287
    html.push(tableHeader('Variant Weight Unit'));
25 - 288
    html.push(tableHeader('Variant Tax Code'));
289
    html.push(tableHeader('Cost per item'));
2 - 290
 
291
    html.push('</tr>');
292
    html.push('</thead>');
293
    html.push('<tbody>');
294
}
295
 
32 - 296
function tableEntry(JsonObj, onShopify) {
2 - 297
    var i, n, barcode, handle;
298
    var tags = [];
299
    var ConditionDescription = JsonObj.ConditionDescription;
300
    var ConditionID = JsonObj.ConditionID;
301
    var CurrentPrice = JsonObj.SellingStatus.CurrentPrice;
302
    var CurrentPriceValue = CurrentPrice === undefined ? 0.00 : Number(CurrentPrice.text);
303
    var price = CurrentPriceValue;
304
    var Description = JsonObj.Description;
305
    var userDescription;
306
    var ItemID = JsonObj.ItemID;
307
    var ItemSpecifics = JsonObj.ItemSpecifics;
6 - 308
    var PictureURL = getJsonArray(JsonObj.PictureDetails.PictureURL);
2 - 309
    var PrimaryCategoryName = JsonObj.PrimaryCategory.CategoryName;
16 - 310
    var Quantity = Number(JsonObj.Quantity);
311
    var QuantitySold = Number(JsonObj.SellingStatus.QuantitySold);
2 - 312
    var SKU = JsonObj.SKU;
313
    var ProductListingDetails = JsonObj.ProductListingDetails;
314
    var weightInGrams = 0;
315
    var categoryName = PrimaryCategoryName;
316
 
55 - 317
    if (Quantity == QuantitySold) {
318
        return;
319
    }
320
 
2 - 321
    var Title = JsonObj.Title;
322
 
323
    if (Number(ConditionID) === 1000) {
324
        tags.push("Condition: Brand New");
325
    } else {
326
        tags.push("Condition: Preowned");
327
    }
328
 
329
    n = (SKU === undefined ? 0 : SKU.indexOf(" - "));
330
    if (n > 0) {
331
        barcode = SKU.substr(n + 3);
332
        barcode = barcode.substr(barcode.indexOf(" ") + 1);
333
        handle = 'B' + barcode;
334
        barcode = '\'' + barcode;
335
        SKU = SKU.substr(0, n);
336
    } else {
337
        if (categoryName.indexOf("Books") != -1 && ProductListingDetails && ProductListingDetails.ISBN && isNumeric(ProductListingDetails.ISBN)) {
338
            barcode = '\'' + ProductListingDetails.ISBN;
339
            handle = 'B' + ProductListingDetails.ISBN;
340
        } else if (ProductListingDetails && ProductListingDetails.UPC && isNumeric(ProductListingDetails.UPC)) {
341
            barcode = '\'' + ProductListingDetails.UPC;
342
            handle = 'B' + ProductListingDetails.UPC;
343
        } else if (ProductListingDetails && ProductListingDetails.ProductReferenceID && isNumeric(ProductListingDetails.ProductReferenceID)) {
344
            barcode = 'R' + ProductListingDetails.ProductReferenceID;
345
            handle = barcode;
346
        } else {
347
            barcode = 'H' + Title.hashCode();
348
            handle = barcode;
349
        }
350
    }
351
 
352
    userDescription = extractDescription(Description);
16 - 353
    if (ItemSpecifics && ItemSpecifics.NameValueList && PrimaryCategoryName.indexOf('DVD') < 0) {
354
        userDescription += '<table><tbody>';
355
        for (i = 0; i < ItemSpecifics.NameValueList.length; i++) {
356
            var itemSpecific = ItemSpecifics.NameValueList[i];
357
            if (itemSpecific.Name != 'All returns accepted' &&
358
                itemSpecific.Name != 'Item must be returned within' &&
359
                itemSpecific.Name != 'Refund will be given as' &&
360
                itemSpecific.Name != 'Return policy details' &&
361
                itemSpecific.Name != 'Restocking Fee' &&
362
                itemSpecific.Name != 'Return shipping will be paid by') {
363
                userDescription += '<tr><td style="padding: 0;">' + itemSpecific.Name + ':</td><td style="padding: 0;">' + itemSpecific.Value + '</td></tr>';
2 - 364
            }
365
        }
16 - 366
        userDescription += '</tbody></table>';
2 - 367
    }
368
 
369
    if (ConditionDescription &&
370
        (userDescription.indexOf('Please view all actual images.') < 0 && userDescription.indexOf('mint condition') < 0)) {
371
        userDescription += '<p>' + ConditionDescription + '</p>';
372
    }
373
 
374
    if (categoryName.indexOf("CDs") != -1) {
375
        categoryName = 'Music CDs';
376
        price = 4.95;
377
    } else if (categoryName.indexOf("Movies") != -1) {
378
        categoryName = 'Movies';
379
        price = 4.95;
380
    } else if (categoryName.indexOf("Books") != -1) {
381
        categoryName = 'Books';
382
        price = 5.95;
383
    } else if (categoryName.indexOf("Trading Cards") != -1) {
384
        if (categoryName.indexOf("Baseball") != -1) {
385
            tags.push('Trading Cards: Baseball');
386
        } else if (categoryName.indexOf("Football") != -1) {
387
            tags.push('Trading Cards: Football');
388
        } else if (categoryName.indexOf("Basketball") != -1) {
389
            tags.push('Trading Cards: Basketball');
390
        } else if (categoryName.indexOf("Soccer") != -1) {
391
            tags.push('Trading Cards: Soccer');
392
        } else if (categoryName.indexOf("Ice Hockey") != -1) {
393
            tags.push('Trading Cards: Ice Hockey');
394
        } else if (categoryName.indexOf("Auto Racing") != -1) {
395
            tags.push('Trading Cards: Auto Racing');
396
        }
397
        categoryName = 'Trading Cards';
398
        price = 1.99;
399
    }
400
    tags.push(categoryName);
401
 
402
    if (liveListing) {
403
        tags.push('_eBay Live');
404
 
53 - 405
        price = round((CurrentPriceValue * 0.90), 1) + 0.05; // always end with 5
2 - 406
        if (categoryName.indexOf("CDs") != -1 && price < 4.95) {
407
            price = 4.95;
408
        } else if (categoryName.indexOf("Movies") != -1 && price < 4.95) {
409
            price = 4.95;
410
        } else if (categoryName.indexOf("Books") != -1 && price < 5.95) {
411
            price = 5.95;
412
        } else if (categoryName.indexOf("Trading Cards") != -1 && price < 1.99) {
413
            price = 1.99;
414
        }
415
    }
416
 
417
    if (JsonObj.ShippingPackageDetails !== undefined) {
418
        if (JsonObj.ShippingPackageDetails.WeightMajor !== undefined) {
419
            weightInGrams += (Number(JsonObj.ShippingPackageDetails.WeightMajor.text) * 453.5);
420
        }
421
 
422
        if (JsonObj.ShippingPackageDetails.WeightMinor !== undefined) {
423
            weightInGrams += (Number(JsonObj.ShippingPackageDetails.WeightMinor.text) * 28.3);
424
        }
425
    }
426
 
427
    html.push('<tr>');
428
 
429
    html.push(tableCell(handle));
430
    html.push(tableCell(Title));
431
    html.push(tableCell(escapeHtml(userDescription)));
432
    html.push(tableCell("MUNA Trading"));
433
    html.push(tableCell(categoryName));
434
    html.push(tableCell(tags.join(',')));
435
    html.push(tableCell("TRUE"));
28 - 436
    html.push(tableCell("Title"));
437
    html.push(tableCell("Default Title"));
25 - 438
    html.push(tableCell(""));
439
    html.push(tableCell(""));
440
    html.push(tableCell(""));
441
    html.push(tableCell(""));
2 - 442
    html.push(tableCell((SKU !== undefined ? SKU : "")));
25 - 443
    html.push(tableCell(Math.floor(weightInGrams)));
2 - 444
    html.push(tableCell("shopify"));
16 - 445
    html.push(tableCell((Quantity - QuantitySold)));
2 - 446
    html.push(tableCell("deny"));
447
    html.push(tableCell("manual"));
448
    html.push(tableCell(price));
25 - 449
    html.push(tableCell(""));
2 - 450
    html.push(tableCell("TRUE"));
451
    html.push(tableCell("TRUE"));
452
    html.push(tableCell(barcode));
32 - 453
    if (onShopify) {
454
        html.push(tableCell(""));
455
        html.push(tableCell(""));
456
        html.push(tableCell(""));
457
    } else {
458
        html.push(tableCell(PictureURL[0].substr(0, PictureURL[0].lastIndexOf('/') + 1) + '$_57.JPG'));
459
        html.push(tableCell(1));
460
        html.push(tableCell(Title + " Image 1"));
461
    }
25 - 462
    html.push(tableCell("FALSE"));
463
    html.push(tableCell(""));
464
    html.push(tableCell(""));
465
    html.push(tableCell(""));
466
    html.push(tableCell(""));
467
    html.push(tableCell(""));
468
    html.push(tableCell(""));
469
    html.push(tableCell(""));
470
    html.push(tableCell(""));
471
    html.push(tableCell(""));
472
    html.push(tableCell(""));
473
    html.push(tableCell(""));
474
    html.push(tableCell(""));
475
    html.push(tableCell(""));
476
    html.push(tableCell(""));
477
    html.push(tableCell(""));
478
    html.push(tableCell(""));
2 - 479
    html.push(tableCell("oz"));
25 - 480
    html.push(tableCell(""));
481
    html.push(tableCell(""));
2 - 482
 
483
    html.push('</tr>');
484
 
32 - 485
    if (!onShopify) {
486
        for (i = 1; i < PictureURL.length; i++) {
487
            html.push('<tr>');
2 - 488
 
32 - 489
            html.push(tableCell(handle));
490
            html.push(tableCell(""));
491
            html.push(tableCell(""));
492
            html.push(tableCell(""));
493
            html.push(tableCell(""));
494
            html.push(tableCell(""));
495
            html.push(tableCell(""));
496
            html.push(tableCell(""));
497
            html.push(tableCell(""));
498
            html.push(tableCell(""));
499
            html.push(tableCell(""));
500
            html.push(tableCell(""));
501
            html.push(tableCell(""));
502
            html.push(tableCell(""));
503
            html.push(tableCell(""));
504
            html.push(tableCell(""));
505
            html.push(tableCell(""));
506
            html.push(tableCell(""));
507
            html.push(tableCell(""));
508
            html.push(tableCell(""));
509
            html.push(tableCell(""));
510
            html.push(tableCell(""));
511
            html.push(tableCell(""));
512
            html.push(tableCell(""));
513
            html.push(tableCell(PictureURL[i].substr(0, PictureURL[i].lastIndexOf('/') + 1) + '$_57.JPG'));
514
            html.push(tableCell(i + 1));
515
            html.push(tableCell(Title + " Image " + (i + 1)));
516
            html.push(tableCell(""));
517
            html.push(tableCell(""));
518
            html.push(tableCell(""));
519
            html.push(tableCell(""));
520
            html.push(tableCell(""));
521
            html.push(tableCell(""));
522
            html.push(tableCell(""));
523
            html.push(tableCell(""));
524
            html.push(tableCell(""));
525
            html.push(tableCell(""));
526
            html.push(tableCell(""));
527
            html.push(tableCell(""));
528
            html.push(tableCell(""));
529
            html.push(tableCell(""));
530
            html.push(tableCell(""));
531
            html.push(tableCell(""));
532
            html.push(tableCell(""));
533
            html.push(tableCell(""));
534
            html.push(tableCell(""));
535
            html.push(tableCell(""));
2 - 536
 
32 - 537
            html.push('</tr>');
538
        }
2 - 539
    }
540
}
541
 
542
function extractDescription(str) {
543
    var startText = "<!-- *************** Place description here ****************** -->";
544
    var startPos = str.indexOf(startText) + startText.length;
545
    var endPos = str.lastIndexOf("<!-- *************** Place description here ****************** -->");
546
    var userDescription = str.substr(startPos, endPos - startPos);
547
    var parser = new DOMParser();
548
    var htmlDoc = parser.parseFromString(userDescription, "text/html");
549
 
550
    return ((htmlDoc.getElementsByTagName('div')[0] !== undefined ? htmlDoc.getElementsByTagName('div')[0].innerHTML : userDescription));
551
}
552
 
553
function tableEnd() {
554
    html.push('</tbody>');
555
    html.push('</table>');
556
    html.push('</div>');
557
 
558
    document.getElementById("csv").innerHTML = html.join('');
559
}
560
 
561
function addNote(itemId) {
562
    var i;
563
    var xml;
564
 
565
    var xw = new XMLWriter('UTF-8', '1.0');
566
    var xhr = new XMLHttpRequest();
567
 
568
    if (!createAddXMLNote(xw, xhr, 'SetUserNotesRequest', 'SetUserNotes', itemId)) {
569
        return;
570
    }
571
 
572
    xml = xw.flush();
573
    xw.close();
574
 
575
    xhr.onload = function() {
16 - 576
        var jsonObj = XMLparse(xhr.responseXML, false);
577
        var obj = jsonObj.SetUserNotesResponse;
2 - 578
        var returnCode = obj.Ack;
19 - 579
        var str;
2 - 580
 
581
        var x = document.getElementById("results");
582
        if (x.className.indexOf("w3-show") == -1) {
583
            x.className += " w3-show";
584
        }
585
 
586
        if (returnCode == 'Success' || (returnCode == 'Warning' && obj.Errors.ErrorCode == '21917108')) {
19 - 587
            str = '<p>' + itemId + ': <strong>' + returnCode + '</strong></p>';
2 - 588
 
589
            if (returnCode == 'Warning') {
19 - 590
                str += "<p>" + obj.Errors.SeverityCode + " (" + obj.Errors.ErrorCode + "): " + escapeHtml(obj.Errors.LongMessage) + "</p>";
2 - 591
            }
19 - 592
 
593
            x.innerHTML += str;
2 - 594
        } else {
595
            x.className += " process-errors";
30 - 596
            str = '<p class="text-danger">' + itemId + ': <strong>' + returnCode + ':</strong></p>';
2 - 597
 
6 - 598
            var errors = getJsonArray(obj.Errors);
19 - 599
            str += "<p>";
6 - 600
            for (i = 0; i < errors.length; i++) {
19 - 601
                str += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
2 - 602
            }
19 - 603
            str += "</p>";
604
 
605
            x.innerHTML += str;
2 - 606
        }
607
 
608
        if (obj.Message) {
609
            x.innerHTML += obj.Message;
610
        }
611
 
612
        --notesToAdd;
613
        ++notesAdded;
614
 
615
        updateProgressBar(maxNotesToAdd, notesAdded);
616
    };
617
 
618
    xhr.send(xml);
619
}
620
 
621
function createAddXMLNote(xw, xhr, xmlrequest, callname, itemId) {
622
    var today = new Date();
623
    var todayYYYYMMDD = today.toISOString().slice(0, 10);
624
 
625
    xw.writeStartDocument();
626
    xw.writeStartElement(xmlrequest);
627
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
628
 
629
    xw.writeStartElement('RequesterCredentials');
630
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
631
    xw.writeEndElement(); /* RequesterCredentials */
632
 
633
    xw.writeElementString('ItemID', itemId);
634
    xw.writeElementString('Action', 'AddOrUpdate');
635
    xw.writeElementString('NoteText', 'Exported to Shopify on ' + todayYYYYMMDD);
636
 
637
    xw.writeElementString('ErrorLanguage', 'en_US');
638
    xw.writeElementString('Version', configeBayTradingVersion);
639
    xw.writeElementString('WarningLevel', configWarningLevel);
640
 
641
    xw.writeEndElement(); /* xmlrequest */
642
    xw.writeEndDocument();
643
 
644
    xhr.open('POST', configProxyUrl, true);
645
    xhr.setRequestHeader('Content-Type', 'text/xml');
646
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
647
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
648
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
649
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
650
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
651
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
652
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);
653
 
654
    return true;
655
}
656
 
32 - 657
function eBaySearch(itemId, onShopify) {
2 - 658
    var i;
659
    var xml;
660
 
661
    var xw = new XMLWriter('UTF-8', '1.0');
662
    var xhr = new XMLHttpRequest();
663
 
664
    if (!createAddXMLSearch(xw, xhr, 'GetItemRequest', 'GetItem', itemId)) {
665
        return;
666
    }
667
 
668
    xml = xw.flush();
669
    xw.close();
670
 
671
    xhr.onload = function() {
16 - 672
        var jsonObj = XMLparse(xhr.responseXML, false);
673
        var obj = jsonObj.GetItemResponse;
2 - 674
        var returnCode = obj.Ack;
19 - 675
        var str;
2 - 676
 
677
        var x = document.getElementById("results");
678
        if (x.className.indexOf("w3-show") == -1) {
679
            x.className += " w3-show";
680
        }
681
 
682
        if (returnCode == 'Success') {
32 - 683
            tableEntry(obj.Item, onShopify);
2 - 684
        } else {
685
            x.className += " process-errors";
30 - 686
            str = '<p class="text-danger">' + itemId + ': <strong>' + returnCode + ':</strong></p>';
2 - 687
 
6 - 688
            var errors = getJsonArray(obj.Errors);
19 - 689
            str += "<p>";
6 - 690
            for (i = 0; i < errors.length; i++) {
19 - 691
                str += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
2 - 692
            }
19 - 693
            str += "</p>";
6 - 694
 
19 - 695
            x.innerHTML += str;
2 - 696
        }
697
 
698
        --pagesToProcess;
699
        ++pagesProcessed;
700
 
701
        updateProgressBar(maxPagesToProcess, pagesProcessed);
702
    };
703
 
704
    xhr.send(xml);
705
}
706
 
707
function createAddXMLSearch(xw, xhr, xmlrequest, callname, itemId) {
708
 
709
    xw.writeStartDocument();
710
    xw.writeStartElement(xmlrequest);
711
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
712
 
713
    xw.writeStartElement('RequesterCredentials');
714
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
715
    xw.writeEndElement(); /* RequesterCredentials */
716
 
717
    xw.writeElementString('ItemID', itemId);
718
    xw.writeElementString('IncludeItemSpecifics', 'true');
719
    xw.writeElementString('DetailLevel', 'ReturnAll');
720
 
721
    xw.writeElementString('ErrorLanguage', 'en_US');
722
    xw.writeElementString('Version', configeBayTradingVersion);
723
    xw.writeElementString('WarningLevel', configWarningLevel);
724
 
725
    xw.writeEndElement(); /* xmlrequest */
726
    xw.writeEndDocument();
727
 
728
    xhr.open('POST', configProxyUrl, true);
729
    xhr.setRequestHeader('Content-Type', 'text/xml');
730
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
731
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
732
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
733
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
734
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
735
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
736
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);
737
 
738
    return true;
739
}
740
 
741
function getListings() {
742
    var x;
743
 
744
    if (eBayAuthTokenFlag === false) {
745
        return;
746
    }
747
 
748
    document.getElementById("logging").innerHTML = '';
749
 
750
    x = document.getElementById("results");
751
    if (x.className.indexOf("w3-show") == -1) {
752
        x.className += " w3-show";
753
    }
754
    x.className = x.className.replace("process-errors", "");
755
    x.innerHTML = '<p><strong>Retrieving Listings...</strong></p>';
756
 
32 - 757
    initProgressBar('Getting Items for Export (Step 1/3)');
2 - 758
 
759
    liveListing = (getRadioValue('rptType') == 'liveList');
760
    tableSorterStart();
761
    maxPagesToProcess = 1;
762
    pagesToProcess = 1;
763
    pagesProcessed = 0;
764
    eBaySorterSearch(1);
765
 
766
    function checkpagesToProcess() {
767
        if (pagesToProcess > 0) {
768
            window.setTimeout(checkpagesToProcess, 100); // wait 100 milliseconds
769
        } else {
770
            tableSorterEnd();
771
            endProgressBar();
772
            // fillSorterListings(); is now in tableSorterEnd()
773
        }
774
    }
775
 
776
    checkpagesToProcess();
777
}
778
 
779
function tableSorterStart() {
780
    document.getElementById("logging").innerHTML = '';
781
    html = [];
782
 
783
    html.push('<h3>Items for Export</h3>');
784
    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>');
30 - 785
    html.push('<div class="border table-responsive">');
2 - 786
    html.push('<table id="' + tableSorterName + '" class="tablesorter">');
787
    html.push('<thead>');
788
    html.push('<tr>');
789
 
6 - 790
    html.push(tableHeaderCheckbox());
2 - 791
    html.push(tableHeader('Image'));
792
    html.push(tableHeader('Title'));
793
    html.push(tableHeader('Item Id'));
794
    html.push(tableHeader('SKU'));
795
    html.push(tableHeader('Category'));
796
    html.push(tableHeader('Format'));
797
    html.push(tableHeader('Price'));
798
    html.push(tableHeader('Quantity'));
799
    html.push(tableHeader('Views'));
800
    html.push(tableHeader('Watchers'));
801
    html.push(tableHeader(liveListing ? 'Start Date' : 'End Date'));
802
    html.push(tableHeader('Note'));
32 - 803
    html.push(tableHeader('Shopify'));
2 - 804
 
805
    html.push('</tr>');
806
    html.push('</thead>');
807
    html.push('<tbody>');
808
}
809
 
810
function tableSorterEntry(JsonObj) {
811
    var i;
812
    var image;
813
    var title;
814
    var itemId;
815
    var sku;
816
    var format;
817
    var price;
818
    var quantity;
819
    var views;
820
    var watchers;
821
    var listingDate;
822
    var privateNote;
823
    var ebayNote;
824
    var relisted;
16 - 825
    var bidCount;
2 - 826
 
827
    for (i = 0; i < JsonObj.length; i++) {
16 - 828
        image = getJsonValue(JsonObj[i].PictureDetails.GalleryURL).replace('http:', 'https:');
2 - 829
        title = getJsonValue(JsonObj[i].Title);
830
        itemId = getJsonValue(JsonObj[i].ItemID);
831
        sku = getJsonValue(JsonObj[i].SKU);
832
        format = getJsonValue(JsonObj[i].ListingType);
16 - 833
        bidCount = (typeof(JsonObj[i].SellingStatus) === 'object' ? Number(getJsonValue(JsonObj[i].SellingStatus.BidCount)) : 0);
2 - 834
        if (format == 'Chinese') {
835
            format = 'Auction';
836
            price = getJsonValue(JsonObj[i].StartPrice.text);
837
        } else {
838
            format = 'Fixed Price';
839
            price = getJsonValue(JsonObj[i].BuyItNowPrice.text);
840
        }
841
        quantity = getJsonValue(JsonObj[i].QuantityAvailable);
842
        views = '';
16 - 843
        watchers = Number(getJsonValue(JsonObj[i].WatchCount));
844
        listingDate = getJsonValue((liveListing ? JsonObj[i].ListingDetails.StartTime : JsonObj[i].ListingDetails.EndTime)).substr(0, 10);
2 - 845
        privateNote = getJsonValue(JsonObj[i].PrivateNotes);
846
        ebayNote = getJsonValue(JsonObj[i].eBayNotes);
847
        relisted = getJsonValue(JsonObj[i].Relisted);
848
 
849
        if (!liveListing) {
850
            if (privateNote.startsWith("Exported to ") || relisted == 'true' || privateNote.startsWith("Sold via ") || privateNote.startsWith("Relisted As New Item")) {
851
                continue;
852
            }
16 - 853
        } else {
854
            if (bidCount > 0) {
855
                continue;
856
            }
2 - 857
        }
858
 
859
        html.push('<tr>');
860
 
861
        html.push(tableCellCheckbox());
30 - 862
        html.push(tableCell('<img class="img-fluid" src="' + image + '" alt="Gallery Image" style="max-height:100px;max-width:100px;">'));
2 - 863
        html.push(tableCell(title));
864
        html.push(tableCell(itemId));
865
        html.push(tableCell(sku));
866
        html.push(tableCellLabel('Category' + itemId));
867
        html.push(tableCell(format));
868
        html.push(tableCell('$' + price));
869
        html.push(tableCell(quantity));
870
        html.push(tableCellLabel('Views' + itemId));
871
        html.push(tableCell(watchers));
872
        html.push(tableCell(listingDate));
873
        html.push(tableCell(privateNote + ' ' + ebayNote));
32 - 874
        html.push(tableCellLabel('Shopify' + itemId));
2 - 875
 
876
        html.push('</tr>');
877
    }
878
}
879
 
880
function tableSorterEnd() {
881
    html.push('</tbody>');
882
    html.push('</table>');
883
    html.push('</div>');
884
 
885
    document.getElementById("logging").innerHTML = html.join('');
886
 
887
    $(function() {
888
        $("#" + tableSorterName).on('tablesorter-initialized', function() {
889
 
890
            // class name to add on tr when checkbox is checked
891
            var highlightClass = 'checked',
892
                // resort the table after the checkbox is modified?
893
                resort = true,
894
                $table = $(this),
895
                c = this.config,
896
                wo = c && c.widgetOptions,
897
                // include sticky header checkbox; if installed
898
                $sticky = c && wo.$sticky || '',
899
                doChecky = function(c, col) {
900
                    $table
901
                        .children('tbody')
902
                        .children('tr:visible')
903
                        .children('td:nth-child( ' + (parseInt(col, 10) + 1) + ' )')
904
                        .find('input[type=checkbox]')
905
                        .each(function() {
906
                            this.checked = c;
907
                            $(this).trigger('change');
908
                        });
909
                };
910
 
911
            $table
912
                .children('tbody')
913
                .on('change', 'input[type=checkbox]', function() {
914
                    // ignore change if updating all rows
915
                    if ($table[0].ignoreChange) {
916
                        return;
917
                    }
918
                    var $this = $(this);
919
                    $this.closest('tr').toggleClass(highlightClass, this.checked);
19 - 920
                    // resort will jump bqack to the top
921
                    // $this.trigger('updateCell', [$this.closest('td'), resort]);
2 - 922
 
923
                    // handle header
7 - 924
                    var rowCount = $('#' + tableSorterName + ' tbody tr').length;
2 - 925
                    var checkedCount = $('#' + tableSorterName + ' tbody .checked').length;
926
                    var ua = window.navigator.userAgent;
927
                    if (checkedCount === 0) {
928
                        $table.add($sticky).find('thead input[type=checkbox]').prop('checked', false);
929
                        $table.add($sticky).find('thead input[type=checkbox]').prop('indeterminate', false);
930
                    } else if (checkedCount === rowCount) {
931
                        $table.add($sticky).find('thead input[type=checkbox]').prop('checked', true);
932
                        $table.add($sticky).find('thead input[type=checkbox]').prop('indeterminate', false);
933
                    } else {
934
                        $table.add($sticky).find('thead input[type=checkbox]').prop('checked', !(ua.indexOf('Trident/') > -1 || ua.indexOf('Edge/') > -1));
935
                        $table.add($sticky).find('thead input[type=checkbox]').prop('indeterminate', true);
936
                    }
937
                    $('#selected-rows').html(checkedCount);
938
                })
939
                .end()
940
                .add($sticky)
941
                .find('thead input[type=checkbox]')
942
                // Click on checkbox in table header to toggle all inputs
943
                .on('change', function() {
944
                    // prevent updateCell for every cell
945
                    $table[0].ignoreChange = true;
946
                    var c = this.checked,
947
                        col = $(this).closest('th').attr('data-column');
948
                    doChecky(c, col);
949
                    // update main & sticky header
950
 
951
                    $table.children('tbody').children('tr:visible').toggleClass(highlightClass, c);
952
                    // update all at once
953
                    $table[0].ignoreChange = false;
954
                    $table.trigger('update', [resort]);
955
 
956
                    // handle header
7 - 957
                    var rowCount = $('#' + tableSorterName + ' tbody tr').length;
2 - 958
                    var checkedCount = $('#' + tableSorterName + ' tbody .checked').length;
959
                    var ua = window.navigator.userAgent;
960
                    if (checkedCount === 0) {
961
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('checked', false);
962
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('indeterminate', false);
963
                    } else if (checkedCount === rowCount) {
964
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('checked', true);
965
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('indeterminate', false);
966
                    } else {
967
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('checked', !(ua.indexOf('Trident/') > -1 || ua.indexOf('Edge/') > -1));
968
                        $table.add($sticky).find('th[data-column=' + col + '] input[type=checkbox]').prop('indeterminate', true);
969
                    }
970
                    $('#selected-rows').html(checkedCount);
971
                })
972
                .on('mouseup', function() {
973
                    return false;
974
                });
975
 
976
        });
977
 
978
        $("#" + tableSorterName).tablesorter({
979
            theme: "blue",
980
            widgets: ["zebra", "stickyHeaders", "filter"],
981
            headers: {
982
                0: {
983
                    sorter: "checkbox"
984
                },
985
                1: {
986
                    sorter: false,
987
                    filter: false
988
                }
989
            },
990
            initialized: function() {
991
                fillSorterListings();
992
            }
993
        });
994
 
995
        $("#" + tableSorterName).bind('filterInit filterEnd', function(event, data) {
996
            $('#filtered-rows').html(data.filteredRows);
997
            $('#total-rows').html(data.totalRows);
998
        });
999
    });
1000
}
1001
 
1002
function eBaySorterSearch(pageNo) {
1003
    var i;
1004
    var xml;
1005
 
1006
    var xw = new XMLWriter('UTF-8', '1.0');
1007
    var xhr = new XMLHttpRequest();
1008
 
1009
    if (!createSorterAddXMLSearch(xw, xhr, 'GetMyeBaySelling', pageNo)) {
1010
        return;
1011
    }
1012
 
1013
    xml = xw.flush();
1014
    xw.close();
1015
 
1016
    xhr.onload = function() {
16 - 1017
        var jsonObj = XMLparse(xhr.responseXML, false);
1018
        var obj = jsonObj.GetMyeBaySellingResponse;
2 - 1019
        var returnCode = obj.Ack;
19 - 1020
        var str;
2 - 1021
 
1022
        var objArray = (liveListing ? obj.ActiveList : obj.UnsoldList);
1023
 
1024
        var x = document.getElementById("results");
1025
 
1026
        if (returnCode == 'Success') {
16 - 1027
            tableSorterEntry(objArray.ItemArray.Item);
2 - 1028
 
1029
            if (Number(objArray.PaginationResult.TotalNumberOfPages) > pageNo) {
1030
                maxPagesToProcess = Number(objArray.PaginationResult.TotalNumberOfPages);
1031
                ++pagesToProcess;
1032
                eBaySorterSearch(pageNo + 1);
1033
            }
1034
 
1035
            updateProgressBar(maxPagesToProcess, pagesProcessed);
1036
        } else {
1037
            x.className += " process-errors";
30 - 1038
            str = '<p class="text-danger">' + obj.CorrelationID + ': <strong>' + returnCode + ':</strong></p>';
6 - 1039
            var errors = getJsonArray(obj.Errors);
19 - 1040
            str += "<p>";
6 - 1041
            for (i = 0; i < errors.length; i++) {
19 - 1042
                str += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
2 - 1043
            }
19 - 1044
            str += "</p>";
6 - 1045
 
19 - 1046
            x.innerHTML += str;
2 - 1047
        }
1048
 
1049
        --pagesToProcess;
1050
        ++pagesProcessed;
1051
 
1052
        updateProgressBar(maxPagesToProcess, pagesProcessed);
1053
    };
1054
 
1055
    xhr.send(xml);
1056
}
1057
 
1058
function createSorterAddXMLSearch(xw, xhr, callname, pageNo) {
14 - 1059
    var outputSelector;
1060
 
2 - 1061
    xw.writeStartDocument();
1062
    xw.writeStartElement(callname + "Request");
1063
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
1064
 
1065
    xw.writeStartElement('RequesterCredentials');
1066
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
1067
    xw.writeEndElement(); /* RequesterCredentials */
1068
 
1069
    xw.writeElementString('DetailLevel', 'ReturnAll');
1070
    xw.writeStartElement('DeletedFromSoldList');
1071
    xw.writeElementString('Include', 'false');
1072
    xw.writeEndElement(); /* DeletedFromSoldList */
1073
    xw.writeStartElement('DeletedFromUnsoldList');
1074
    xw.writeElementString('Include', 'false');
1075
    xw.writeEndElement(); /* DeletedFromUnsoldList */
1076
    xw.writeStartElement('ScheduledList');
1077
    xw.writeElementString('Include', 'false');
1078
    xw.writeEndElement(); /* ScheduledList */
1079
    xw.writeStartElement('SellingSummary');
1080
    xw.writeElementString('Include', 'false');
1081
    xw.writeEndElement(); /* SellingSummary */
1082
    xw.writeStartElement('SoldList');
1083
    xw.writeElementString('Include', 'false');
1084
    xw.writeEndElement(); /* SoldList */
1085
 
1086
    xw.writeStartElement(liveListing ? 'UnsoldList' : 'ActiveList');
1087
    xw.writeElementString('Include', 'false');
1088
    xw.writeEndElement(); /* ActiveList */
1089
 
1090
    xw.writeStartElement(liveListing ? 'ActiveList' : 'UnsoldList');
1091
    xw.writeElementString('Include', 'true');
1092
    xw.writeElementString('IncludeNotes', 'true');
1093
    xw.writeStartElement('Pagination');
1094
    xw.writeElementString('EntriesPerPage', '200');
1095
    xw.writeElementString('PageNumber', '' + pageNo);
1096
    xw.writeEndElement(); /* Pagination*/
1097
    xw.writeEndElement(); /* UnsoldList> */
1098
 
14 - 1099
    outputSelector = liveListing ? 'ActiveList.' : 'UnsoldList.';
1100
 
16 - 1101
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.SellingStatus.BidCount');
14 - 1102
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.BuyItNowPrice');
1103
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.eBayNotes');
1104
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.ItemID');
16 - 1105
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.ListingDetails.' + (liveListing ? 'StartTime' : 'EndTime'));
14 - 1106
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.ListingType');
1107
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.PrivateNotes');
1108
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.QuantityAvailable');
1109
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.PictureDetails');
1110
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.Relisted');
1111
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.SKU');
1112
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.Title');
1113
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.StartPrice');
1114
    xw.writeElementString('OutputSelector', outputSelector + 'ItemArray.Item.WatchCount');
1115
    xw.writeElementString('OutputSelector', outputSelector + 'PaginationResult');
1116
 
2 - 1117
    xw.writeElementString('ErrorLanguage', 'en_US');
1118
    xw.writeElementString('Version', configeBayTradingVersion);
1119
    xw.writeElementString('WarningLevel', configWarningLevel);
1120
 
1121
    xw.writeEndElement(); /* xmlrequest */
1122
    xw.writeEndDocument();
1123
 
1124
    xhr.open('POST', configProxyUrl, true);
1125
    xhr.setRequestHeader('Content-Type', 'text/xml');
1126
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
1127
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
1128
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
1129
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
1130
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
1131
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
1132
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);
1133
 
1134
    return true;
1135
}
1136
 
1137
function fillSorterListings() {
1138
    var i;
1139
    var x = document.getElementById(tableSorterName);
1140
    var fillList = [];
1141
    var maxItems = 20;
1142
 
1143
    listingsToFill = 0;
1144
    maxListingsToFill = 0;
1145
    listingsFilled = 0;
1146
 
32 - 1147
    initProgressBar('Filling Listing Information (Step 2/3)');
2 - 1148
 
1149
    for (i = 2; i < x.rows.length; i++) {
1150
        fillList.push(x.rows[i].cells[3].innerHTML);
1151
    }
6 - 1152
 
16 - 1153
    for (i = 0; (i * maxItems) < fillList.length; i++) {
2 - 1154
        ++listingsToFill;
1155
        ++maxListingsToFill;
1156
        fillSorterListing(fillList.slice(i * maxItems, (i + 1) * maxItems));
1157
    }
1158
 
1159
    function checkListingsToFill() {
1160
        if (listingsToFill > 0) {
1161
            window.setTimeout(checkListingsToFill, 100); // wait 100 milliseconds
1162
        } else {
1163
            $("#" + tableSorterName).trigger("update");
1164
 
1165
            endProgressBar();
32 - 1166
            findOnShopify();
1167
        }
1168
    }
2 - 1169
 
32 - 1170
    checkListingsToFill();
1171
}
1172
 
1173
function findOnShopify() {
1174
    var i;
1175
    var x = document.getElementById(tableSorterName);
1176
 
1177
    listingsToFind = 0;
1178
    maxListingsToFind = 0;
1179
    listingsFound = 0;
1180
 
1181
    initProgressBar('Finding Listings already on Shopify (Step 3/3)');
1182
 
1183
    for (i = 2; i < x.rows.length; i++) {
1184
        ++listingsToFind;
1185
        ++maxListingsToFind;
54 - 1186
        findShopifyListing(x.rows[i].cells[3].innerHTML, x.rows[i].cells[2].innerText).then(function(env) {
32 - 1187
		    --listingsToFind;
1188
            ++listingsFound;
1189
            updateProgressBar(maxListingsToFind, listingsFound);
1190
        });
1191
    }
1192
 
1193
    function checkListingsToFind() {
1194
        if (listingsToFind > 0) {
1195
            window.setTimeout(checkListingsToFind, 100); // wait 100 milliseconds
1196
        } else {
1197
            $("#" + tableSorterName).trigger("update");
1198
 
1199
            endProgressBar();
1200
 
2 - 1201
            x = document.getElementById("form1div");
1202
            x.className = x.className.replace(" w3-show", "");
1203
            x = document.getElementById("form2div");
1204
            x.className += " w3-show";
1205
 
1206
            x = document.getElementById("results");
1207
            x.innerHTML += '<p><strong>Report Finished!</strong></p>';
1208
            if (!x.className.includes("process-errors")) {
1209
                setTimeout(function() {
1210
                    x.className = x.className.replace(" w3-show", "");
1211
                }, 3000);
1212
            }
1213
        }
1214
    }
1215
 
32 - 1216
    checkListingsToFind();
2 - 1217
}
1218
 
32 - 1219
const findShopifyListing = stopcock(function request(itemId, title) {
1220
	var xhttp = new XMLHttpRequest();
1221
	xhttp.onreadystatechange = function() {
1222
		if (this.readyState == 4 && this.status == 200) {
1223
			var json = JSON.parse(this.responseText);
1224
			if (json.count !== undefined) {
1225
			    var x = document.getElementById('Shopify' + itemId);
1226
			    x.innerHTML = (json.count == 0 ? 'No' : 'Yes');
1227
			}
1228
 
1229
            return Promise.resolve(true);
1230
		}
1231
	};
1232
 
53 - 1233
    title = cleanTitleForShopifySearch(title);
1234
 
32 - 1235
	xhttp.open("GET", configProxyUrl, true);
40 - 1236
	xhttp.setRequestHeader("X-Proxy-Url", configShopifyUrl + configShopifyProductCountUrl + '?title=' + title);
32 - 1237
	xhttp.setRequestHeader("X-LEAVE-ENCODED", "1");
1238
	xhttp.send();
1239
});
1240
 
2 - 1241
function fillSorterListing(itemIdList) {
1242
    var i;
1243
    var xml;
1244
 
1245
    var xw = new XMLWriter('UTF-8', '1.0');
1246
    var xhr = new XMLHttpRequest();
1247
 
1248
    if (!createSorterFillXMLSearch(xw, xhr, 'GetMultipleItems', itemIdList)) {
1249
        return;
1250
    }
1251
 
1252
    xml = xw.flush();
1253
    xw.close();
1254
 
1255
    xhr.onload = function() {
16 - 1256
        var jsonObj = XMLparse(xhr.responseXML, false);
1257
        var obj = jsonObj.GetMultipleItemsResponse;
2 - 1258
        var returnCode = obj.Ack;
19 - 1259
        var str;
2 - 1260
 
1261
        var x = document.getElementById("results");
1262
 
1263
        if (returnCode == 'Success') {
1264
            for (i = 0; i < obj.Item.length; i++) {
1265
                var itemId = getJsonValue(obj.Item[i].ItemID);
1266
                document.getElementById("Views" + itemId).innerHTML = getJsonValue(obj.Item[i].HitCount);
1267
                document.getElementById("Category" + itemId).innerHTML = getJsonValue(obj.Item[i].PrimaryCategoryName);
6 - 1268
            }
2 - 1269
        } else {
1270
            x.className += " process-errors";
30 - 1271
            str = '<p class="text-danger"><strong>' + returnCode + ':</strong></p>';
2 - 1272
 
6 - 1273
            var errors = getJsonArray(obj.Errors);
19 - 1274
            str += "<p>";
6 - 1275
            for (i = 0; i < errors.length; i++) {
19 - 1276
                str += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
2 - 1277
            }
1278
            x.innerHTML += "</p>";
19 - 1279
 
1280
            x.innerHTML += str;
2 - 1281
        }
1282
 
1283
        --listingsToFill;
1284
        ++listingsFilled;
1285
        updateProgressBar(maxListingsToFill, listingsFilled);
1286
    };
1287
 
1288
    xhr.send(xml);
1289
}
1290
 
1291
function createSorterFillXMLSearch(xw, xhr, callname, itemIdList) {
1292
    var i;
6 - 1293
 
2 - 1294
    xw.writeStartDocument();
1295
    xw.writeStartElement(callname + "Request");
1296
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
1297
 
1298
    for (i = 0; i < itemIdList.length; i++) {
1299
        xw.writeElementString('ItemID', itemIdList[i]);
1300
    }
1301
 
1302
    xw.writeElementString('ErrorLanguage', 'en_US');
1303
    xw.writeElementString('Version', configeBayShoppingVersion);
1304
    xw.writeElementString('WarningLevel', configWarningLevel);
1305
 
1306
    xw.writeEndElement(); /* xmlrequest */
1307
    xw.writeEndDocument();
1308
 
1309
    xhr.open('POST', configProxyUrl, true);
1310
    xhr.setRequestHeader('Content-Type', 'text/xml');
1311
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
1312
    xhr.setRequestHeader('X-EBAY-API-VERSION', configeBayShoppingVersion);
1313
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', callname);
1314
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
1315
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
1316
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
1317
    xhr.setRequestHeader('X-EBAY-API-REQUEST-ENCODING', 'XML');
1318
    xhr.setRequestHeader('X-Proxy-URL', configeBayShopping);
1319
 
1320
    return true;
1321
}
1322
 
1323
    </script>
17 - 1324
    <script>includeHTML();</script>
2 - 1325
 
1326
</body>
1327
</html>