
function checklength(stext)
{
    var button=document.getElementById("searchbutton");
    if (stext.value.length<2) button.disabled=true; else button.disabled=false;
}

function expand(id)
{
    var expandrow=document.getElementById(id);
    if (expandrow.style.display=="none") expandrow.style.display="block"; else expandrow.style.display="none";
}
function togglemenu()
{  var searchtool=document.getElementById("searchtool");
   var triangle=document.getElementById("triangle");
   var button=document.getElementById("togglebutton");
   if (firsttime==1) {document.getElementById("filter").value="";firsttime=0;}
   
   if (triangle.style.display=="block")
   { 
        triangle.style.display="none";
        searchtool.style.display="block";
        button.value="Show Clickable Map";
   }
   else 
   {
        triangle.style.display="block";
        searchtool.style.display="none";
        button.value="Show Search Tool";
   }
}

function cleanup()
{
    var stext=document.getElementById("filter");
    var result=document.getElementById("result");
    var resultcount=document.getElementById("resultcount");
    result.innerHTML="";
    stext.value="";
    resultcount.innerHTML="";
}

function search()
{
    var stext=document.getElementById("filter").value;
    var res,res1;
    stext=stext.toUpperCase();
    var result=document.getElementById("result");
    var resultcount=document.getElementById("resultcount");
    var found=new Array();
    if (stext.length<2) return;
	//document.getElementById("doccount").innerHTML=DocTitles.length;
	res1=DocTitles.length+"<br>";
    for (i=0;i<DocTitles.length;i++)
    {
		var title=DocTitles[i].toUpperCase();
		var summary=DocSummaries[i].toUpperCase();
		var code=DocCodes[i].toUpperCase();
		var f=0;
		if (title.indexOf(stext)>-1) {f=1;}
		if (summary.indexOf(stext)>-1) {f=1;}
		if (code.indexOf(stext)>-1) {f=1;}
		if (f==1) found[found.length]=i;
		//res1=res1+DocTitles[i]+"<br>";
    }
    res="<table id='t1' class='example table-autosort table-autofilter table-filtered-rowcount:t1filtercount table-rowcount:t1allcount'>";
    res=res+"<thead>";
    res=res+"<tr>";
    res=res+"<th>No</th>";
    res=res+"<th class='table-sortable:default'>Document Title</th>";
    res=res+"<th class='table-filterable table-sortable:default'>Document Level</th>";
    res=res+"<th class='table-filterable table-sortable:default'>Document Area</th>";
    res=res+"<th class='table-filterable table-sortable:default'>Document Topic</th>";
    res=res+"<th>Year</th>";
    res=res+"<th>PDF</th>";
    res=res+"</tr>";
    res=res+"</thead>";
    res=res+"<tbody>";
    for (i=0;i<found.length;i++)
    {
        var color;
        if ((i%2)==0) color=" bgcolor=#eeeeee "; else color=" bgcolor=#eeeeee ";
        res=res+"<tr"+color+">";
        res=res+"<td align=right width=5% nowrap style='cursor:hand;padding-right:6px;' onclick='expand("+i+")' title='Click to see document summary'><b>"+(i+1)+"</b></td>"
        res=res+"<td onclick='expand("+i+")' style='cursor:hand;' title='Click to see document summary'>";
        res=res+DocTitles[found[i]]+" - (<font color=red>"+DocCodes[found[i]]+"</font>)";
        res=res+"</td>"
        res=res+"<td width=10% >"+DocLevels[found[i]]+"</td>";
        res=res+"<td width=10% >"+DocAreas[found[i]]+"</td>";
        res=res+"<td width=10% >"+DocTopics[found[i]]+"</td>";
        res=res+"<td width=7% align=center>"+DocPublicationYears[found[i]]+"</td>";
        res=res+"<td width=5% nowrap align=center valign=center>";
        if (DocURLs[found[i]].length>4) res=res+"<a target=_blank href="+DocURLs[found[i]]+"><img border=0 src='TriangleFiles/acrobat.gif' style='margin-top:4px;border:none;' ></a>";
        res=res+"</td>";
        res=res+"</tr>";
        res=res+"<tr id='"+i+"'style='display: none;'><td>&nbsp;<td colspan=5>"+DocSummaries[found[i]]+"</td><td>&nbsp;</td></tr>"
    }
    res=res+"</tbody>";
    res=res+"<tfoot>"
	res=res+"<tr><th colspan='6' align='center'>"+found.length+"&nbsp;of "+DocTitles.length+"&nbsp;documents match filter(s)</th></tr>"
    res=res+"</tfoot>"
    res=res+"</table>";        
        //document.write(res1);
    result.innerHTML=res;
    resultcount.innerHTML=found.length;
} 

