var eDiv = false; // declare a div to place event details into
var nMouseY = 0;
var nMouseX = 0;

function eventInit() {
	// safely add event handlers to the event calendar
	var theTables = document.getElementsByTagName('table');
	var theLinks;
	
	for (var i=0; i<theTables.length; i++) {
		if (theTables[i].className == 'MtVCal') {
			theLinks = theTables[i].getElementsByTagName('a');
			for (var c=0; c<theLinks.length; c++) {
				if (theLinks[c].id.search(/^d_/) != -1) {
					theLinks[c].onmouseover = function  () { eventHover(this); }
					theLinks[c].onmouseout = function  () { eventHoverOut(this); }
					theLinks[c].onclick = function  () { exhr.getEventsForDay(this); }
				}
			}
		}
	}
	
	eDiv = document.createElement('div');
	eDiv.className = 'eventDetailHover';
	eDiv.onclick = function () { this.style.visibility = 'hidden'; }
	document.body.appendChild(eDiv);
}

if (document.captureEvents)
{
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = getCoords;
} else if (document.all) {
   document.onmousemove=getCoords;
}

function getCoords(e)
{ // monitor mouse movements and record them... Please don't adjust this bit
	if (!e) e = window.event;
	if (document.all)
	{ // for IE
		// document.documentElement... is Standards Compliance mode for IE... clientY is document-relative so has to be offset with scrollTop and scrollLeft
		nMouseY = (e.clientY + (document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop)); 
		nMouseX = (e.clientX + (document.documentElement ? document.documentElement.scrollLeft : document.body.scrollLeft));
	} else { // for Gecko-based browsers... no offset necessary as the coords are screen-relative (slightly odd)
		nMouseY = (e.pageY + 5); 
		nMouseX = (e.pageX + 12) ;
	}
}

function eventHover(theLink) {
	var lArr = new Array();
	lArr = theLink.id.split('_');
	var disp = document.getElementById('MtVCalDisp' + lArr[1]);
	disp.innerHTML = '<span class="eventInfoDate">'+lArr[2]+'/'+lArr[3]+'/'+lArr[4]+'</span><br/>';
	disp.innerHTML += '<span class="eventInfoText">' + (lArr[5] == '0' ? 'There are no events for today' : 'There ' + (lArr[5] == '1' ? 'is' : 'are') + ' ' + lArr[5] + ' event' + (lArr[5] == '1' ? '' : 's') + ' today') + '</span>';
}

function eventHoverOut(theLink) {
	var lArr = new Array();
	lArr = theLink.id.split('_');
	var disp = document.getElementById('MtVCalDisp' + lArr[1]);
	disp.innerHTML = '&nbsp;';
}

function objXHR()
{ // scan the function clicked and act on it using the Ajax interthingy
	var url; // URL to send HTTP requests to
	var timer; // timer for timing things
	var XHR; // XMLHttpRequest object
	var xmlDoc; // object for requesting XML documents from the server
	var xmlIEFlag; // linked to xmlDoc, this discerns between IE and the better browser XML implementations
	var _responseXML; // holds XML formed responses from the server
	var _responseText; // holds any textual response from the server
	var request; // associative array to hold requests to be sent
	var additional; // additional settings in an associative array
	
	// vars added in addition to base vars
	var session_id; // current customer session ID
	
	this.request = new Array();
	this.additional = new Array();
	this.createXHR();
}

objXHR.prototype.xmlDoc = function() {
	this.xmlDoc = false;
	if (document.implementation && document.implementation.createDocument)
	{ // decent browser
		this.xmlDoc = document.implementation.createDocument("", "", null);
		this.xmlIEFlag = false;
	} else if (window.ActiveXObject) { // crappy IE
		this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		this.xmlIEFlag = true;
	} else { // hmm... no XML request support
		this.xmlDoc = false;
	}
	xmlDoc.async = false;
}

objXHR.prototype.createXHR = function () { // this code has been modified from the Apple developers website
	this.XHR = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) { // decent, normal, law abiding browsers
    	try { // make sure the object can be created
			this.XHR = new XMLHttpRequest();
        } catch(e) { // it can't
			this.XHR = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) { // this does stuff too
       	try {
        	this.XHR = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		this.XHR = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		this.XHR = false;
        	}
		}
    }
}

objXHR.prototype.getData = function(strMode, resFunc) { // send a request to the server in either GET or POST
	// resfunc is the string name of the function that will handle the response when it arrives
	// determine the transport method (can only send POST for some reason)
	// resFunc is the name of the prototype function to be called on data arrival
	
	strMode = (strMode.toLowerCase() == 'post' ? 'post' : 'get');
	var _this = this; // scope resolution
	this.createXHR();

	if (this.XHR) {
		this.XHR.onreadystatechange = function () {
			if (_this.XHR.readyState == 4) {
			// only if "OK"
				if (_this.XHR.status == 200) {
					_this._responseXML = _this.XHR.responseXML;
					_this._responseText = _this.XHR.responseText;
					_this.responseHandler(resFunc);
				} else {
					alert('Status returned - ' + _this.XHR.statusText);
				}
			}
		}
		this.XHR.open(strMode.toLowerCase(), this.url, true);
		if (strMode.toLowerCase() == 'post')	this.XHR.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		this.XHR.send(this.compileRequest());
	} else {
		var mess = "I couldn't contact the server!\n\nIf you use IE please allow ActiveX objects to run";
		alert (mess);
	}
}

objXHR.prototype.compileRequest = function () {
	// parse the request array into a URL encoded string
	var ret = ''; // return request string
	
	for (var e in this.request) {
		ret += e + '=' + this.request[e].replace(/&/, '%26') + '&';
	}
	
	return (ret.substr(0, ret.length - 1));
}

objXHR.prototype.responseHandler = function (theFunction) { // redirect responses from the server to the right function
	// to add a function simply declare a prototype function and the pass the exact name
	// as the second parameter
	// ### NOTE ### function strings must be passed with parantheses
	
	this.request = new Array();
	eval('this.'+theFunction);
}

/// prototype functions below here are not necessary for the function of objHXR

objXHR.prototype.getEventsForDay = function (theLink) {
	if (theLink !== false) {
		// extract the relevant data from the ID of the link
		var data = new Array();
		data = theLink.id.split('_');
		if (data[5] == 0) return false; // there are no events today
		
		eDiv.innerHTML = '';
		eDiv.innerHTML = '<div class="eventPleaseWait">Please wait</div>';
		eDiv.style.top = (nMouseY - (eDiv.offsetHeight / 2)) + 'px';
		eDiv.style.left = (nMouseX - (eDiv.offsetWidth / 2)) + 'px';
		eDiv.style.visibility = 'visible';
		this.request['stat'] = 'getEventsForDay';
		this.request['day'] = data[2];
		this.request['month'] = data[3];
		this.request['year'] = data[4];
		this.getData('post', 'getEventsForDay(false)');
	} else {
		eDiv.innerHTML = this._responseText;
	}
	
	return false;
}

var exhr = new objXHR;