Subversion Repositories munaweb

Rev

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

//
// Authorization
//
//bugbug//var configRuName = 'XxXRuNamexxxxxxxxxxxxxxxxxxxxxxx'; // must have the same length as the actual token
var configRuName = 'Uwe_Jacobs-UweJacob-MUNATr-jwkrg'
var configAppid = 'XxXAppid';
var configDevid = 'XxXDevid';
var configCertid = 'XxXCertid';
var configDiscogsToken = 'XxXDiscogsToken';
var configUSPSUserId = 'XxXUSPSUserId';
var configShopifyApiKey = 'XxXShopifyApiKey';
var configShopifyPassword = 'XxXShopifyPassword';
var configUPSAccessKey = 'XxXUPSAccessKey';
var configUPSUsername = 'XxXUPSUsername';
var configUPSPassword = 'XxXUPSPassword';

//
// Globals
//
var configServiceEndpoint = 'https://api.ebay.com/ws/api.dll';
var configWarningLevel = "Low";
var configSigninUrl = 'https://signin.ebay.com/ws/eBayISAPI.dll';
var configeBayLoginUrl = 'https://www.munatrading.linkpc.net/ebay/ebayLogin.php';
var configOauthTokenUrl = 'https://api.ebay.com/identity/v1/oauth2/token';
var configXmlRequestEntriesPerPage = '100';
var configLinkedPayPal = 'paypal@munatrading.com';
var configListingUrl = 'https://www.munatrading.linkpc.net/ebay/listings/';
var configDiscogsBaseUrl = 'https://www.discogs.com';
var configDiscogsApiBaseUrl = 'https://api.discogs.com';
var configDiscogsApiUrl = configDiscogsApiBaseUrl + '/database/search';
var configDiscogsPriceUrl = configDiscogsApiBaseUrl + '/marketplace/price_suggestions/';
var configDiscogsUserAgent = 'MUNA_Trading/1.0 +http://www.munatrading.com';
var configProxyUrl = 'https://www.munatrading.linkpc.net/proxy.php';
var configShopifyUrl = 'https://' + configShopifyApiKey + ':' + configShopifyPassword + '@muna-trading.myshopify.com/admin/';
var configShopifyProductsUrl = 'products.json';
var configShopifyProductCountUrl = 'products/count.json';
var configShopifyOrdersUrl = 'orders.json';
var configShopifyTransactionsUrl1 = 'orders/';
var configShopifyTransactionsUrl2 = '/transactions.json';
var configShopifyOrderLimit = 250;
var configeBayTradingVersion = '1085';
var configeBayMarketingVersion = 'v1.4.0';
var configeBayFinding = 'https://svcs.ebay.com/services/search/FindingService/v1';
var configeBayFindingVersion = '1.13.0';
var configeBayPostOrder = 'https://api.ebay.com/post-order/v2';
var configeBayShopping = 'http://open.api.ebay.com/shopping';
var configeBayShoppingVersion = '1063';
var configeBayAdCampaign = 'https://api.ebay.com/sell/marketing/v1/ad_campaign/';
var configUPSUrl = 'https://onlinetools.ups.com/ups.app/xml/Track';
var configUSPSUrl = 'https://secure.shippingapis.com/ShippingApi.dll';
var configGoogleMapsKey = 'AIzaSyBeSjH1CypAnYMYy_LIHqe8ngaAC6O-FWE';

var configTaxStateId = 'VA';
var configTaxRate = '6.000';
var configZip = '20120';
var configGetRecommendations = true;
var configDoesNotApply = 'Does not apply';
var configDefaultCountry = 'United States';
var configDefaultLanguage = 'English';
var configImage1Extension = ' Front.jpg';
var configImage2Extension = ' Rear.jpg';
var configdescriptionImageExtension = ' Front and Rear - small.jpg';
var configeBaySellerName = 'muna_trading';
var configAutoAcceptPrice = 0.85000;
var configMinBestOfferPrice = 0.65000;

var sessionId = '';
var eBayAuthToken = '';
var eBayAuthExpiration = '';
var eBayAuthTokenFlag = false;

