function getXmlHttpRequestObject() {	
	if (window.XMLHttpRequest) {		
		return new XMLHttpRequest(); //Not IE	
	} else if(window.ActiveXObject) {		
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE	
	} 
}

//Get browser specific XmlHttpRequest object.
var receiveReq = getXmlHttpRequestObject();


/*business-directory forward type asyncronous request.
*****************************************************/

function outputKeywordBrowser() {
	
	var partKw = document.getElementById('tbxKwdFwdType').value;
	
	if (partKw.length > 1) {
		//encode
		partKw = partKw.replace(/\+/g, "%2b");
		partKw = escape(partKw)
		partKw = partKw.replace(/%A3/g, "%c2%a3")
		partKw = partKw.replace(/%A6/g, "%c2%a6")
		partKw = partKw.replace(/%AC/g, "%c2%ac")
		getBios(partKw);
	} else {
		clearCatList();
	}
	
}

function customReplace(string,text,by) {
	// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength > strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


function getBios(encPartKw) {

	//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//Setup the connection as a GET call
		//True explicity sets the request to asyncronous (default).
		receiveReq.open("GET", "ajaxCustom.aspx?fn=biogetfwdtyp&k="+encPartKw, true);
		//Set the function that will be called when the XmlHttpRequest objects state changes.
		receiveReq.onreadystatechange = gotBios; 
		//Make the actual request.
		receiveReq.send(null);
	}			
}

//Called every time our XmlHttpRequest objects state changes.
function gotBios() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
		
		clearCatList();
		
		var targetDiv = document.getElementById('divCatBrowser');
		
		//Get the bios list.
		var xmldoc = receiveReq.responseXML;
		var bios_nodes = xmldoc.getElementsByTagName("bios"); 
		var n_bios = bios_nodes.length;	
		
		var combo_count = -1; //Initialise below starting array
		var kw_loc_combo = new Array();
		var kwHtml = "";
		var lnHtml = "";
		
		if (n_bios > 0 ) {
		
			var htmlOut = "";
			
			htmlOut += '<table cellspacing="0" cellpadding="0" border="0" width="100%">'
				
			for (i = 0; i < n_bios; i++) {		
				var typ_node = bios_nodes[i].getElementsByTagName("typ");	
				var txt_node = bios_nodes[i].getElementsByTagName("txt");			
				var url_node = bios_nodes[i].getElementsByTagName("url");		
				if 	(typ_node[0].firstChild.nodeValue=='kw') {
					kwHtml = '<a href="'+url_node[0].firstChild.nodeValue+'" class="mediumTag" style="word-wrap: break-word;">'+txt_node[0].firstChild.nodeValue+'</a>';
					lnHtml = "";
					combo_count++;
					kw_loc_combo[combo_count] = {"kwHtml":kwHtml,"lnHtml":lnHtml};
				} else {
					if (kw_loc_combo[combo_count]) {
						if (kw_loc_combo[combo_count].lnHtml.length == 0) {
							kw_loc_combo[combo_count].lnHtml = '<a href="'+url_node[0].firstChild.nodeValue+'" class="prominent_link" style="word-wrap: break-word;">'+txt_node[0].firstChild.nodeValue+'</a>';
						} else {
							kw_loc_combo[combo_count].lnHtml += ' | <a href="'+url_node[0].firstChild.nodeValue+'" class="prominent_link" style="word-wrap: break-word;">'+txt_node[0].firstChild.nodeValue+'</a>';
						}
					}
				}				
			}
			
			for (i = 0; i < kw_loc_combo.length; i++) {	
				if (kw_loc_combo[i].lnHtml.length == 0) {
					htmlOut += '<tr>';
					htmlOut += '<td colspan="2" valign="top" width="50%" style="padding-left:20px;padding-top:10px;padding-bottom:10px;padding-right:20px;border:#d3d3d3 1px solid;">';
					htmlOut += kw_loc_combo[i].kwHtml;
					htmlOut += '</td>';
					htmlOut += '</tr>';
				} else {
					htmlOut += '<tr>';
					htmlOut += '<td rowspan="2" valign="top" width="50%" style="padding-left:20px;padding-top:10px;padding-bottom:10px;padding-right:20px;border-left:#d3d3d3 1px solid;border-top:#d3d3d3 1px solid;border-bottom:#d3d3d3 1px solid;">';
					htmlOut += '<div style="_width:210px; max-width: 210px; overflow: hidden;">';
					htmlOut += kw_loc_combo[i].kwHtml;
					htmlOut += '</div>';
					htmlOut += '</td>';
					htmlOut += '<td valign="top" width="50%" style="padding-left:20px;padding-top:10px;padding-bottom:10px;padding-right:20px;background-color:#f2f9ff;border:#d3d3d3 1px solid;">';
					htmlOut += '<div class="large_bold_text">in...</div>';
					htmlOut += '</td>';
					htmlOut += '</tr>';
					htmlOut += '<tr>';
					htmlOut += '<td valign="top" width="50%" style="padding-left:20px;padding-top:10px;padding-bottom:10px;padding-right:20px;border-left:#d3d3d3 1px solid;border-right:#d3d3d3 1px solid;border-bottom:#d3d3d3 1px solid;">';
					htmlOut += '<div style="_width:210px; max-width: 210px; overflow: hidden;">';
					htmlOut += kw_loc_combo[i].lnHtml;
					htmlOut += '</div>';
					htmlOut += '</td>';
					htmlOut += '</tr>';
				}
			}	
			
			htmlOut += '</table>'
			
			targetDiv.innerHTML = htmlOut;		
		
		}
	}
}

