Subversion Repositories munaweb

Rev

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

Rev Author Line No. Line
2 - 1
//
2
// Globals
3
//
4
var configServiceEndpoint = 'https://api.ebay.com/ws/api.dll';
5
var configRuName = 'Uwe_Jacobs-UweJacob-MUNATr-jwkrg';
6
var configWarningLevel = "Low";
7
var configAppid = 'UweJacob-MUNATrad-PRD-d132041a0-85284729';
8
var configDevid = '00fd6fda-3751-4095-b733-3899b20431ad';
9
var configCertid = 'PRD-132041a078aa-1ee6-4300-9454-6c5b';
10
var configSigninUrl = 'https://signin.ebay.com/ws/eBayISAPI.dll';
3 - 11
var configeBayLoginUrl = 'https://ujsoftware.linkpc.net/ebay/ebayLogin.php';
12
var configOauthTokenUrl = 'https://api.ebay.com/identity/v1/oauth2/token';
2 - 13
var configDiscogsToken = 'zFvVdCdHTtQnDHCxEFTJiBhalyHFUsjdyFPCjbqP&ty';
14
var configXmlRequestEntriesPerPage = '100';
15
var configUSPSUserId = '275MUNAT7574';
16
var configLinkedPayPal = 'paypal@munatrading.com';
17
var configListingUrl = 'https://ujsoftware.linkpc.net/ebay/listings/';
18
var configDiscogsBaseUrl = 'https://www.discogs.com';
36 - 19
var configDiscogsApiBaseUrl = 'https://api.discogs.com';
20
var configDiscogsApiUrl = configDiscogsApiBaseUrl + '/database/search';
21
var configDiscogsPriceUrl = configDiscogsApiBaseUrl + '/marketplace/price_suggestions/';
2 - 22
var configDiscogsUserAgent = 'MUNA_Trading/1.0 +http://www.munatrading.com';
23
var configProxyUrl = 'https://ujsoftware.linkpc.net/proxy.php';
7 - 24
var configShopifyApiKey = '41f0d3bf0e8e114496b198938996d9d8';
25
var configShopifyPassword = 'f169694c488f45ccf187c92676765889';
26
var configShopifyUrl = 'https://' + configShopifyApiKey + ':' + configShopifyPassword + '@muna-trading.myshopify.com/admin/';
2 - 27
var configShopifyProductsUrl = 'products.json';
32 - 28
var configShopifyProductCountUrl = 'products/count.json';
2 - 29
var configShopifyOrdersUrl = 'orders.json';
30
var configShopifyTransactionsUrl1 = 'orders/';
31
var configShopifyTransactionsUrl2 = '/transactions.json';
32
var configShopifyOrderLimit = 250;
33
var configeBayTradingVersion = '1085';
34
var configeBayMarketingVersion = 'v1.4.0';
35
var configeBayFinding = 'https://svcs.ebay.com/services/search/FindingService/v1';
36
var configeBayFindingVersion = '1.13.0';
17 - 37
var configeBayPostOrder = 'https://api.ebay.com/post-order/v2';
2 - 38
var configeBayShopping = 'http://open.api.ebay.com/shopping';
39
var configeBayShoppingVersion = '1063';
40
var configeBayAdCampaign = 'https://api.ebay.com/sell/marketing/v1/ad_campaign/';
41
var configUPSAccessKey = 'DD53C5F37DF74D28';
42
var configUPSUrl = 'https://onlinetools.ups.com/ups.app/xml/Track';
43
var configUPSUsername = 'muna_trading';
44
var configUPSPassword = 'ZX83tbf!w7';
45
var configUSPSUrl = 'https://secure.shippingapis.com/ShippingApi.dll';
46
var configGoogleMapsKey = 'AIzaSyBeSjH1CypAnYMYy_LIHqe8ngaAC6O-FWE';
47
 
48
var configTaxStateId = 'VA';
49
var configTaxRate = '6.000';
50
var configZip = '20120';
51
var configGetRecommendations = true;
52
var configDoesNotApply = 'Does not apply';
53
var configDefaultCountry = 'United States';
54
var configDefaultLanguage = 'English';
55
var configImage1Extension = ' Front.jpg';
56
var configImage2Extension = ' Rear.jpg';
57
var configdescriptionImageExtension = ' Front and Rear - small.jpg';
58
var configeBaySellerName = 'muna_trading';
59
var configAutoAcceptPrice = 0.85000;
60
var configMinBestOfferPrice = 0.65000;
61
 
