/*
 *  Name:			Simple affiliate tracking.
 *  Author:		Kevin Veatch Design, Inc.
 *  Date: 04/02/2008
 *
 *  Notes:
 *   This script tracks purchases made by site visitors that come through an affiliated site.  This is accomplished as follows:
 *
 *   First, the affiliate adds an 'affiliate' parameter to their links to this site, for example ?affiliate=AffiliatedSiteName
 *   This script looks for the "affiliate" parameter in the URL and, if the parameter is set, sets a temporary cookie (which expires when the browser closes)
 *   for the current site named 'affiliate' with the value of the 'affiliate' parameter.  This script then redirects the browser to the current page with
 *   the GET parameters removed.
 *
 *   If the 'affiliate' cookie is set, this script searches the page for links with a CSS class of "cartLink", and then appends an "h:affiliate"
 *   parameter, which causes FoxyCart to add a hidden field named 'affiliate' to the order.
 *
 *   The customer will not see this field in their receipt or receipt email; it will only show on the administrator's copy of the receipt.
 */
$(document).ready(function () {
	var params = window.location.search.split(/[?&=]/).slice(1);
	for (var ndx = 0; ndx < params.length; ndx += 2) {
		if (params[ndx] == "affiliate") {
			$.cookie(params[ndx], (ndx + 1 < params.length ? params[ndx+1] : null));
			window.location.href = window.location.href.replace(window.location.search, '')	// Redirect with GET parameters removed.
		}
	}

	if ($.cookie("affiliate"))
		$('.cartLink').attr('href', $('.cartLink').attr('href')+"&h:affiliate="+$.cookie("affiliate"));	// Update links with hidden field.
});

