// This function parses comma separated name=value
// argument pairs from the query string of the URL.
// It stores the name=value pairs in
// properties of an object and then returns that object
//
// Jim K - From Orielly JSB pp 244
// http://www.jamesrking.com/left_brain/javascript/parse_a_querystring.asp
//
function getArgs() {
	var args = new Object();
	// Get Query String
	var query = location.search.substring(1);
	// Split query at the comma
	var pairs = query.split("&");

	// Begin loop through the querystring
	for(var i = 0; i < pairs.length; i++) {

		// Look for "name=value"
		var pos = pairs[i].indexOf('=');
		// if not found, skip to next
		if (pos == -1) continue;
		// Extract the name
		var argname = pairs[i].substring(0,pos);

		// Extract the value
		var value = pairs[i].substring(pos+1);
		// Store as a property
		args[argname.toLowerCase()] = unescape(value.toLowerCase());
	}
	return args; // Return the Object
}

// This function sets a tracking cookie
// from: http://www.blazonry.com/javascript/cookies.php
function setCookie(name, value, days) {
	 // default session cookie if days empty
	if (!days) days = 0;
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + days*24*60*60*1000);
	// Split document.host to get top level domain
	var host = document.location.host;
	var pairs = host.split(".");
	var domain = pairs[pairs.length-2] + "." + pairs[pairs.length-1];
        document.cookie = name + "=" + escape(value) + "; expires=" + expdate.toGMTString() + "; domain=" + domain + "; path=/";
}

function getCookies () {
	// Get the document.cookie
	var dc = document.cookie;
        var args = new Object();
        // Split cookie at the semicolon
        var pairs = dc.split(";");
        // Begin loop through the cookie
        for(var i = 0; i < pairs.length; i++) {

                // Look for "name=value"
                var pos = pairs[i].indexOf('=');
                // if not found, skip to next
                if (pos == -1) continue;
                // Extract the name
                var argname = pairs[i].substring(0,pos);

                // Extract the value
                var value = pairs[i].substring(pos+1);
                // Store as a property
                args[argname.toLowerCase()] = unescape(value.toLowerCase());
        }
        return args; // Return the Object
}

function clearCookie (name) {
        setCookie(name, "", -2);
}

function getTrackingCode() {
	var args = getArgs();
	var cookies = getCookies();

	if (args.id) {
		clearCookie("id");
		setCookie("id", args.id, "60");
		return args.id;
	}
	if (cookies.id) {
		return cookies.id;
	}
	if (document.referrer.indexOf("google") != -1) {
		setCookie("id", "gs", "60");
		return "gs";
	}
	if (document.referrer.indexOf("yahoo") != -1) {
		setCookie("id", "yh", "60");
		return "yh";
	}
	return null;
}

function rewriteShopCartLink(url, refCode, carturl, linkname) {
        trackingCode = getTrackingCode();

        if (trackingCode == null) {
          url = url + "?referrer=" + refCode + "&" + carturl;
        }
        else {
          url = url + "?referrer=" + "[" + trackingCode + "]." + refCode + "&" + carturl;
        }

        document.write("<A HREF=\"" + url + "\"> " + linkname + "</a>");
}

function showTrackingCode() {
        trackingCode = getTrackingCode();

        if (trackingCode) {
          document.write(trackingCode);
        }
}

function writeTrackingCode() {
	trackingCode = getTrackingCode();
	document.write(trackingCode);
}

getTrackingCode();
