//Function that allows multiple calls to functions upon loading of the web page
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
		window[fnc]();		
      };
    }
    else 
      window.onload = fnc;
  }
}

//Function to round numbers, changing the rlength variable indicates the number of decimal places to round to
function roundNumber(theNumber) 
{
  var rnum = theNumber;
  var rlength = 0;
  if (rnum > 8191 && rnum < 10485) 
  {
	rnum = rnum-5000;
	var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);		
  } 
  else 
  {
	var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  }
  return newnumber;
}

//Function that converts words in a string to the proper case
function PCase(STRING)
{
  var strReturn_Value = "";
  var iTemp = STRING.length;
  if(iTemp==0)
  {
    return"";
  }
  var UcaseNext = false;
  strReturn_Value += STRING.charAt(0).toUpperCase();
  for(var iCounter=1;iCounter < iTemp;iCounter++)
  {
    if(UcaseNext == true)
	{
	  strReturn_Value += STRING.charAt(iCounter).toUpperCase();
	}
	else
	{
	  strReturn_Value += STRING.charAt(iCounter).toLowerCase();
	}
	var iChar = STRING.charCodeAt(iCounter);
	if(iChar == 32 || iChar == 45 || iChar == 46)
	{
	  UcaseNext = true;
	}
	else
	{
	  UcaseNext = false
	}
	if(iChar == 99 || iChar == 67)
	{
	  if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109)
	  {
	    UcaseNext = true;
	  }
	}
  }
return strReturn_Value;
}

//Function that returns an object having .x and .y properties which are the coordinates of the named anchor, relative to the page.
function getAnchorPosition(anchorname) 
{	
  var useWindow=false;
  var coordinates=new Object();
  var x=0,y=0;
  var use_gebi=false, use_css=false, use_layers=false;
  if (document.getElementById) { use_gebi=true; }
  else if (document.all) { use_css=true; }
  else if (document.layers) { use_layers=true; }	
  if (use_gebi && document.all) 
  {
	x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
	y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
  }
  else if (use_gebi) 
  {
	var o=document.getElementById(anchorname);
	x=AnchorPosition_getPageOffsetLeft(o);
	y=AnchorPosition_getPageOffsetTop(o);
  }
  else if (use_css) 
  {
	x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
	y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
  }
  else if (use_layers) 
  {
	var found=0;
	for (var i=0; i<document.anchors.length; i++) {
	  if (document.anchors[i].name==anchorname) { found=1; break; }
	}
	if (found==0) 
	{
	  coordinates.x=0; coordinates.y=0; return coordinates;
	}
	x=document.anchors[i].x;
	y=document.anchors[i].y;
  }
  else 
  {
	coordinates.x=0; coordinates.y=0; return coordinates;
  }
  coordinates.x=x;
  coordinates.y=y;
  return coordinates;
}

// Function getting the page offset left position
function AnchorPosition_getPageOffsetLeft (el) 
{
  var ol=el.offsetLeft;
  while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
  return ol;
}

// Function getting the page offset top position
function AnchorPosition_getPageOffsetTop (el) 
{
  var ot=el.offsetTop;
  while((el=el.offsetParent) != null) { ot += el.offsetTop; }
    return ot;
}

//Function that bookmarks a web page
function BookmarkPage(title, url)
{
  if (document.all)
  window.external.AddFavorite(url, title);
  else if (window.sidebar)
  window.sidebar.addPanel(title, url, "")
}

//Function that loads the court or agency drop down when the page is initially loaded by calling the import court or agency drop down function that takes in the area (court/agency) indicated.  Before the court or agency drop down is loaded, a message is displayed informing the user that the drop down is being loaded.
function loadCourtOrAgencyDropDown(Area) 
{
  var divTag = document.createElement('div');
  divTag.setAttribute('id','CourtOrAgency');
  divTag.id = 'CourtOrAgency';
  var loadingMessage = document.createTextNode("Loading...");
  divTag.appendChild(loadingMessage);
  document.getElementById('CourtOrAgencyDisplay').appendChild(divTag);
  importCourtOrAgencyDropDown(Area)
}

//Function that imports the court or agency drop down data from the xml document by receiving the area (court/agency) indicated, creating an xml document object, calling the display court or agency drop down function and loading the url which creates an xml document for the pertinent information using server side processing.  The server side process page receives the area (court/agency) indicated and the particular request (getting the court or agency drop down), so therefore, the server knows what to process.
function importCourtOrAgencyDropDown(Area)
{
  if (document.implementation && document.implementation.createDocument)
  {
	xmlDoc1 = document.implementation.createDocument("", "", null);
	xmlDoc1.onload = displayCourtOrAgencyDropDown;
  }
  else if (window.ActiveXObject)
  {
	xmlDoc1 = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc1.onreadystatechange = function () {
		if (xmlDoc1.readyState == 4) displayCourtOrAgencyDropDown()
	};
  }
  else
  {
	alert('Your browser can\'t handle this script');
	return;
  }
  xmlDoc1.load("/AM/CustomSource/ASPCode/caselawsearchprocesssimple.asp?Area=" + Area + "&theRequest=getCourtOrAgencyDropDown");	
}