//
// Prototypes
//
String.prototype.hashCode = function() {
        var hash = 0,
                i, chr;
        if (this.length === 0) return hash;
        for (i = 0; i < this.length; i++) {
                chr = this.charCodeAt(i);
                hash = ((hash << 5) - hash) + chr;
                hash |= 0; // Convert to 32bit integer
        }

        return (hash < 0 ? (hash * -1) : hash);
};

Number.prototype.pad = function(size) {
        var s = String(this);
        while (s.length < (size || 2)) {
                s = "0" + s;
        }
        return s;
};

String.prototype.toProperCase = function () {
    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};

Date.prototype.yyyymmdd = function() {
        var mm = this.getMonth() + 1; // getMonth() is zero-based
        var dd = this.getDate();

        return [this.getFullYear(), (mm > 9 ? '' : '0') + mm, (dd > 9 ? '' : '0') + dd].join('-');
};

String.prototype.contains = function(it) { return this.indexOf(it) != -1; };

String.prototype.b64encode = function() {
    return btoa(unescape(encodeURIComponent(this)));
};
String.prototype.b64decode = function() {
    return decodeURIComponent(escape(atob(this)));
};

//
// Functions
//
function getJsonArray(val) {
    if (val === undefined) {
        return ([]);
    }

    return(val instanceof Array ? val : [val]);
}

function round(value, decimals) {
  return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}

// Polyfill for Internet Explorer
Math.trunc = Math.trunc || function(x) {
        var n = x - x % 1;
        return n === 0 && (x < 0 || (x === 0 && (1 / x !== 1 / 0))) ? -0 : n;
};

function tableCell(str) {
        return ('<td>' + str + '</td>');
}

function tableCellHidden(str) {
        return ('<td class="w3-hide">' + str + '</td>');
}

function tableHeader(str) {
        return ('<th>' + str + '</th>');
}

function tableCellLabel(str) {
        return ('<td id="' + str + '"></td>');
}

function tableCellLabelHidden(str, val) {
        return ('<td id="' + str + '" class="w3-hide">' + val + '</td>');
}

function tableCellAndLabel(str, label) {
        return ('<td id="' + label + '">' + str + '</td>');
}

function tableHeaderHidden(str) {
        return ('<th class="w3-hide">' + str + '</th>');
}

function tableHeaderCheckbox() {
        return ('<th class="sorter-checkbox filter-false"><input type="checkbox"></th>');
}

function tableCellCheckbox() {
        return ('<td><input type="checkbox"></td>');
}

function escapeHtml(unsafe) {
        return unsafe
                .replace(/&/g, "&amp;")
                .replace(/</g, "&lt;")
                .replace(/>/g, "&gt;")
                .replace(/"/g, "&quot;")
                .replace(/'/g, "&#039;");
}

function pad(num, size) {
    var s = num+"";
    while (s.length < size) s = "0" + s;
    return s;
}

function isset () {
  var a = arguments;
  var l = a.length;
  var i = 0;
  var undef;

  if (l === 0) {
    return false;
  }

  while (i !== l) {
    if (a[i] === undef || a[i] === null) {
      return false;
    }
    i++;
  }

  return true;
}

function getJsonValue(obj){
  return (isset(obj) ? obj : "");
}

function writeCookie() {
        document.cookie = 'eBayAuthToken=' + encodeURIComponent(eBayAuthToken) + ';expires=' + eBayAuthExpiration + ';';
}

function readCookie() {
        var name = 'eBayAuthToken=';
        var decodedCookie = decodeURIComponent(document.cookie);
        var ca = decodedCookie.split(';');
        for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') {
                        c = c.substring(1);
                }
                if (c.indexOf(name) === 0) {
                        return c.substring(name.length, c.length);
                }
        }
        return "";
}

function isNumeric(input) {
        return (input - 0) == input && ('' + input).trim().length > 0;
}

function getRadioValue(name) {
        var group = document.getElementsByName(name);

        for (var i = 0; i < group.length; i++) {
                if (group[i].checked) {
                        return group[i].value;
                }
        }

        return '';
}