62
var sessionId = '';
63
var eBayAuthToken = '';
64
var eBayAuthExpiration = '';
65
var eBayAuthTokenFlag = false;
66
 
67
//
68
// Prototypes
69
//
70
String.prototype.hashCode = function() {
71
	var hash = 0,
72
		i, chr;
73
	if (this.length === 0) return hash;
74
	for (i = 0; i < this.length; i++) {
75
		chr = this.charCodeAt(i);
76
		hash = ((hash << 5) - hash) + chr;
77
		hash |= 0; // Convert to 32bit integer
78
	}
79
 
80
	return (hash < 0 ? (hash * -1) : hash);
81
};
82
 
83
Number.prototype.pad = function(size) {
84
	var s = String(this);
85
	while (s.length < (size || 2)) {
86
		s = "0" + s;
87
	}
88
	return s;
89
};
90
 
91
String.prototype.toProperCase = function () {
92
    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
93
};
94
 
95
Date.prototype.yyyymmdd = function() {
96
	var mm = this.getMonth() + 1; // getMonth() is zero-based
97
	var dd = this.getDate();
98
 
99
	return [this.getFullYear(), (mm > 9 ? '' : '0') + mm, (dd > 9 ? '' : '0') + dd].join('-');
100
};
101
 
102
String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
103
 
19 - 104
String.prototype.b64encode = function() {
105
    return btoa(unescape(encodeURIComponent(this)));
17 - 106
};
19 - 107
String.prototype.b64decode = function() {
108
    return decodeURIComponent(escape(atob(this)));
17 - 109
};
110
 
2 - 111
//
112
// Functions
113
//
6 - 114
function getJsonArray(val) {
115
    if (val === undefined) {
116
        return ([]);
117
    }
118
 
119
    return(val instanceof Array ? val : [val]);
120
}
121
 
2 - 122
function round(value, decimals) {
123
  return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
124
}
125
 
126
// Polyfill for Internet Explorer
127
Math.trunc = Math.trunc || function(x) {
128
	var n = x - x % 1;
129
	return n === 0 && (x < 0 || (x === 0 && (1 / x !== 1 / 0))) ? -0 : n;
130
};
131
 
132
function tableCell(str) {
133
	return ('<td>' + str + '</td>');
134
}
135
 
136
function tableCellHidden(str) {
137
	return ('<td class="w3-hide">' + str + '</td>');
138
}
139
 
140
function tableHeader(str) {
141
	return ('<th>' + str + '</th>');
142
}
143
 
144
function tableCellLabel(str) {
145
	return ('<td id="' + str + '"></td>');
146
}
147
 
148
function tableCellLabelHidden(str, val) {
149
	return ('<td id="' + str + '" class="w3-hide">' + val + '</td>');
150
}
151
 
152
function tableCellAndLabel(str, label) {
153
	return ('<td id="' + label + '">' + str + '</td>');
154
}
155
 
156
function tableHeaderHidden(str) {
157
	return ('<th class="w3-hide">' + str + '</th>');
158
}
159
 
6 - 160
function tableHeaderCheckbox() {
2 - 161
	return ('<th class="sorter-checkbox filter-false"><input type="checkbox"></th>');
162
}
163
 
164
function tableCellCheckbox() {
165
	return ('<td><input type="checkbox"></td>');
166
}
167
 
