
// Description:
//	This file contains the definition for the WT javascript object.  The WT object
//	provides an API for gathering information about the page or action you wish to
//	report to WebTrends, and communicates this information to the WebTrends data
//	collection server.
//
// Usage:
//	To include WebTrends tracking in your site/pages, you must first
//	include this file:
//		<script type="text/javascript" src="[server_hosting_this_file]/WT.js"></script> 
//
//	Next, to use the WT object to track your events, you must initialize it:
//		WT.init([site_id]);
//	where [site_id] is the unique identifier WebTrends uses to identify your site.
//
//	After it is initialized you reference the WT object through the current document
//	object, "document.wt".  You can use any of the WT methods to manipulate the
//	data send to the data collection server.
//	To send the tracking data to the DCS, call the "send" method:
//		document.wt.send();
//
// History:
//	7/8/03   - SD  - Created.
//  8/4/03   - SD  - Added "siteId=" to displayAll().
//	8/24/05	- TJS - Added trackInterstitial function.


/**========================  WT  ==========================
' @Method		WT
' @Purpose		Constructor for WT object.
'
' @Param		siteId (String) The unique ID for the site the WT object is being
'						instantiated for.
' @Param		dcsServer (String) Optional. The URL of the DCS (data collection
'						service) server.
'						Default is "dcs.mattel.com".
'
' @Author		Scott Dyke
' @Date Written 7/8/03
''--*/
function WT(
	siteId,
	dcsServer)
{

	this.dcsImgarray = new Array;	// dcs_imgarray
	this.dcsImgCnt = 0;				// dcs_ptr
	this.curDate = new Date();		// dCurrent
	this.DCS = new Object();
	this.WT = new Object();
	this.DCSext = new Object();

	if (dcsServer) {
		this.dcsServer = dcsServer;
	} else {
		this.dcsServer = "dcs.mattel.com";
	}
	this.dcsSiteId = siteId;

	this.DCS.dcsuri = "";
	this.debug = false;

			//  initialize the WT and DCS collections
	this.initialize();
			//  read/process meta tags
	this.readMetaTags();
	//makeWTVars();
	//detectAll();
//	dcs_TAG(TagPath);
}


/**========================  init  ==========================
' @Method		init
' @Purpose		Static method to initialize the WT instance and
'				add it to the current document object.
'
' @Param		siteId (String) The unique ID for the site the WT object is being
'						instantiated for.
' @Param		dcsServer (String) Optional. The URL of the DCS (data collection
'						service) server.
'						Default is "dcs.mattel.com".
'
' @Author		Scott Dyke
' @Date Written	7/8/03
''--*/
WT.init = function(
	siteId,
	dcsServer)
{
	document.wt = new WT(siteId, dcsServer);
}


/**========================  setSiteId  ==========================
' @Method		setSiteId
' @Purpose		Sets the site ID used by WebTrends to associate the event(s)
'				with a site.
'
' @Param		id (String) The unique ID for the site.
'
' @Author		Scott Dyke
' @Date Written	7/8/03
''--*/
WT.prototype.setSiteId = function(
	id)
{
	this.dcsSiteId = id;
}


/**========================  setUri  ==========================
' @Method		setUri
' @Purpose		Sets the URI reported to the DCS, the page the event
'				occurred on.
'
' @Param		uri (String) The URI to report to the DCS.
'
' @Author		Scott Dyke
' @Date Written	7/8/03
''--*/
WT.prototype.setUri = function(
	uri)
{
	this.DCS.dcsuri = uri;
}


/**========================  getHostName  ==========================
' @Method		getHostName
' @Purpose		Returns the host name to be reported to the DCS.
'
' @Returns		String containing the host name to be reported to the DCS.
'
' @Author		Scott Dyke
' @Date Written	7/9/03
''--*/
WT.prototype.getHostName = function()
{
	return this.DCS.dcssip;
}


/**========================  setHostName  ==========================
' @Method       setHostName
' @Purpose      Sets the host name to be reported to the DCS.
'
' @Param        host (String) The host name to report to the DCS.
'
' @Author       Scott Dyke
' @Date Written 7/9/03
''--*/
WT.prototype.setHostName = function(
	host)
{
	this.DCS.dcssip = host;
}


