// { guj.js
var paths = [];
var CT = {Queue: {calls: [], busy: false}}; 

var CTC_BASE_TIME = 1041404400;
var CTC_PREVIOUS_OFFSET = 3600;
var CT_I_SET_ORIGIN = false;
var CT_I_SET_REPEAT = false;
var CT_I_REVISIT = false;
CT.isIE = document.all ? true : false;

//CT_I_... is an internally defined variable
//window.CT_C_... is an internally named variable that the customer sets
//window.CT_X_... is a customer defined variable to be written as a biscuit

CT.readCookies = function() {
	if (typeof(CT.cookies) == 'undefined') {
		var cookieArray = document.cookie.split(/; ?/);
		CT.cookies = [];
		for (var i = 0; i < cookieArray.length; i++) {
			var vals = cookieArray[i].split(/=/, 2);
			CT.cookies[vals[0]] = vals[1];
		}
	}
} // CT.readCookies

CT.pushInto = function(vals, key, value) {
	if (typeof(vals) != 'object') {
		if (typeof(vals) == 'undefined')
			vals = [];
		else
			vals = [vals];
	}
	if (typeof(vals[key]) == 'undefined')
		vals[key] = [];
	vals[key].push(value);
} // CT.pushInto

//- fields MAY be undefined. If set it MUST be either null or an array of parameters to pass, encoded 
//   as "key=value". e.g. fields = [ "some_param=somevalue", "some_other_param=blah" ];
//- server MAY be undefined. If set it MUST be either null or a non-empty string indicating where to send the message
//   to. e.g. server ="jdc.ct.com". do not include protocol or trailing /s.
function CT_RecordView(request, method, fields, server, referrer, onCompletion ) {
	//set defaults
	var basicCall = true;
	if (request == null) {
		if (window.CT_C_Request != null)
			request = window.CT_C_Request;
		else
			request = document.location;
	} else {
		basicCall = false;
	}
	if (typeof(window.CT_C_Referrer) == 'undefined')
		window.CT_C_Referrer = document.referrer;
	if (typeof(referrer) == 'undefined')
		referrer = window.CT_C_Referrer;
	fields = fields || []; //default
	server = server || CT_I_Path; //default

	if (CT_I_FirstPartyJDC)
		CT.setCookies(); // set up any cookies needed

	if (typeof(window.CT_R_PID) != 'undefined') {
		//if defined, add CT_R_PID to page requested to identify the page
		request += (/\?/.test(request) ? '&' : '?') + 'CT_R_PID=' + encodeURIComponent(window.CT_R_PID);
	}
	var orderTotal = (typeof(window.CT_C_OrderTotal) != 'undefined') ? window.CT_C_OrderTotal : 0;

	// handle biscuits first
	var biscuits = new Array();
	for (var i in window) {
		// if variable name starts with CT_X_ followed by something else 
		if (i.substring(0, 5) == 'CT_X_' && i.length > 5) {
			var s = encodeURIComponent(window[i] + '');
			biscuits.push(i + '=' + s);
		}
	}
	var ctxValue = biscuits.join('%26');

	// setup request fields
	var vals = fields.concat(); //clone array into vals
	CT.pushInto(vals, 'i', CT_I_Datasets);
	CT.pushInto(vals, 'r', request);
	if (CT_I_FirstPartyJDC) {
		if (window.CT_I_FirstPartyDomain && window.CT_I_FirstPartyDomain != '')
			CT.pushInto(vals, 'fp', window.CT_I_FirstPartyDomain);
		else
			CT.pushInto(vals, 'fp', 1);
		if (CT_I_FirstPartyCookies)
			CT.pushInto(vals, 'c', document.cookie); // record all first party cookies
		else {
			var cookie = [];
			for (var i = 0; i < CT.cookies.length; i++)
				if (i.match(/^Persistent_id_|^Revisit_|^Session_|^CTINC$/))
					cookie.push(i+'='+CT.cookies[i]);
			if (CT_I_REVISIT) {
				var id = CT_I_Datasets[0];
				cookie.push('Revisit_' + id + '=1');
			}
			CT.pushInto(vals, 'c', cookie.join('; '));
		}
	} else if (CT_I_FirstPartyCookies)
		CT.pushInto(vals, 'c', document.cookie); // record all first party cookies

	if (method != null) {
		CT.pushInto(vals, 's', method); //method
		CT.pushInto(vals, 'f', referrer);
	} else {
		CT.pushInto(vals, 'f', referrer);
		if (orderTotal > 0)
			CT.pushInto(vals, 'e', orderTotal);
	}
	if (typeof(CT_I_Subdomain) != 'undefined')
		CT.pushInto(vals, 'd', CT_I_Subdomain);
	if (ctxValue != '')
		CT.pushInto(vals, 'U', ctxValue); //biscuits
	if (!basicCall)
		CT.pushInto(vals, 'g', 1) //use graphic as response
	if (typeof(CT_I_Protocol) == 'undefined')
		window.CT_I_Protocol = window.location.protocol;
	CT.pushInto(vals, 'sd', window.screen.width + 'x' + window.screen.height); //screen dimensions

	// if origin or repeat is needed...
	if (CT_I_SET_ORIGIN) {
		CT.pushInto(vals, 'setOrigin', 1);
		CT_I_SET_ORIGIN = false;
	}
	if (CT_I_SET_REPEAT) {
		CT.pushInto(vals, 'setRepeat', 1);
		CT_I_SET_REPEAT = false;
	}

	CT.addToQueue(
		window.CT_I_Protocol + server,
		vals,
		onCompletion
	);
} // CT_RecordView()

