// MXS - Use of AJAX on the CSA site
/*
setup tooltip for a page

the tile to set the bodyattributes must call onload="AjaxTooltip.sendRequest(\'/jsp/definitions.xml\');"

Call the definition with these commands
<html:link href='javascript:void(0);' styleId="tripcancellation" onmouseout="nd();" onmouseover="com.csatp.getTooltip(this.id)">Trip Cancellation</html:link>
<noscript><sslext:link page="/proddef.do?def=tripcancellation" title="See the definition for Trip Cancellation"><img src="/images/icon-help.gif" alt="image of a question mark" class="helpimg"/></sslext:link></noscript>

The overDiv Div is in the template and loads with every page

the ajax.js and overlib.js files are in the template and load with every page.
*/

// Create the tooltip object
AjaxTooltip = new Object();
AjaxTooltip.responsestring = "actionOnReady not run";
AjaxTooltip.requester;



// 1st Step - Pass the function the url of the page and what tip to look for and return a string
AjaxTooltip.sendRequest = function(xmlurl)
{
	// Set Vars
	this.xmlurl = xmlurl;

	// Run Objects
	this.initializeRequestor();
	this.requester.open("GET",this.xmlurl,true);
	this.requester.send(null);
}



// 2nd Step - This gets the data
AjaxTooltip.getTip = function(name)
{
	// Set Vars
	this.name = name;

	// Run Objects
	if (this.requester != null) {
        if (navigator.appName=="Microsoft Internet Explorer"){
            this.requester.onreadystatechange = new Function(this.actionOnReady());
        }else{
            this.requester.onreadystatechange = this.actionOnReady();
        }
    }
};



// SUPPORTING FUNCTIONS ///////////////////////////////////////////////////////////////////////////

// Set up the XMLHttpRequest object
AjaxTooltip.initializeRequestor = function()
{
	this.requester = null;

	/* Check for running connections */
	if (this.requester != null && this.requester.readyState != 0 && this.requester.readyState != 4){
		this.requester.abort();
	}
	
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			this.requester = new XMLHttpRequest();
        } catch(e) {
			this.requester = null;
			return false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	this.requester = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		this.requester = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		this.requester = null;
          		return false;
        	}
		}
    }
};



// Update the text in a HTML element if ready
AjaxTooltip.actionOnReady = function()
{
	//if (this.requester != null) {
    if (this.requester) {
        if (this.requester.readyState==4) {
            if (this.requester.status == 200) {
                this.processData();
                if (document.getElementById){
                    document.getElementById("overDiv").innerHtml = this.responsestring;
                }else if(document.all){
                    document.all("overDiv").innerHtml = this.responsestring;
                }
            } else {
                if (document.getElementById){
                    document.getElementById("overDiv").innerHtml = "Not able to retrieve description";
                }else if(document.all){
                    document.all("overDiv").innerHtml = "Not able to retrieve description";
                }
            }
        }
    }
};



// Get the data from the childnode of the specified name attribute
AjaxTooltip.processData = function()
{
	this.responsestring = "No Definitions Found";

	//Code to do xml processing
    var response = this.requester.responseXML.documentElement;

    for (var i=0; i<response.childNodes.length; i++){
		if (response.childNodes.item(i).nodeName=="definition" && response.childNodes.item(i).attributes[0].value==this.name){
			//alert(response.childNodes.item(i).childNodes.item(3).childNodes.item(0).data);
			if (navigator.appName=="Microsoft Internet Explorer" && document.all){
				this.responsestring = "<strong>" + response.childNodes.item(i).childNodes.item(0).childNodes.item(0).data + "</strong><br/>";
				this.responsestring += response.childNodes.item(i).childNodes.item(1).childNodes.item(0).data;
			}
			else if (document.getElementById){
				this.responsestring = "<strong>" + response.childNodes.item(i).childNodes.item(1).childNodes.item(0).data + "</strong><br/>";
				this.responsestring += response.childNodes.item(i).childNodes.item(3).childNodes.item(0).data;
			}
			//alert(this.responsestring);
			return;
		}
	}

	// Code to do string processing
	//this.responsestring = this.requester.responseText;
};