function removeDuplicateRows($table){
    function getVisibleRowText($row){
        return $row.find('td:visible').text().toLowerCase();
    }

    $table.find('tr').each(function(index, row){
        var $row = $(row);

        $row.nextAll('tr').each(function(index, next){
            var $next = $(next);
            if(getVisibleRowText($next) == getVisibleRowText($row)) {
                $next.remove();
            }
        });
    });
}

function calculateGtin12Res(upc) {
        var i;

        if (!isNumeric(upc) || upc.length !== 11) {
                return -1;
        }

        var sum = 0;

        for (i = 0; i < 11; i += 2) {
                sum += (parseInt(upc.substr(i, 1)) * 3);
        }

        for (i = 1; i < 11; i += 2) {
                sum += parseInt(upc.substr(i, 1));
        }

        var res = sum % 10;

        if (res > 0) {
                res = 10 - res;
        }

        return res;
}


function isValidISBN10Code(isbn) {
        var sum, weight, digit, check, i;

        if (isbn.length == 10) {
                weight = 10;
                sum = 0;
                for (i = 0; i < 9; i++) {
                        digit = parseInt(isbn[i]);
                        sum += weight * digit;
                        weight--;
                }
                check = 11 - (sum % 11);
                if (check == 10) {
                        check = 'X';
                }
                return (check == isbn[isbn.length - 1].toUpperCase());
        }

        return false;
}

function escapeXml(unsafe) {
        return unsafe.replace(/[<>&'"]/g, function(c) {
                switch (c) {
                        case '<':
                                return '&lt;';
                        case '>':
                                return '&gt;';
                        case '&':
                                return '&amp;';
                        case '\'':
                                return '&apos;';
                        case '"':
                                return '&quot;';
                }
        });
}


/*
/ eBay Login
*/
function eBayLogin() {
        if (eBayAuthTokenFlag === false) {
                getSessionID();
        }
}

function getSessionID() {
        // Expects function requireNewLogin
        // Expects <div id="results">

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

        xw.writeStartDocument();
        xw.writeStartElement('GetSessionIDRequest');
        xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
        xw.writeElementString('RuName', configRuName);
        xw.writeEndElement(); /* GetSessionIDRequest */
        xw.writeEndDocument();

        var my_xml = xw.flush();
        xw.close();

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

        xhr.onload = function() {
        var xmlDoc = xhr.responseXML;
                var returnCode = xmlDoc.getElementsByTagName("Ack")[0].childNodes[0].nodeValue;

                if (returnCode == 'Success') {
                        sessionId = xmlDoc.getElementsByTagName("SessionID")[0].childNodes[0].nodeValue;

                        var win = window.open(configSigninUrl + '?SignIn&RuName=' + configRuName + '&SessID=' + sessionId, "eBay Login", "");
                        var pollTimer = window.setInterval(function() {
                                if (win.closed !== false) {
                                        window.clearInterval(pollTimer);
                                        getToken();
                        }
                        }, 200);
                } else {
                        requireNewLogin();

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

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

        xhr.send(my_xml);
}

function getToken() {
        var x;
        var i;
        var xw = new XMLWriter('UTF-8', '1.0');

        xw.writeStartDocument();
        xw.writeStartElement('FetchTokenRequest');
        xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
        xw.writeElementString('SessionID', sessionId);
        xw.writeEndElement(); /* FetchTokenRequest */
        xw.writeEndDocument();

        var my_xml = xw.flush();
        xw.close();

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

        xhr.onload = function() {
        var xmlDoc = xhr.responseXML;
                var returnCode = xmlDoc.getElementsByTagName("Ack")[0].childNodes[0].nodeValue;

                if (returnCode == 'Success') {
                        eBayAuthToken = xmlDoc.getElementsByTagName("eBayAuthToken")[0].childNodes[0].nodeValue;
                        eBayAuthExpiration = new Date(xmlDoc.getElementsByTagName("HardExpirationTime")[0].childNodes[0].nodeValue).toUTCString();
                        writeCookie();
                        connected();
                } else {
                        requireNewLogin();

                        x = document.getElementById("results");
                        if (x.className.indexOf("w3-show") == -1) {
                                x.className += " w3-show";
                        }

                        x.innerHTML = "<p><strong>" + returnCode + ":</strong></p>";

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

        xhr.send(my_xml);
}

function getUrlParameter(win, name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(win.location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

function feebackStarImage(score) {
        if (score > 999999) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootSlvr_25x25.gif" alt="Silver shooting star">';
        } else if (score > 499999) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootGrn_25x25.gif" alt="Green shooting star">';
        } else if (score > 99999) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootRed_25x25.gif" alt="Red shooting star">';
        } else if (score > 49999) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/icon/iconShootPrpl_25x25.gif" alt="Purple shooting star">';
        } else if (score > 24999) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootTeal_25x25.gif" alt="Turquoise shooting star">';
        } else if (score > 9999) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconShootYllw_25x25.gif" alt="Yellow shooting star">';
        } else if (score > 4999) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconGreenStar_25x25.gif" alt="Green star">';
        } else if (score > 999) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconRedStar_25x25.gif" alt="Red star">';
        } else if (score > 499) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconPurpleStar_25x25.gif" alt="Purple star">';
        } else if (score > 99) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconTealStar_25x25.gif" alt="Turquoise star">';
        } else if (score > 49) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconBlueStar_25x25.gif" alt="Blue star">';
        } else if (score > 9) {
                return '<img src="https://ir.ebaystatic.com/pictures/aw/pics/icon/iconYellowStar_25x25.gif" alt="Yellow star">';
        }
        return '';
}

