 var sectionDivId="";
 var urlQuery="";

function displaySectionLevelHierarchy(queryString,id){
  	var displayState = document.getElementById("sectionHintBox" +id).style.display;
	var displayStateValue = document.getElementById("sectionHint" +id).value;
		if (displayState == 'block' && displayStateValue == "sectionsDisplayed") {
		document.getElementById("plus" +id).style.display = "inline";
		document.getElementById("minus" +id).style.display = "none";
		document.getElementById("sectionHintBox" +id).style.display = "none";
		return;
		} 
		else if (displayStateValue == "sectionsDisplayed"){
		document.getElementById("minus" +id).style.display = "inline";
		document.getElementById("plus" +id).style.display = "none";
		document.getElementById("sectionHintBox" +id).style.display = "block";
		return;
	}
			  
        
	 sectionDivId = id;
	 var url =  null;
	 urlQuery = queryString;
		url = "/web/search/catalogueSearchAction.html?method=displaySectionLevelHierarchy&searchHierarchyParam="+encodeURIComponent(queryString);
   	  //Do the Ajax call
	  if (window.XMLHttpRequest){ // Non-IE browsers
			  req = new XMLHttpRequest();
			  //A call-back function is define so the browser knows which function to call after the server gives a reponse back
			  req.onreadystatechange = displaySections;
			  try {
				   req.open("GET", url, true); //was get
			      } catch (e) {
				  alert("Cannot connect to server");
			    }
			  req.send(null);
			} else if (window.ActiveXObject) { // IE      
	         req = new ActiveXObject("Microsoft.XMLHTTP");
			  if (req) {
					req.onreadystatechange = displaySections;
					req.open("GET", url, true);
					req.send();
			  }
		   }
     }

	    //Callback function
   function displaySections(){
	   	   
	   if (req.readyState == 4) { // Complete
			 var textToSplit = req.responseText;
		   
			/* check for the session expired */
			var checkSession = textToSplit.split("><");
			if(checkSession[0]=="<html"){		
				return;
			}
		    var sectionList = "";
			var urlCount = textToSplit.indexOf("||||");
		    var url = textToSplit.substring(0,urlCount );
		    var temp =  textToSplit.substring(urlCount+4, textToSplit.length);
			var returnElements = temp.split("||");
			if(returnElements[0] == "") {
				 //alert('No Associated Sections');
				return;
			}
	 	  	for ( var i=0; i<returnElements.length; i++ ){
		       valueLabelPair = returnElements[i].split("|");
		       var section = valueLabelPair[0];
		       var first = section.indexOf("(");
		       var last = section.indexOf(")");
		       var sectionName =  section.substring(0,first);
		       var binCount = section.substring(first+1,last);
		       var query = valueLabelPair[1]; 
		       sectionList = sectionList +"<li><a href='"+url+query +"'>"+ sectionName +"</a><span>("+binCount+")</span></li>";
	      }
        
		document.getElementById("minus" +sectionDivId).style.display = "inline";
		document.getElementById("plus" +sectionDivId).style.display = "none";
		document.getElementById("sectionHintBox" +sectionDivId).style.display = "block";
		document.getElementById("sectionHint" +sectionDivId).innerHTML = sectionList;
		document.getElementById("sectionHint" +sectionDivId).value = "sectionsDisplayed";
     }
  
   }


