/*
    Filename: 	linktrack_funcs.js 
	Purpose:  	clientside tracking functionality. Reads flattened SED CMS html pages and transforms
				all links within the page with tracking data by replacing CMS created placeholders with channel / page info. 
				Also contains a method for tracking form submitions. All code is based on orginally cminclude.js file
				within the wanadooo portal.
	Author:		Phil Darley
	Date:		15.12.2005
*/

//URL for html tracking page must add ? @ end for parameters
var trackURL = "/pagebuilder/admin/tools/tool_track.htm?";
var bp_gobutton = "/images/sedtools/shared/";

function buildgoButton(){
	switch(getChannelName().toLowerCase()){
		case "auctions":
			bp_gobutton += "go_auctions.gif";
		break;
		case "betplaywin":
			bp_gobutton += "go_betplaywin.gif";
		break;
		case "motoring":
			bp_gobutton += "go_motoring.gif";
		break;
		case "communicate":
			bp_gobutton += "go_communicate.gif";
		break;
		case "entertainment":
			bp_gobutton += "go_entertainment.gif";
		break;
		case "games":
			bp_gobutton += "go_games.gif";
		break;
		case "membercentre":
			bp_gobutton += "go_membercentre.gif";
		break;
		case "mobilezone":
			bp_gobutton += "go_mobilezone.gif";
		break;
		case "money":
			bp_gobutton += "go_money.gif";
		break;
		case "music":
			bp_gobutton += "go_music.gif";
		break;
		case "news":
			bp_gobutton += "go_news.gif";
		break;
		case "property":
			bp_gobutton += "go_property.gif";
		break;
		case "shopping":
			bp_gobutton += "go_shopping.gif";
		break;
		case "sport":
			bp_gobutton += "go_sport.gif";
		break;
		case "travel":
			bp_gobutton += "go_travel.gif";
		break;
		default:
			bp_gobutton += "go.gif";
		break;
	}
	
}


// only write the tool tracking iframe for http pages
function drawTrackFrame(){	
	if (document.URL.substring(0,5) != 'https' && typeof document.getElementById("tooltrack")!= null ) 	{
		document.write('<div id="div_tooltrack" style="display:none;"><iframe id="tooltrack" name="tooltrack" width="0" height="0" src="about:blank"></iframe></div>');
		var iframe_loaded = "no";	
	}
}

function appendTrackFrame(){
	if (document.URL.substring(0,5) != 'https' && typeof document.getElementById("tooltrack")!= null ){
		var newIFrame = document.createElement("iFrame");
		
		newIFrame.setAttribute("id","tooltrack");
		newIFrame.setAttribute("name","tooltrack");
		newIFrame.setAttribute("src","about:blank");
		
		newIFrame.setAttribute("style","display:none;");
		newIFrame.setAttribute("height","0");
		newIFrame.setAttribute("width","0");
	
		document.body.appendChild(newIFrame);
	}
}

//function to get name of the current page
function getPageName()
{
	var s_str1 = new String();
	var s_str2 = new String();
	if (document.location.pathname == "/"){
		s_thisPagename = "Today";
	}
	else{
		var thePathnameShort = document.location.pathname;
		s_str1 = thePathnameShort.substring(1,thePathnameShort.length);
		var s_thisPagename = "";
		for (var h=0; h<s_str1.length; h++){
			if (s_str1.charAt(h) == "/")
				{s_thisPagename = s_thisPagename + "_";}
			else
				{s_thisPagename = s_thisPagename + s_str1.charAt(h);}
		}
	}
	if (s_thisPagename.indexOf("default") != -1) s_thisPagename = "Today";
	return s_thisPagename;
}

//function to return channel name, if we are in a channel
function getChannelName(){
	var theChannelName = "";
	document.location.pathname.split("/").length > 1? 
			theChannelName = document.location.pathname.split("/")[1] : theChannelName = "";
	return theChannelName;
	
}

//function to track all links within the DOM for the current page
function track_links(){

	var i_articlelinkno = 1;
	var reFileExt = /\..*/;
	var s_thisPagename = getPageName().replace(reFileExt,""); //gives us the replacement value for the bp_trackingPage value
	var re = /[^_a-zA-Z0-9]/g; //Regex for removing special characters
	var a_doclinks = document.links; //create an array of all link objects in the page
	var s_temp; //temp var to hold the replacement strings
	var s_thisChannel = getChannelName().replace(re,""); //replacement value for the bp_trackingChannel

	//Loop over all the links within the page replacing the placeholders
	for (var i_doclinks=0; i_doclinks<a_doclinks.length; i_doclinks++){
		
		s_temp = new String (unescape(a_doclinks[i_doclinks].href));
		s_temp = s_temp.replace("<!--linkfromvariable-->", s_thisPagename);
		s_temp = s_temp.replace("<!--channelnamevariable-->", s_thisChannel);
		if (s_temp.indexOf("%bp_archiveLinkNo%") != 0)	{
			s_temp = s_temp.replace("%bp_archiveLinkNo%", i_articlelinkno);
			i_articlelinkno++;
		}
		
		//Extra code to strip // from TP external links 
		if (s_temp.indexOf("//redirect") != -1)	{	
			s_temp = s_temp.replace("//redirect", "/redirect");	
		}
		
		document.links[i_doclinks].href = s_temp;
	}
}