//Function that reads through the xml document court or agency tags and pulls the information into a drop down for display on the web page
function displayCourtOrAgencyDropDown()
{
  var x = xmlDoc1.getElementsByTagName('courtoragencies');
  var selectTag = document.createElement('select');
  selectTag.setAttribute('name','CourtOrAgency');
  selectTag.name = 'CourtOrAgency';
  selectTag.setAttribute('id','CourtOrAgency');
  selectTag.id = 'CourtOrAgency';
  selectTag.setAttribute('onChange','loadPrintReadyVersionLink();loadBookmarkThisPageLink()');  
  var functionName = "dropDownChange";
  var theFunction = "selectTag.onchange = function " + functionName + "() { loadPrintReadyVersionLink();loadBookmarkThisPageLink() };";		
  selectTag.onchange = eval(theFunction);
  selectTag.setAttribute('style','width:240px');
  selectTag.style.width = '240px';
    
  for (j=0;j<x[0].childNodes.length;j++)
  {
	if (x[0].childNodes[j].nodeType != 1) continue;
	var optionTag = document.createElement('option');
	var isSelected = x[0].childNodes[j].getAttribute('selected');
	if (isSelected == 'yes')
	{
	  optionTag.setAttribute('selected','selected');
	  optionTag.selected = 'selected';
	}
	optionTag.setAttribute('value',x[0].childNodes[j].getAttribute('value'));
	optionTag.value = x[0].childNodes[j].getAttribute('value');
	var tagValue = document.createTextNode(x[0].childNodes[j].firstChild.nodeValue);
	optionTag.appendChild(tagValue);
	selectTag.appendChild(optionTag);					
  }	
  if (document.getElementById('CourtOrAgencyDisplay').hasChildNodes())
  {
	document.getElementById('CourtOrAgencyDisplay').removeChild(document.getElementById('CourtOrAgency'));
	document.getElementById('CourtOrAgencyDisplay').appendChild(selectTag);
  }
  else
  {
	document.getElementById('CourtOrAgencyDisplay').appendChild(selectTag);
  }				
}

//Function that loads the number of cases per page drop down when the page is initially loaded by calling the import number of cases per page drop down function that takes in the value of the number of cases per page drop down.  Before the number of cases per page drop down is loaded, a message is displayed informing the user that the drop down is being loaded.
function loadNumberOfCasesPerPageDropDown(NumLines) 
{
  var divTag = document.createElement('div');
  divTag.setAttribute('id','NumLines');
  divTag.id = 'NumLines';
  var loadingMessage = document.createTextNode("Loading...");
  divTag.appendChild(loadingMessage);
  document.getElementById('NumberOfCasesPerPageDisplay').appendChild(divTag);
  importNumberOfCasesPerPageDropDown(NumLines)
}

//Function that imports the number of cases per page drop down data from the xml document by receiving the value of the number of cases per page drop down, creating an xml document object, calling the display number of cases per page drop down function and loading the url which creates an xml document for the pertinent information using server side processing.  The server side process page receives the number of cases per page value and the particular request (getting the number of cases per page drop down), so therefore, the server knows what to process.
function importNumberOfCasesPerPageDropDown(NumLines)
{
  if (document.implementation && document.implementation.createDocument)
  {
	xmlDoc2 = document.implementation.createDocument("", "", null);
	xmlDoc2.onload = displayNumberOfCasesPerPageDropDown;
  }
  else if (window.ActiveXObject)
  {
	xmlDoc2 = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc2.onreadystatechange = function () {
		if (xmlDoc2.readyState == 4) displayNumberOfCasesPerPageDropDown()
	};
  }
  else
  {
	alert('Your browser can\'t handle this script');
	return;
  }
  xmlDoc2.load("/AM/CustomSource/ASPCode/caselawsearchprocesssimple.asp?NumLines=" + NumLines + "&theRequest=getNumberOfCasesPerPageDropDown");	
}

//Function that reads through the xml document number of cases per page tags and pulls the information into a drop down for display on the web page
function displayNumberOfCasesPerPageDropDown()
{
  var x = xmlDoc2.getElementsByTagName('numberofcasesperpages');
  var selectTag = document.createElement('select');
  selectTag.setAttribute('id','NumLines');
  selectTag.id = 'NumLines';
  selectTag.setAttribute('name','NumLines');
  selectTag.name = 'NumLines';
  selectTag.setAttribute('onChange','loadPrintReadyVersionLink();loadBookmarkThisPageLink()');  
  var functionName = "dropDownChange2";
  var theFunction = "selectTag.onchange = function " + functionName + "() { loadPrintReadyVersionLink();loadBookmarkThisPageLink() };";		
  selectTag.onchange = eval(theFunction);
  selectTag.setAttribute('style','width:240px');
  selectTag.style.width = '240px';
  
  for (j=0;j<x[0].childNodes.length;j++)
  {
	if (x[0].childNodes[j].nodeType != 1) continue;
	var optionTag = document.createElement('option');
	var isSelected = x[0].childNodes[j].getAttribute('selected');
	if (isSelected == 'yes')
	{
	  optionTag.setAttribute('selected','selected');
	  optionTag.selected = 'selected';
	}
	optionTag.setAttribute('value',x[0].childNodes[j].getAttribute('value'));
	optionTag.value = x[0].childNodes[j].getAttribute('value');
	var tagValue = document.createTextNode(x[0].childNodes[j].firstChild.nodeValue);
	optionTag.appendChild(tagValue);
	selectTag.appendChild(optionTag);					
  }	
  if (document.getElementById('NumberOfCasesPerPageDisplay').hasChildNodes())
  {
	document.getElementById('NumberOfCasesPerPageDisplay').removeChild(document.getElementById('NumLines'));
	document.getElementById('NumberOfCasesPerPageDisplay').appendChild(selectTag);
  }
  else
  {
	document.getElementById('NumberOfCasesPerPageDisplay').appendChild(selectTag);
  }
}

//Function that sets the order by option value when an option is clicked
function setOrderByOptions()
{ // Huh? Commented out.
 /* if (document.getElementById('OrderBy').value == "FilingDate")
  {
	document.getElementById('OrderBy').value = "Parties";
  }
  else
  {
	document.getElementById('OrderBy').value = "FilingDate";
  }  */
}

