var WTA;
// Declare global variable to hold all the tracking parameters
var v={};
var dotImgId;
var images=[];
var index=0;
var env = "";

// Empty resource residing on the metrics server
var trackingResource = "/529common/trpwa_alaska.txt";

// LogFlag to be passed so that the entry is not filtered by the Sensor
var logFlag = "Log=1";

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}


// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

function getEnv()
{
	var hostname = window.location.hostname;
	if (hostname.indexOf("peecm") != -1)
	{
		env = "stage";
		return;
	}
}

getEnv();

if (!WTA) WTA = {};
WTA.globalNamespace = this;

// create namespace
WTA.createNamespace = function(name){
    if (name.charAt(0) != '.' && name.charAt(name.length-1) != '.' && name.indexOf("..") == -1){
        var parts = name.split('.');
        var container = WTA.globalNamespace;
        var names = "";
        for (var i=0; i < parts.length; i++){
            var part = parts[i];
            names = names + part;
            // build Namespace
            if (part.substring(0,1) != part.substring(0,1).toUpperCase() || part == 'WTA'){ // don't handle classes
                if (!container[part])container[part] = {};
                container = container[part];
                if (!container.NAME) container.NAME = names;
                if (i < parts.length -1) names = names + '.';
            }
        }
        var namespace = container;
        return namespace;
    }
};

// Create namespace for the class
WTA.createNamespace('WTA.utils');

