/*
 * SharePath Tier0 suite
 * Copyright (c) Correlsense Inc.
 * All rights reserved.
 *
 * $Id: sptier0.js 364 2011-07-31 14:04:51Z alexk@CORRELSENSE.COM $
 */

var _SP_SUPPORT_OLD_MODE = false;
var _sp_t0_isTier0Enabled = false;
var _sp_t0_prefix = "spt0";
var _sp_t0_cookie = _sp_t0_prefix+"RQTTFB";
var _sp_t0_ttfb = new Date();
var _sp_t0_options = "referrer";

function _sp_t0_get_version() {
	var SPVERSION = "SPPageCollector-2.1.3.22";
	return SPVERSION;
}

function _sp_t0_getXMLHttpRequest() {
	var xmlhttp = null;

	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlhttp;
}

function _sp_t0_saveCookie(name, value) 
{
	window.document.cookie = name+"="+value+"; path=/";
}

function _sp_t0_readCookie(name) 
{
	name += "=";
	var ca = window.document.cookie.split(';');
	for (var i=0;i<ca.length;i++) {
		var c = ca[i].replace(/^[ ]*/, "");
		if (c.indexOf(name) == 0) {
			return c.substring(name.length, c.length);
		}
	}
	return null;
}

function _sp_t0_deleteCookie(name) 
{
	window.document.cookie = name + "=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function _sp_t0_serializeDate(value) 
{
	function lpad(value, len) {
		value=String(value);
		return "0000000000".substring(0, len-value.length)+value;
	}

	if (value == null) {
		return null;
	}
	else {
		return (
			value.getFullYear()+
			lpad(eval(value.getMonth()+1), 2)+
			lpad(value.getDate(), 2)+
			lpad(value.getHours(), 2)+
			lpad(value.getMinutes(), 2)+
			lpad(value.getSeconds(), 2)+
			lpad(value.getMilliseconds(), 3)
		);
	}
}

function _sp_t0_deserializeDate(value) 
{
	return new Date(
		value.substring(0, 4),
		value.substring(4, 6)-1,
		value.substring(6, 8),
		value.substring(8, 10),
		value.substring(10, 12),
		value.substring(12, 14),
		value.substring(14, 17)
	);
}

function _sp_t0_spliturl(url) {
	var ret = new Array();
	var n = url.indexOf("://");
	if (n != -1 && url.substring(0, n).indexOf("/") == -1) {
		ret["protocol"] = url.substring(0, n);
		var rest = url.substring(n+3);
		var n = rest.indexOf("/");
		var host = rest;
		var uri = "";
		if (n != -1) {
			host = rest.substring(0, n);
			uri = rest.substring(n);
		}
		n = host.indexOf("@");
		if (n != -1) {
			host=substring(n+1);
		}
		ret["host"] = host;
		ret["uri"] = uri;
	}
	else {
		ret = _sp_t0_spliturl(window.document.location.href);
		if (url.substring(0, 1) != "/") {
			uri = ret["uri"];
			var n = uri.indexOf("?");
			if (n != -1) {
				uri = uri.substring(0, n);
			}
			n = uri.lastIndexOf("/");
			if (n != -1) {
				uri = uri.substring(0, n);
			}
			ret["uri"] = uri + "/" + url;
		}
		else {
			ret["uri"] = url;
		}
	}

	return ret;
}

function _sp_t0_beginNetwork() {
	var url_components = _sp_t0_spliturl(window.document.location.href);
	var fixed_url = url_components["protocol"] + "://" + url_components["host"] + url_components["uri"];
	_sp_t0_saveCookie(_sp_t0_cookie, _sp_t0_serializeDate(new Date()) + ":" + escape(fixed_url));
}

function _sp_t0_sendTier0(Note, StatusCode, _RequestTTFB, _ResponseTTFB, _RenderingTime, url, remote_id) {
	var xmlhttp = _sp_t0_getXMLHttpRequest();
	var RequestTTFB = _sp_t0_serializeDate(_RequestTTFB);
	var ResponseTTFB = _sp_t0_serializeDate(_ResponseTTFB);
	var RenderingTime = _sp_t0_serializeDate(_RenderingTime);

	try {
		if (!_sp_t0_isTier0Enabled || !sp_t0_cb_isInstrumeted()) {
			return;
		}
	}
	catch(e) {}

	var tier0Url = "/Tier0.html";
	try {
		tier0Url = sp_t0_cb_getTier0URL();
	} catch(e) {}
	tier0Url += "?";

	var headers = new Array();
	var fixed_url = null;
	var old_fixed_url = null;

	if (remote_id == "") {
		remote_id = null;
	}

	if (remote_id == null) {
		var url_components = _sp_t0_spliturl(url);
		fixed_url = url_components["protocol"] + "://" + url_components["host"] + url_components["uri"];
		old_fixed_url = url_components["uri"].replace("?", "&");
	}

	if (_SP_SUPPORT_OLD_MODE) {
		tier0Url += (
			"last_page_time=" + ( RequestTTFB == null ? ResponseTTFB : RequestTTFB ) +
			"&request_return_time=" + ResponseTTFB +
			"&render_time=" + RenderingTime
		);

		if (remote_id == null) {
			tier0Url +=  "&page=" + old_fixed_url;
		}
		else {
			tier0Url += "&remote_id=" + remote_id;
		}
	}

	try {
		var user=sp_t0_cb_getUser();
		if (user != null) {
			if (_SP_SUPPORT_OLD_MODE) {
				tier0Url += "&user_name=" + escape(user);
			}
			headers["X-SharePath-RUM-UserID"] = user;
		}
	}
	catch(e) {}
	try {
		var extra_parameters=sp_t0_cb_getExtraParameters();
		if (extra_parameters != null) {
			if (_SP_SUPPORT_OLD_MODE) {
				tier0Url += "&" + extra_parameters;
			}
			headers["X-SharePath-RUM-Parameters"] = extra_parameters;
		}
	}
	catch(e) {}

	try { xmlhttp._sp_t0_skip = true; } catch(e) {}

	headers["Cache-Control"] = "no-cache";
	headers["Pragma"] = "no-cache";
	headers["If-Modified-Since"] = "Sat, 1 Jan, 2000 00:00:00 GMT";

	headers["X-SharePath-RUM-Producer"] = "sptier0.js-" + _sp_t0_get_version();
	headers["X-SharePath-RUM-Note"] = Note;
	headers["X-SharePath-RUM-StatusCode"] = StatusCode;
	if (RequestTTFB != null) {
		headers["X-SharePath-RUM-RequestTTFB"] = RequestTTFB;
	}
	headers["X-SharePath-RUM-ResponseTTFB"] = ResponseTTFB;
	headers["X-SharePath-RUM-RenderingTime"] = RenderingTime;
	if (remote_id != null) {
		if (_SP_SUPPORT_OLD_MODE) {
			headers["X-SharePathEventID"] = remote_id;
		}
		headers["X-SharePath-RUM-RemoteID"] = remote_id;
	}
	else {
		headers["X-SharePath-RUM-Referrer"] = fixed_url;
	}

	try {
		xmlhttp.open("GET", tier0Url, false);
		var header;
		for (header in headers) {
			xmlhttp.setRequestHeader(header, headers[header]);
		}
		xmlhttp.send(null);
	}
	catch(e) {}
}

function _sp_t0_onbeforeunload() 
{
	_sp_t0_beginNetwork();
}

function _sp_t0_onload() 
{
	var render = new Date();

	var options = _sp_t0_options;
	try {
		options = sp_t0_cb_getOptions();
	}
	catch(e){}

	try {
		if (!sp_t0_cb_isInstrumeted()) {
			return;
		}
	}
	catch(e) {}

	var cookie = _sp_t0_readCookie(_sp_t0_cookie);
	_sp_t0_deleteCookie(_sp_t0_cookie);

	var RequestTTFB = null;
	if (cookie != null) {
		var components = cookie.split(":");
		if (components.length == 2) {
			if (options.search("referrer") == -1) {
				RequestTTFB = _sp_t0_deserializeDate(components[0]);
			}
			else {
				if (window.document.referrer != null && window.document.referrer != "") {
					var url_components = _sp_t0_spliturl(window.document.referrer);
					var fixed_referrer = url_components["protocol"] + "://" + url_components["host"] + url_components["uri"];
					if (unescape(components[1]) == fixed_referrer) {
						RequestTTFB = _sp_t0_deserializeDate(components[0]);
					}
				}
			}
		}
	}

	var remote_id = null;
	try {
		remote_id = sp_t0_cb_getRemoteID();
	}
	catch(e) {}
	
	_sp_t0_sendTier0(
		"dhtml",
		200,
		RequestTTFB,
		_sp_t0_ttfb,
		render,
		window.document.location.href,
		remote_id
	);
}

(function() {
	if (_sp_t0_getXMLHttpRequest() != null) {
		_sp_t0_isTier0Enabled = true;
	}
	try {
		if (navigator.appName == 'Microsoft Internet Explorer') {
			var bho = new ActiveXObject("SPIEBHOS.Bridge");
			if (bho.IsBHOActive()) {
				_sp_t0_isTier0Enabled = false;
			}
		}
	}
	catch(e){}
})();

(function() {
	if (_sp_t0_isTier0Enabled) {
		var _sp_t0_window=window;
		try {
			_sp_t0_window=sp_t0_cb_getWindow();
		}
		catch(e) {}
		if (window.document.all) {
			_sp_t0_window.attachEvent("onload", _sp_t0_onload);
			_sp_t0_window.attachEvent("onbeforeunload", _sp_t0_onbeforeunload);
		}
		else {
			_sp_t0_window.addEventListener("load", _sp_t0_onload, false);
			_sp_t0_window.addEventListener("beforeunload", _sp_t0_onbeforeunload, false);
		}
	}
})();