/**========================  setDebug  ==========================
' @Method       setDebug
' @Purpose      Turns debugging on or off.
'
' @Param        onOrOff (boolean) <code>True</code> to turn debugging on,
'						<code>false</code> to turn it off.
'
' @Author       Scott Dyke
' @Date Written 7/9/03
''--*/
WT.prototype.setDebug = function(
	onOrOff)
{
	this.debug = onOrOff;
	
	if (this.debug) {
		alert('WT Flash/Tile debug on')
	} else {
		alert('WT Flash/Tile debug off')
	}
}


/**========================  buildQSPair  ==========================
' @Method       buildQSPair
' @Purpose      Builds a name/value pair for use in a query string, of
'				the form "&name=value".
'
' @Param        name (String) The name of the parameter.
' @Param        value (String) The value of the parameter.
'
' @Return		String containing the name/value pair prepared for use
'				in a querystring.
'
' @Author       Scott Dyke
' @Date Written 7/8/03
''--*/
WT.prototype.buildQSPair = function(
	name, 
	value)
{
	return "&" + name + "=" + escape(value);
}


/**========================  addQSVars  ==========================
' @Method       addQSVars
' @Purpose      Adds the parameters from the given url/querystring to
'				the internal collections.  Parameters beginning with
'				"WT." are added to the WT collection.  The remainder
'				are added to the DCSext collection.
'
' @Param        url (String) The URL or querystring containing the parameters
'						to add to the information sent to the DCS.
'
' @Author       Scott Dyke
' @Date Written 7/8/03
''--*/
WT.prototype.addQSVars = function(
	url)
{
	var temp;	
	var pos = url.indexOf("?");

			// if querystring exists
	if (pos >= 0) {
		var qs = url.substring(pos + 1);

		if (qs.indexOf("=") >= 0) {
					// split into name/value pairs
			var params = qs.split('&');

			for (var i = 0; i < params.length; i++) { // add to WT collection
				if (params[i].indexOf('WT.') >= 0) {
					temp = dcsqri[i].split('=');
					if (temp[1]) {
						this.WT[temp[0].replace("WT.", "")] = temp[1];
					}
				} else {		// add to DCSext collection for customer parameters
					temp = params[i].split('=');
					if (temp[1]) {
						this.DCSext[temp[0]] = temp[1];
					}
				}
			}
		}

				// if target exists
		if (url.indexOf("#") >= 0) {
			var target = String("target");
			this.DCSext[target] = url.substring(url.indexOf("#") + 1);
		}
	}
}


/**========================  send  ==========================
' @Method       send
' @Purpose      Sends all of the data defined for the page/event to
'				the DCS server.
'
' @Author       Scott Dyke
' @Date Written 7/8/03
''--*/
WT.prototype.send = function()		//  was dcs_TAG
{
	var dcsServer = this.dcsServer;
	if ((this.dcsSiteId != null) && (this.dcsSiteId != "")) {
		dcsServer += "/" + this.dcsSiteId;
	}
	var trackUrl = "http" + (window.location.protocol.indexOf('https:') == 0 ? 's' : '')
			+ "://" + dcsServer + "/dcs.gif?";
	var qs = "";

	for (N in this.DCS) {
		qs += this.buildQSPair(N, this.DCS[N]);
	}
	for (N in this.WT) {
		qs += this.buildQSPair("WT." + N, this.WT[N]);
	}
	for (N in this.DCSext) {
		qs += this.buildQSPair(N, this.DCSext[N]);
	}
			//  hack off the beginning "&", so we don't get "?&..."
	qs = qs.substring(1);
	trackUrl += qs;

	if (this.debug) {
//		alert("send: " + trackUrl);
		this.displayAll();
	}
	this.createImage(trackUrl);
}


/**========================  displayAll  ==========================
' @Method       displayAll
' @Purpose      Displays all of the data currently contained in the internal
'				collections in an alert box.  This is used for testing purposes.
'				It can be called directly from the browser by entering
'				"javascript:document.wt.displayAll();" in the address bar.
'
' @Author       Scott Dyke
' @Date Written 7/8/03
''--*/
WT.prototype.displayAll = function()
{
	var vars;
	
	vars = "****DCS Collection****\n";
	for (N in this.DCS) {
		vars += N + "=" + this.DCS[N] + "\n";
	}

	vars += "\n****WT Collection****\n";
	for (N in this.WT) {
		vars += N + "=" + this.WT[N] + "\n";
	}

	vars += "\n****DCSext Collection****\n";
	for (N in this.DCSext) {
		vars += N + "=" + this.DCSext[N] + "\n";
	}

	vars += "\n\nsiteId= " + this.dcsSiteId + "\n"
			+ "# Of Calls to DCS " + this.dcsImgarray.length + "\n";


	alert(vars);
}


