// JavaScript Document

/*
 * 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 returns that object.
 */
function getArgs(  ) 
{
    var args = new Object(  );
    var query = location.search.substring(1);     
      // Get query string
    var pairs = query.split("&");
     // Break at comma
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
          // Look for "name=value"
        if (pos == -1) continue;
          // If not found, skip
        var argname = pairs[i].substring(0,pos);
          // Extract the name
        var value = pairs[i].substring(pos+1);
          // Extract the value
        args[argname] = unescape(value);
         // Store as a property
       // In JavaScript 1.5, use decodeURIComponent(  ) 
       // instead of escape(  )
    }
    return args;     // Return the object
}
 

function getCookieVal (offset) 
{ 
	var endstr = document.cookie.indexOf (";", offset); 
	if (endstr == -1) 
		endstr = document.cookie.length; 
	return unescape(document.cookie.substring(offset, endstr)); 
} 

function GetCookie (name) 
{ 
	var arg = name + "="; 
	var alen = arg.length; 
	var clen = document.cookie.length; 
	var i = 0; 
	while (i < clen) 
	{ 
		var j = i + alen; 
		if (document.cookie.substring(i, j) == arg) 
			return getCookieVal (j); 
		i = document.cookie.indexOf(" ", i) + 1; 
		if (i == 0) break; 
	} 
	return null; 
} 

function SetCookie (name, value) 
{ 
	var argv = SetCookie.arguments; 
	var argc = SetCookie.arguments.length; 
	var expires = (argc > 2) ? argv[2] : null; 
	var path = (argc > 3) ? argv[3] : null; 
	var domain = (argc > 4) ? argv[4] : null; 
	var secure = (argc > 5) ? argv[5] : false; 
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) + 
	((domain == null) ? "" : ("; domain=" + domain)) + 
	((secure == true) ? "; secure" : ""); 
} 



function CaptureTrackingInfo()
{
	var args = getArgs();
	var strSource=""
	var strCampaign= args.c
	var strAdGroup = args.a
	var strAd = args.ad
	var strFindWhat = args.s
	var strKanoodle = args.source
	var strOther = args.other
	var strOffer = args.o
	var strGeneric = args.src
	var strEmailAddress = args.n
	var intCECClientID = args.cid
	var strPath = args.p
			
	if (strKanoodle != 'undefined')
		strSource = strSource + "Kanoodle_Campaign_" + strCampaign;
	if (strFindWhat != 'undefined')
		strSource = strSource + "FindWhat";
	if ( strAdGroup != 'undefined')
		strSource = strSource + "Google_Campaign_"+ strCampaign + "_AdGroup_" + strAdGroup + "_Ad_" + strAd;
	if (strOther != 'undefined')
		strSource = strSource + strOther
	if (strOffer != 'undefined')
		strSource = strSource + "&o=" + strOffer 
	if (strGeneric != 'undefined')
		strSource = strSource + strGeneric
	if (strEmailAddress  != 'undefined')  // We know we have a ContactMax User
		strSource = strSource + "&n=" + strEmailAddress + "&c=" + strCampaign  + "&cid=" + intCECClientID + "&p=" + strPath 	
										
	SetCookie ("source", strSource) 	 
}


function GetTrackingInfo()
{
	return GetCookie("source");
}

function makeTracker()
{
	var domain = "localhost/maxapp"
//	var domain = "www.contactmax.com"
	document.write("<img src='http://"+ domain + "/convert.asp")
	document.write("?source=" + GetTrackingInfo())	
	document.write("' height='1' width='1'>")
}

function SetTrackingInfo(pStrSource)
{
	SetCookie ("source", pStrSource) 
}