168
function escapeHtml(unsafe) {
169
	return unsafe
170
		.replace(/&/g, "&amp;")
171
		.replace(/</g, "&lt;")
172
		.replace(/>/g, "&gt;")
173
		.replace(/"/g, "&quot;")
174
		.replace(/'/g, "&#039;");
175
}
176
 
177
function pad(num, size) {
178
    var s = num+"";
179
    while (s.length < size) s = "0" + s;
180
    return s;
181
}
182
 
183
function isset () {
184
  var a = arguments;
185
  var l = a.length;
186
  var i = 0;
187
  var undef;
188
 
189
  if (l === 0) {
190
    return false;
191
  }
192
 
193
  while (i !== l) {
194
    if (a[i] === undef || a[i] === null) {
195
      return false;
196
    }
197
    i++;
198
  }
199
 
200
  return true;
201
}
202
 
203
function getJsonValue(obj){
204
  return (isset(obj) ? obj : "");
205
}
206
 
207
function writeCookie() {
208
	document.cookie = 'eBayAuthToken=' + encodeURIComponent(eBayAuthToken) + ';expires=' + eBayAuthExpiration + ';';
209
}
210
 
211
function readCookie() {
212
	var name = 'eBayAuthToken=';
213
	var decodedCookie = decodeURIComponent(document.cookie);
214
	var ca = decodedCookie.split(';');
215
	for (var i = 0; i < ca.length; i++) {
216
		var c = ca[i];
217
		while (c.charAt(0) == ' ') {
218
			c = c.substring(1);
219
		}
220
		if (c.indexOf(name) === 0) {
221
			return c.substring(name.length, c.length);
222
		}
223
	}
224
	return "";
225
}
226
 
227
function isNumeric(input) {
228
	return (input - 0) == input && ('' + input).trim().length > 0;
229
}
230
 
231
function getRadioValue(name) {
232
	var group = document.getElementsByName(name);
233
 
234
	for (var i = 0; i < group.length; i++) {
235
		if (group[i].checked) {
236
			return group[i].value;
237
		}
238
	}
239
 
240
	return '';
241
}
242
 
243
function removeDuplicateRows($table){
244
    function getVisibleRowText($row){
245
        return $row.find('td:visible').text().toLowerCase();
246
    }
247
 
248
    $table.find('tr').each(function(index, row){
249
        var $row = $(row);
250
 
251
        $row.nextAll('tr').each(function(index, next){
252
            var $next = $(next);
253
            if(getVisibleRowText($next) == getVisibleRowText($row)) {
254
                $next.remove();
255
            }
256
        });
257
    });
258
}
259
 
260
function isValidUpcOrEanCode(str) {
261
	if (!isNumeric(str)) {
262
		return(false);
263
	}
264
 
265
	if (str.length == 12) {
266
		return(isValidUpcCode(str));
267
	} else if (str.length == 13) {
268
		return(isValidEanCode(str) === 0 ? true : false);
269
	}
270
 
271
	return(false);
272
}
273
 
274
function isValidEanCode(ean) {
275
    var result = 0;
276
    for (var counter = (ean.length - 1); counter >= 0; counter--) {
277
        result = result + parseInt(ean.charAt(counter)) * (1 + (2 * (counter % 2)));
278
    }
279
 
280
    return (10 - (result % 10)) % 10;
281
}
282
 
283
function isValidUpcCode(upc) {
284
	if (!isNumeric(upc) || upc.length !== 12) {
285
		return false;
286
	}
287
 
6 - 288
	res = calculateUpcRes(upc.substr(0, 11));
289
 
290
	if (parseInt(upc.substr(11, 1)) != res) {
291
		return false;
292
	}
293
 
294
	return true;
295
}
296
 
297
function calculateUpcRes(upc) {
298
	var i;
299
 
300
	if (!isNumeric(upc) || upc.length !== 11) {
301
		return -1;
302
	}
303
 
2 - 304
	var sum = 0;
305
 
306
	for (i = 0; i < 11; i += 2) {
307
		sum += (parseInt(upc.substr(i, 1)) * 3);
308
	}
309
 
310
	for (i = 1; i < 11; i += 2) {
311
		sum += parseInt(upc.substr(i, 1));
312
	}
313
 
314
	var res = sum % 10;
315
 
316
	if (res > 0) {
317
		res = 10 - res;
318
	}
319
 
6 - 320
	return res;
2 - 321
}
322
 
6 - 323
 
2 - 324
function isValidISBNCode(isbn) {
325
	var sum, weight, digit, check, i;
326
 
327
	if (isbn.length == 13) {
328
		sum = 0;
329
		for (i = 0; i < 12; i++) {
330
			digit = parseInt(isbn[i]);
331
			if (i % 2 == 1) {
332
				sum += 3 * digit;
333
			} else {
334
				sum += digit;
335
			}
336
		}
337
		check = (10 - (sum % 10)) % 10;
338
		return (check == isbn[isbn.length - 1]);
339
	}
340
 
341
	if (isbn.length == 10) {
342
		weight = 10;
343
		sum = 0;
344
		for (i = 0; i < 9; i++) {
345
			digit = parseInt(isbn[i]);
346
			sum += weight * digit;
347
			weight--;
348
		}
349
		check = 11 - (sum % 11);
350
		if (check == 10) {
351
			check = 'X';
352
		}
353
		return (check == isbn[isbn.length - 1].toUpperCase());
354
	}
355
 
356
	return false;
357
}
358
 
359
function escapeXml(unsafe) {
360
	return unsafe.replace(/[<>&'"]/g, function(c) {
361
		switch (c) {
362
			case '<':
363
				return '&lt;';
364
			case '>':
365
				return '&gt;';
366
			case '&':
367
				return '&amp;';
368
			case '\'':
369
				return '&apos;';
370
			case '"':
371
				return '&quot;';
372
		}
373
	});
374
}
375
 
376
 
377
/*
378
/ eBay Login
379
*/
380
function eBayLogin() {
381
	if (eBayAuthTokenFlag === false) {
382
		getSessionID();
383
	}
384
}
385
 
386
function getSessionID() {
387
	// Expects function requireNewLogin
388
	// Expects <div id="results">
389
 
390
	var i;
391
	var xw = new XMLWriter('UTF-8', '1.0');
392
 
393
	xw.writeStartDocument();
394
	xw.writeStartElement('GetSessionIDRequest');
395
	xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
396
	xw.writeElementString('RuName', configRuName);
397
	xw.writeEndElement(); /* GetSessionIDRequest */
398
	xw.writeEndDocument();
399
 
400
	var my_xml = xw.flush();
401
	xw.close();
402
 
403
	var xhr = new XMLHttpRequest();
404
	xhr.open('POST', configProxyUrl, true);
405
	xhr.setRequestHeader('Content-Type', 'text/xml');
406
	xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
407
	xhr.setRequestHeader('X-EBAY-API-DEV-NAME', configDevid);
408
	xhr.setRequestHeader('X-EBAY-API-CERT-NAME', configCertid);
3 - 409
	xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
2 - 410
	xhr.setRequestHeader('X-EBAY-API-CALL-NAME', 'GetSessionID');
411
	xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
412
	xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);
413
 
414
	xhr.onload = function() {
5 - 415
        var xmlDoc = xhr.responseXML;
416
		var returnCode = xmlDoc.getElementsByTagName("Ack")[0].childNodes[0].nodeValue;
2 - 417
 
418
		if (returnCode == 'Success') {
5 - 419
			sessionId = xmlDoc.getElementsByTagName("SessionID")[0].childNodes[0].nodeValue;
2 - 420
 
421
			var win = window.open(configSigninUrl + '?SignIn&RuName=' + configRuName + '&SessID=' + sessionId, "eBay Login", "");
422
			var pollTimer = window.setInterval(function() {
423
				if (win.closed !== false) {
424
					window.clearInterval(pollTimer);
425
					getToken();
3 - 426
        		}
2 - 427
			}, 200);
428
		} else {
429
			requireNewLogin();
6 - 430
 
2 - 431
			var x = document.getElementById("results");
432
			if (x.className.indexOf("w3-show") == -1) {
433
				x.className += " w3-show";
434
			}
435
 
436
			x.innerHTML = "<p><strong>" + returnCode + ":</strong></p>";
5 - 437
			var errors = xmlDoc.getElementsByTagName("Errors");
2 - 438
			x.innerHTML += "<p>";
5 - 439
			for (i = 0; i < errors.length; i++) {
440
				x.innerHTML += errors[0].getElementsByTagName("SeverityCode")[0].innerHTML + " (" + errors[0].getElementsByTagName("ErrorCode")[0].innerHTML + "): " + escapeHtml(errors[0].getElementsByTagName("LongMessage")[0].innerHTML) + "<br/>";
2 - 441
			}
442
			x.innerHTML += "</p>";
443
		}
444
	};
445
 
446
	xhr.send(my_xml);
447
}
448
 
449
function getToken() {
450
	var x;
451
	var i;
452
	var xw = new XMLWriter('UTF-8', '1.0');
453
 
454
	xw.writeStartDocument();
455
	xw.writeStartElement('FetchTokenRequest');
456
	xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
457
	xw.writeElementString('SessionID', sessionId);
458
	xw.writeEndElement(); /* FetchTokenRequest */
459
	xw.writeEndDocument();
460
 
461
	var my_xml = xw.flush();
462
	xw.close();
463
 
464
	var xhr = new XMLHttpRequest();
465
	xhr.open('POST', configProxyUrl, true);
466
	xhr.setRequestHeader('Content-Type', 'text/xml');
467
	xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
468
	xhr.setRequestHeader('X-EBAY-API-DEV-NAME', configDevid);
469
	xhr.setRequestHeader('X-EBAY-API-CERT-NAME', configCertid);
3 - 470
	xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
2 - 471
	xhr.setRequestHeader('X-EBAY-API-CALL-NAME', 'FetchToken');
472
	xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
473
	xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);
474
 
475
	xhr.onload = function() {
5 - 476
        var xmlDoc = xhr.responseXML;
477
		var returnCode = xmlDoc.getElementsByTagName("Ack")[0].childNodes[0].nodeValue;
2 - 478
 
479
		if (returnCode == 'Success') {
5 - 480
			eBayAuthToken = xmlDoc.getElementsByTagName("eBayAuthToken")[0].childNodes[0].nodeValue;
481
			eBayAuthExpiration = new Date(xmlDoc.getElementsByTagName("HardExpirationTime")[0].childNodes[0].nodeValue).toUTCString();
2 - 482
			writeCookie();
483
			connected();
484
		} else {
485
			requireNewLogin();
486
 
487
			x = document.getElementById("results");
488
			if (x.className.indexOf("w3-show") == -1) {
489
				x.className += " w3-show";
490
			}
491
 
492
			x.innerHTML = "<p><strong>" + returnCode + ":</strong></p>";
493
 
5 - 494
			x.innerHTML = "<p><strong>" + returnCode + ":</strong></p>";
495
			var errors = xmlDoc.getElementsByTagName("Errors");
2 - 496
			x.innerHTML += "<p>";
5 - 497
			for (i = 0; i < errors.length; i++) {
498
				x.innerHTML += errors[0].getElementsByTagName("SeverityCode")[0].innerHTML + " (" + errors[0].getElementsByTagName("ErrorCode")[0].innerHTML + "): " + escapeHtml(errors[0].getElementsByTagName("LongMessage")[0].innerHTML) + "<br/>";
2 - 499
			}
500
			x.innerHTML += "</p>";
501
		}
502
	};
503
 
504
	xhr.send(my_xml);
505
}
506
 