//Function that imports the page title data from the xml document by receiving the current value of the court or agency drop down, creating an xml document object, calling the display page title function and loading the url which creates an xml document for the pertinent information using server side processing.  The server side process page receives the value of the court or agency drop down and the particular request (getting the page title), so therefore, the server knows what to process.
function importPageTitle(CourtOrAgency)
{
  if (document.implementation && document.implementation.createDocument)
  {
	xmlDoc3 = document.implementation.createDocument("", "", null);
	xmlDoc3.onload = displayPageTitle;
  }
  else if (window.ActiveXObject)
  {
	xmlDoc3 = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc3.onreadystatechange = function () {
		if (xmlDoc3.readyState == 4) displayPageTitle()
	};
  }
  else
  {
	alert('Your browser can\'t handle this script');
	return;
  }
  xmlDoc3.load("/AM/CustomSource/ASPCode/caselawsearchprocesssimple.asp?CourtOrAgency=" + CourtOrAgency + "&theRequest=getPageTitle");	
}

//Function that reads through the xml document page title tag and pulls the information into the web page for display
function displayPageTitle()
{
  var x = xmlDoc3.getElementsByTagName('pagetitles');
  var divTag = document.createElement('div');
  divTag.setAttribute('id','PageTitle');
  divTag.id = 'PageTitle';  
  for (j=0;j<x[0].childNodes.length;j++)
  {
	if (x[0].childNodes[j].nodeType != 1) continue;
	var tagValue = document.createTextNode(x[0].childNodes[j].firstChild.nodeValue);
  }  
  var h3Tag = document.createElement('h3');  
  h3Tag.appendChild(tagValue);
  divTag.appendChild(h3Tag);					
  if (document.getElementById('TitleDisplay').hasChildNodes())
  {
	document.getElementById('TitleDisplay').removeChild(document.getElementById('PageTitle'));
	document.getElementById('TitleDisplay').appendChild(divTag);
  }
  else
  {
	document.getElementById('TitleDisplay').appendChild(divTag);
  }  
}

//Function that loads the print-ready version link when the page is initially loaded by calling the display print-ready version link function
function loadPrintReadyVersionLink() 
{
  displayPrintReadyVersionLink();
}

//Calls the onload function to load the print-ready version link upon loading of the web page
addOnloadEvent(loadPrintReadyVersionLink);

//Function that displays the print ready version link to the web browser
function displayPrintReadyVersionLink() {
  var divTag = document.createElement('div');
  divTag.setAttribute('id','PrintReady');
  divTag.id = 'PrintReady';
}

//Function that loads the bookmark this page link when the page is initially loaded by calling the display bookmark this page link function
function loadBookmarkThisPageLink() 
{
  displayBookmarkThisPageLink();
}

//Calls the onload function to load the bookmark this page link upon loading of the web page
addOnloadEvent(loadBookmarkThisPageLink);

//Function that displays the bookmark this page link to the web browser
function displayBookmarkThisPageLink()
{
  var divTag = document.createElement('div');
  divTag.setAttribute('id','Bookmark');
  divTag.id = 'Bookmark';
  var theLink = document.createElement('a');
  var theURL = "http://www.wisbar.org/AM/CustomSource/ASPCode/caselawsearch.asp?SMod=simple&NoLoad=Yes&Area=" + document.getElementById('CourtOrAgency').value + "&NumLines=" + document.getElementById('NumLines').value + "&OrderBy=" + getSortOrder();
  var theTitle = "Wisconsin case law simple search";
  var theHREF = "javascript:BookmarkPage('" + theTitle + "','" + theURL + "')"; 
  theLink.setAttribute('href',theHREF);
  theLink.href = theHREF;  
  theLink.setAttribute('className','SideNav2');
  theLink.className = 'SideNav2';
  var imgTag = document.createElement('img');
  imgTag.setAttribute('src','/AM/Graphics/template/Interior/bookmark.gif');
  imgTag.src = '/AM/Graphics/template/Interior/bookmark.gif';
  imgTag.setAttribute('width','23');
  imgTag.width = '23';
  imgTag.setAttribute('height','22');
  imgTag.width = '22';
  imgTag.setAttribute('align','middle');
  imgTag.align = 'middle';
  var bookmarkThisPageText = document.createTextNode(" Bookmark this page");  
  theLink.appendChild(imgTag);
  theLink.appendChild(bookmarkThisPageText);
  divTag.appendChild(theLink);
}

//Function that imports the alert message data from the xml document by receiving the message to display, creating an xml document object, calling the display alert message function and loading the url which creates an xml document for the pertinent information using server side processing.  The server side process page receives the message to display and the particular request (getting the alert message), so therefore, the server knows what to process.
function importAlertMessage(TheMessage)
{
  if (document.implementation && document.implementation.createDocument)
  {
	xmlDoc4 = document.implementation.createDocument("", "", null);
	xmlDoc4.onload = displayAlertMessage;
  }
  else if (window.ActiveXObject)
  {
	xmlDoc4 = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc4.onreadystatechange = function () {
		if (xmlDoc4.readyState == 4) displayAlertMessage()
	};
  }
  else
  {
	alert('Your browser can\'t handle this script');
	return;
  }
  xmlDoc4.load("/AM/CustomSource/ASPCode/caselawsearchprocesssimple.asp?Message=" + TheMessage + "&theRequest=getAlertMessage");	
}

