//Used by XMLHttpRequest
var xmlHttp;
//Main variable that will hold the overlib box
var content;

/*Main function to load the XML file and then create the box*/
function loadXML() {
    xmlHttp=GetXmlHttpObject(stateChanged);
	//Here we try to access the linkBase which is stored in this current directory
    xmlHttp.open("GET",linkBase, true);
    xmlHttp.send(null);
}

/*Check whether the call is completed or not*/
function stateChanged() 
{ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	 // if we retrieve the file OK then just get the box
	  if (xmlHttp.status==200) {
		         xmlString = xmlHttp.responseText;
				 //When the call is complete call getLinkBox to process the XML document
		         getLinkBox(xmlString);
	    }
	  //If we haven't managed to get the linkbase then try to return and empty box
	  else{
	    getEmptyLinkBox();
    }
  }
} 

/*Try to get the browser independent XMLHttpRequest object*/
function GetXmlHttpObject(handler)
{ 
    var objXmlHttp=null;

    if (navigator.userAgent.indexOf("Opera")>=0){
        objXmlHttp=new XMLHttpRequest(); 
        return objXmlHttp;
    }//if
    if (navigator.userAgent.indexOf("MSIE")>=0){ 
        var strName="Msxml2.XMLHTTP";
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
            strName="Microsoft.XMLHTTP";
        }//if 
        try{ 
            objXmlHttp=new ActiveXObject(strName);
            objXmlHttp.onreadystatechange=handler; 
            return objXmlHttp;
        }//try 
        catch(e){ 
            alert("Error. Scripting for ActiveX might be disabled"); 
            return; 
        }//catch 
    }//if 
    if (navigator.userAgent.indexOf("Mozilla")>=0){
        objXmlHttp=new XMLHttpRequest();
        objXmlHttp.onload=handler;
        objXmlHttp.onerror=handler; 
        return objXmlHttp;
    }//if
}
/*This funcation parses the XML string by using the right browser parser and then creates the linkbox*/
function getLinkBox(xmlString)
{
	 var xmlDoc;
	
	 //Check if the browser is IE
	 if (window.ActiveXObject){
	 	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  	xmlDoc.async="false";
	   	xmlDoc.loadXML(xmlString);
	 }//if
	 // Otherwise code for Mozilla, Firefox, Opera, etc.
	 else{
		var parser = new DOMParser();
	    xmlDoc=parser.parseFromString(xmlString,"text/xml");
	 }//else
		
	 var linkList=xmlDoc.getElementsByTagName("link");
	 content='<div id=\'mainBox\'><div id=\'boxTitle\'>Who refers to this page?<a href=\'javascript:return cClick();\' title=\'Click to Close\' onClick=\'return cClick();\'><img id=\'boxClose\' src=\"'+imageLocation+'\"alt=\'Click to Close\'\/><\/a></div><div id=\'linkList\'>';
	 var linkListDiv='';
	 if(linkList.length==0){
		linkListDiv='<p>No Links Available!<br\/><br\/></p>';
	 }//if
	 else{
          linkListDiv='<ul>'
	 	for(var i=0;i<linkList.length;i++){
	 		var title=linkList[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
	 		var url= linkList[i].getElementsByTagName("url")[0].childNodes[0].nodeValue;
        	linkListDiv = linkListDiv+'<li><a href=\''+url+'\'>'+title+'</a></li>';
		}//for
		linkListDiv = linkListDiv+'</ul>'
	}//else
	content= content+linkListDiv+'</div></div>';
}

function getEmptyLinkBox(){
	content='<div id=\'mainBox\'><div id=\'boxTitle\'>Who refers to this page?<a href=\'javascript:return cClick();\' title=\'Click to Close\' onClick=\'return cClick();\'><img id=\'boxClose\' src=\"'+imageLocation+'\"alt=\'Click to Close\'\/><\/a></div><div id=\'linkList\'>';
    var linkListDiv='<p>No Links Available!<br\/><br\/></p>';
	content= content+linkListDiv+'</div></div>';
}