/**========================  initialize  ==========================
' @Method       initialize
' @Purpose      Initializes the core WT and DCS collections based on
'				the current page settings.
'
' @Author       Scott Dyke
' @Date Written 7/8/03
''--*/
WT.prototype.initialize = function()		//  was dcs_var
{
	this.WT.tz = this.curDate.getTimezoneOffset();
	this.WT.ul = (navigator.appName == "Netscape") ? navigator.language : navigator.userLanguage;
	this.WT.cd = screen.colorDepth;
	this.WT.sr = screen.width + "x" + screen.height;
	this.WT.jo = navigator.javaEnabled() ? "Yes" : "No";
	this.WT.ti = document.title;
	this.WT.js = "Yes";
	this.DCS.dcsdat = this.curDate.getTime();
	if ((window.document.referrer != "") && (window.document.referrer != "-")) {
		if (!(navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) < 4) ){
			this.DCS.dcsref = window.document.referrer;
		}
	}

	if (this.DCS.dcsuri == "") {
		this.DCS.dcsuri = window.location.pathname;
	}
	this.DCS.dcsqry = window.location.search;
	this.DCS.dcssip = window.location.hostname;
}


/**========================  clearCollection  ==========================
' @Method       clearCollection
' @Purpose      Removes all items from the given collection(s).
'
' @Param        arg (collection) The collection(s) to be cleared.  Any number of
'						collections may be given.
'
' @Author       Scott Dyke
' @Date Written 7/8/03
''--*/
WT.prototype.clearCollection = function()
{

	for (var k = 0; k < arguments.length; k++) {
		var collection = arguments[k];
		for (var item in collection) {
			delete collection[item];
		}
	}
}


/**========================  readMetaTags  ==========================
' @Method       readMetaTags
' @Purpose      Reads the <META> tags in the current page and updates
'				data in the WT, DCS, and DCSext collections accordingly.
'
' @Author       Scott Dyke
' @Date Written 7/8/03
''--*/
WT.prototype.readMetaTags = function()
{
	var MRV = "";
	var foundMetas = false;
	var myDocumentElements;

	if (document.all) {
		foundMetas = true;
		myDocumentElements = document.all.tags("meta");
	}
	if (!foundMetas && document.documentElement) {
		foundMetas = true;
		myDocumentElements = document.getElementsByTagName("meta");
	}
	if (foundMetas) {
		for (var i = 1; i <= myDocumentElements.length; i++) {
			myMeta = myDocumentElements.item(i - 1);
			if (myMeta.name.indexOf('WT.') == 0) {
				this.WT[myMeta.name.substring(3)] = myMeta.content;
			}
			if (myMeta.name.indexOf('DCSext.') == 0) {
				this.DCSext[myMeta.name.substring(7)] = myMeta.content;
			}
			if (myMeta.name.indexOf('DCS.') == 0) {
				this.DCS[myMeta.name.substring(4)] = myMeta.content;
			}
		}
	}
}


/**========================  createImage  ==========================
' @Method       createImage
' @Purpose      Dynamically creates a new image in the current document
'				with the given source.  This is used by <code>send</code>
'				to communicate the data from the internal collections
'				to the DCS server.
'
' @Param        src (String) The URL for the image to create.
'
' @Author       Scott Dyke
' @Date Written 7/8/03
''--*/
WT.prototype.createImage = function(
	src)
{
	if (document.images) {
		this.dcsImgarray[this.dcsImgCnt] = new Image;
		this.dcsImgarray[this.dcsImgCnt].src = src;
		this.dcsImgCnt++;
	}
	//alert(this.dcsImgarray.length);
}