WTA.utils.tags ={
  data_count:0,
  // initialize method.. This adds the basic tracking parameters to the v global variable.
  // 
  initializealaska:function(){
      if (typeof v != "undefined")
	  {
		  v["v_pn"]=document.URL;
		  v["v_site"]="ALSK";
	      v["v_event"]="LD";
	  }
	  data_count=0;
  },

 // this method add the template type of the page rendered. This method is mainly used for Retail currently. 
 // Template type can something like Core, Promotional etc.

 addtemplate:function(template){
      if (typeof v != "undefined")
	  {
		  v["v_template"]=template;
	  }
  },


  // this method is used to track different components placed on the V7 page.
  // the method accepts the component, its placement and its type as parameter
  // There may be multiple components displayed on the page
  // Thus this method gets called multiple times.
  addComponent:function(component,placement,comp_type){
	  if (typeof v != "undefined")
	  {
		  if (data_count == 0)
		  {
			v["v_page_data"] = "v_comp=" + component + "|" + "v_plmt=" + placement + "|" + "v_comp_type=" + comp_type;
		  }
		  else
		  {
			v["v_page_data"] += "~" + "v_comp=" + component + "|" + "v_plmt=" + placement + "|" + "v_comp_type=" + comp_type;
		  }
		  data_count++;
	  }
  },

  // Once all the data which needs to be tracked during Page Load event has been added, this method is called to send the data to metrics 
  // page dot server
  flush:function(){
      var now = new Date();
//	  getMetricsServer();
	  var ct = "<img src=";
	  var cd = ""; 
	  var cu = trackingResource+"?"+logFlag; 
	  var ce = " border=0 height=1 width=1>";

	  var c = {};
//	  c["sw"] = screen.width;
//	  c["sh"] = screen.height;
//	  c["cd"] = screen.colorDepth;
      c["cb"] = now.getTime();
	  var co = "";

	  for ( cKey in c ) {
		  co = co+"&"+cKey+"="+escape(c[cKey]);
	  }
	  
	  var d = {};
//	  d["dt"] = document.title;
	  d["dr"] = document.referrer;
	  d["du"] = document.URL;

	// Version check based upon the values entered above in "Globals"
	  v["v_fv"]=GetSwfVer();

	  if (env != "")
	  {
		v["v_env"]=env;
	  }

	  var vo = "";

	  for ( dKey in d ) {
		  vo = vo+"&"+dKey+"="+escape(d[dKey]);
	  }

	  if (typeof v != "undefined") {
		  for ( vKey in v ) {
			  vo = vo+"&"+vKey+"="+escape(v[vKey]);
		  }
	  }
	  document.write(ct,cd,cu,vo,co,ce);
  },


  // this method is called for Retail application when a link is clicked on a page and a JavaScript is called when the link is clicked. 
  // This method is similar to sendretaillink. The only difference is that it has another parameter called the placement of the link clicked
  // The user is not taken to another page. Something like when Expand/Collapse is called on FAQ page or when a tab is clicked on a pag
  sendalaskalinkp:function(component,linktext,linkplmt){
	return WTA.utils.tags.sendlinkptag(component,linktext,linkplmt,"ALSK");
  },

  // This method adds the tracking parameter needed to track the link click event
  // It create a tracking string and then send the data to the send method
  sendlinkptag:function(component,linktext,linkplmt,site){
	var queryparam = "v_site=" + site + "&v_event=CLK&v_comp=" + escape(component) + "&v_link=" + escape(linktext) + "&v_linkplmt=" + (linkplmt) + "&v_edu=" + escape(document.URL);
	WTA.utils.tags.send(queryparam);
  },

  // If the link clicked sends the user to a site which is external to TRP domains, then this method is called
  // This method is called when the onclick event calls a javascript method rather than an absolute URL
  sendalaskaexitjs:function(element,component,linktext,linkplmt){
	return WTA.utils.tags.sendexitjstag(element,component,linktext,linkplmt,"ALSK");
  },

  // this method is called to append tracking parameters to the URL where the user needs to go to.
  sendexitjstag:function(element,component,linktext,linkplmt,site){
	var extlnkref = "";
	if (typeof element != undefined)
	{
		if (typeof element.href != "undefined")
		{
			extlnkref = element.href;
		}
		else
		{
			extlnkref = element;
		}
	}
	var queryparam = "v_site=" + site + "&v_event=EXTCLK&v_comp=" + escape(component) + "&v_link=" + escape(linktext) + "&v_linkplmt=" + (linkplmt) + "&v_edu=" + escape(document.URL) + "&v_extlnk=" + escape(extlnkref);
	WTA.utils.tags.send(queryparam);
  },

  // If the link clicked sends the user to a site which is external to TRP domains, then this method is called
  // This method is called when the onclick event calls an absolute URL and not a javascript method
  sendalaskaexiturl:function(element,component,linktext,linkplmt){
	 return WTA.utils.tags.sendexiturltag(element,component,linktext,linkplmt,"ALSK");
  },

  // this method is called to append tracking parameters to the URL where the user needs to go to.
  sendexiturltag:function(element,component,linktext,linkplmt,site){
	var extlnkref = "";
	if (typeof element != undefined)
	{
		if (typeof element.href != "undefined")
		{
			extlnkref = element.href;
		}
		else
		{
			extlnkref = element;
		}
	}

	var queryparam = "v_site=" + site + "&v_event=EXTCLK&v_link=" + escape(linktext) + "&v_linkplmt=" + escape(linkplmt) + "&v_edu=" + escape(document.URL) + "&v_extlnk=" + escape(extlnkref);
	WTA.utils.tags.send(queryparam);
  },

  // If the link clicked sends the user to a site which is external to TRP domains, then this method is called
  // This method is called when the onclick event calls an absolute URL and not a javascript method
  sendalaskaexiturl2:function(element,component,linktext,linkplmt){
	 return WTA.utils.tags.sendexiturltag2(element,component,linktext,linkplmt,"ALSK");
  },

  // this method is called to append tracking parameters to the URL where the user needs to go to.
  sendexiturltag2:function(element,component,linktext,linkplmt,site){
	var extlnkref = "";
	if (typeof element != undefined)
	{
		if (typeof element.href != "undefined")
		{
			extlnkref = element.href;
		}
		else
		{
			extlnkref = element;
		}
	}

	var queryparam = "v_site=" + site + "&v_event=EXTCLK&v_link=" + escape(linktext) + "&v_linkplmt=" + escape(linkplmt) + "&v_edu=" + escape(document.URL) + "&v_extlnk=" + escape(extlnkref);
	WTA.utils.tags.send(queryparam);
	window.setTimeout(function(){WTA.utils.tags.navigate(element.href);}, 1000);
    return false;
  },

  // this method is called when a link is clicked on a page and a JavaScript is called when the link is clicked. 
  // This method is similar to sendalaskalink. The only difference is that it has another parameter called the placement of the link clicked
  // The user is not taken to another page. Something like when Expand/Collapse is called on FAQ page or when a tab is clicked on a pag
  sendalaskalinkp:function(component,linktext,linkplmt){
	return WTA.utils.tags.sendlinkptag(component,linktext,linkplmt,"ALSK");
  },

  // This method adds the tracking parameter needed to track the link click event
  // It create a tracking string and then send the data to the send method
  sendlinkptag:function(component,linktext,linkplmt,site){
	var queryparam = "v_site=" + site + "&v_event=CLK&v_comp=" + escape(component) + "&v_link=" + escape(linktext) + "&v_linkplmt=" + (linkplmt) + "&v_edu=" + escape(document.URL);
	WTA.utils.tags.send(queryparam);
  },

  // this method is called to track when the onChange events with in a drop down list box
  sendalaskaonchange:function(element,component,additionalparam){
	return WTA.utils.tags.sendonchangetag(element,component,"ALSK",additionalparam);
  },
 
  // this method is called to append tracking parameters and sends a page dot to the metric server.
  sendonchangetag:function(element,component,site,additionalparam){
	var queryparam = "v_site=" + site + "&v_event=CHNG&v_comp=" + escape(component) + "&v_slctd=" + escape(element.options[element.selectedIndex].value) + "&v_slctd_desc=" + escape(element.options[element.selectedIndex].text);
	if (typeof additionalparam != "undefined")
	{
		queryparam += "&" + additionalparam;
	}
	WTA.utils.tags.send(queryparam);
  },

  // create a page dot and sends the information over to metrics server
  send:function(queryparam){
//	  getMetricsServer();
	  if (env != "")
	  {
		queryparam += "&v_env=" + env;
	  }

      var now = new Date();
      var wtatrpImg = document.createElement("img");
//	  var src = metricsServer + trackingResource + "?" + logFlag + "&" + queryparam + "&cb="+now.getTime();
	  var src = trackingResource + "?" + logFlag + "&" + queryparam + "&cb="+now.getTime();

      wtatrpImg.setAttribute("src",src);
      wtatrpImg.width = wtatrpImg.height = 1;

      // remove the old image if it exists
      if (dotImgId != 'undefined' && dotImgId != '' && dotImgId != null)
      {
         document.body.removeChild(dotImgId);
      }
      dotImgId = document.body.appendChild(wtatrpImg);
  },

  // create a page dot and sends the information over to metrics server
  send2:function(queryparam){
//	  getMetricsServer();
	  if (env != "")
	  {
		queryparam += "&v_env=" + env;
	  }

      var now = new Date();
//	  var src = "http:" + metricsServer + trackingResource + "?" + logFlag + "&" + queryparam + "&cb="+now.getTime();
	  var src = trackingResource + "?" + logFlag + "&" + queryparam + "&cb="+now.getTime();
        
      if (document.images){
		images[index]=new Image();
		images[index].src=src;
		index++;
	  }
  },

  navigate:function(url){
    document.location.href = url;
   }
};
