// REFERER DETECT FOR CA.CLICKINC.COM

	var affSoft_scriptLoc = 'https://ca.clickinc.com/clicks/servlet/Click?merchant=70138';
	var affSoft_kbId = 0;
	var affSoft_queryString = window.location.search.substring(1);
	var affSoft_imgdata = '';
	var affSoft_subdata = '';
	var affSoft_promodata = "";
	var affSoft_overwrite = 0;
	var affSoft_refdata = document.referrer;
	var affSoft_daysToLive = 365;
	var affSoft_affReferralValue = "";
	var multiDomain = false;
	if (affSoft_queryString.length > 0) {
		var pairs = affSoft_queryString.split('&');
								
		for(var i = 0; i < pairs.length; i++) {
			var pairs2 = pairs[i].split('=');
			
			switch(pairs2[0].toLowerCase()) {
				case 'affid':
					affSoft_kbId = pairs2[1];
					break;
				case 'kbid':
					affSoft_kbId = pairs2[1];
					break;
				case 'id':
					if(affSoft_kbId==0){
						affSoft_kbId = pairs2[1];
					}
					break;
				case 'img':
					affSoft_imgdata = pairs2[1];
					break;
				case 'sub':
					affSoft_subdata = pairs2[1];
					break;
				case 'promotion':
					affSoft_promodata = pairs2[1];
					break;
				case 'affreferral':
					affSoft_affReferralValue = pairs2[1];
					break;
			}
		} //end for
	}//end if

// checking for the click Id and same Click or not 

if (affSoft_promodata != ""){
	SetCookie("promotion", affSoft_promodata, affSoft_daysToLive);
 }

 if ( aff_IsNewCustomer()==true ) { //aff_IsAffiliateClick()==true && 
		aff_Post();
		// Store a flag in the form of a cookie
		if ( affSoft_kbId != null ) {
			SetCookie("asm", affSoft_kbId, affSoft_daysToLive);
		}
		if (affSoft_affReferralValue != null && affSoft_affReferralValue != "") {
			SetCookie("affreferral", affSoft_affReferralValue, affSoft_daysToLive);
		}
		SetCookie("asmReferral", affSoft_refdata, affSoft_daysToLive);
 }

/*
function aff_IsAffiliateClick() { 
	if (affSoft_kbId!=null && affSoft_kbId!=0) {
		return true;
	} else {
		return false;
	}
 }
*/

function aff_IsNewCustomer() { 
	if ( GetCookie('asm')==null && GetCookie('asmReferral')==null ) {
		return true;
	} else {
		return false;
	}
}
		
//Posting Click
function aff_Post() {

	var img = new Image();
	var url = affSoft_scriptLoc + '&affId=' + affSoft_kbId;
	if (affSoft_imgdata != '') 	{
		url += '&img=' + affSoft_imgdata+'&type=banner';
	} else {
		url +='&type=text';
	}
    if (affSoft_subdata != '') {
		url += '&sub=' + affSoft_subdata;
	}
	if (affSoft_refdata != '') {
		url += '&referrer=' + escape(affSoft_refdata);
	}	
    img.src = url;	
}
	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 WriteCookie (name, value, expires) 
	{
		var argv = SetCookie.arguments;
		var argc = SetCookie.arguments.length;
		var path = "/";

			//inserted by Andrew Herron to fix cross-sub-domain cookie compatibility, 
			if (multiDomain) {
				rootDomain = document.domain;
			} else {
				domain = document.domain;
				dparts = domain.split(".");
				if (dparts.length == 3) {
					rootDomain = dparts[1]+"."+dparts[2];
				} else {
					rootDomain = domain;
				}
			}

		var secure = (argc > 5) ? argv[5] : false;
		
		var cookie = name + "=" + escape(value) +
			((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
			((path == null) ? "" : ("; path=" + path)) +
			((rootDomain == null) ? "" : ("; domain=" + rootDomain)) +
			((secure == true) ? "; secure" : "");
		
		document.cookie = cookie;
	}

	function DeleteCookie (name) 
	{
		var exp = new Date();
		exp.setTime (exp.getTime() - 1000000000);  // This cookie is history (changed -1 to make it previous time)
		var cval = GetCookie (name);
		document.cookie =name + "=" + cval + "; expires=" + exp.toGMTString();
	}

	function SetCookie(name, value, expiredays)
	{
		var expdate = new Date(); 
		expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * expiredays)); 
		WriteCookie(name, value, expdate);  
	}

	
function submitAction(form){
	form.referedBy.value= GetCookie('asm');
	form.referedUrl.value = GetCookie('asmReferral');
	form.submit();
}
