//MCID tracking
var cookis_input = document.URL.indexOf('://');
if (cookis_input != -1){ 
	cookaddr_str = document.URL.substring(cookis_input+3, document.URL.length);
}
else{
	cookaddr_str = document.URL;
}
cookis_input = cookaddr_str.indexOf('?');
if (cookis_input != -1){ 
	cookaddr_str = cookaddr_str.substring(0, cookis_input);
}

//search for MCID
var is_input = document.URL.indexOf('?');

if (is_input != -1){ 
	addr_str = document.URL.substring(is_input+1, document.URL.length);
	var tokenName = '';
	var tokenVal = '';
	var trackType = 1;
	var foundMCID = 0;
	var foundKWID = 0;
	var foundADID = 0;
	var mcid = '';
	var kwid = '0';
	var adid = '0';
	
	//Search Url String
	for (count = 0; count < addr_str.length; count++){
		if (addr_str.charAt(count) == '&') {
			if(tokenName.toLowerCase() == 'mcid'){
				foundMCID = 1;
				mcid = tokenVal;
			}
			if(tokenName.toLowerCase() == 'kwid'){
				foundKWID = 1;
				kwid = tokenVal;
			}
			if(tokenName.toLowerCase() == 'adid'){
				foundADID = 1;
				adid = tokenVal;
			}
			trackType = 1;
			tokenName = '';
			tokenVal = '';
		}
		if (addr_str.charAt(count) == '='){
			trackType = 2;
		}
		if (addr_str.charAt(count) != '&' && addr_str.charAt(count) != '='){
			if(trackType == 1){
				tokenName = tokenName + addr_str.charAt(count);
			}
			if(trackType == 2){
				tokenVal = tokenVal + addr_str.charAt(count);
			}
		}
	}	
	
	//Final Check
	if(tokenName.toLowerCase() == 'mcid'){
		foundMCID = 1;
		mcid = tokenVal;
	}
	if(tokenName.toLowerCase() == 'kwid'){
		foundKWID = 1;
		kwid = tokenVal;
	}
	if(tokenName.toLowerCase() == 'adid'){
		foundADID = 1;
		adid = tokenVal;
	}	
	
	if(foundMCID == 1 && getCookie('bisMCID').length == 0){
		setCookie('bisMCID',mcid);	
	}
	
	if(foundKWID == 1 && getCookie('bisKWID').length == 0){
		setCookie('bisKWID',kwid);	
	}
	
	if(foundADID == 1 && getCookie('bisADID').length == 0){
		setCookie('bisADID',adid);	
	}
	
	if(foundMCID == 1){
		//landing page - link
		if(getCookie('bisMCID').length > 0){			
			document.bisFollowPixIM.src = 'http://console.bisglobal.net/act_mrkLogClick.aspx?refPage=' + cookaddr_str + '&atid=2&adid=' + getCookie('bisADID') + '&mcid=' + getCookie('bisMCID') + '&kwid=' + getCookie('bisKWID');
		}		
	}
	else{
		//not landing page - click
		if(getCookie('bisMCID').length > 0){
			document.bisFollowPixIM.src = 'http://console.bisglobal.net/act_mrkLogClick.aspx?refPage=' + cookaddr_str + '&atid=3&adid=' + getCookie('bisADID') + '&mcid=' + getCookie('bisMCID') + '&kwid=' + getCookie('bisKWID');
		}
	}
}
else{
	//not landing page - click
	if(getCookie('bisMCID').length > 0){
		document.bisFollowPixIM.src = 'http://console.bisglobal.net/act_mrkLogClick.aspx?refPage=' + cookaddr_str + '&atid=3&adid=' + getCookie('bisADID') + '&mcid=' + getCookie('bisMCID') + '&kwid=' + getCookie('bisKWID');
	}
}

//alert(document.bisFollowPixIM.src);

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return '';
	} 
	else{
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1){
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) + 
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}