3 - 507
function getUrlParameter(win, name) {
508
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
509
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
510
    var results = regex.exec(win.location.search);
511
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
6 - 512
}
3 - 513
 
2 - 514
function feebackStarImage(score) {
515
	if (score > 999999) {
516
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootSlvr_25x25.gif" alt="Silver shooting star">';
517
	} else if (score > 499999) {
518
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootGrn_25x25.gif" alt="Green shooting star">';
519
	} else if (score > 99999) {
520
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootRed_25x25.gif" alt="Red shooting star">';
521
	} else if (score > 49999) {
522
		return '<img src="https://ir.ebaystatic.com/pictures/aw/icon/iconShootPrpl_25x25.gif" alt="Purple shooting star">';
523
	} else if (score > 24999) {
524
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootTeal_25x25.gif" alt="Turquoise shooting star">';
525
	} else if (score > 9999) {
526
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootYllw_25x25.gif" alt="Yellow shooting star">';
527
	} else if (score > 4999) {
528
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconGreenStar_25x25.gif" alt="Green star">';
529
	} else if (score > 999) {
530
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconRedStar_25x25.gif" alt="Red star">';
531
	} else if (score > 499) {
532
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconPurpleStar_25x25.gif" alt="Purple star">';
533
	} else if (score > 99) {
534
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconTealStar_25x25.gif" alt="Turquoise star">';
535
	} else if (score > 49) {
536
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconBlueStar_25x25.gif" alt="Blue star">';
537
	} else if (score > 9) {
538
		return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconYellowStar_25x25.gif" alt="Yellow star">';
539
	}
540
	return '';
541
}
542
 
