/*
	Engineering Analytics
	Main JS
	Clint D Holsinger
	Project Begin: 20081203
*/

/******************************************************************************************************************************
 *
 *	AJAX handling functions
 *
 ******************************************************************************************************************************/

// JavaScript Document

function AJAXConnection()
{
	this.XMLHTTP = new Object();
	this.Done = false;
	
	try { 
		this.XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP"); } //Get a ref to orig MS XML HTTP Object
	catch (e) { 
		try { 
			this.XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP"); }  //Get a ref to the updated MS XML HTTP Object
		catch (e) { 
			try { 
				this.XMLHTTP = new XMLHttpRequest(); } // Get a ref to the standard XML HTTP Object
			catch (e) { 
				this.XMLHTTP = false;
			}
		}
	}
	if (!this.XMLHTTP) return null; //AJAX Unsupported

	return this;
}

AJAXConnection.prototype.Connect = function(sURL, sMethod, sVars, fnDone) {

	if (!this.XMLHTTP) return false;

	sMethod = sMethod.toUpperCase();
	this.Done = false;
	this.CallBack = fnDone; this.Method = sMethod;
	this.URL = sURL; this.Data = sVars;
	
	try { // Open for GET or POST
		if (sMethod == "GET") {
			var qStr = sURL + "?" + sVars;
			this.XMLHTTP.open(sMethod, qStr, true);
			sVars = "";
		} else {
			this.XMLHTTP.open(sMethod, sURL, true);
			this.XMLHTTP.setRequestHeader("Method", "POST " + sURL + " HTTP/1.1");
			this.XMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		
		var tmp = this;
		this.XMLHTTP.onreadystatechange = function(){
//			alert(tmp.XMLHTTP.readyState); // DEBUG
			if (tmp.XMLHTTP.readyState == 4 && !tmp.Done) {
				tmp.Done = true;
				tmp.CallBack(tmp.XMLHTTP);
			}
		};

		this.XMLHTTP.send(sVars); // Send the data (only for POST)
	}
	catch(z) { 
		alert(z.number + " " + z.description);
		return false; }
		
	return true;
}

function RemoveDir(xURL) {
	
	xURL = xURL.substr(0, xURL.lastIndexOf("/", xURL.length - 2) + 1)
	
	return xURL;
}

/* To fix any relative paths that are passed in. */
function FixRelativeURL(xURL, xRootURL) {
	var tmpURL = xURL;
	var tmpRootURL = xRootURL;
	
	if (tmpURL.substr(0, 6) == "mailto") { return xURL; };
	
try {
	if (!(tmpURL.substr(0, 4) == "http")) {
		if (!(tmpRootURL.substr(tmpRootURL.length - 1) == "/")) { tmpRootURL += "/"; }
		
		if (tmpURL.substr(0, 1) == "/") {
			tmpURL = tmpURL.substr(1);
			tmpRootURL = tmpRootURL.substr(0, tmpRootURL.indexOf("/", tmpRootURL.indexOf("//") + 2) + 1);
		} else {
			while (tmpURL.substr(0, 3) == "../") {
				tmpURL = tmpURL.substr(3);
				tmpRootURL = RemoveDir(tmpRootURL);
			}
		}

		return tmpRootURL + tmpURL;
	}
} catch (e) { }
	
	return xURL;
}


/* ******************************************************************** 
*
*	Main Functions
*
******************************************************************** */

var ReturnURL = "/scripts/ENGRAnalytics.aspx";

PageVisited();

function Done() {
	return true;
}

function PageVisited() {
	var URL = unescape(window.location);
	
	var Conn = new AJAXConnection();
	
	var Data = "DestURL=" + URL + "&EventAction=PAGE_LOAD";
	Conn.Connect(ReturnURL, "POST", Data, Done)
	
	var As = document.getElementsByTagName("A");
	var A;
	var i = 0;
	
	for (i = 0; i < As.length; i++) {
		A = As[i];
		A.onmousedown = A_MouseDown;
	}
	
}


function A_MouseDown(event) {
	var URL = "" + unescape(window.location);
	var ToURL = "" + unescape(this.getAttribute("href"));

	ToURL = FixRelativeURL(ToURL, URL);

	var Conn = new AJAXConnection();
	
	var Data = "DestURL=" + ToURL + "&FromURL=" + URL + "&EventAction=LINK_CLICK";
	Conn.Connect(ReturnURL, "POST", Data, Done)

	return true;
}


/* ******************************************************************** 
******************************************************************** */