/**========================  flashTrack  ==========================
' @Method       flashTrack
' @Purpose      To be used by flash actionscripts to report events to
'				the DCS server.
'
' @Param
'
' @Author       Scott Dyke
' @Date Written 7/17/03
''--*/
WT.prototype.flashTrack = function()
{
	this.DCS.dcsuri = null; 
	this.clearCollection(this.DCSext);
//	this.clearCollection(this.WT, this.DCSext);
//	this.initialize();

	if ((typeof(arguments[0]) == "object") && (arguments[0].constructor == Array)) {
alert("it's an array!");
	} else {

		for (var k = 0; k < arguments.length; k += 2) { 
			if (arguments[k].indexOf('WT.') == 0){ 
				this.WT[arguments[k].substring(3)] = arguments[k + 1]; 
			} 
			if (arguments[k].indexOf('DCS.') == 0) { 
				this.DCS[arguments[k].substring(4)] = arguments[k + 1]; 
			} 
			if (arguments[k].indexOf('DCSext.') == 0) { 
				this.DCSext[arguments[k].substring(7)] = arguments[k + 1]; 
			} 
			if (arguments[k].indexOf('dcsID') == 0) { 
				this.dcsSiteId = arguments[k + 1]; 
			} 
		} 
	}

	this.DCS.dcsdat = this.curDate.getTime(); 

	if (this.DCS.dcsuri == null) { 
		this.DCS.dcsuri = this.WT.ti + ".ftrk"; 
	} 
//this.displayAll();
	this.send();

			// following line for testing only
			// will display parameters if hostname = fpdev, but never for live
	//if (this.DCS.dcssip == "fpdev") {
	//	this.displayAll();
	//}
}

function FlashTrack() 
{ 
	document.wt.flashTrack();
}
/*
WT.prototype.flashAryToStr = function(
	ary)
{
	var str = "";

	for (var k = 2; k < ary.length; k++) {
		arrTrack = ary[k].split("=");
		if (arrTrack[0].indexOf("DCS.") == -1 && arrTrack[0].indexOf("WT.") == -1) {
			if (eval(arrTrack[1]) != undefined){
				retVal += ", 'DCSext." + arrTrack[0] + "', '";
				sTemp = replaceSymbols(eval(arrTrack[1])) + "'";
				retVal += sTemp;
			}
		} else {
			retVal += ", '" + arrTrack[0] + "', '" + arrTrack[1]+"'";
		}
	}

	return str;
}


WT.prototype.replaceSymbols = function(
	str)
{
	var retVal = str;
	var arrReplace = new Array("'", "&#39;", "$", "&#36;", "%", "&#37;", "*", "&#42;", "+", "&#43;", "@", "&#64;", "^", "&#94;", "`", "&#96;", "{", "&#123;", "|", "&#124;", "}", "&#125;", "~", "&#126;", "~", "&#126;", "ÿ", "&#255;", "þ", "&#254;", "ý", "&#253;", "ü", "&#252;", "û", "&#251;", "ú", "&#250;", "ù", "&#249;", "ø", "&#248;", "÷", "&#247;", "ö", "&#246;", "õ", "&#245;", "ô", "&#244;", "ó", "&#243;", "ò", "&#242;", "ñ", "&#241;", "ð", "&#240;", "ï", "&#239;", "î", "&#238;", "í", "&#237;", "ì", "&#236;", "ë", "&#235;", "ê", "&#234;", "é", "&#233;", "è", "&#232;", "ç", "&#231;", "æ", "&#230;", "å", "&#229;", "ä", "&#228;", "ã", "&#227;", "â", "&#226;", "á", "&#225;", "à", "&#224;", "ß", "&#223;", "Þ", "&#222;", "Ý", "&#221;", "Ü", "&#220;", "Û", "&#219;", "Ú", "&#218;", "Ù", "&#217;", "Ø", "&#216;", "×", "&#215;", "Ö", "&#214;", "Õ", "&#213;", "Ô", "&#212;", "Ó", "&#211;", "Ò", "&#210;", "Ñ", "&#209;", "Ð", "&#208;", "Ï", "&#207;", "Î", "&#206;", "Í", "&#205;", "Ì", "&#204;", "Ë", "&#203;", "Ê", "&#202;", "É", "&#201;", "È", "&#200;", "Ç", "&#199;", "Æ", "&#198;", "Å", "&#197;", "Ä", "&#196;", "Ã", "&#195;", "Â", "&#194;", "Á", "&#193;", "À", "&#192;", "¿", "&#191;", "¾", "&#190;", "½", "&#189;", "¼", "&#188;", "»", "&#187;", "º", "&#186;", "¹", "&#185;", "¸", "&#184;", "·", "&#183;", "¶", "&#182;", "µ", "&#181;", "´", "&#180;", "³", "&#179;", "²", "&#178;", "±", "&#177;", "°", "&#176;", "¯", "&#175;", "®", "&#174;", "¬", "&#172;", "«", "&#171;", "ª", "&#170;", "©", "&#169;", "¨", "&#168;", "§", "&#167;", "¦", "&#166;", "¥", "&#165;", "¤", "&#164;", "£", "&#163;", "¢", "&#162;", "¡", "&#161;", "™", "&#153;", "”", "&#148;", "—", "&#151;", "’", "&quot;", "´", "&quot;", "‘", "&quot;", "…", "&#133;", "„", "&#987;", "“", "&#789;", "–", "&#173;", "¡", "&#0161;", "<br>", "--BREAK--", "<BR>", "--BREAK--", "</nobr>", "--NOBREAKEND--", "</NOBR>", "--NOBREAKEND--", "<nobr>", "--NOBREAK--", "<NOBR>", "--NOBREAK--");

	//testDCS = "'DCSext.Before', 'Loop'";
	for(var i = 0; i < arrReplace.length; i += 2){
		var cnt = 0;
		while (cnt < retVal.length){
			var start = retVal.indexOf(arrReplace[i], cnt);
			if (start == -1) {
				break;
			} else {
				var before = retVal.substr(0, start);
				var after = retVal.substr(start + arrReplace[i].length, retVal.length);
				retVal = before + arrReplace[i + 1] + after;
				cnt = before.length + arrReplace[i].length;
			}
		}
	}

	return retVal;
}
*/


