﻿
/**
* @name Affiliate-Network Recognition
* @description Analyzes the URL in order to determine whether the current uses comes from one of our affiliates; if an affiliate is detected the value will be saved to a cookie
* @param networks array of affiliate-objects that contain information about our partner-networks
* @param cookieName the name of the affiliate cookie (e.g. AffiliateNetworkInformation)
* @param cookieValueSeperator this string seperates the affiliate network name from the affiliate id in the cookie value
* @return bool returns true if an affiliate was recognized and the cookie was saves successfully
*/
function recognizeAffiliateNetworks(networks, cookieName, cookieValueSeperator) {
    if (!networks) { return false; }
    var result = false;

    /* get relevant configuration values from network settings */
    var providers = [];
    for (var n = 0; n < networks.length; n++) {
        var network = networks[n];
        if (network) {

            var entry = {};

            for (var i = 0; i < network.settings.length; i++) {
                var setting = network.settings[i];

                if (setting.name.toLowerCase() === "recognitionpattern") {
                    entry.networkName = network.name;
                    entry.networkId = network.id;
                    entry.recognitionPattern = new RegExp(setting.value, "i");
                } else if (setting.name.toLowerCase() === "cookielifetime") {
                    entry.cookieLifetime = Number(setting.value);
                } else if (setting.name.toLowerCase() === "redirectpattern") {
                    entry.redirectPattern = new RegExp(setting.value, "i");
                }
            }

            if (entry.networkName && entry.networkId && entry.recognitionPattern) {
                providers.push(entry);
            }
        }
    }

    /* test if current URL matches one of the supplied patterns */
    for (var n = 0; n < providers.length; n++) {
        var provider = providers[n];

        if (provider.recognitionPattern.test(document.location)) {
            /* determine cookie expiration date */
            var cookieLifeTime = provider.cookieLifetime;
            if (!provider.cookieLifetime) {
                cookieLifeTime = 60;
            }
            cookieLifeTime = cookieLifeTime * 1000 * 60 * 60 * 24;
            var today = new Date();
            today.setTime(today.getTime());
            var expires_date = new Date(today.getTime() + (cookieLifeTime));

            /* determine affiliate id */
            var affiliateId = {};
            var affiliateIdMatches = provider.recognitionPattern.exec(document.location);
            if (affiliateIdMatches.length > 1) {
                affiliateId = affiliateIdMatches[affiliateIdMatches.length - 1];
            } else {
                affiliateId = "affiliate-Id-is-not-specified";
            }

            /* save affiliate information to cookie */
            document.cookie = cookieName + "=" + provider.networkName + cookieValueSeperator + escape(affiliateId) + ";expires=" + expires_date + "; path=" + "/";

            /* redirect */
            if (provider.redirectPattern) {
                var redirectUrl = {};
                var redirectUrlMatches = provider.redirectPattern.exec(document.location);
                if (redirectUrlMatches.length > 1) {
                    redirectUrl = redirectUrlMatches[redirectUrlMatches.length - 1];

                    document.location = redirectUrl;
                }
            }
        }
    }

    return result;
}

/**
* @name delete_cookie
* @description remove/overwrite cookie information
* @param name The name of the cookie that is to be deleted
* @return True if the cookie was successfully removed
*/
function delete_cookie(name) {
    try {
        document.cookie = name + "=;expires=Thu, 01-Jan-70 00:00:01 GMT;path=/";
        return true
    } catch (err) { }
    return false;
}