var xpathSize; //used for displaying chunked results ( 1 2 3 4 >> )
var arrSize;
/**
Temporary function for reading in URL variables

index = index of URL variable of choice
*/
var urlHash = new Array();

/**
Get URL variables for parent.location.hash
e.g. http://alfalfa.ucdavis.edu#q=Putnam
*/
function parseURL()
{
    var URL = parent.location.href;

    URL = URL.split("?")[1]; //get right portion after first "?"
   
    if(URL != null && URL.indexOf("=") != -1)
    {
        URL = URL.split("&"); //URL is now an array of URL variables
       
        for(var i = 0; i < URL.length; i++) //store into hash table
        {
            //alert(URL[i].split("=")[0] + " = " + URL[i].split("=")[1].replace(/(%20)/g, " "));
            urlHash["'"+URL[i].split("=")[0]+"'"] = URL[i].split("=")[1].replace(/(%20)/g, " ");
        }
    }
}
function getURL(variable)
{
    return urlHash["'"+variable+"'"];
}

/**
for normal URL variables
*/
/** not currently using this function
function parseURL()
{
	var URL = document.location.href;
	URL = URL.split("?")[1]; //get right portion after first "?"
	
	if(URL != null)
	{
		URL = URL.split("#")[0]; //get portion left of #section
	
		if(URL != null)
		{
			URL = URL.split("&"); //URL is now an array of URL variables
			
			for(var i = 0; i < URL.length; i++) //store into hash table
			{
				//alert(URL[i].split("=")[0] + " = " + URL[i].split("=")[1].replace(/(%20)/g, " "));
				urlHash["'"+URL[i].split("=")[0]+"'"] = URL[i].split("=")[1].replace(/(%20)/g, " ");
			}
		}
	}
}*/


/**
Function for reading in XML file for IE and other browsers.
Each section is for each browser set, returns xmlDoc object.

fname = xml file name including .xml
*/

/**
function loadXMLDoc(fname)
{
	try //Internet Explorer
	{
	   xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e) //not internet explorer
	{
	  try //Firefox, Mozilla, Opera, etc.
	  {
			xmlDoc = document.implementation.createDocument("","",null);
	   }
	   catch(e) {alert(e.message)} //non-supported browser
	}
	try 
	{
	   xmlDoc.async=false;
	   xmlDoc.load(fname);
	   //alert("igothere");
	   return(xmlDoc);
	}
	catch(e) {alert('ERROR: ' + e.message)}
}*/


function loadXMLDoc(fname)
{
	var curXmlDoc;
	
	// code for IE
	if (window.ActiveXObject)
	{
	  curXmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  curXmlDoc.async=false;   
	  curXmlDoc.load(fname);
	}
	
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
	   var req = new XMLHttpRequest();
	   req.open("GET", fname , false); 
	   req.send(null);
	   curXmlDoc = req.responseXML;
	  //xmlDoc = document.implementation.createDocument("","",null);
	}
	else
	{
	  alert('Your browser cannot handle this script');
	}
	
	return(curXmlDoc);
}


/**
Function for xpath for IE and other browsers.
Each section is for each browser set, both return an array of nodes.

whereStr = xpath string
limit = boolean value for setting a limit
limitAmt = integer for how many values to return (set limit first else set this to 0)
start = starting index
*/
function getXpath(whereStr, limitAmt, curXmlDoc)
{
	var curMenu;
	var nodes;
	
	if(window.ActiveXObject) //IE
	{	
		curXmlDoc.setProperty("SelectionLanguage", "XPath");	
		nodes = curXmlDoc.selectNodes(whereStr);
		xpathSize = nodes.length;
	}
	else if(document.implementation && document.implementation.createDocument) //FIREFOX
	{
		nodes = curXmlDoc.evaluate(whereStr, curXmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
		xpathSize = nodes.snapshotLength;
	}
	
	//alert(xpathSize);
	
	if(limitAmt != -1 && xpathSize > limitAmt)  //limitAmt is set
		arraySize = limitAmt;
	else
		arraySize = xpathSize; //nodes.length <= limitAmt or not set

	curMenu = new Array(arraySize);
	
	if(window.ActiveXObject)
	{
		for(i=0; i<arraySize; i++)
		{
			curMenu[i] = nodes[i];
		}
	}
	else if(document.implementation && document.implementation.createDocument) //FIREFOX
	{
		for(i=0; i<arraySize; i++)
		{
			curMenu[i] = nodes.snapshotItem(i);
		}
	}
	
	return curMenu;
}

function getDropDownMenu(whereStr, limitAmt, curXmlDoc, searchText)
{
	var nodes;
	
	if(window.ActiveXObject) //IE
	{	
		curXmlDoc.setProperty("SelectionLanguage", "XPath");	
		nodes = curXmlDoc.selectNodes(whereStr);
		xpathSize = nodes.length;
	}
	else if(document.implementation && document.implementation.createDocument) //FIREFOX
	{
		nodes = curXmlDoc.evaluate(whereStr, curXmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
		xpathSize = nodes.snapshotLength;
	}
	
	//alert(xpathSize);
	
	if(limitAmt != -1 && xpathSize > limitAmt)  //limitAmt is set
		arraySize = limitAmt;
	else
		arraySize = xpathSize; //nodes.length <= limitAmt or not set
	
	var topMenu = new Array();
	var btmMenu = new Array();
	var done = false;
	var i = 0;
	
	//alert(topMenu.length); alert(xpathSize);
	
	for(i = 0; i < xpathSize; i ++)
	{
		if(window.ActiveXObject)
			curText = nodes[i].getElementsByTagName('k')[0].childNodes[0].nodeValue;
		else if(document.implementation && document.implementation.createDocument)
			curText = nodes.snapshotItem(i).getElementsByTagName('k')[0].childNodes[0].nodeValue;
		
		if(REG_EXP_MATCH("^"+searchText, curText.toLowerCase()))
		{
			//alert("starts with:" + curText);
			topMenu[topMenu.length] = curText;
		}
		else
		{
			//alert("doesn't:" + curText);
			btmMenu[btmMenu.length] = curText;
		}
		
		if(topMenu.length == arraySize)
		{
			//alert(topMenu.length);
			return topMenu;
			//done = true;
			//allTopMenu = true;
		}
		//else finish filling both arrays
	}
	//alert(btmMenu.length);
	var j = topMenu.length;
	i = 0;
	while(topMenu.length < arraySize)
	{
		topMenu[j] = btmMenu[i];
	i++;
	j++;
	}
	return topMenu;
}

function REG_EXP_MATCH(regExpStr, matchMe)
{
	var reg_exp = new RegExp(regExpStr);
	return matchMe.match(reg_exp);
}

/**
	//code for Firefox, Opera, Safari/Chrome etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		
		if(limit && xpathSize > limitAmt)
		{
			alert("broke limit");
			menu = new Array(limitAmt);
			for(i=0; i<limitAmt; i++)
			{
				menu[i] = nodes.snapshotItem(i);
			}
		}
		else
		{
			if( (nodes.snapshotLength - start) < displaySize)
			{
				menu = new Array(nodes.snapshotLength - start);
			}
			else
				menu = new Array(displaySize);
			
			var j = 0;
			for(var i=start; i<(start + menu.length); i++)
			{
				menu[j] = nodes.snapshotItem(i);
				j++;
			}
		}
	}
	
	return menu;
}
*/