//Function that reads through the xml document alert message tag and pulls the information into the web page for display
function displayAlertMessage()
{
  var x = xmlDoc4.getElementsByTagName('alertmessages');
  var divTag = document.createElement('div');
  divTag.setAttribute('id','AlertMessage');
  divTag.id = 'AlertMessage';
  divTag.setAttribute('style','margin-left:15px');
  divTag.style.margin='15px';
  var fontTag = document.createElement('font');
  var bTag = document.createElement('b');
  fontTag.setAttribute('style','color:red');  
  fontTag.style.color = 'red';  
  for (j=0;j<x[0].childNodes.length;j++)
  {
	if (x[0].childNodes[j].nodeType != 1) continue;
	var tagValue = document.createTextNode(x[0].childNodes[j].firstChild.nodeValue);
	var tagValue2 = x[0].childNodes[j].firstChild.nodeValue;
  }
  bTag.appendChild(tagValue);
  fontTag.appendChild(bTag);  
  divTag.appendChild(fontTag);
  if (tagValue2 == 'No results found for your search.  Please try again.')
  {
    var pTag = document.createElement('p');
    divTag.appendChild(pTag);
	var theText = document.createTextNode('The following suggestions may assist you:');
	divTag.appendChild(theText);
	var pTag = document.createElement('p');
	divTag.appendChild(pTag);
	var ulTag = document.createElement('ul');
	var liTag = document.createElement('li');
	theText = document.createTextNode('Our system does not support the Boolean operator Not.');
	liTag.appendChild(theText);
	ulTag.appendChild(liTag);
	var liTag = document.createElement('li');
	theText = document.createTextNode('Boolean operators cannot be used at the end of a phrase.');
	liTag.appendChild(theText);
	ulTag.appendChild(liTag);
	var liTag = document.createElement('li');
	theText = document.createTextNode('Check spelling.');
	liTag.appendChild(theText);
	ulTag.appendChild(liTag);
	var liTag = document.createElement('li');
	var theLink = document.createElement('a');
	var theHREF = 'javascript:;';
	theLink.setAttribute('href',theHREF);
	theLink.href = theHREF;
	theLink.setAttribute('className','morelink');
	theLink.className = 'morelink';
	var theClickEvent = "document.getElementById('thelinks1').style.display='none';;document.getElementById('thelinks2').style.display='block';;document.getElementById('simplesearchtips').style.display='block';";
	theLink.setAttribute('onClick',theClickEvent);
	var functionName = "displaySimpleSearchTips";
  	var theFunction = "theLink.onclick = function " + functionName + "() { " + theClickEvent + "};";		
	theLink.onclick = eval(theFunction);
	theText = document.createTextNode('Search Tips');
	theLink.appendChild(theText);
	liTag.appendChild(theLink);
	ulTag.appendChild(liTag);
	divTag.appendChild(ulTag);	
  }
  if (document.getElementById('AlertMessageDisplay').hasChildNodes())
  {
	document.getElementById('AlertMessageDisplay').removeChild(document.getElementById('AlertMessage'));
	document.getElementById('AlertMessageDisplay').appendChild(divTag);
  }
  else
  {
	document.getElementById('AlertMessageDisplay').appendChild(divTag);
  }
  if (document.getElementById('RecordDisplay').hasChildNodes()) 
  {
	document.getElementById('RecordDisplay').removeChild(document.getElementById('sortedtable'));
	document.getElementById('RecordDisplay').removeChild(document.getElementById('Pagination'));	
  }
}