/*
function displayAll()
{
	document.wt.displayAll();
}
*/

// ========================= TrackInterstitial() ===============================
// This function 'head-fakes' functionality of old interstatial pages such as golink.asp, kwtrack.asp etc.
// Receives:	type = string to denote tracking type
//				paramName = name applied to DCSext parameter
//				paramValue = value applied to DCSext parameter
WT.prototype.trackInterstitial = function(
	type, 
	paramName, 
	paramValue, 
	path)
{
	var sPage;
	var sType = type.toLowerCase();
	
	paramValue = paramValue.toString();
	switch (sType){
		case 'homepage':
			sPage = 'golink.asp';
			break;
		case 'search':
			sPage = 'kwtrack.asp';
			break;
		case 'demo':
			sPage = 'demoview.asp';
			break;	
		case 'game':
			sPage = 'game.asp';	
			break;
		case 'coloring':
			sPage = 'coloringpage.asp';
			break;
		case 'microsite':
			sPage = 'microsite.asp';
			break;	
		case 'lpproduct':
			sPage = 'littlepeople/products/product.asp';
			break;	
		case 'lpgame':
			sPage = 'littlepeople/game.asp';
			break;
		case 'path':
			sPage = path;
			break;
		case 'rebate':
			sPage = path + paramValue + '_print.asp';
			break;
		case 'sweeps':
			sPage = 'ref/'+ path + '/' + paramValue.toLowerCase() + '_' + paramName.toLowerCase() + '.asp';
			break;
		case 'video':
			sPage = path + 'videoOfferForm.asp';
			break;
		case 'party':
			sPage = 'party.asp';
			break;
		case 'onlinegame':
			sPage= 'onlinegame.asp';
			break; 
		case 'fpaspx':
			sPage = path;
			break;			
		case 'ecomm':					
			sPage = path;			
			break;	
		case 'pid':					
			sPage = path;			
			break;						
	}
	
	if (sType == "fpaspx"){
		this.DCS.dcsuri = '/fp.aspx';
		this.DCSext[paramName] = paramValue;
		this.DCSext.s= sPage;
		//dcs_TAG(TagPath);
		this.send();
		}
	else if (sType == "pid"){		
		this.DCS.dcsuri = '/fp.aspx';
		this.DCSext.s= sPage;		
		this.DCSext.pid = paramName;		
		this.DCSext.realp = paramValue;		
		//dcs_TAG(TagPath);			
		this.send();
		}						
	else if (sType == "ecomm"){		
		this.DCS.dcsuri = '/fp.aspx';
		this.DCSext.s= 'fpstore';
		this.DCSext[paramName] = paramValue;		
		this.DCSext.realp = sPage;		
		//dcs_TAG(TagPath);			
		this.send();
		}		
	else{		
		this.DCS.dcsuri = '/us/' + sPage;		
		this.DCSext[paramName] = paramValue;
		//dcs_TAG(TagPath);
		this.send();
	}			
}
