<!--
// ============================================================================================
// ============================================================================================
// 
// This Javascript Code is part of the Synergyx (http://www.synergyx.com) affiliate tracking 
// system and is Copyright 2009, Paul Galloway. All Rights Reserved
//
// Use of this code in part or in full is expressly forbidden except by 
// written permission from Paul Galloway, paulg [AT] paulgalloway.com
//
// ============================================================================================
// ============================================================================================

function GetAffnumCookie()
{
	var cookiecontent;
	if(document.cookie.length > 0) 
	{
		var cookiename = 'affnum=';
		var cookiebegin = document.cookie.indexOf(cookiename);
		var cookieend = 0;
		if(cookiebegin > -1) 
		{
			cookiebegin += 7;
			cookieend = document.cookie.indexOf(";",cookiebegin);
			if(cookieend < cookiebegin) 
			{ 
				cookieend = document.cookie.length; 
			}
			cookiecontent = document.cookie.substring(cookiebegin,cookieend);
		}
		else { cookiecontent = ''; }
	}
	else { cookiecontent = ''; }
		
	cookiecontent = cookiecontent.replace(/\%(25)*21/, "!");
	return cookiecontent;
}
	
	
function WriteCookie(name,value) 
{
	var exp = '';
	var today = new Date();
	var expdate = today.getTime() + (cookieExpiration * 24 * 60 * 60 * 1000);
	today.setTime(expdate);
	exp = '; expires=' + today.toGMTString();
	document.cookie = name + "=" + value + '; domain=.imtopdog.com; path=/' + exp;
}


function PrintAffiliateCode(strng) 
{
	strng = strng.replace(/_AFFNUM_/g, sgx_anum);
	document.write(strng);
}
	
var u_anum = '';
var c_anum = GetAffnumCookie();

var args = location.search;
if (args.charAt(0) == '?')
{
	u_anum = args.substring(1);
}

var inhouseNumber = 10000;
var sgx_anum = inhouseNumber; //default
var cookieExpiration = 365; //specified in days
document.cookie = "cookiestatus=enabled; domain=.imtopdog.com; path=/;";

u_anum = u_anum.replace(/\%(25)*21/, "!");
c_anum = c_anum.replace(/\%(25)*21/, "!");
	
var unum_fields = u_anum.split("!");
var cnum_fields = c_anum.split("!");
		
if ( (unum_fields[0] != '' && unum_fields[0] != inhouseNumber) || (unum_fields[0] == inhouseNumber && cnum_fields[0] == inhouseNumber) ) { sgx_anum = u_anum; } //URL-passed affiliate number takes precedence over cookie affiliate number (except in cases where cookie affnum is inhouse affnum)
else if (cnum_fields[0] != '') { sgx_anum = c_anum; }

if (sgx_anum != 'none' && sgx_anum != '') { WriteCookie('pgjvp',sgx_anum); }

// Upload this as "afftrk.js" and load in HEAD section with (assuming root web location): script src="/afftrk.js"  
// (be sure to place inside script tags)

// Insert this anywhere in your page where you want the affiliate number to show up:
// PrintAffiliateCode('Affnum is _AFFNUM_'); (put inside script tags)

//-->