CT.addToQueue = function(url, vals, onCompletion) {
	CT.Queue.calls.push({url: url, vals: vals, onCompletion: onCompletion});
	CT.Queue.process();
} // CT.addToQueue

function CT_ProcClick(evt) {
	var url;
	var wait;
	var elem;

	evt = (evt) ? evt : ((window.event) ? event : null);
	var leftClick = CT.isIE || evt.which == 1;
	if (evt && leftClick) {
		elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
		while (elem) {
			url = '';
			switch(elem.tagName) {
				case 'A':
				case 'AREA':
					url = elem.href;
					break;
				case 'INPUT':
					if (elem.form) {
						elem = elem.form;
						url = elem.action;
						if (url == '')
							url = document.location.href;
					} else {
						url = '';
					}
					break;
			}
			switch(elem.tagName) {
				case 'A':
				case 'AREA':
					if (url != '') {
						wait = false;
						if (!CT_LocalLink(url)) {
							CT_RecordView(url, 'EXIT', null, null, window.location.href, function() { window.location.href = url; });
							wait = true;
						} else if (CT_TrackFileExtension(url)) {
							CT_RecordView(url, 'GET', null, null, window.location.href, function() { window.location.href = url; });
							wait = true;
						}
						if (wait) {
							if (evt.preventDefault)
								evt.preventDefault();
							return false;
						}
					}
					elem = 0;
					break;
				default:
					elem = (elem.parentElement) ? elem.parentElement : elem.parentNode;
					break;
			}
		}
	}
} // CT_ProcClick

function CT_TrackFileExtension(link) {
	link = link.split(/\?/)[0]; // get everything up to but not including ?
	link = link.toLowerCase();
	var l1 = link.length;
	for (var index = 0; index < CT_I_OtherFileExtensionsToReport.length; index++)
		if (CT_I_OtherFileExtensionsToReport[index] != '' && link.substring(l1-CT_I_OtherFileExtensionsToReport[index].length-1) == '.' + CT_I_OtherFileExtensionsToReport[index].toLowerCase())
			return true;
	return false;
} // CT_TrackFileExtension