function exportTableToCSV(tablename, filename, utf8Flag = false) {
    var csv = [];
    var rows = document.getElementById(tablename).querySelectorAll("table tr");
    var dq = '"';
    var nq = "'";

    if (utf8Flag) {
        csv.push("\uFEFF");
    }

    for (var i = 0; i < rows.length; i++) {
        var row = [], cols = rows[i].querySelectorAll("td, th");

        for (var j = 0; j < cols.length; j++) {
            if (cols[j].className.indexOf("w3-hide") == -1) {
                if (isNaN(cols[j].innerText)) {
                    row.push(dq + cols[j].innerText.replace(/"/g, '""') + dq);
                } else {
                    if (cols[j].innerText.includes(".")) {
                        row.push(cols[j].innerText);
                    } else {
                        row.push((cols[j].innerText.length > 7 ? nq : "") + cols[j].innerText);
                    }
                }
            }
        }

        csv.push(row.join(","));
    }

    // Download CSV file
    downloadCSV(csv.join("\n"), filename, utf8Flag);
}

function downloadCSV(csv, filename, utf8Flag = false) {
    var csvFile;
    var downloadLink;

    // CSV file
    if (utf8Flag) {
        csvFile = new Blob([csv], {type: "text/csv;charset=utf-8"});
    } else {
        csvFile = new Blob([csv], {type: "text/csv"});
    }

    // Download link
    downloadLink = document.createElement("a");

    // File name
    downloadLink.download = filename;

    // Create a link to the file
    downloadLink.href = window.URL.createObjectURL(csvFile);

    // Hide download link
    downloadLink.style.display = "none";

    // Add the link to DOM
    document.body.appendChild(downloadLink);

    // Click download link
    downloadLink.click();
}

/* Progress Bar HTML
<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>
*/
function initProgressBar(title) {
        var elem = document.getElementById("progressBar");
          elem.style.width = 0 + '%';
        elem.innerHTML = 0 + '%';

        elem = document.getElementById("progressBarDiv");
          if (elem.className.indexOf("w3-show") == -1) {
                    elem.className += " w3-show";
        }

          elem = document.getElementById("progressBarHeader");
        elem.innerHTML = title;
}

function updateProgressBar(maximum, current) {
        var elem = document.getElementById("progressBar");
        var width = (current / maximum) * 100;
        elem.style.width = width + '%';
        elem.innerHTML = width.toFixed(0) + '%';
}

function endProgressBar() {
    var elem = document.getElementById("progressBarDiv");
                elem.className = elem.className.replace(" w3-show", "");
}

// Progress Bar Modal
/*
<div class="modal" id="progressBarDiv">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 id="progressBarHeader"></h4>
      </div>
      <div class="modal-body">
        <div class="progress">
          <div id="progressBar" class="progress-bar" style="width:0%">0%</div>
        </div>
      </div>
    </div>
  </div>
</div>
*/
function initProgressBarModal(title) {
        var elem = document.getElementById("progressBar");
    elem.style.width = '0%';
        elem.innerHTML = '0%';

    elem = document.getElementById("progressBarHeader");
        elem.innerHTML = title;

        $("#progressBarDiv").modal("show");
}

function endProgressBarModal() {
        $("#progressBarDiv").modal("hide");
}

function extractTextContent(s) {
  var span = document.createElement('span');
  span.innerHTML = s;
  return span.textContent || span.innerText;
}

// xxxxx WIP
function sortHTMLTable(table, column = 0, type = 's', hasFooter = false) {
  var rows, switching, i, x, y, shouldSwitch;
  var nonData = (hasFooter ? 2 : 1);
  table = document.getElementById(table);
  switching = true;
  /* Make a loop that will continue until
  no switching has been done: */
  while (switching) {
    // Start by saying: no switching is done:
    switching = false;
    rows = table.rows;
    /* Loop through all table rows (except the
    first, which contains table headers): */
    for (i = 1; i < (rows.length - nonData); i++) {
      // Start by saying there should be no switching:
      shouldSwitch = false;
      /* Get the two elements you want to compare,
      one from current row and one from the next: */
      x = rows[i].getElementsByTagName("TD")[column];
      y = rows[i + 1].getElementsByTagName("TD")[column];
      // Check if the two rows should switch place:
      if (type == 'i') { // integer
        if (Number(x.innerHTML) > Number(y.innerHTML)) {
          // If so, mark as a switch and break the loop:
          shouldSwitch = true;
          break;
        }
      } else if (type == 'f') { // float
        if (Number(x.innerHTML).toFixed(2) > Number(y.innerHTML).toFixed(2)) {
          // If so, mark as a switch and break the loop:
          shouldSwitch = true;
          break;
        }
      } else if (type == 'c') { // currency
        if (Number(x.innerHTML.substr(1)).toFixed(2) > Number(y.innerHTML.substr(1)).toFixed(2)) {
          // If so, mark as a switch and break the loop:
          shouldSwitch = true;
          break;
        }
      } else if ('d') { // date
        if (Date(x.innerHTML) > Date(y.innerHTML)) {
          // If so, mark as a switch and break the loop:
          shouldSwitch = true;
          break;
        }
      } else { // string
        if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
          // If so, mark as a switch and break the loop:
          shouldSwitch = true;
          break;
        }
      }
    }
    if (shouldSwitch) {
      /* If a switch has been marked, make the switch
      and mark that a switch has been done: */
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
    }
  }
}

// Update table sorter counters after processed deleting rows
// <p>Showing <span id="filtered-rows">0</span> of <span id="total-rows">0</span> / <span id="selected-rows">0</span> selected.</p>
function tableSorterUpdateCounters(tableName) {
    $("#" + tableName).trigger("update").trigger("appendCache").trigger("applyWidgets").trigger('search', false);

    var rowCount = $('#' + tableName + ' tbody tr').length;
    var filteredCount = rowCount - $('#' + tableName + ' tbody tr:hidden').length;
    var checkedCount = $('#' + tableName + ' tbody .checked').length;

    $('#total-rows').html(rowCount);
    $('#filtered-rows').html(filteredCount);
    $('#selected-rows').html(checkedCount);
    $("#" + tableName).find('thead input[type=checkbox]').prop('checked', false);
    $("#" + tableName).find('thead input[type=checkbox]').prop('indeterminate', false);
}

// flattens an object (recursively!), similarly to Array#flatten
// e.g. flatten({ a: { b: { c: "hello!" } } }); // => "hello!"
function flatten(object) {
  var check = _.isPlainObject(object) && _.size(object) === 1;
  return check ? flatten(_.values(object)[0]) : object;
}

function XMLparse(xml, flattenFlag = true) {
  var data = {};

  if (xml === null) {
    return data;
  }

  var isText = xml.nodeType === 3,
      isElement = xml.nodeType === 1,
      body = xml.textContent && xml.textContent.trim(),
      hasChildren = xml.children && xml.children.length,
      hasAttributes = xml.attributes && xml.attributes.length;

  // if it's text just return it
  if (isText) { return xml.nodeValue.trim(); }

  // if it doesn't have any children or attributes, just return the contents
  if (!hasChildren && !hasAttributes) { return body; }

  // if it doesn't have children but _does_ have body content, we'll use that
  if (!hasChildren && body.length) { data.text = body; }

  // if it's an element with attributes, add them to data.attributes
  if (isElement && hasAttributes) {
    data.attributes = _.reduce(xml.attributes, function(obj, name, id) {
      var attr = xml.attributes.item(id);
      obj[attr.name] = attr.value;
      return obj;
    }, {});
  }

  // recursively call #XMLparse over children, adding results to data
  _.each(xml.children, function(child) {
    var name = child.nodeName;

    // if we've not come across a child with this nodeType, add it as an object
    // and return here
    if (!_.has(data, name)) {
      data[name] = XMLparse(child, flattenFlag);
      return;
    }

    // if we've encountered a second instance of the same nodeType, make our
    // representation of it an array
    if (!_.isArray(data[name])) { data[name] = [data[name]]; }

    // and finally, append the new child
    data[name].push(XMLparse(child, flattenFlag));
  });

   // if we can, let's fold some attributes into the body
   _.each(data.attributes, function(value, key) {
   if (data[key] != null) { return; }
     data[key] = value;
     delete data.attributes[key];
   });

  // if data.attributes is now empty, get rid of it
  if (_.isEmpty(data.attributes)) { delete data.attributes; }

  // simplify to reduce number of final leaf nodes and return
  return (flattenFlag ? flatten(data) : data);
}

/* HTML include */
function includeHTML() {
    var z, i, elmnt, file, xhttp;
    /* Loop through a collection of all HTML elements: */
    z = document.getElementsByTagName("*");
    for (i = 0; i < z.length; i++) {
        elmnt = z[i];
        /* search for elements with a certain atrribute:*/
        file = elmnt.getAttribute("w3-include-html");
        if (file) {
            /* Make an HTTP request using the attribute value as the file name: */
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4) {
                    if (this.status == 200) {
                        elmnt.innerHTML = this.responseText;
                    }
                    if (this.status == 404) {
                        elmnt.innerHTML = "<p>Page not found.</p>";
                    }
                    /* Remove the attribute, and call this function once more: */
                    elmnt.removeAttribute("w3-include-html");
                    includeHTML();
                }
            };
            xhttp.open("GET", file, true);
            xhttp.send();
            /* Exit the function: */
            return;
        }
    }
}

function cleanTitleForShopifySearch(title) {
    var cleanTitle = '';
    var len;
    var arr = [];
    var i, j;

    len = title.indexOf('(');;
    if (len > 0) {
        title = title.substr(0, len - 1);
    }

    len = title.indexOf('[');;
    if (len > 0) {
        title = title.substr(0, len - 1);
    }

    arr = title.split(' ');
    for (i = 0; i < arr.length; i++) {
        if (i > 0) {cleanTitle += '_';}
        for (j = 0; j < arr[i].length; j++) {
            cleanTitle += cleanCharForShopifySearch(arr[i][j]);
        }
    }

    return(cleanTitle);
}

function cleanCharForShopifySearch(c) {
    if (c == '?' ||
        c == '&' ||
        c == '=' ||
        c == '+' ||
        c == '$' ||
        c == '%' ||
        c.charCodeAt(0) > 127) {
            c = encodeURIComponent(c);
    }

    return c;
}