function track_ttlinks(IframeId)	{

	//alert(IframeId);

	var reFileExt = /\..*/;
	var s_thisPagename = getPageName().replace(reFileExt,""); //gives us the replacement value for the bp_trackingPage value
	var re = /[^_a-zA-Z0-9]/g; //Regex for removing special characters
	var s_thisChannel = getChannelName().replace(re,""); //replacement value for the bp_trackingChannel
	var s_temp; //temp var to hold the replacement strings
	var a_tablinks = "";
	var browsertype="";
	
	//track links in tabbed tools
	
	
	
	if (typeof document.getElementById(IframeId) !='undefined')	
	{
	
		if (typeof document.getElementById(IframeId).contentDocument != 'undefined')	{
			if (typeof document.getElementById(IframeId).contentDocument.links != 'undefined')	{
				a_tablinks =  document.getElementById(IframeId).contentDocument.links;
				browsertype="moz";
			}
		}
		
		if (typeof document.frames != 'undefined')	{
			if (typeof document.frames[IframeId].document != 'undefined')	{	
				if (typeof document.frames[IframeId].document.links != 'undefined')	{
					a_tablinks = document.frames[IframeId].document.links;
					browsertype="ie";		
				}		
			}
		}
						
		if (browsertype != "")	{
		
			for (i_tablinks=0; i_tablinks<a_tablinks.length; i_tablinks++)	{
				
				i_articlelinkno = 1;
		
				if (browsertype == "moz") s_temp = new String (unescape(a_tablinks[i_tablinks].href));
				if (browsertype == "ie") s_temp = new String (unescape(a_tablinks[i_tablinks]));
				
				s_temp = s_temp.replace("<!--linkfromvariable-->", s_thisPagename);
				s_temp = s_temp.replace("<!--channelnamevariable-->", s_thisChannel);
			
				if (s_temp.indexOf("%bp_archiveLinkNo%") != 0)	{
					s_temp = s_temp.replace("%bp_archiveLinkNo%", i_articlelinkno);
					i_articlelinkno++;
				}
				
				//Extra code to strip // from TP external links 
				if (s_temp.indexOf("//") != 0)	{
					s_temp = s_temp.replace("//redirect", "/redirect");
				}
				if (browsertype == "moz") {document.getElementById(IframeId).contentDocument.links[i_tablinks].href = s_temp;}
				if (browsertype == "ie")  {document.frames[IframeId].document.links[i_tablinks].setAttribute('href',s_temp);}
			
			}
		}
	}
}

//Function to track dropdown links
function track_dropdown_links(linkToTrack)	{

	var reFileExt = /\..*/;
	var s_thisPagename = getPageName().replace(reFileExt,""); //gives us the replacement value for the bp_trackingPage value
	var re = /[^_a-zA-Z0-9]/g; //Regex for removing special characters
	var s_thisChannel = getChannelName().replace(re,""); //replacement value for the bp_trackingChannel
	var s_temp; //temp var to hold the replacement strings
	var i_articlelinkno = 1;
	s_temp = new String (unescape(linkToTrack));
	s_temp = s_temp.replace("<!--linkfromvariable-->", s_thisPagename);
	s_temp = s_temp.replace("<!--channelnamevariable-->", s_thisChannel);
	if (s_temp.indexOf("%bp_archiveLinkNo%") != 0)	{
		s_temp = s_temp.replace("%bp_archiveLinkNo%", i_articlelinkno);
		i_articlelinkno++;
	}
	
	//Extra code to strip // from TP external links 
	if (s_temp.indexOf("//") != 0)	{
		
		s_temp = s_temp.replace("//redirect", "/redirect");
		
	}
	
	return s_temp;
}

//function to track form submissions
function track(trackObj, s_link, s_act, partner_func){
		appendTrackFrame();
		try	{
			if (typeof partner_func != 'undefined'){	
				if (partner_func != true){
					return false;
				}
			}
			var rnd;
			rnd = Math.random();
			
			var s_act_nohttp = new String(s_act);
			if (s_act_nohttp.substring(0,7) == 'http://'){
				s_act_nohttp = s_act_nohttp.substring(7,s_act_nohttp.length);
			}
			var tracking_string = trackURL + "linkfrom="+ getPageName()+"&article="+trackObj+"&link="+s_link+"&linkto="+s_act_nohttp+"&rnd="+rnd;
			document.getElementById("tooltrack").src = tracking_string;
		}
		catch (e)	{
			alert(e);return false;
		}
		return true;
}

function WUKInit()	{	
	
	track_links();
	//Only set up a segQS var and loop if it isn't already defined.
	if (typeof segQS == 'undefined' && typeof rsinetsegs != 'undefined') {
		var segQS;
		segQS = rsinetsegs.length > 0 ? "s0=" + rsinetsegs[0] : "";
		for (var i = 1; i < rsinetsegs.length; i++)	{
		 	segQS += ("&s" + i + "=" + rsinetsegs[i]);
		}
	}
}

buildgoButton();