36 - 543
function exportTableToCSV(tablename, filename, utf8Flag = false) {
544
    var csv = [];
2 - 545
    var rows = document.getElementById(tablename).querySelectorAll("table tr");
6 - 546
    var dq = '"';
547
    var nq = "'";
36 - 548
 
549
    if (utf8Flag) {
550
        csv.push("\uFEFF");
551
    }
2 - 552
 
553
    for (var i = 0; i < rows.length; i++) {
554
        var row = [], cols = rows[i].querySelectorAll("td, th");
555
 
556
        for (var j = 0; j < cols.length; j++) {
6 - 557
            if (cols[j].className.indexOf("w3-hide") == -1) {
558
                if (isNaN(cols[j].innerText)) {
559
                    row.push(dq + cols[j].innerText.replace(/"/g, '""') + dq);
560
                } else {
10 - 561
                    if (cols[j].innerText.includes(".")) {
562
                        row.push(cols[j].innerText);
563
                    } else {
14 - 564
                        row.push((cols[j].innerText.length > 7 ? nq : "") + cols[j].innerText);
10 - 565
                    }
6 - 566
                }
567
            }
2 - 568
        }
569
 
570
        csv.push(row.join(","));
571
    }
572
 
573
    // Download CSV file
36 - 574
    downloadCSV(csv.join("\n"), filename, utf8Flag);
2 - 575
}
576
 
36 - 577
function downloadCSV(csv, filename, utf8Flag = false) {
2 - 578
    var csvFile;
579
    var downloadLink;
580
 
581
    // CSV file
36 - 582
    if (utf8Flag) {
583
        csvFile = new Blob([csv], {type: "text/csv;charset=utf-8"});
584
    } else {
585
        csvFile = new Blob([csv], {type: "text/csv"});
586
    }
2 - 587
 
588
    // Download link
589
    downloadLink = document.createElement("a");
590
 
591
    // File name
592
    downloadLink.download = filename;
593
 
594
    // Create a link to the file
595
    downloadLink.href = window.URL.createObjectURL(csvFile);
596
 
597
    // Hide download link
598
    downloadLink.style.display = "none";
599
 
600
    // Add the link to DOM
601
    document.body.appendChild(downloadLink);
602
 
603
    // Click download link
604
    downloadLink.click();
605
}
606
 
607
/* Progress Bar HTML
608
<div id="progressBarDiv" class="w3-container w3-padding w3-margin w3-card-4 w3-hide">
609
	<h2 id="progressBarHeader"></h2>
610
  <div class="w3-light-grey">
611
      <div id="progressBar" class="w3-container w3-green w3-center" style="width:0%">0%</div>
612
  </div>
613
</div>
614
*/
615
function initProgressBar(title) {
616
  	var elem = document.getElementById("progressBar");
617
	  elem.style.width = 0 + '%';
618
  	elem.innerHTML = 0 + '%';
619
 
620
  	elem = document.getElementById("progressBarDiv");
621
	  if (elem.className.indexOf("w3-show") == -1) {
622
		    elem.className += " w3-show";
623
  	}
624
 
625
	  elem = document.getElementById("progressBarHeader");
626
  	elem.innerHTML = title;
627
}
628
 
629
function updateProgressBar(maximum, current) {
10 - 630
	var elem = document.getElementById("progressBar");
631
	var width = (current / maximum) * 100;
632
	elem.style.width = width + '%';
633
	elem.innerHTML = width.toFixed(0) + '%';
2 - 634
}
635
 
636
function endProgressBar() {
637
    var elem = document.getElementById("progressBarDiv");
638
		elem.className = elem.className.replace(" w3-show", "");
639
}
640
 
10 - 641
// Progress Bar Modal
642
/*
643
<div class="modal" id="progressBarDiv">
644
  <div class="modal-dialog">
645
    <div class="modal-content">
646
      <div class="modal-header">
647
        <h4 id="progressBarHeader"></h4>
648
      </div>
649
      <div class="modal-body">
650
        <div class="progress">
651
          <div id="progressBar" class="progress-bar" style="width:0%">0%</div>
652
        </div>
653
      </div>
654
    </div>
655
  </div>
656
</div>
657
*/
658
function initProgressBarModal(title) {
659
  	var elem = document.getElementById("progressBar");
660
    elem.style.width = '0%';
661
  	elem.innerHTML = '0%';
662
 
663
    elem = document.getElementById("progressBarHeader");
664
  	elem.innerHTML = title;
19 - 665
 
10 - 666
  	$("#progressBarDiv").modal("show");
667
}
668
 
669
function endProgressBarModal() {
670
  	$("#progressBarDiv").modal("hide");
671
}
672
 
673
function extractTextContent(s) {
674
  var span = document.createElement('span');
675
  span.innerHTML = s;
676
  return span.textContent || span.innerText;
677
}
678
 
6 - 679
// xxxxx WIP
2 - 680
function sortHTMLTable(table, column = 0, type = 's', hasFooter = false) {
681
  var rows, switching, i, x, y, shouldSwitch;
682
  var nonData = (hasFooter ? 2 : 1);
683
  table = document.getElementById(table);
684
  switching = true;
685
  /* Make a loop that will continue until
686
  no switching has been done: */
687
  while (switching) {
688
    // Start by saying: no switching is done:
689
    switching = false;
690
    rows = table.rows;
691
    /* Loop through all table rows (except the
692
    first, which contains table headers): */
693
    for (i = 1; i < (rows.length - nonData); i++) {
694
      // Start by saying there should be no switching:
695
      shouldSwitch = false;
696
      /* Get the two elements you want to compare,
697
      one from current row and one from the next: */
698
      x = rows[i].getElementsByTagName("TD")[column];
699
      y = rows[i + 1].getElementsByTagName("TD")[column];
700
      // Check if the two rows should switch place:
701
      if (type == 'i') { // integer
702
        if (Number(x.innerHTML) > Number(y.innerHTML)) {
703
          // If so, mark as a switch and break the loop:
704
          shouldSwitch = true;
705
          break;
706
        }
707
      } else if (type == 'f') { // float
708
        if (Number(x.innerHTML).toFixed(2) > Number(y.innerHTML).toFixed(2)) {
709
          // If so, mark as a switch and break the loop:
710
          shouldSwitch = true;
711
          break;
712
        }
713
      } else if (type == 'c') { // currency
714
        if (Number(x.innerHTML.substr(1)).toFixed(2) > Number(y.innerHTML.substr(1)).toFixed(2)) {
715
          // If so, mark as a switch and break the loop:
716
          shouldSwitch = true;
717
          break;
718
        }
719
      } else if ('d') { // date
720
        if (Date(x.innerHTML) > Date(y.innerHTML)) {
721
          // If so, mark as a switch and break the loop:
722
          shouldSwitch = true;
723
          break;
724
        }
725
      } else { // string
726
        if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
727
          // If so, mark as a switch and break the loop:
728
          shouldSwitch = true;
729
          break;
730
        }
731
      }
732
    }
733
    if (shouldSwitch) {
734
      /* If a switch has been marked, make the switch
735
      and mark that a switch has been done: */
736
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
737
      switching = true;
738
    }
739
  }
7 - 740
}
741
 
742
// Update table sorter counters after processed deleting rows
743
// <p>Showing <span id="filtered-rows">0</span> of <span id="total-rows">0</span> / <span id="selected-rows">0</span> selected.</p>
744
function tableSorterUpdateCounters(tableName) {
745
    $("#" + tableName).trigger("update").trigger("appendCache").trigger("applyWidgets").trigger('search', false);
19 - 746
 
7 - 747
    var rowCount = $('#' + tableName + ' tbody tr').length;
748
    var filteredCount = rowCount - $('#' + tableName + ' tbody tr:hidden').length;
749
    var checkedCount = $('#' + tableName + ' tbody .checked').length;
19 - 750
 
7 - 751
    $('#total-rows').html(rowCount);
752
    $('#filtered-rows').html(filteredCount);
753
    $('#selected-rows').html(checkedCount);
754
    $("#" + tableName).find('thead input[type=checkbox]').prop('checked', false);
755
    $("#" + tableName).find('thead input[type=checkbox]').prop('indeterminate', false);
10 - 756
}
757
 
758
// flattens an object (recursively!), similarly to Array#flatten
759
// e.g. flatten({ a: { b: { c: "hello!" } } }); // => "hello!"
760
function flatten(object) {
761
  var check = _.isPlainObject(object) && _.size(object) === 1;
762
  return check ? flatten(_.values(object)[0]) : object;
763
}
764
 
765
function XMLparse(xml, flattenFlag = true) {
766
  var data = {};
767
 
17 - 768
  if (xml === null) {
769
    return data;
770
  }
19 - 771
 
10 - 772
  var isText = xml.nodeType === 3,
773
      isElement = xml.nodeType === 1,
774
      body = xml.textContent && xml.textContent.trim(),
775
      hasChildren = xml.children && xml.children.length,
776
      hasAttributes = xml.attributes && xml.attributes.length;
777
 
778
  // if it's text just return it
779
  if (isText) { return xml.nodeValue.trim(); }
780
 
781
  // if it doesn't have any children or attributes, just return the contents
782
  if (!hasChildren && !hasAttributes) { return body; }
783
 
784
  // if it doesn't have children but _does_ have body content, we'll use that
785
  if (!hasChildren && body.length) { data.text = body; }
786
 
787
  // if it's an element with attributes, add them to data.attributes
788
  if (isElement && hasAttributes) {
789
    data.attributes = _.reduce(xml.attributes, function(obj, name, id) {
790
      var attr = xml.attributes.item(id);
791
      obj[attr.name] = attr.value;
792
      return obj;
793
    }, {});
794
  }
795
 
796
  // recursively call #XMLparse over children, adding results to data
797
  _.each(xml.children, function(child) {
798
    var name = child.nodeName;
799
 
800
    // if we've not come across a child with this nodeType, add it as an object
801
    // and return here
802
    if (!_.has(data, name)) {
803
      data[name] = XMLparse(child, flattenFlag);
804
      return;
805
    }
806
 
807
    // if we've encountered a second instance of the same nodeType, make our
808
    // representation of it an array
809
    if (!_.isArray(data[name])) { data[name] = [data[name]]; }
810
 
811
    // and finally, append the new child
812
    data[name].push(XMLparse(child, flattenFlag));
813
  });
814
 
815
   // if we can, let's fold some attributes into the body
816
   _.each(data.attributes, function(value, key) {
817
   if (data[key] != null) { return; }
818
     data[key] = value;
819
     delete data.attributes[key];
820
   });
821
 
822
  // if data.attributes is now empty, get rid of it
823
  if (_.isEmpty(data.attributes)) { delete data.attributes; }
824
 
825
  // simplify to reduce number of final leaf nodes and return
826
  return (flattenFlag ? flatten(data) : data);
17 - 827
}
828
 
829
/* HTML include */
830
function includeHTML() {
831
    var z, i, elmnt, file, xhttp;
832
    /* Loop through a collection of all HTML elements: */
833
    z = document.getElementsByTagName("*");
834
    for (i = 0; i < z.length; i++) {
835
        elmnt = z[i];
836
        /* search for elements with a certain atrribute:*/
837
        file = elmnt.getAttribute("w3-include-html");
838
        if (file) {
839
            /* Make an HTTP request using the attribute value as the file name: */
840
            xhttp = new XMLHttpRequest();
841
            xhttp.onreadystatechange = function() {
842
                if (this.readyState == 4) {
843
                    if (this.status == 200) {
844
                        elmnt.innerHTML = this.responseText;
845
                    }
846
                    if (this.status == 404) {
847
                        elmnt.innerHTML = "<p>Page not found.</p>";
848
                    }
849
                    /* Remove the attribute, and call this function once more: */
850
                    elmnt.removeAttribute("w3-include-html");
851
                    includeHTML();
852
                }
853
            };
854
            xhttp.open("GET", file, true);
855
            xhttp.send();
856
            /* Exit the function: */
857
            return;
858
        }
859
    }
860
}