function loadXMLDoc(dname)
{
	var xmlDoc;
// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	xmlDoc.async=false;
	xmlDoc.load(dname);
	return(xmlDoc);
}

var DocTitles = new Array();
var DocSummaries = new Array();
var DocURLs = new Array();
var DocPublicationYears = new Array();
var DocCodes = new Array();
var DocLevels = new Array();
var DocAreas = new Array();
var DocTopics = new Array();
var DocNESSeries = new Array();
var DocDraft = new Array();
var DocPrinciples = new Array();
var DocObjectives = new Array();
var DocGuides = new Array();
var DocReports = new Array();
var tblAreas= new Array();
var tblTopics= new Array();
var tblAreaDoc = new Array();
var tblTopicDoc = new Array();
var firsttime = 1;

var DocNo=0;
var nsub=0;
var nsum=0;

tblAreas[0]="Nuclear General";
tblAreas[1]="Nuclear Power";
tblAreas[2]="Nuclear Fuel Cycle";
tblAreas[3]="Radioactive Waste Management";

tblTopics[0]="Management Systems";
tblTopics[1]="Human Resources";
tblTopics[2]="Nuclear Infrastructure and Planning";
tblTopics[3]="Economics";
tblTopics[4]="Energy System Analysis";
tblTopics[5]="Knowledge Management";
tblTopics[6]="Technology Development";
tblTopics[7]="Design of Nuclear Power Plants";
tblTopics[8]="Operation of Nuclear Power Plants";
tblTopics[9]="Non Electrical Applications";
tblTopics[10]="Research Reactors";
tblTopics[11]="Resources";
tblTopics[12]="Fuel Engineering";
tblTopics[13]="Spent Fuel Management and Reprocessing";
tblTopics[14]="Advanced Fuel Cycles";
tblTopics[15]="Research Reactors NFC and Materials";
tblTopics[16]="Radioactive Waste Management";
tblTopics[17]="Decommissioning of Nuclear Facilities";
tblTopics[18]="Remediation of Sites";

