﻿
var debugMode = false;
/*
$(document).ready(function()
{
    $('a').each(function()
    {
        var href = $(this).attr('href');

        if (href != undefined && href.match('http://|https://') && href.indexOf(window.location.hostname) == -1)
        {
            $(this).bind('click', function()
            {
                TrackCrossLink('Outbound Link', window.location.pathname, $(this).attr('href'), '');
            });
        }
    });
});
*/

/// <summary>
/// Constructs and sends the event tracking call to the Google Analytics Tracking Code. 
/// Use this to track visitor behavior on your website that is not related to a web page visit, 
/// such as interaction with a Flash video movie control or any user event that does not trigger a page request.
/// </summary>
/// <param name="category">The general event category (e.g. "Videos").</param>
/// <param name="action">The action for the event (e.g. "Play").</param>
/// <param name="label">An optional descriptor for the event.</param>
/// <param name="value">
/// An optional value to be aggregated with. parameter differs from the others in that it is of 
/// type INTEGER rather than string, so use it to assign a numerical value to a tracked page object.
/// </param>
function TrackEvent(category, action, label, value)
{
    try
    {
        if (debugMode)
            alert("TrackEvent('" + category + "', '" + action + "', '" + label + "', '" + value + "')");
        else
            pageTracker._trackEvent(category, action, label, value);
    }
    catch (err) {if (debugMode) { alert(err) }  }
}

function TrackPageLoad(start, stop)
{
    try
    {
        var pageLoadTimeElapse = stop - start;  // Value in milliseconds
        pageTracker._trackEvent('Page Performance', 'Load Time', document.location.href, pageLoadTimeElapse);
    }
    catch (err) { if (debugMode) { alert(err) } }
}

function TrackViewStateSize()
{
    try
    {
        var size = document.forms[0].__VIEWSTATE.value.length; // Value in bytes
        pageTracker._trackEvent('Page Performance', 'ViewState Size', document.location.href, size);
    }
    catch (err) { if (debugMode) { alert(err) } }
}

/// <summary>
/// Creates a transaction object with the given values. As with _addItem(),
/// this method handles only transaction tracking and provides no additional ecommerce functionality.
/// Therefore, if the transaction is a duplicate of an existing transaction for that session, the old
/// transaction values are over-written with the new transaction values. Arguments for this method are
/// matched by position, so be sure to supply all parameters, even if some of them have an empty value.
/// </summary>
/// <param name="orderId">Required. Internal unique order id number for this transaction.</param>
/// <param name="affiliation">Partner or store affiliation (undefined if absent).</param>
/// <param name="total">Required. Total dollar amount of the transaction.</param>
/// <param name="tax">Tax amount of the transaction.</param>
/// <param name="shipping">Shipping charge for the transaction.</param>
/// <param name="city">City to associate with transaction.</param>
/// <param name="state">State to associate with transaction.</param>
/// <param name="country">Country to associate with transaction.</param>
function CreateTransaction(orderId, affiliation, total, tax, shipping, city, state, country) 
{
    try 
    {
        if (debugMode)
            alert("CreateTransaction('" + orderId + "', '" + affiliation + "', '" + total + "', '" + tax + "', '" + shipping + "', '" + city + "', '" + state + "', '" + country + "')");
        else
            pageTracker._addTrans(orderId, affiliation, total, tax, shipping, city, state, country);
    }
    catch (err) { if (debugMode) { alert(err) } }
}


/// <summary>
/// Creates a transaction object with the given values. As with _addItem(),
/// </summary>
/// <param name="orderId">Order ID of the transaction to associate with item.</param>
/// <param name="sku">Required. Item's SKU code.</param>
/// <param name="name">Product name.</param>
/// <param name="category">Product category.</param>
/// <param name="price">Required. Product price.</param>
/// <param name="quantity">Required. Purchase quantity.</param>
function AddItemToTransaction(orderId, sku, name, category, price, quantity)
{
    try
    {
        if (debugMode)
            alert("AddItemToTransaction('" + orderId + "', '" + sku + "', '" + name + "', '" + category + "', '" + price + "', '" + quantity + "')");
        else
            pageTracker._addItem(orderId, sku, name, category, price, quantity);
    }
    catch (err) { if (debugMode) { alert(err) } }
}

function TrackPageView(url)
{
    try
    {
        url = '/Click/' + url;
        url = url.replace('/Click//', '/Click/');
    
        if (debugMode)
            alert("TrackPageView('" + url + "')");
        else
            pageTracker._trackPageview(url);
    }
    catch (err) { if (debugMode) { alert(err) } }
}

function TrackPageViewDetailed(category, action, label, value)
{
    try
    {
        var fullPath = '/' + category + '/' + action + '/' + label;
        fullPath = fullPath.replace('//', '/');

        TrackPageView(fullPath);
        TrackEvent(category, action, label, value);
    }
    catch (err) { if (debugMode) { alert(err) } }
}

/* Track all outbound links */
function TrackCrossLink(category, fromUrl, toUrl, value)
{
    if (debugMode)
    {
        alert("TrackCrossLink('" + category + "', '" + fromUrl + "', '" + toUrl + "', '" + value + "')'");
        TrackPageView(toUrl);
    }
    else
    {
        TrackEvent(category, fromUrl, toUrl, value);
        TrackPageView(toUrl);
    }
}

function TrackVar(name)
{
    if (debugMode)
        alert("TrackVar('" + name + "')'");
    //else
    //    pageTracker._setVar(name);
}