var affUser;
var progId;
var webId;
var bannerId;
var qs=new Querystring(); 

var httpUrl="http://www.cashengines.com/affiliateintegrationscript/affaction.asp"; 
var httpsUrl="https://www.cashengines.com/affiliateintegrationscript/affaction.asp"; 

//check if there is a cookie
function isVisited()
{
	//window.alert(GetCookie("RMW_" + act))
	if(GetCookie("RMW_" + act)==null)
	{
		return false;
	}
	else
	{
		return true;
	}
}

//check if it is secure
function isSecure()
{
	var loc = document.location.toString(); 
	var index = loc.indexOf(":"); 
      	var http = loc.substring(0,index); 
		if (http == "https") 
	{ 
	    return true;
	} 
	else
	{
	    return false; 	
	}	

}

// check prefix  ( if is an affiliate )
function IsAffiliate()
{
var strAff=revshare.substring(0,3);
if (strAff=="aff")
{
 return true;  	
}
return false;
}

//split revshare
function SplitRevshare()
{
var arg;
//window.alert(revshare);
arg = revshare.split('_');
//window.alert(arg[0]);
affUser=arg[1];
progId=arg[2];
webId=arg[3];
bannerId=arg[4];
}


//store cookie
function SetCookie (name,value,expires,secure,path,domain)
{
path="/";
document.cookie=name + "=" + escape(value) + ((expires==null) ? "" : ("; expires=" + expires.toGMTString())) + ((path==null) ? "" : ("; path=" + path)) + 

((domain==null) ? "" : (";domain=" + domain)) +((secure==true) ? "; secure" : "");
}


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 deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


	if (IsAffiliate() && !isVisited())    		
	{
	//	SplitRevshare();
		var URL;
		var QUERY;
	   	QUERY = "?revshare=" + revshare + "&saleAmount="+saleAmount+"&primaryTid="+primaryTid+"&secondaryTid="+secondaryTid+"&action="+act;
		
		URL = httpUrl;
		if (isSecure())
		{
		 URL = httpsUrl;
		}
		 URL += QUERY;
				
		document.write('<img border="0" width="1" height="1" src="' + URL + '">');
		
		//set cookie
		var exp = new Date(); 
		//24 hours on cookies
		exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24));  

		SetCookie ( "RMW_" + act,"true",exp,isSecure());
//	}
	}
	
	
	
//querystring helper functions	
function Querystring()
{
// get the query string, ignore the ? at the front.
	var querystring=location.search.substring(1,location.search.length);
	querystring = querystring.toLowerCase();
	
// parse out name/value pairs separated via &
	var args = querystring.split('&');

// split out each name = value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		temp = unescape(pair[0]).split('+');
		temp0 = temp.join(' ');
		
		temp = unescape(pair[1]).split('+');
		temp1 = temp.join(' ');
		
		this[temp0]=temp1;
	}

	this.get=Querystring_get;
}

function Querystring_get(strKey,strDefault)
{
	var value=this[strKey];
	if (value==null)
	{value=strDefault;}
	
	return value;
}


