var tagging = {
    /// <summary>
    /// Global tagging object for CAI web sites
    /// </summary>

    GoogleAnalytics: {

        _pageTracker: Object(),
        _initialized: Boolean(),
        _trackingCode: String(),
        _trackingType: String(),
        _pageNameRegExp: RegExp(/[%]/g),

        initialize: function(trackingCode, trackingType) {
            /// <summary>
            /// Initialize the Google Analytics tagging
            /// </summary>
            /// <param name="trackingCode" type="string" optional="false">Code of the Google Analytics account</param>
            /// <param name="trackingType" type="string" optional="true">Type of Google Analytics tagging (urchin|ga as default)</param>
            tagging.GoogleAnalytics._trackingCode = trackingCode;
            tagging.GoogleAnalytics._trackingType = (trackingType == null ? "ga" : trackingType.toLowerCase());

            var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/" + tagging.GoogleAnalytics._trackingType + ".js' type='text/javascript'%3E%3C/script%3E"));

            tagging.GoogleAnalytics._initialized = true;
        },

        tagPageView: function(pageName) {
            /// <summary>
            /// Tag a page with Google Analytics
            /// </summary>
            /// <param name="pageName" type="string" optional="true">Name of the web page</param>

            if (!tagging.GoogleAnalytics._initialized) return;

            pageName = (pageName == null ? "" : (pageName.substring(0, 1) == "/" ? pageName : "/" + pageName));

            //... Remove undesired characters
            pageName = pageName.replace(tagging.GoogleAnalytics._pageNameRegExp, "");

            //... Replace spaces by underscores
            var regexp = new RegExp(/\s+/g);
            pageName = pageName.replace(regexp, "_");

            try {
                if (tagging.GoogleAnalytics._trackingType == "urchin") {
                    _uacct = tagging.GoogleAnalytics._trackingCode;

                    if (pageName == "") {
                        urchinTracker();
                    }
                    else {
                        urchinTracker(pageName);
                    }
                }
                else {
                    tagging.GoogleAnalytics._pageTracker = _gat._getTracker(tagging.GoogleAnalytics._trackingCode);

                    if (pageName == "") {
                        tagging.GoogleAnalytics._pageTracker._trackPageview();
                    }
                    else {
                        tagging.GoogleAnalytics._pageTracker._trackPageview(pageName);
                    }
                }
            } catch (err) { }
        }
    },

    CoreMetrics: {

        _initialized: Boolean(),
        _clientId: String(),
        _domain: String(),
        _category: String(),

        initialize: function(clientId, domain, category) {
            /// <summary>
            /// Initialize the CoreMetrics tagging
            /// </summary>
            /// <param name="clientId" type="string" optional="false">Id of the CoreMetrics account</param>
            /// <param name="domain" type="string" optional="false">Domain name of the web site (ex: www.inneovplus.de)</param>
            /// <param name="category" type="string" optional="true">Default category to used if no category is specified</param>

            if (clientId == null) clientId = "";
            if (clientId == "") return;

            tagging.CoreMetrics._clientId = clientId;
            tagging.CoreMetrics._domain = domain;
            tagging.CoreMetrics._category = (category == null ? "" : category);

            var cmJsHost = "http://share.activecosmetics-itservices.com/js/";
            document.write(unescape("%3Cscript src='" + cmJsHost + "eluminate.js' type='text/javascript'%3E%3C/script%3E"));
            document.write(unescape("%3Cscript src='" + cmJsHost + "cmdatatagutils.js' type='text/javascript'%3E%3C/script%3E"));

            tagging.CoreMetrics._initialized = true;
        },

        tagPageView: function(pageName, category, searchTerm, searchResults) {
            /// <summary>
            /// Tag a page with the pageView tag of CoreMetrics
            /// </summary>
            /// <param name="pageName" type="string" optional="true">Name of the web page</param>
            /// <param name="category" type="string" optional="true">Category name of the CDF file</param>
            /// <param name="searchTerm" type="string" optional="true">Onsite search term used to get to the current page</param>
            /// <param name="searchResults" type="string" optional="true">Number of results returned on the Search Results page</param>

            if (!tagging.CoreMetrics._initialized) return;

            if (pageName == null) pageName = "";
            if (category == null) category = "";
            if (searchTerm == null) searchTerm = "";
            if (searchResults == null) searchResults = "";

            //... If the page name is empty, get the page path name
            if (pageName == "") pageName = window.location.pathname;

            //... Use the default category if not specified
            if (category == "") category = tagging.CoreMetrics._category;

            try {
                cmSetProduction();
                cm_ClientID = tagging.CoreMetrics._clientId;
                cm_JSFPCookieDomain = tagging.CoreMetrics._domain;

                if (searchResults != "" && searchResults != "")
                    cmCreatePageviewTag(pageName, category, searchTerm, searchResults);
                else
                    cmCreatePageviewTag(pageName, category, null, null);
            } catch (err) { }
        },

        tagProduct: function(productId, productName, category) {
            /// <summary>
            /// Tag a page with the product tag of CoreMetrics
            /// </summary>
            /// <param name="productId" type="string" optional="false">Alphanumeric string that uniquely identifies a product</param>
            /// <param name="productName" type="string" optional="false">Name of the product being viewed</param>
            /// <param name="category" type="string" optional="true">Category name of the CDF file</param>

            if (!tagging.CoreMetrics._initialized) return;

            if (productId == null) productId = "";
            if (productName == null) productName = "";
            if (category == null) category = "";

            //... Do nothing if the product id or product name are not present
            if (productId == "" || productName == "") return;

            //... Use the default category if not specified
            if (category == "") category = tagging.CoreMetrics._category;

            try {
                cmSetProduction();
                cm_ClientID = tagging.CoreMetrics._clientId;
                cm_JSFPCookieDomain = tagging.CoreMetrics._domain;

                cmCreateProductviewTag(productId, productName, category);
            } catch (err) { }
        },

        tagRegistration: function(customerId, email, city, state, zip, newsletterName, subscribedFlag) {
            /// <summary>
            /// Tag a page with the registration tag of CoreMetrics
            /// </summary>
            /// <param name="customerId" type="string" optional="false">Alphanumeric string that is relatively long-lived and consistent for a given user from visit to visit</param>
            /// <param name="email" type="string" optional="true">Email address for the customer</param>
            /// <param name="city" type="string" optional="true">Customer’s City</param>
            /// <param name="state" type="string" optional="true">Customer’s State</param>
            /// <param name="zip" type="string" optional="true">Customer’s Zip Code</param>
            /// <param name="newsletterName" type="string" optional="true">Name of the newsletter to which the customer is subscribed or unsubscribed</param>
            /// <param name="subscribedFlag" type="string" optional="true">‘Y’ or ‘N’ to indicate if the customer subscribed or unsubscribed to the Newsletter</param>

            if (!tagging.CoreMetrics._initialized) return;

            if (customerId == null) customerId = "";

            //... Do nothing if the customer id is not present
            if (customerId == "") return;

            try {
                cmCreateRegistrationTag(customerId, email, city, state, zip, newsletterName, subscribedFlag);
            } catch (err) { }
        },

        tagError: function(pageId, category) {
            /// <summary>
            /// Tag a page with the error tag of CoreMetrics
            /// </summary>
            /// <param name="pageId" type="string" optional="true">Page name used to uniquely identify the given page in Coremetrics</param>
            /// <param name="category" type="string" optional="true">Category name of the CDF file</param>

            if (!tagging.CoreMetrics._initialized) return;

            if (pageId == null) pageId = "";
            if (category == null) category = "";

            //... If the page id is empty, get the page path name
            if (pageId == "") pageId = window.location.pathname;

            //... Use the default category if not specified
            if (category == "") category = tagging.CoreMetrics._category;

            try {
                cmSetProduction();
                cm_ClientID = tagging.CoreMetrics._clientId;
                cm_JSFPCookieDomain = tagging.CoreMetrics._domain;

                cmCreateErrorTag(pageId, category);
            } catch (err) { }
        }
    },

    tagPageElement: function(elementId, elementCategory, label, value) {
        /// <summary>
        /// Tag a page element or event with every active tagging processes
        /// </summary>
        /// <param name="elementId" type="string" optional="false">The unique identifier or name for the Element</param>
        /// <param name="elementCategory" type="string" optional="true">The category used to classify the Element (Mandatory for Google Analytics, optional for CoreMetrics)</param>
        /// <param name="label" type="string" optional="true">(Google Analytics) An optional string to provide additional dimensions to the event data</param>
        /// <param name="value" type="string" optional="true">(Google Analytics) An optional integer to provide numerical data about the user event</param>
        if (!tagging.CoreMetrics._initialized && !tagging.GoogleAnalytics._initialized) return;

        if (elementId == null) elementId = "";
        if (elementCategory == null) elementCategory = "";

        //... Do nothing if the element id is not present
        if (elementId == "") return;

        if (tagging.CoreMetrics._initialized) {
            try {
                cmSetProduction();
                cm_ClientID = tagging.CoreMetrics._clientId;
                cm_JSFPCookieDomain = tagging.CoreMetrics._domain;

                if (elementCategory != "")
                    cmCreatePageElementTag(elementId, elementCategory);
                else
                    cmCreatePageElementTag(elementId);
            } catch (err) { }
        }

        if (tagging.GoogleAnalytics._initialized && tagging.GoogleAnalytics._trackingType == "ga") {
            try {
                tagging.GoogleAnalytics._pageTracker = _gat._getTracker(tagging.GoogleAnalytics._trackingCode);
                tagging.GoogleAnalytics._pageTracker._trackEvent(elementCategory, elementId, label, value);
            } catch (err) { }
        }
    }
}