for (var i=0;i<tblAreas.length;i++) tblAreaDoc[i]=new Array();
for (var i=0;i<tblTopics.length;i++) tblTopicDoc[i]=new Array();

	xmlDoc=loadXMLDoc("NESDocuments.xml");
	var x=xmlDoc.documentElement;
	var y=xmlDoc.documentElement.childNodes;

	for (i=0;i<y.length;i++)
	{
		DocAreas[DocNo]=new Array();
		DocTopics[DocNo]=new Array();
		if (y[i].nodeType!=3)
		{
			for (z=0;z<y[i].childNodes.length;z++)
			{
				if (y[i].childNodes[z].nodeType!=3)
				{
					if (y[i].childNodes[z].nodeName=="Title") {
						DocTitles[DocNo]=y[i].childNodes[z].childNodes[0].nodeValue;
						DocNo++;
					}
					if (y[i].childNodes[z].nodeName=="Level") {
						if (y[i].childNodes[z].childNodes[0].nodeValue=="Basic Principles") DocPrinciples[DocPrinciples.length]=DocNo-1;
						if (y[i].childNodes[z].childNodes[0].nodeValue=="Objectives") DocObjectives[DocObjectives.length]=DocNo-1;
						if (y[i].childNodes[z].childNodes[0].nodeValue=="Guides") DocGuides[DocGuides.length]=DocNo-1;
						if (y[i].childNodes[z].childNodes[0].nodeValue=="Reports") DocReports[DocReports.length]=DocNo-1;
					}
					if (y[i].childNodes[z].nodeName=="Summary") DocSummaries[DocNo-1]=y[i].childNodes[z].childNodes[0].nodeValue;
					if (y[i].childNodes[z].nodeName=="Level") DocLevels[DocNo-1]=y[i].childNodes[z].childNodes[0].nodeValue;
					if (y[i].childNodes[z].nodeName=="Code") DocCodes[DocNo-1]=y[i].childNodes[z].childNodes[0].nodeValue;
					if (y[i].childNodes[z].nodeName=="NESSeries") DocNESSeries[DocNo-1]=y[i].childNodes[z].childNodes[0].nodeValue;
					if (y[i].childNodes[z].nodeName=="Draft") DocDraft[DocNo-1]=y[i].childNodes[z].childNodes[0].nodeValue;
					if (y[i].childNodes[z].nodeName=="PublicationYear") DocPublicationYears[DocNo-1]=y[i].childNodes[z].childNodes[0].nodeValue;
					if (y[i].childNodes[z].nodeName=="IAEAURL") {
					    DocURLs[DocNo-1]=y[i].childNodes[z].childNodes[0].nodeValue;
					    DocURLs[DocNo-1].replace(/&amp;/,"&");
					}
					if (y[i].childNodes[z].nodeName=="Area") {
						DocAreas[DocNo-1][DocAreas[DocNo-1].length]=y[i].childNodes[z].childNodes[0].nodeValue;
						for (l=0;l<tblAreas.length;l++)
						{
							if (y[i].childNodes[z].childNodes[0].nodeValue==tblAreas[l]) tblAreaDoc[l][tblAreaDoc[l].length]=DocNo-1;
						}
					}
					if (y[i].childNodes[z].nodeName=="Topic") {
						DocTopics[DocNo-1][DocTopics[DocNo-1].length]=y[i].childNodes[z].childNodes[0].nodeValue;
						for (l=0;l<tblTopics.length;l++)
						{
							if (y[i].childNodes[z].childNodes[0].nodeValue==tblTopics[l]) tblTopicDoc[l][tblTopicDoc[l].length]=DocNo-1;
						}
					}
				}
			}
		}
	} 
	var DocTopicPrincipleCounts=new Array();
	var DocTopicObjectiveCounts=new Array();
	var DocTopicGuideCounts=new Array();
	var DocTopicReportCounts=new Array();
	for (i=0;i<tblTopics.length;i++) {
		DocTopicPrincipleCounts[i]=0;
		DocTopicObjectiveCounts[i]=0;
		DocTopicGuideCounts[i]=0;
		DocTopicReportCounts[i]=0;
		for (j=0;j<tblTopicDoc[i].length;j++) {
		   if (DocLevels[tblTopicDoc[i][j]]=="Basic Principles") DocTopicPrincipleCounts[i]++;
		   if (DocLevels[tblTopicDoc[i][j]]=="Objectives") DocTopicObjectiveCounts[i]++;
		   if (DocLevels[tblTopicDoc[i][j]]=="Guides") DocTopicGuideCounts[i]++;
		   if (DocLevels[tblTopicDoc[i][j]]=="Reports") DocTopicReportCounts[i]++;
		}
	}
	var DocAreaPrincipleCounts=new Array();
	var DocAreaObjectiveCounts=new Array();
	var DocAreaGuideCounts=new Array();
	var DocAreaReportCounts=new Array();
	for (i=0;i<tblAreas.length;i++) {
		DocAreaPrincipleCounts[i]=0;
		DocAreaObjectiveCounts[i]=0;
		DocAreaGuideCounts[i]=0;
		DocAreaReportCounts[i]=0;
		for (j=0;j<tblAreaDoc[i].length;j++) {
		   if (DocLevels[tblAreaDoc[i][j]]=="Basic Principles") DocAreaPrincipleCounts[i]++;
		   if (DocLevels[tblAreaDoc[i][j]]=="Objectives") DocAreaObjectiveCounts[i]++;
		   if (DocLevels[tblAreaDoc[i][j]]=="Guides") DocAreaGuideCounts[i]++;
		   if (DocLevels[tblAreaDoc[i][j]]=="Reports") DocAreaReportCounts[i]++;
		}
	}		
	//for (i=0;i<tblTopics.length;i++) {
	//	document.write("tblTopics(" + i + ")= "+ tblTopics[i] +"<br>");
	//	for (j=0;j<tblTopicDoc[i].length;j++) {
	//	   document.write("    tblTopicDoc(" + i +","+j+ ")= "+ DocTitles[tblTopicDoc[i][j]] +"<br>");
	//	}
	//}	
	//for (i=0;i<tblAreas.length;i++) {
	//	document.write("tblAreas(" + i + ")= "+ tblAreas[i] +"<br>");
	//	for (j=0;j<tblAreaDoc[i].length;j++) {
	//	   document.write("    tblAreaDoc(" + i +","+j+ ")= "+ DocTitles[tblAreaDoc[i][j]] +"<br>");
	//	}
	//}	
	
	// for (i=0;i<DocNo;i++) {
		//   document.write(DocTitles[i] + "<br>" + DocAreas[i].length +"<br>");
		//   for (j=0;j<DocAreas[i].length;j++)
		//       document.write("----"+DocAreas[i][j] + "<br>");
	// }
	//document.getElementById("doccount").innerHTML=DocTitles.length;
	//document.write("DocNo1="+DocNo+"<br>");