function CT_LocalLink(link) {
	var pieces = link.match(/^https?:\/\/([^\/]+)\//);
	if (pieces) {
		var l = pieces[1].length;
		for (var i = 0; i < CT_I_LocalLinks.length; i++){
			if (pieces[1] == CT_I_LocalLinks[i])
				return true;
		}
		return false;
	}
	return true;
} // CT_LocalLink

CT.Queue.onBackChannelSubmissionComplete = function( form ) {
} // CT.Queue.onBackChannelSubmissionComplete

CT.renderHiddenField = function( name, value ) {
	var newField = document.createElement('input');
	newField.type = 'hidden';
	newField.name = name;
	newField.value = value;
	newField.id = 'id_' + name;
	return newField;
} // CT.renderHiddenField

CT.encodeHex = function(string) {
	var len = string.length;
	var output = '';
	for (var i = 0; i < len; i++) {
		var c = '00'+string.charCodeAt(i).toString(16).toUpperCase();
		output += c.substring(c.length - 2);
	}
	return output;
} // CT.encodeHex

CT.postHiddenForm = function(obj) {
	var targetUrl = obj.url;
	var formData = obj.vals;
	var onPostComplete = obj.onCompletion;
	var remotingDiv = CT.getRemotingDiv(targetUrl);

	//fill form with hidden input data
	CT.Queue.busy = true;
	var output = [];
	var dataLength = targetUrl.length;
	for( var key in formData ) {
		if (typeof(formData[key]) == 'object') {
			for (var j = 0; j < formData[key].length; j++) {
				if (key == 'x' || key == 'X' || key == 'U' || key == 'c')
					output.push(key+'='+encodeURIComponent(formData[key][j]));
				else {
					remotingDiv.form.appendChild(CT.renderHiddenField(key, formData[key][j]));
					dataLength += (key+'='+encodeURIComponent(formData[key][j])).length + 1;
				}
			}
		}
	}
	remotingDiv.form.appendChild(CT.renderHiddenField('JDCDATA', CT.encodeHex(output.join('&'))));
	dataLength += ('JDCDATA='+CT.encodeHex(output.join('&'))).length + 1;
	if (dataLength > 2000 && CT.isIE)
		remotingDiv.form.method = 'post';

	remotingDiv.locationChangeTimer = setInterval(
		function() {
			var bLocationChanged = false;
			try {
				bLocationChanged = ( "about:blank" != (CT.isIE ? remotingDiv.iframe.document.location.href : remotingDiv.iframe.location));
			} catch(err) {
				//if we're not allowed to look at the iframe's location (which is why the exception was thrown)
				//then it must have changed!
				bLocationChanged = true;
			}

			if( bLocationChanged ) {
				clearInterval( remotingDiv.locationChangeTimer );
				if (typeof(onPostComplete) == 'function')
					onPostComplete();
				CT.Queue.calls.shift();
				CT.Queue.busy = false;
				CT.removeRemotingDiv();
				CT.Queue.process(); // if there are any more, queue them
			}
		},
		200
	);

	remotingDiv.form.submit();
} // CT.postHiddenForm

CT.Queue.process = function() {
	if (!CT.Queue.busy && CT.Queue.calls.length)
		CT.postHiddenForm(CT.Queue.calls[0]);
} // CT.Queue.process

CT.getRemotingDiv = function(url) {
	var remotingDiv = CT.setupRemotingDiv();
	if (!remotingDiv.setup) {
		remotingDiv.setup = true;
		remotingDiv.innerHTML = '<iframe name="remotingFrame" id="remotingFrame" style="border:0;width:0;height:0;"></iframe>';
		remotingDiv.iframe = frames['remotingFrame'];

		var form = document.createElement('form');
		form.style.border = '0px';
		form.style.height = '0px';
		form.style.width = '0px';
		form.style.display = 'none';
		form.setAttribute('id', 'remotingForm');
		form.target = 'remotingFrame';
		form.setAttribute('method', 'get');
		form.setAttribute('action', url + (url.indexOf('?') == -1 ? '?' : ( url.search(/\?$/) ? '' : '&')) + 'rnd=' + Math.random());
		remotingDiv.form = form;
		remotingDiv.appendChild(form);
	}

	return remotingDiv;
} // CT.getRemotingDiv

CT.pruneTree = function(node) {
	if (node) {
		while (node.hasChildNodes()) {
			CT.pruneTree(node.childNodes[0]);
			node.removeChild(node.childNodes[0]);
		}
	}
} // CT.pruneTree

CT.removeRemotingDiv = function() {
	var remotingDiv = document.getElementById('remotingDiv');
	if (typeof(remotingDiv) != 'undefined') {
		CT.pruneTree(remotingDiv);
		remotingDiv.setup = false;
	}
} // CT.removeRemotingDiv

CT.setupRemotingDiv = function() {
	var remotingDiv = document.getElementById('remotingDiv');
	if (!remotingDiv) {
		remotingDiv = document.createElement('span');
		remotingDiv.id = 'remotingDiv';
		remotingDiv.setup = false;
		document.body.appendChild(remotingDiv);
	}
	return remotingDiv;
} // CT.setupRemotingDiv

CT.generateUniqueKey = function() {
	var key = '0000' + parseInt((new Date).getTime() + '' + (Math.floor(Math.random()) % 10)).toString(16).toUpperCase();
	return key.substring(key.length - 12);
} // CT.generateUniqueKey

CT.setCookie = function(name, value, domain, expires) {
	var string = name + '=' + value + '; domain=' + domain;
	if (typeof(expires) != 'undefined')
		string += '; expires=' + expires;
	document.cookie = string;
	CT.cookies[name] = value;
} // CT.setCookie

CT.tenYearsExpiration = function() {
	var expires = new Date();
	expires.setFullYear(expires.getFullYear() + 10);
	return expires;
} // CT.tenYearsExpiration

CT.setCookies = function() {
	var id = CT_I_Datasets[0];
	var unique = CT.generateUniqueKey();
	var Persistent = 'Persistent_id_'+id;
	var Session = 'Session_'+id;
	var expires = CT.tenYearsExpiration();
	var startTime = Math.floor((new Date).getTime() / 1000);
	if (typeof(CT.cookies[Persistent]) == 'undefined') {
		CT.setCookie(Persistent, unique+':'+startTime, CT_I_FirstPartyDomain, expires.toGMTString());
		CT_I_SET_ORIGIN = true;
		CT_I_SET_REPEAT = true;
		CT_I_REVISIT = false;
	} else {
		var persistentValues = CT.cookies[Persistent].split(/:/, 2);
		var sessionStarted = startTime - CTC_PREVIOUS_OFFSET;
		if (persistentValues[1] < sessionStarted) {
			CT.setCookie(Persistent, persistentValues[0]+':'+Math.floor((new Date).getTime() / 1000), CT_I_FirstPartyDomain, expires.toGMTString());
			CT_I_SET_REPEAT = true;
		}
		CT_I_REVISIT = true;
	}
	if (typeof(CT.cookies[Session]) == 'undefined')
		CT.setCookie(Session, unique, CT_I_FirstPartyDomain);
} // CT.setCookies

CT.onLoadHandler = function(e) {
	if (CT.onLoadOld != undefined)
		CT.onLoadOld();
	CT.setupRemotingDiv();
	CT_RecordView();
} // CT.onLoadHandler

if (CT_I_EnableExitTracking) {
	if (document.addEventListener) //handle DOM 2 (Mozilla 6)
		document.addEventListener('click', CT_ProcClick, false);
	else
		document.attachEvent('onclick', CT_ProcClick);
}
CT.readCookies();

if (window.navigator.userAgent.search('/MSIE 6/') > 1) {
	// MSIE 6
	CT.onLoadOld = window.onload;
	window.addEvent('domready', function(){CT.onLoadHandler();});
} else {
	CT.onLoadOld = window.onload;
	window.onload = CT.onLoadHandler;
}

// } guj.js
