// JavaScript Document
// Related Links
// created 10/03/2005 by PN
// finds relevant links in XML page based on keywords pre-positioned in related links table
//
// v 0.2

function findNode(nodeAny,strName) {

	var nodeChildren = nodeAny.childNodes;

	for (i=0; i < nodeChildren.length; i++) {
		if (nodeChildren[i].nodeName == strName) {
			return nodeChildren[i];
		}
	}
	
	return null;
}

function searchLinks(colElements,keyword) {
// searches for keywords within elements of XML doc
	var passURL = "";
	var aNode, urlNode;
	
	
	for (var i=0; i < colElements.length; i++) {
		
		aNode = findNode(colElements[i],"Anchor");
		
		if (aNode.firstChild.nodeValue == keyword) { //Anchor
			urlNode = findNode(colElements[i],"Href");
			passURL = urlNode.firstChild.nodeValue; //Href
			break;	
		}
	}
	
	return passURL;

}

var req;

function loadXMLDoc(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
				changeDiv("relLinks","none"); // do not show related links
          		req = false;
        	}
		}
    }
	if(req) {
	
		req.onreadystatechange = processReqChange;
		try {
			req.open("GET", url, true); // set to true for asynch
			req.send(null);
		} catch(e) {
			alert("at line 63: " + e);
		}
		
	}
}

function processReqChange() {
    // only if req shows "loaded"
	
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
           createLinks();
			
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

function changeDiv(the_div,the_change)
{
  var the_style = getStyleObject(the_div);
  if (the_style != false)
  {
    the_style.display = the_change;
  }
}

function getStyleObject(objectId) {
  if (document.getElementById && document.getElementById(objectId)) {
    return document.getElementById(objectId).style;
  } else if (document.all && document.all(objectId)) {
    return document.all(objectId).style;
  } else {
    return false;
  }
}

function grabXML() {
	
	var urlXML = "/searchtools/AtoZfinal.xml";
	//**** DEV *** var urlXML = "/hpinfo/AtoZfinal.xml";
	document.domain = "hp.com";
	
	/*try {
		netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
	}
	catch(e)
	{
		alert("Permission UniversalBrowserRead denied.");
	}*/
	
	loadXMLDoc(urlXML);
	
	
	
}

function appendTrack(aURL,aKeyword) {
	// appends tracking information to URL
	var urlSnippet = "";
	
	// check to see if URL already uses question mark
	if (aURL.indexOf("?") > 0){
		urlSnippet = "&";
	} else {
		urlSnippet = "?";
	}
	
	urlSnippet += "jumpid=re_r138/";

	filename = location.pathname.substring(location.pathname.lastIndexOf("/")+1);
	
	filename = filename.substring(0,filename.indexOf("."));
	
	urlSnippet += filename + "/" + encodeURIComponent(aKeyword);
		
	return aURL + urlSnippet;
}

function createLinks() {

	var divNode = document.getElementById("relLinks");
	
	
	
	var trNode = findNode(findNode(findNode(divNode,"TABLE"),"TBODY"),"TR");
	
	
	var anchorNode;
	
	trNode = trNode.nextSibling; // skip first TR row
	
	if (trNode.nodeName != "TR") 
	{
		trNode = trNode.nextSibling.nextSibling; // workaround for Firefox which has additional nodes
	}
	trNode = trNode.nextSibling; // skip second TR row
	
	while (trNode != null) {
		// loop through TR nodes
		
		if (trNode.nodeName == "TR") {
		
			if (trNode.childNodes.length == 3) { // anchor tag should always be in 3rd TD tag
				anchorNode = findNode(trNode.childNodes[2],"A");
			} else if (trNode.childNodes.length == 7) {
				anchorNode = findNode(trNode.childNodes[5],"A");	// workaround for Firefox which has additional nodes
			}
			
	
			if (anchorNode != null) {
				for (var i=0; i < anchorNode.attributes.length; i++) {
					if (anchorNode.attributes[i].nodeName == "href") {
			
						// pass collection from xml doc and keyword
			
						//alert(anchorNode.firstChild.nodeValue);
						theURL = searchLinks(req.responseXML.documentElement.getElementsByTagName("Link"),anchorNode.firstChild.nodeValue); 
						//alert(theURL);
						if (theURL.length > 0) {
							anchorNode.attributes[i].nodeValue = appendTrack(theURL,anchorNode.firstChild.nodeValue);
						}
						
					}
	
				}	
			}
		}
		
		trNode = trNode.nextSibling;
	} 
		

	divNode.style.visibility = "visible";
	

}

// start XML grab after window is loaded
window.onload = grabXML;