//Function that validates the form to make sure the user selected a court or agency and that they entered search criteria in the keywords field.  If the user did not select a court or agency or they did not enter search criteria in the keywords field, the appropriate alert message is displayed on the web page by calling the import alert message function that passes through the message to display.  If the user did select a court or agency and they did enter search criteria in the keywords field, then the import database records function is called.
function validateForm()
{
  if (document.form1.CourtOrAgency.selectedIndex == 0)
  {
  	importAlertMessage('Please select one of the court or agency options');
  }
  else if (document.form1.Keywords.value == "")
  {
  	//importAlertMessage('Please type a search criteria in the Keywords field');
	importPageTitle(document.form1.CourtOrAgency.value);
  	importDatabaseRecords("+", document.form1.Page.value, document.form1.LastRecordToDisplay.value);
  }
  else
  {
	importPageTitle(document.form1.CourtOrAgency.value);
  	importDatabaseRecords(document.form1.Keywords.value.replace(/'/, "\'"), document.form1.Page.value, document.form1.LastRecordToDisplay.value);
  }
}

//Function that imports the database records from the xml document by receiving the keywords, the order by specification (date or case name), the page that the user is on (the default is set to 1 for the first page of cases), the last record to display on the page, the court or agency, and the number of cases per page to display.  The function creates an xml document object, calls the display database records function and loads the url which creates an xml document for the pertinent information using server side processing.  The server side process page receives the keywords, the order by specification (date or case name), the page that the user is on, the last record to be displayed on the page, the court or agency, the number of cases per page and the particular request (getting the database records), so therefore, the server knows what to process
function importDatabaseRecords(Keywords, Page, LastRecordToDisplay, CourtOrAgency,  NumLines, OrderBy)
{
  if (document.getElementById('AlertMessageDisplay').hasChildNodes())
  {
	document.getElementById('AlertMessageDisplay').removeChild(document.getElementById('AlertMessage'));	
  }
  document.getElementById("RecordDisplay").innerHTML = "<div align='center'><h2>Searching....</h2><br><img src='/AM/Graphics/bigrotation.gif'></div>";
  if (document.implementation && document.implementation.createDocument)
  {
	xmlDoc5 = document.implementation.createDocument("", "", null);
	xmlDoc5.onload = displayDatabaseRecords;
  }
  else if (window.ActiveXObject)
  {
	xmlDoc5 = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc5.onreadystatechange = function () {
		if (xmlDoc5.readyState == 4) displayDatabaseRecords()
	};
  }
  else
  {
	alert('Your browser can\'t handle this script');
	return;
  }
  if (CourtOrAgency == null)
  {
    CourtOrAgency = document.getElementById('CourtOrAgency').value;
  }
  if (NumLines == null)
  {
    NumLines = document.getElementById('NumLines').value;
  }
	if (OrderBy == null) {
		OrderBy = getSortOrder();
	}
	// Escape the keywords
	xmlDoc5.load("/AM/CustomSource/ASPCode/caselawsearchprocesssimple.asp?Keywords=" + escape(Keywords) + "&Page=" + Page + "&LastRecordToDisplay=" + LastRecordToDisplay + "&CourtOrAgency=" + CourtOrAgency + "&NumLines=" + NumLines + "&OrderBy=" + OrderBy + "&theRequest=getDatabaseRecords");  
}

//Function that reads through the xml document record tags and pulls the information into div tags for displaying the Case Law Cases.  In addition to displaying the cases, the script also creates the pagination navigation listed at the bottom of the web page.
function displayDatabaseRecords()
{
  var w = xmlDoc5.getElementsByTagName('records');
  var RecordCount = Number(w[0].getAttribute('recordcount'));
  var Page = Number(w[0].getAttribute('page'));
  var FirstRecordToDisplay = Number(w[0].getAttribute('firstrecordtodisplay'));
  var LastRecordToDisplay = Number(w[0].getAttribute('lastrecordtodisplay'));
  var theCounter = FirstRecordToDisplay;  
  var NumLines = Number(w[0].getAttribute('numlines'));
  var OrderBy = w[0].getAttribute('orderby');
  var CourtOrAgency = Number(w[0].getAttribute('courtoragency'));
  var Keywords = w[0].getAttribute('keywords');
  
  //The script that builds the new link for the print-ready version
  var divTag = document.createElement('div');
  divTag.setAttribute('id','PrintReady');
  divTag.id = 'PrintReady';
  
  //The script that builds the new link for the bookmark this page
  var divTag = document.createElement('div');
  divTag.setAttribute('id','Bookmark');
  divTag.id = 'Bookmark';
  var theLink = document.createElement('a');
  var theURL = "http://www.wisbar.org/AM/CustomSource/ASPCode/caselawsearch.asp?SMod=simple&Keywords=" + Keywords + "&Page=" + Page + "&LastRecordToDisplay=" + LastRecordToDisplay + "&Area=" + CourtOrAgency + "&NumLines=" + NumLines + "&OrderBy=" + OrderBy;
  if (CourtOrAgency == '1')
  {
	var theTitle = "Wisconsin Supreme Court opinions listing - keyword simple search for - " + Keywords + " - cases per page - " + NumLines;	  	
  }
  else if (CourtOrAgency == '2')
  {
	var theTitle = "Wisconsin Court of Appeals opinions listing - keyword simple search for - " + Keywords + " - cases per page - " + NumLines;	  	
  }
  else if (CourtOrAgency == '4')
  {
	var theTitle = "Wisconsin Tax Appeals decisions listing - keyword simple search for - " + Keywords + " - cases per page - " + NumLines;	  	
  }
  else if (CourtOrAgency == '5')
  {
	var theTitle = "Wisconsin WERC Grievance Awards listing - keyword simple search for - " + Keywords + " - cases per page - " + NumLines;	  	
  }
  else if (CourtOrAgency == '6')
  {
	var theTitle = "Wisconsin WERC Decisions listing - keyword simple search for - " + Keywords + " - cases per page - " + NumLines;	  	
  }
  var theHREF = "javascript:BookmarkPage('" + theTitle + "','" + theURL + "')";  
  theLink.setAttribute('href',theHREF);
  theLink.href = theHREF;
  theLink.setAttribute('className','SideNav2');
  theLink.className = 'SideNav2';
  var imgTag = document.createElement('img');
  imgTag.setAttribute('src','/AM/Graphics/template/Interior/bookmark.gif');
  imgTag.src = '/AM/Graphics/template/Interior/bookmark.gif';
  imgTag.setAttribute('width','23');
  imgTag.width = '23';
  imgTag.setAttribute('height','22');
  imgTag.width = '22';
  imgTag.setAttribute('align','middle');
  imgTag.align = 'middle';
  var bookmarkThisPageText = document.createTextNode(" Bookmark this page");  
  theLink.appendChild(imgTag);
  theLink.appendChild(bookmarkThisPageText);
  divTag.appendChild(theLink);
  
  var theCoordinates = getAnchorPosition('anchor');
  var theAttributeValue = "javascript:scroll("+ theCoordinates.x + "," + (theCoordinates.y-130) + ")";
  
  var divTagRecords = document.createElement('div');  
  divTagRecords.setAttribute('id','sortedtable');  
  divTagRecords.id = 'sortedtable';
  divTagRecords.setAttribute('style','margin-left:15px;');
  divTagRecords.style.margin='15px';
    
  //The script that displays the first line of records  
  var x = xmlDoc5.getElementsByTagName('record');
  if (x.length == 0)
  {
    importAlertMessage('No results found for your search.  Please try again.');
  }
  else
  {
	if (RecordCount > 1 && RecordCount < 500)
	{
	  var theFieldValue1 = document.createTextNode("The search found ");
	  var bTag = document.createElement('b');
	  var theFieldValue2 = document.createTextNode(RecordCount);
	  bTag.appendChild(theFieldValue2);
	  var theFieldValue3 = document.createTextNode(" documents in our library.");
	  divTagRecords.appendChild(theFieldValue1);
	  divTagRecords.appendChild(bTag);
	  divTagRecords.appendChild(theFieldValue3);      
	}
	else if (RecordCount == 500)
	{
	  var theFieldValue1 = document.createTextNode("The search exceeded the maximum threshold.  Listed below is the top ");
	  var bTag = document.createElement('b');
	  var theFieldValue2 = document.createTextNode(RecordCount);
	  bTag.appendChild(theFieldValue2);
	  var theFieldValue3 = document.createTextNode(" documents from our library that matched your search criteria.");
	  divTagRecords.appendChild(theFieldValue1);
	  divTagRecords.appendChild(bTag);
	  divTagRecords.appendChild(theFieldValue3);      
	}
	else
	{
	  var theFieldValue1 = document.createTextNode("The search found ");
	  var bTag = document.createElement('b');
	  var theFieldValue2 = document.createTextNode(RecordCount);
	  bTag.appendChild(theFieldValue2);
	  var theFieldValue3 = document.createTextNode(" document in our library.");
	  divTagRecords.appendChild(theFieldValue1);
	  divTagRecords.appendChild(bTag);
	  divTagRecords.appendChild(theFieldValue3);      
	}
  }
  for (i=0;i<x.length;i++)
  {
	var hrTag = document.createElement('hr');
	divTagRecords.appendChild(hrTag);
	var theFieldValue = document.createTextNode(theCounter + ". ");
	theCounter = theCounter + 1;
	var theLink = document.createElement('a');
	var theHREF = x[i].getAttribute('link');
	theLink.setAttribute('href',theHREF);
	theLink.href = theHREF;
	theLink.setAttribute('className','morelink');
	theLink.className = 'morelink';
	theLink.setAttribute('target','_blank');
	theLink.target='_blank';	
	var bTag = document.createElement('b');
	bTag.appendChild(theFieldValue);
	divTagRecords.appendChild(bTag);
	for (j=0;j<x[i].childNodes.length;j++)
	{
	  if (x[i].childNodes[j].nodeType != 1) continue;
		var theFieldName = x[i].childNodes[j].nodeName;
		if (theFieldName == "casename" || theFieldName == "petitioner" || theFieldName == "employer")
		{
		  if (CourtOrAgency == 4 || CourtOrAgency == 5 || CourtOrAgency == 6)
		  {
		    var theFieldValue = document.createTextNode(PCase(x[i].childNodes[j].firstChild.nodeValue + ", No. "));
		  }
		  else
		  {
		    var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue + ", No. ");
		  }		  
	  	  theLink.appendChild(theFieldValue);
	  	  divTagRecords.appendChild(theLink);
		}
		if (theFieldName == "docketnumber")
		{
		  var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
		  theLink.appendChild(theFieldValue);
	  	  divTagRecords.appendChild(theLink);
		}
		if (theFieldName == "filingdate")
		{		  
		  if (CourtOrAgency == '1')
          {
		    var TheCourtOrAgency = "WI Supreme Court";
		  }
		  else if (CourtOrAgency == '2')
		  {
		    var TheCourtOrAgency = "WI Court of Appeals"
		  }
		  else if (CourtOrAgency == '4')
		  {
		    var TheCourtOrAgency = "Tax Appeals"
		  }
		  else if (CourtOrAgency == '5')
		  {
		    var TheCourtOrAgency = "WERC Grievance Awards"
		  }
		  else if (CourtOrAgency == '6')
		  {
		    var TheCourtOrAgency = "WERC Decisions"
		  }
		  var FiledText = document.createTextNode("Filed: ");
		  var bTag = document.createElement('b');
		  var iTag = document.createElement('i');
	      bTag.appendChild(iTag);
		  iTag.appendChild(FiledText);		  
		  var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue + ", " + TheCourtOrAgency);
		  var iTag = document.createElement('i');
		  iTag.appendChild(theFieldValue);
		  var brTag = document.createElement('br');		  
		  divTagRecords.appendChild(brTag);
		  divTagRecords.appendChild(bTag);
		  divTagRecords.appendChild(iTag);
		}		
		if (theFieldName == "arbitrator")
		{
		  var ArbitratorText = document.createTextNode("Arbitrator: ");
		  var bTag = document.createElement('b');
		  var iTag = document.createElement('i');
	      bTag.appendChild(iTag);
		  iTag.appendChild(ArbitratorText);
		  if (CourtOrAgency == 4 || CourtOrAgency == 5 || CourtOrAgency == 6)
		  {
		    var theFieldValue = document.createTextNode(PCase(x[i].childNodes[j].firstChild.nodeValue));
		  }
		  else
		  {
		    var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
		  }
		  var iTag = document.createElement('i');
		  iTag.appendChild(theFieldValue);
		  var brTag = document.createElement('br');
		  divTagRecords.appendChild(brTag);
		  divTagRecords.appendChild(bTag);
		  divTagRecords.appendChild(iTag);
		}
		if (theFieldName == "examiner")
		{
		  var ExaminerText = document.createTextNode("Examiner: ");
		  var bTag = document.createElement('b');
		  var iTag = document.createElement('i');
	      bTag.appendChild(iTag);
		  iTag.appendChild(ExaminerText);
		  if (CourtOrAgency == 4 || CourtOrAgency == 5 || CourtOrAgency == 6)
		  {
		    var theFieldValue = document.createTextNode(PCase(x[i].childNodes[j].firstChild.nodeValue));
		  }
		  else
		  {
	        var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
		  }
		  var iTag = document.createElement('i');
		  iTag.appendChild(theFieldValue);
		  var brTag = document.createElement('br');
		  divTagRecords.appendChild(brTag);
		  divTagRecords.appendChild(bTag);
		  divTagRecords.appendChild(iTag);
		}
		if (theFieldName == "petitioner2")
		{
		  var PetitionerText = document.createTextNode("Petitioner: ");
		  var bTag = document.createElement('b');
		  var iTag = document.createElement('i');
	      bTag.appendChild(iTag);
		  iTag.appendChild(PetitionerText);
		  if (CourtOrAgency == 4 || CourtOrAgency == 5 || CourtOrAgency == 6)
		  {
		    var theFieldValue = document.createTextNode(PCase(x[i].childNodes[j].firstChild.nodeValue));
		  }
		  else
		  {
		    var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
		  }
		  var iTag = document.createElement('i');
		  iTag.appendChild(theFieldValue);
		  var brTag = document.createElement('br');
		  divTagRecords.appendChild(brTag);
		  divTagRecords.appendChild(bTag);
		  divTagRecords.appendChild(iTag);
		}
		if (theFieldName == "citation")
		{
		  var CitationText = document.createTextNode("Public Citation: ");
		  var bTag = document.createElement('b');
		  var iTag = document.createElement('i');
	      bTag.appendChild(iTag);
		  iTag.appendChild(CitationText);
		  var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
		  var iTag = document.createElement('i');
		  iTag.appendChild(theFieldValue);
		  var brTag = document.createElement('br');
		  divTagRecords.appendChild(brTag);
		  divTagRecords.appendChild(bTag);
		  divTagRecords.appendChild(iTag);
		}
		if (theFieldName == "summary")
		{
		  var SummaryText = document.createTextNode("Summary: ");
		  var bTag = document.createElement('b');
		  var iTag = document.createElement('i');
	      bTag.appendChild(iTag);
		  iTag.appendChild(SummaryText);
		  var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue + " ...");
		  var brTag = document.createElement('br');
		  divTagRecords.appendChild(brTag);
		  divTagRecords.appendChild(bTag);
		  divTagRecords.appendChild(theFieldValue);
		}
		for (w=1;w<3;w++)
		{
		  if (theFieldName == "casetextq1" + w)
		  {		  
		    var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
			if (w == 1)
			{
		      var brTag = document.createElement('br');
		      divTagRecords.appendChild(brTag);
			}
		    var ulTag = document.createElement('ul');
		    var liTag = document.createElement('li');
		    liTag.appendChild(theFieldValue);
		    ulTag.appendChild(liTag);
		    divTagRecords.appendChild(ulTag);
		  }
		  if (theFieldName == "casetextoriginalterm" + w)
		  {		  
		    var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
			var theLink = document.createElement('a');			
			if (w == 1)
			{
			  var TheKeyword = x[i].childNodes[j].firstChild.nodeValue;
			  var TheCounter = 1;
			}
			else
			{
			  if (x[i].childNodes[j].firstChild.nodeValue.toUpperCase() == TheKeyword.toUpperCase())
			  {
			    TheCounter = TheCounter + 1;
			  }
			  else
			  {
			    TheCounter = 1;
			  }
			}
			var theHREF = "/AM/CustomSource/ASPCode/caseshow.asp?Hig=on&SearchTerm=" + x[i].childNodes[j].firstChild.nodeValue + "&urlpath=" + x[i].childNodes[j].getAttribute('urlloc') + "#L" + TheCounter;
	        theLink.setAttribute('href',theHREF);
	  	    theLink.href = theHREF;
		    theLink.setAttribute('className','morelink');
	  	    theLink.className = 'morelink';
			theLink.setAttribute('target','_blank');
	        theLink.target='_blank';
	  	    theLink.appendChild(theFieldValue);
	  	    liTag.appendChild(theLink);		    
		  }
		  if (theFieldName == "casetextq2" + w)
		  {		  
		    var theFieldValue = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
			liTag.appendChild(theFieldValue);		    
		  }
		}
	}	
  }  
  
  //The script that builds the pagination navigation
  var NumCasesPerPage = NumLines;
  var TotalRecords = RecordCount;
  var NumberOfPages = roundNumber(+TotalRecords/NumCasesPerPage);  
  
  if ((TotalRecords/NumCasesPerPage) - NumberOfPages > 0)
  {
    NumberOfPages = NumberOfPages + 1; 
  }
  
  var divTagPagination = document.createElement('div');
  divTagPagination.setAttribute('id','Pagination');  
  divTagPagination.id = 'Pagination';
  divTagPagination.setAttribute('align','center')
  divTagPagination.align = 'center';
  
  if (NumberOfPages > 1)
  {
    var brTag = document.createElement('br');
	divTagPagination.appendChild(brTag);
		
	var theData = document.createTextNode("More results:");
	divTagPagination.appendChild(theData);
	var brTag = document.createElement('br');
	divTagPagination.appendChild(brTag);
	
	if (Page != 1)
	{
	  var theLink = document.createElement('a');
	  theLink.setAttribute('href',theAttributeValue);
	  theLink.href = theAttributeValue;
	  var PreviousPage = Page - 1;
	  LastRecordToDisplay = PreviousPage * NumLines;	  
	  var theClickEvent = "importDatabaseRecords('" + Keywords.replace(/'/, "\'") + "', '" + PreviousPage + "', '" + LastRecordToDisplay + "', '" + CourtOrAgency + "', '" + NumLines + "', '" + OrderBy + "')";
	  theLink.setAttribute('onClick',theClickEvent);
	  var functionName = "pageClickPrevious";
  	  var theFunction = "theLink.onclick = function " + functionName + "() { " + theClickEvent + "};";		
	  theLink.onclick = eval(theFunction);		
	  var theData = document.createTextNode("Previous");
	  theLink.appendChild(theData);
	  divTagPagination.appendChild(theLink);
	  var theData = document.createTextNode(" | ");
      divTagPagination.appendChild(theData);
	}
	
	if (Page > 10)
	{
	  var theLink = document.createElement('a');
	  theLink.setAttribute('href',theAttributeValue);
	  theLink.href = theAttributeValue;
	  if (Page % 10 > 0)
	  {
	    var StartNumber = Page - (Page % 10 - 1)
		var EndNumber = StartNumber + 9;
	  }
	  else
	  {
		var StartNumber = Page - 9
		var EndNumber = StartNumber + 9;
	  }	  
	  var PreviousPageSet = StartNumber - 10;
	  LastRecordToDisplay = PreviousPageSet * NumLines;
	  var theClickEvent = "importDatabaseRecords('" + Keywords.replace(/'/, "\'") + "', '" + PreviousPageSet + "', '" + LastRecordToDisplay + "', '" + CourtOrAgency + "', '" + NumLines + "', '" + OrderBy + "')";
	  theLink.setAttribute('onClick',theClickEvent);
	  var functionName = "pageClickPreviousSet";
  	  var theFunction = "theLink.onclick = function " + functionName + "() { " + theClickEvent + "};";		
	  theLink.onclick = eval(theFunction);		
	  var theData = document.createTextNode((StartNumber - 10) + "-" + (StartNumber - 1));
	  theLink.appendChild(theData);
	  divTagPagination.appendChild(theLink);
	  var theData = document.createTextNode(" | ");
      divTagPagination.appendChild(theData);
	  
	  if (EndNumber > NumberOfPages)
	  {
	    EndNumber = NumberOfPages;
	  }
	}	
	else
	{
	  var StartNumber = 1;
	  var EndNumber = 10;	  
	}
	if (NumberOfPages <= 10)
	{
	  EndNumber = NumberOfPages;
	}	
	for (w=StartNumber;w<=EndNumber;w++)
	{
	  if (w <= NumberOfPages)
	  {
	    if (Page == w)
	    {
		  var theData = document.createTextNode(w);
		  divTagPagination.appendChild(theData);				
	    }
	    else
	    {
		  var theLink = document.createElement('a');
		  theLink.setAttribute('href',theAttributeValue);
		  theLink.href = theAttributeValue;		
		  LastRecordToDisplay = w * NumLines;		
		  var theClickEvent = "importDatabaseRecords('" + Keywords.replace(/'/, "\'") + "', '" + w + "', '" + LastRecordToDisplay + "', '" + CourtOrAgency + "', '" + NumLines + "', '" + OrderBy + "')";
		  theLink.setAttribute('onClick',theClickEvent);
		  var functionName = "pageClick" + w;
  		  var theFunction = "theLink.onclick = function " + functionName + "() { " + theClickEvent + "};";		
		  theLink.onclick = eval(theFunction);		
		  var theData = document.createTextNode(w);
		  theLink.appendChild(theData);
		  divTagPagination.appendChild(theLink);				
	    }	  
	    if (w != EndNumber)
	    {
		  var theData = document.createTextNode(" | ");
		  divTagPagination.appendChild(theData);		
	    }
	  }
    }
	if ((EndNumber - StartNumber == 9) && EndNumber < NumberOfPages)
	{
	  var theData = document.createTextNode(" | ");
      divTagPagination.appendChild(theData);
	  var theLink = document.createElement('a');
	  theLink.setAttribute('href',theAttributeValue);
	  theLink.href = theAttributeValue;
	  var NextPageSet = StartNumber + 10;
	  LastRecordToDisplay = NextPageSet * NumLines;	  
	  var theClickEvent = "importDatabaseRecords('" + Keywords.replace(/'/, "\'") + "', '" + NextPageSet + "', '" + LastRecordToDisplay + "', '" + CourtOrAgency + "', '" + NumLines + "', '" + OrderBy + "')";
	  theLink.setAttribute('onClick',theClickEvent);
	  var functionName = "pageClickNextSet";
  	  var theFunction = "theLink.onclick = function " + functionName + "() { " + theClickEvent + "};";		
	  theLink.onclick = eval(theFunction);
	  if (w + 9 > NumberOfPages)
	  {
		var theData = document.createTextNode((w) + "-" + (NumberOfPages));
	  }
	  else
	  {
		var theData = document.createTextNode((w) + "-" + (w + 9));
	  }
	  theLink.appendChild(theData);
	  divTagPagination.appendChild(theLink);
	}
	if (Page < NumberOfPages)
	{		
	  var theData = document.createTextNode(" | ");
      divTagPagination.appendChild(theData);
	  var theLink = document.createElement('a');
	  theLink.setAttribute('href',theAttributeValue);
	  theLink.href = theAttributeValue;
	  var NextPage = Page + 1;
	  LastRecordToDisplay = NextPage * NumLines;
	  var theClickEvent = "importDatabaseRecords('" + Keywords.replace(/'/, "\'") + "', '" + NextPage + "', '" + LastRecordToDisplay + "', '" + CourtOrAgency + "', '" + NumLines + "', '" + OrderBy + "')";
	  theLink.setAttribute('onClick',theClickEvent);
	  var functionName = "pageClickNext";
  	  var theFunction = "theLink.onclick = function " + functionName + "() { " + theClickEvent + "};";		
	  theLink.onclick = eval(theFunction);		
	  var theData = document.createTextNode("Next");
	  theLink.appendChild(theData);
	  divTagPagination.appendChild(theLink);
	}	
  }
  
  //The script that controls the display of the div tags within the search page
  document.getElementById("RecordDisplay").innerHTML = "";    
  if (document.getElementById('RecordDisplay').hasChildNodes()) 
  {
	document.getElementById('RecordDisplay').removeChild(document.getElementById('sortedtable'));
	document.getElementById('RecordDisplay').removeChild(document.getElementById('Pagination'));
	document.getElementById('RecordDisplay').appendChild(divTagRecords);
	document.getElementById('RecordDisplay').appendChild(divTagPagination);
  } 
  else 
  {
	document.getElementById('RecordDisplay').appendChild(divTagRecords);
	document.getElementById('RecordDisplay').appendChild(divTagPagination);
  }    
}