vcLinkArray = new Array();

window.onload = function() { Init(); };

//How long the cookie should last... In Days
var cookieLife = 1;	 

//Overwrite Cookies?
//1=Yes and 0=No 
//Ex: If user visits with kbid=1001, then later with kbid=1002, do you want to overwrite the first cookie? 
var overwriteCookies = 1; 

var scriptLoc = ''; //URL to the users Processor.aspx script. 
 
//Determine http or https 
if (document.URL.indexOf('https:') > -1) 
{ 
	scriptLoc = 'https://'; 
} 
else 
{ 
	scriptLoc = 'http://'; 
} 

var domain = scriptLoc + 'www.microsoftaffiliates.net/';
domain = ModifyDomainIfTest(domain);

//Finish off url 
scriptLoc = domain + 'MAPProc.aspx';	//URL to the users Processor.aspx script. 
 
 
//------------------------------------------------------- 
//         Don't edit anything below this line!! 
//------------------------------------------------------- 
var img = new Image(); 
var aImg = new Image();
var kbId = 0; 

//Build the url 
var url = scriptLoc + '?'; 
url += '&curUrl=' + escape(document.URL); 
url += '&refUrl=' + escape(document.referrer); 
url += '&c=' + escape(window.screen.colorDepth) 
url += '&sw=' + escape(self.screen.width); 
url += '&sh=' + escape(self.screen.height); 
url += '&winid=' + escape(window.name); 
url += '&ow=' + overwriteCookies.toString();

//create a date object 1 day ahead
var cookieLifeDate = new Date();
cookieLifeDate.setDate(cookieLifeDate.getDate()  + 1);


SetCookie('test', '1', cookieLifeDate);
var testVal = GetCookie('test');
DeleteCookie('test');

var hasCookies = 0;

if(testVal == '1')
	hasCookies = 1;

url += "&cookies=" + hasCookies.toString();

img.src = url; 

if ((GetCookie('kbid') == null) || (overwriteCookies == 1)) 
{ 
	kbid = GetQSItem('kbid');//Only look for kbid if there is a query string 
	if(kbid == null) kbid = 0;
} 
 
//If kbId is set, then set a cookie. 
if (kbId != 0) 
{ 
	SetCookie('kbid', kbId, cookieLife);		//'kbid' is cookie name, kbId is the javascript variable, 1 (or the last variable) is the amount of days before the cookie expires. 
} 

 //----------------
//Action Processor
//----------------

function AProc(aID, profit, id, life, link)
{
	var qs = GetQS(link.href);
	var guid = v9_GetVCLinkGuid(link);

	aImg = new Image();
	var x = Math.round(Math.random()*9999999);
	aImg.src = domain + 'AProc.aspx?aID=' + aID.toString() + '&p=' + profit.toString() + '&id=' + escape(id.toString()) + '&l=' + escape(life) + '&curUrl=' + escape(document.URL) + '&x=' + x.toString() + ((guid != null) ? '&suid=' + guid : '');

	pause(500);

}
 

function pause(ms)
{

	d = new Date(); //today's date
	while (1)
	{
		mill=new Date(); // Date Now
		diff = mill-d; //difference in milliseconds
		if( diff > ms ) {break;}
	}
}


//----------------
//Valid Click functions
//----------------

function v9_GetVCLinkGuid(link)
{
	//Loop through the vcArray, and if link objects match, return its GUID
	for(var i = 0; i < vcLinkArray.length; i++)
		if(vcLinkArray[i][0] == link)
			return vcLinkArray[i][1];
			
	return null;
		
}

function Init()
{
	
	buildLinkArray(); 
	vc = new Vc(ModifyDomainIfTest('http://msx.kowabunga.net/1.php'));

	
}

function getGUID(length)
{

	var guid = ''

	for(var i = 0; i < length; i++)
	{
		var x = 0;
		while( (x >= 58 && x <= 64 ) || (x >= 91 && x <= 96) || x == 0 )
			x = Math.floor(Math.random()*74) + 48;

		guid += String.fromCharCode(x);

	}

	return guid;
}

function buildLinkArray()
{


	//Get all anchor elements on the page
	var links = document.getElementsByTagName('a');
	var vcIdx = 0;

	//Look through each link on the page, and store each one with an attached MyAP action call along with an associated GUID
	for(var i = 0; i < links.length; i++)
	{
		var s = links[i].getAttribute('onclick');
            
		if(s != null && s.toString().indexOf('vc.Mc(this)') != -1)  //AProc
		{
			vcLinkArray[vcIdx] = new Array(2);
			vcLinkArray[vcIdx][0] = links[i];
			vcLinkArray[vcIdx][1] = getGUID(32);
			vcIdx++;


		}
	}
}

//----------------
//QS Parser
//----------------
function GetQS(url)
{
	var start = url.indexOf('?');
	
	if(start != -1)
		return url.substring(start+1);		

	return null;
}


function GetQSItem(item, qs)
{
 
	if(qs == null)
		return null;

	if (qs.length > 0) 
	{ 
		var pairs = qs.split('&'); 
			 
		for(var i = 0; i < pairs.length; i++) 
		{ 
			var pairs2 = pairs[i].split('='); 
			 
			switch(pairs2[0].toLowerCase()) 
			{ 
				case item.toLowerCase(): 
					return pairs2[1]; 
					break; 
			} 
		} //end for 
	}//end if 

	return null;
}

 