function clearCatList() {
	document.getElementById('divCatBrowser').innerHTML = "";
}



/*business-directory location asyncronous request.
*************************************************/

var elId = "";
var elX = 0;
var elY = 0;

var elX2 = 0;
var elY2 = 0;

var to;

function outputLocs(theKId, theElId, e) {
	
	if (!e) { e = window.event; }
	
	//stop any triggered hide
	clearTimeout(to);
	
	//drop any elements we failed to get rid of
	var targetDiv = document.getElementById('divLocDrop');
	targetDiv.innerHTML="";
		
	//Get the locs
	elId=theElId;
	elX=e.clientX;
	elY=e.clientY;
	
	//Keep the encoded string intact
	getLocs(theKId);
}
	
function getLocs(theKId) {
	//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//Setup the connection as a GET call
		//True explicity sets the request to asyncronous (default).
		receiveReq.open("GET", "ajaxCustom.aspx?fn=biogetloc&k="+theKId, true);
		//Set the function that will be called when the XmlHttpRequest objects state changes.
		receiveReq.onreadystatechange = gotLocs; 
		//Make the actual request.
		receiveReq.send(null);
	}			
}

//Called every time our XmlHttpRequest objects state changes.
function gotLocs() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
			
		var targetDiv = document.getElementById('divLocDrop');
		
		//Get the locations.
		var xmldoc = receiveReq.responseXML;
		var location_nodes = xmldoc.getElementsByTagName("loc"); 
		var n_locations = location_nodes.length	
		
		//add in the header
		if (n_locations > 0 ) {
		
			var headerDiv = document.createElement('div');
			headerDiv.setAttribute('id','divLocDropHeader');
			headerDiv.innerHTML = document.getElementById(elId).innerHTML+' in...';
			headerDiv.className = "large_bold_text";
			headerDiv.style.paddingLeft = "10px";
			headerDiv.style.paddingRight = "30px";
			headerDiv.style.marginTop = "5px";
			headerDiv.style.marginBottom = "5px";
			headerDiv.style.zIndex = "+299";
			targetDiv.appendChild(headerDiv)
			
			var newdiv = document.createElement('div');
			newdiv.setAttribute('id','locs_'+elId);
			newdiv.setAttribute('style','background-color:#ffffff;z-index:+199;');

			var htmlOut = "";
						
			for (i = 0; i < n_locations; i++) {			
				var place_node = location_nodes[i].getElementsByTagName("pl");			
				var url_node = location_nodes[i].getElementsByTagName("url");			
				htmlOut += '<div style="background-color:#ffffff;text-align:left;padding-top:5px;padding-bottom:5px;padding-right:10px;padding-left:10px;"><a class="section_link" href="'+url_node[0].firstChild.nodeValue+'">';
				htmlOut += place_node[0].firstChild.nodeValue;
				htmlOut += '</a></div>';
			}
					
			newdiv.innerHTML = htmlOut;
			targetDiv.appendChild(newdiv);


			var popX = elX+10;
			var popY = elY;
			targetDiv.style.left = popX+'px';
			targetDiv.style.top = popY+'px';
			
			var ua = navigator.userAgent.toLowerCase();
			var isIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) );
			if (!isIE) {
				targetDiv.addEventListener('mouseout',hideLocsTimed, false);
			} else {
				targetDiv.attachEvent('onmouseout',hideLocsTimed);
			}
			
			targetDiv.style.display = "";
		
		}
	}
}

function hideLocsTimed() {

	//clear any previous hide
	clearTimeout(to);
	
	//reset the timeout
	to = setTimeout("hideLocs()", 500);
}

function hideLocs() {
	//Remove the locs
	var targetDiv = document.getElementById('divLocDrop');

	//See if the mouse is on the element
	var mouseX = elX2;
	var mouseY = elY2;
	var leftX = cumulativeOffsetX(targetDiv);
	var upperY = cumulativeOffsetY(targetDiv);
	var rightX = leftX+targetDiv.clientWidth;
	var lowerY = upperY+targetDiv.clientHeight;
		
	//Make sure the mouse is not over the box
	if (!(mouseX >= leftX && mouseX <= rightX && mouseY >= upperY && mouseY <= lowerY)) {
		targetDiv.style.display = "none";
		targetDiv.innerHTML="";
	}	
}

function busDirInit() {
	var ua = navigator.userAgent.toLowerCase();
    var isIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) );
    if (!isIE) {
        document.addEventListener('mousemove',setMouseXY, false);
    } else {
        document.attachEvent('onmousemove',setMouseXY);
    }
}

addLoadEvent(busDirInit);

function setMouseXY(e) {
	if (!e) { e = window.event; }
	if (e)
	{ 
		if (e.pageX || e.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			elX2 = e.pageX;
			elY2 = e.pageY;
		}
		else if (e.clientX || e.clientY) { // works on IE6,FF,Moz,Opera7
			// Note: I am adding together both the "body" and "documentElement" scroll positions
			//       this lets me cover for the quirks that happen based on the "doctype" of the html page.
			//         (example: IE6 in compatibility mode or strict)
			//       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
			//       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH 
			//         (from info at http://www.quirksmode.org/js/doctypes.html)
			elX2 = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			elY2 = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	}
}

function cumulativeOffsetX(element) {
    var valueL = 0;
    if (element) {
	do {
		valueL += element.offsetLeft || 0;
		element = element.offsetParent;
	} while (element);
	}
    return valueL;
};
    
function cumulativeOffsetY(element) {
    var valueT = 0;
    if (element) {
	do {
		valueT += element.offsetTop  || 0;
		element = element.offsetParent;
	} while (element);
	}
    return valueT;
};