var pageTracker = {
    /// <summary>
    /// Deprecated object to support old flash calls
    /// </summary>

    _trackPageview: function(pageName) {
        /// <summary>
        /// Old function called to tag a page, do nothing anymore
        /// </summary>
    }
}

function sageflash(swfName, pageName) {
    /// <summary>
    /// Deprecated method to support old Sage flash calls
    /// </summary>
    /// <param name="swfName" type="string" optional="false">Swf name</param>
    /// <param name="pageName" type="string" optional="false">Name of the web page</param>

    tagging.GoogleAnalytics.tagPageView(pageName);
    tagging.CoreMetrics.tagPageView(pageName);
}

function log(pageName) {
    /// <summary>
    /// Deprecated method to support old Sage flash calls
    /// </summary>
    /// <param name="pageName" type="string" optional="false">Name of the web page</param>

    tagging.GoogleAnalytics.tagPageView(pageName);
    tagging.CoreMetrics.tagPageView(pageName);
}

function tagPage(pageName, category) {
    /// <summary>
    /// Global tagging method for client events
    /// </summary>
    /// <param name="pageName" type="string" optional="true">Name of the web page</param>
    /// <param name="category" type="string" optional="true">Category name of the CDF file (for CoreMetrics)</param>

    tagging.GoogleAnalytics.tagPageView(pageName);
    tagging.CoreMetrics.tagPageView(pageName, category);
}