// byuidahCustom.js
//
// byuidah Custom JavaScript Functions
//

// Open full text in a New Window 1=yes | 0=No
newFullTextWindow = 1;

var iButtonImg = '/images/i.jpg';
var helpImage = '/images/i_small.jpg';
var deniedPage = '/byuidahDenied.html';
var WFsbSuppress0 = false;

function showHelp( strURL )
{
  window.open(strURL,'Help','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=500,height=400');
}

// Return a comma-delimited list of wf_names for a category
function formCategoryList(categoryName)
{
  dbValue='';
  for (i=0; i< category.length; i++)
  {
    if (categoryName == category[i][0][0])
    {
      for (j=0; j<category[i][1].length; j++)
      {
        dbValue += ',' + category[i][1][j];
      }
      break;
    }
  }
  return dbValue.substr(1);
}


// Get sTerm from wf_search_structure
function setSubjectCookie()
{
  var selCatText = document.wfform.Databases.options[document.wfform.Databases.selectedIndex].text;
  document.cookie = "sb=" + selCatText + ";Path=/;Domain=.webfeat.org;";
  document.wfform.sb.value = selCatText;
}

function showCategoryBoxSelected(selectName, categoryOmit, selectedCat)
{
  if (typeof(selectName) == 'undefined')
  {
    selectName = 'Databases';
  }
    
  if (typeof(categoryOmit) == 'undefined')
  {
    categoryOmit = '';
  }
    
  document.write('<select name="' + selectName + '">');
  selectedCat = selectedCat.replace(/\%20/g, ' ');
  for (i=0; i< category.length; i++)
  {
    if (categoryOmit.indexOf(category[i][0][0]) >= 0)
    {
      continue;
    }
    
    sOptionValue = '';
    sOptionText = '';
     
    sOptionText = category[i][0][0];
    for (j = 0; j < category[i][1].length; j++)
    {
      sOptionValue += ',' + category[i][1][j];
    }
      
    sOptionValue = sOptionValue.substr(1);
    document.write('<option value="' + sOptionValue + '" ');
    if (selectedCat.indexOf(category[i][0][0]) >= 0)
    {
      document.write('selected');
    }
    document.write('>');
    document.write(sOptionText + '</option>\n');
  }
  document.write('</select>');
}

function createList()
{
  strDbs ='';
  dbList = '';
  for (i=0; i < document.wfform.Dbs.length; i++)
  {
    if (document.wfform.Dbs[i].checked)
    {
      strDbs = document.wfform.Dbs[i].value;
    }
  }
    
  if (strDbs != '')
  {
    if (strDbs == 'articles')
    { 
      dbList = formCategoryList('General Articles');
      document.wfform.sb.value='General%20Articles';
    }
    else if (strDbs == 'books')
    {
      dbList = formCategoryList('BYUI Library Catalog');
      document.wfform.sb.value='BYUI%20Library%20Catalog';
    }
    else if (strDbs == 'bookart')
    {
      dbList = formCategoryList('General Books and Articles');
      document.wfform.sb.value='General%20Books%20and%20Articles';
    }
    else if (strDbs == 'dictent')
    {
      dbList = formCategoryList('Dictionaries and Encyclopedias');
      document.wfform.sb.value='Dictionaries%20and%20Encyclopedias';
    }
    else if (strDbs == 'contiss')
    {
      dbList = formCategoryList('Controversial Issues');
      document.wfform.sb.value='Controversial%20Issues';
    }
    else if (strDbs == 'images')
    {
      dbList = dbList + formCategoryList('Images');
      document.wfform.sb.value='Images';
    }
  }
      
  document.wfform.Databases.value = dbList;
}//end of createList

function processForm()
{
var sb = document.wfform.sb.value;
sb = sb.replace(/\&/g, '%26');

var wfdbs = document.wfform.Databases.value;
var google = document.wfform.google.value;
var wfterm1 = document.wfform.wf_term1.value;

  for (i=0; i < document.wfform.Dbs.length; i++)
  {
    if (document.wfform.Dbs[i].checked)
    {
      strDbs = document.wfform.Dbs[i].value;
    }
  }
  
  var gCatWinAttr = '';
  var newURL = '';
  if (wfterm1 == '')
  {
    alert('Please enter a search term.');
    return false;
  }
  
  if (document.wfform.Dbs[1].checked)
  {
    newURL = 'http://hip.byui.edu/ipac20/ipac.jsp?menu=search&aspect=basic_search&npp=10&ipp=20&profile=mck&index=.GW&term=' + escape(document.wfform.wf_term1.value) + '&aspect=basic_search';
    gCatWinAttr = 'resizable=yes,scrollbars=yes';
    newWin = window.open(newURL, '_cat', gCatWinAttr);
    newWin.focus();
    return false;
  }


  else if (document.wfform.Dbs[6].checked)
  {
    newURL = 'http://hip.byui.edu/ipac20/ipac.jsp?menu=search&aspect=basic_search&npp=10&ipp=20&profile=mck&ri=&limitbox_1=CO01+%3D+cg_ebk&index=.GW&term=' + escape(document.wfform.wf_term1.value);
    var gCatWinAttr = 'resizable=yes,scrollbars=yes';
    newWin = window.open(newURL, '_cat', gCatWinAttr);
    newWin.focus();
    return false;
  }    

      else if ((document.wfform.Dbs[0].checked) || (document.wfform.Dbs[2].checked) || (document.wfform.Dbs[3].checked) || (document.wfform.Dbs[4].checked) || (document.wfform.Dbs[5].checked))
  {
                                //Switching from creating a nexturl call to generating a regular form
                 document.write('<div style="visibility: hidden">');
                  if (google == "YES")
                  {
                  document.write('<form name="wfform" target="_blank" action="http://wfxsearch.webfeat.org/wfsearch/search" method="POST">');
                  }
                  else
                  {
    document.write('<form name="wfform" action="http://wfxsearch.webfeat.org/wfsearch/search" method="POST">');
                }
    document.write('<input type="hidden" name="Command" value="Search">');
    document.write('<input type="hidden" name="Client_ID" value="12090">');
    document.write('<input type="hidden" name="rtmpl" value="wfx_ui1.jsp">');
    document.write('<input type="hidden" name="format" value="JS">');       
    document.write('<input type="hidden" name="utf-8" value="wf_codeset">');    
    document.write('<input type="hidden" name="noserial" value="1">');

                document.write('Enter term: <input type="text" name="wf_term1" size="20" value="' + wfterm1 + '">');
    document.write('<input type="hidden" name="wf_field1" value="wf_keyword">');
                document.write('<input type="hidden" name="wf_all_years" value="yes">'); 
                document.write('<input type="hidden" name="Databases" value="' + wfdbs + '">');
                document.write('<input type="hidden" name="sb" value="' + sb + '">');

                document.write('<input type="submit" value="submit">');
                document.write('</form>');
                document.write('</div>');
                
                document.wfform.submit();
    return true;
  }
}//end of processForm



function showQuickSearchForm()
{
  if (typeof(TestHost) == 'undefined')
  {
    document.write('<FORM name="wfform" action="http://wfxsearch.webfeat.org/wfsearch/search?cid=12090" method="POST" onSubmit="createList(); return processForm();">');
  }
  else
  {
    document.write('<FORM name="wfform" action="http://' + TestHost + '/BYUIdahAuth.asp" method="post" onSubmit="createList(); return processForm();">');
  }
  
  document.write('<input type="hidden" name="Databases" value="">');
  document.write('<input type="hidden" name="sb" value="">');
 document.write('<input type="hidden" name="google" value="NO">');
  
  document.write('<input name="wf_term1" type="text" id="searchTerm">');
  document.write('<input type="submit" name="Submit" value="Go"> <a href="http://wfxsearch.webfeat.org/wfsearch/search?cid=12090" target="_blank">Advanced Search</a><br>');
  document.write('<font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">');
  document.write('<input type=radio name="Dbs" value=bookart checked><font color="#000000">Books & Articles</font><br>');
  document.write('<input type=radio name="Dbs" value=books><font color="#000000">Books</font><br>');
  document.write('<input type=radio name="Dbs" value=articles><font color="#000000">Articles</font><br>');
  document.write('<input type=radio name="Dbs" value=dictent><font color="#000000">Dictionaries & Encyclopedias</font><br>');
  document.write('<input type=radio name="Dbs" value=images><font color="#000000">Images/Movies</font><br>');
  document.write('<input type=radio name="Dbs" value=contiss><font color="#000000">Controversial Issues</font><br>');
  document.write('<input type=radio name="Dbs" value=ebooks><font color="#000000">E-books</font><br>');
  
  document.write('</font></form>');
}

function showQuickSearchFormGoogle()
{
  if (typeof(TestHost) == 'undefined')
  {
    document.write('<FORM name="wfform" target="_blank" action="http://wfxsearch.webfeat.org/wfsearch/search?cid=12090" method="post" onSubmit="createList(); return processForm();">');
  }
  else
  {
    document.write('<FORM name="wfform" target="_blank" action="http://' + TestHost + '/BYUIdahAuth.asp" method="post" onSubmit="createList(); return processForm();">');
  }
  
  document.write('<input type="hidden" name="Databases" value="">');
  document.write('<input type="hidden" name="sb" value="">');
  //Adding this hidden field so only the Google form will open in a new window for results
   document.write('<input type="hidden" name="google" value="YES">');

  document.write('<input name="wf_term1" type="text" id="searchTerm">');
  document.write('<input type="submit" name="Submit" value="Go"> <a href="http://wfxsearch.webfeat.org/wfsearch/search?cid=12090" target="_blank">Advanced Search</a><br>');
  document.write('<font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">');
  document.write('<input type=radio name=Dbs value=bookart checked><font color="#000000">Books & Articles</font><br>');
  document.write('<input type=radio name=Dbs value=books><font color="#000000">Books</font><br>');
  document.write('<input type=radio name=Dbs value=articles><font color="#000000">Articles</font><br>');
  document.write('<input type=radio name=Dbs value=dictent><font color="#000000">Dictionaries & Encyclopedias</font><br>');
  document.write('<input type=radio name=Dbs value=images><font color="#000000">Images/Movies</font><br>');
  document.write('<input type=radio name=Dbs value=contiss><font color="#000000">Controversial Issues</font><br>');
  document.write('<input type=radio name=Dbs value=ebooks><font color="#000000">E-books</font><br>');
  
  document.write('</font></form>');
}

// Select / Deselect all function
function changeSelect(checkCategory)
{
	var checkOn = false;

	if (document.wfform.select.checked)
		checkOn = true;

	if (document.wfform.Databases.length == null)
	{
		// Only one checkbox present
		document.wfform.Databases.checked = checkOn;
	}
	else
	{
		for (i=0; i< document.wfform.Databases.length; i++)
			document.wfform.Databases[i].checked = checkOn;
	}

	if(checkCategory)
	{
		for (i=0;i<category.length - 1; i++)
		{
		document.wfform.CategoryBoxes[i].checked = checkOn;
		}//end of for
	}//end of if


}//end of changeSelect


function checkDBs()
{
  var sCookieText = '';
  var sDatabaseCookie = '';
  var count = document.wfform.Databases.length;
  var noneSelected = true;
  var missing = 1;
  var temp = '';

  if (document.wfform.wf_term1.value.length > 0)
  {
    missing = 0;
  }
  else
  {
    if (document.wfform.wf_extraterm.value.length > 0)
    {
      missing = 0;
      document.wfform.wf_term1.value = document.wfform.wf_extraterm.value;
    }
  }

  if (missing == 1)
  {
  	alert('Please enter a search term!');
  	return false;
  }

  sCookieText += "wf_keyword:";
  sCookieText += document.wfform.wf_term1.value + ":";

  sCookieText = sCookieText.substr(0, sCookieText.length - 1);
  document.cookie = 'wf_search_structure=' + sCookieText + '; Path=/;Domain=.webfeat.org;';

  if (document.wfform.Databases.value != null)
  {
    sDatabaseCookie += document.wfform.Databases.value;
    document.cookie = 'wf_selected_databases=' + sDatabaseCookie + '; Path=/;Domain=.webfeat.org';
    return true;
  }
  else
  {
    if (document.wfform.Databases.length == null)
    {
      // Only one checkbox present
      if (document.wfform.Databases.checked == true)
      noneSelected = false;
    }
    else
    {
      noneSelected = true;
      for (i=0; i< document.wfform.Databases.length; i++)
      {
        if (document.wfform.Databases[i].checked == true)
        {
          noneSelected = false;
          sDatabaseCookie += document.wfform.Databases[i].value + ',';
          //break;
        }
      }//end of for
    }//end of else
    if (noneSelected == true)
    {
      alert('Please select at least one database!');
      return false;
    }
    else
    {
      sDatabaseCookie = sDatabaseCookie.substr(0, sDatabaseCookie.length - 1);
      document.cookie = 'wf_selected_databases=' + sDatabaseCookie + '; Path=/;Domain=.webfeat.org';
      return true;
    }
  }
}

function validateTerms()
{
  boolDBs = checkDBs();
  if (boolDBs == false) return false;

  //Check for non-empty term
  if (document.wfform.wf_term1.value.length <= 0)
  {
    if (document.wfform.wf_extraterm.value.length <= 0)
    {
      alert( "Please enter a search term before proceeding" );
      return false;
    }
    else
    {
      document.wfform.wf_term1.value = document.wfform.wf_extraterm.value;
    }
  }
  return true;
}



function validateSubjectSearch()
{
var sb = document.wfform.sb.value;
sb = sb.replace(/\&/g, '%26');

                boolDBs = checkDBs();
                if (boolDBs == false) return false;

                //Check for non-empty term
                if (document.wfform.wf_term1.value.length <= 0)
                {
                                alert( "Please enter a search term before proceeding" )
                                return false;
                }

                var strDatabases = '';
                for (var loop=0; loop < document.wfform.Databases.length; loop++)
                {
                                if (document.wfform.Databases[loop].checked == true)
                                {
                                                if (strDatabases == "")
                                                                strDatabases= document.wfform.Databases[loop].value + "";
                                                else
                                                                strDatabases += "," + document.wfform.Databases[loop].value;
                                }
                }

                var wfterm1 = document.wfform.wf_term1.value;

                //Switching from creating a nexturl call to generating a regular form
                  document.write('<div style="visibility: hidden">');
    document.write('<form name="wfform" action="http://wfxsearch.webfeat.org/wfsearch/search" method="POST">');
    document.write('<input type="hidden" name="Command" value="Search">');
    document.write('<input type="hidden" name="Client_ID" value="12090">');
    document.write('<input type="hidden" name="rtmpl" value="wfx_ui1.jsp">');
    document.write('<input type="hidden" name="format" value="JS">');       
    document.write('<input type="hidden" name="wf_codeset" value="utf-8">');    
    document.write('<input type="hidden" name="noserial" value="1">');

                document.write('Enter term: <input type="text" name="wf_term1" size="20" value="' + wfterm1 + '">');
    document.write('<input type="hidden" name="wf_field1" value="wf_keyword">');
                document.write('<input type="hidden" name="wf_all_years" value="yes">'); 
                document.write('<input type="hidden" name="Databases" value="' + strDatabases + '">');
                document.write('<input type="hidden" name="sb" value="' + sb + '">');

                document.write('<input type="submit" value="Search">');
                document.write('</form>');
                document.write('</div>');
                
                document.wfform.submit();

                return true;
}


var CHECKBOX_TEMPLATE_D = '';
function showSubjectHomePage(categoryNameBYUI)
{
if (typeof(TestHost) == 'undefined')
	document.write('<FORM name="wfform" action="http://wfxsearch.webfeat.org/wfsearch/search?cid=12090" method="POST" target="_top" onsubmit="return validateSubjectSearch();">');
else
	document.write('<FORM name="wfform" action="http://' + TestHost + '/BYUIdahWFXAuth.asp" method="POST" target="_top" onsubmit="return validateSubjectSearch();">');
document.write('<input type="hidden" name="sb" value="' + categoryNameBYUI + '">');
document.write('  <table border="0">');
document.write('    <tr> ');
document.write('      <td colspan="3" bgcolor="#CCCCCC"> <br>');
document.write('          <input name="wf_term1" type="text" size="40">');
document.write('          <input name="wfsubmit" type="submit" id="Search2" value="Search">');
document.write('          &nbsp; <a target="_top" href="http://wfxsearch.webfeat.org/wfsearch/search?cid=12090">Advanced Search</a><br>');

// Subject Names
var g_CategoryTemplate = '';
g_CategoryTemplate += ' &nbsp;&nbsp; <<checkbox>> Select/Deselect All';
showCatForBYUIDAH(g_CategoryTemplate, categoryNameBYUI);

document.write('       </td>');
document.write('    </tr>');

// Subject List
//var g_CheckboxTemplate = '';
//g_CheckboxTemplate += '<td width="5%" align="right" valign="middle"> <<checkbox>></td>';
//g_CheckboxTemplate += '<td width="70%" align="left" valign="middle">';
//g_CheckboxTemplate += '<<native link>><<database name>></label></td><td width="10%" align="right" valign="middle"> <<description link>></td></tr>';
//showDBsForCatNameBYUIDAH(g_CheckboxTemplate, categoryNameBYUI, "Details");


var format = new Array('');
var omitDatabase = '';
var desclink = 'Details';
var top = '';

CHECKBOX_TEMPLATE_D += '<td width="5%" align="right" valign="middle"> <<checkbox>></td>';
CHECKBOX_TEMPLATE_D += '<td width="70%" align="left" valign="middle">';
CHECKBOX_TEMPLATE_D += '<<native link>><<database name>></label></td><td width="10%" align="right" valign="middle"> <<description link>></td></tr>';
showDBsBYUI(categoryNameBYUI, desclink, 1, omitDatabase, format, top);

onLoadCat();

document.write('  </table>');
document.write('</form>');
}

// Make sure there are no undefined variables in the list
function onLoadFormUpdateBYUI()
{
  if (aSearchStruct)
  {
    if (document.wfform.wf_term1)
    {
      document.wfform.wf_term1.value = aSearchStruct[1];
    }
  }
}

function setFormBYUI()
{
  var sCookieText = '';
  var sDatabaseCookie = '';

  setSubjectCookie();

  sCookieText += 'wf_keyword):(';
  sCookieText += document.wfform.wf_term1.value;

  document.cookie = 'wf_search_structure=' + sCookieText + '; Path=/;Domain=.webfeat.org';
}

// Popup the thesaurus search

function initThesaurus(wfThrsName)
{
  // Retrieve the search information from the form
  var localClientID = document.wfform.Client_ID.value;

  if (localClientID == 'nysl_temp')
    localClientID = 'nysl';

  // Create URL
  var thrsUrl = 'http://wfsearch.webfeat.org//webfeat.dll?Command=Search&format=JS&rtmpl=thrs';
  thrsUrl += '&Databases=' + wfThrsName;
  thrsUrl += '&Client_ID=' + localClientID;
  thrsUrl += '&wf_field1=' + aSearchStruct[0];
  thrsUrl += '&wf_term1=' + aSearchStruct[1];
  thrsUrl += '&wf_op2=' + aSearchStruct[2];
  thrsUrl += '&wf_field2=' + aSearchStruct[3];
  thrsUrl += '&wf_term2=' + aSearchStruct[4];
  thrsUrl += '&wf_op3=' + aSearchStruct[5];
  thrsUrl += '&wf_field3=' + aSearchStruct[6];
  thrsUrl += '&wf_term3=' + aSearchStruct[7];

  // open the Thesaurus window
  // Attributes for the new Thesaurus window
  var gThrsWinAttr = 'height=300,width=250,resizable=yes,scrollbars=yes,location=no,status=0,toolbar=no,';
  gThrsWinAttr += 'personalbar=no,menubar=no,left=730,top=150,screenX=730,screenY=150';
  newWin = window.open(thrsUrl, '_thrs', gThrsWinAttr);
  newWin.focus();
  //newWin.document.open();
  //newWin.document.writeln(thrsUrl);
  //newWin.document.close();
}

function onLoadCat()
{

	document.wfform.checkCat.checked = true;

	for (q=0; q<document.wfform.Databases.length; q++)
	{
		document.wfform.Databases[q].checked = true;
	}
}

function checkSelCat(sBool, categoryNum)
{
	for (p=0; p<category[categoryNum][1].length; p++)
	{
		for (q=0; q<document.wfform.Databases.length; q++)
		{
			if (document.wfform.Databases[q].value == category[categoryNum][1][p])
				document.wfform.Databases[q].checked = sBool;
		}
	}
}

function showCatForBYUIDAH(GTemplate, categoryName)
{
	var sTemplate = GTemplate;
	var catNum = 0;

	for (p=0; p<category.length; p++)
	{
		if (category[p][0][0] == categoryName)
		{
			catNum = p;
			break;
		}
	}

	sCheckbox = '<input type="checkbox" onClick="checkSelCat(this.checked, ' + catNum + ')" title="Category Checkbox" name="checkCat" value="';
	sCheckbox += catNum + '" checked>';

	sTemplate = sTemplate.replace(/<<checkbox>>/g, sCheckbox);
	sTemplate = sTemplate.replace(/<<category name>>/g, categoryName);

	// alert(sTemplate);
	document.write(sTemplate);
}




//This and showDBsBYUI are called from showSubjectHomePage().
  function stripLO(dbname1, dbname2)
  {
    dbname1 = dbname1.replace(/\<\!-- link only --\>/, '');
    dbname2 = dbname2.replace(/\<\!-- link only --\>/, '');
    return (dbname1 > dbname2) - (dbname1 < dbname2);
  }



function showDBsBYUI(categoryName, descriptionText, nCols, omitDatabase, format, top)
{
  // Make sure everything is defined or the default

  var goodDbs = new Array();
  var goodDBNames = new Array();

  if(typeof(g_CheckboxTemplate) == 'undefined')
  {
    g_CheckboxTemplate = CHECKBOX_TEMPLATE_D;
  }

  if(typeof(g_CheckboxName) == 'undefined')
  {
    g_CheckboxName = 'Databases';
  }

  if (typeof(categoryName) == 'undefined')
  {
    categoryName = category[0][0][0];
  }

  if(typeof(descriptionText) == 'undefined')
  {
    descriptionText = '';
  }

  if(typeof(nCols) == 'undefined')
  {
    nCols = 1;
  }

  if(typeof(omitDatabase) == 'undefined')
  {
    omitDatabase = '';
  }

  if(typeof(format) == 'undefined')
  {
    format = '';
  }

  if(typeof(top) == 'undefined')
  {
    top = '';
  }

  if(typeof(anchors) == 'undefined')
  {
    var anchors = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
  }
  



  for (i=0; i< category.length; i++)
  {
    if (category[i][0][0] == categoryName)
    {
      var offset = 0;
      var dbCount = category[i][1].length;
      var goodCount = 0;

      for(j=0; j<category[i][1].length; j++)
      {
        wfname = category[i][1][j];
        //alert('Working on: ' + wfname);
        for (k = 0; k < databaseList.length; k++)
        {
          if (databaseList[k][0] == wfname)
          {
            //alert('Found: ' + databaseList[k][1]);
            dbArray = databaseList[k];
            goodDbs[goodCount] = wfname;
            goodDBNames[goodCount] = databaseList[k][1];
            goodCount++;
          }
        }
      }
	  
	  
	  //These two lines are needed to sort alphabetically
	  
      goodDBNames = goodDBNames.sort(stripLO);
     // for (x = 0; x < goodDBNames.length; x++)
     // {
      //  document.write(goodDBNames[x] + "<BR>");
     // }
      goodDbs = goodDbs.sort();
     // document.write(goodDbs);
	  
	  
	  
	  
      if (typeof(nCols) != 'undefined')
      {
        if (nCols == 1)
        {
          offset = 0;
        }
        else
        {
          offset = Math.ceil((dbCount) / nCols);
        }
      }

      var useIdx = 0;
      var rowNum = 0;
      var colNum = 0;
      var count = 0;
      var shouldColor = 0;
      var text='';
      var textanchor='';
      var letterChange = 0;

      for (j = 0; j < goodDBNames.length; j++)
      {
        if (offset != 0)
        {
          useIdx = (colNum * offset) + rowNum;
        }
        else
        {
          useIdx = j;
        }

        wfname = goodDbs[useIdx];
        for (k=0; k<databaseList.length; k++)
        {
         // if (databaseList[k][0] == wfname)
		   if (goodDBNames[j] == databaseList[k][1])
          {
            dbArray = databaseList[k];
          }
        }
      
		
		
		
		
		
		

		
        if (count == 0)
        {
          if (offset % 2 == 0)
          {
            sTemplate = '<tr class="datarowson">' + g_CheckboxTemplate;
            currentColor = 'on';
          }
          else
          {
            sTemplate = '<tr class="datarowsoff">' + g_CheckboxTemplate;
            currentColor = 'off';
          }
        }
        else
        {
          sTemplate = g_CheckboxTemplate;
        }

        sCheckbox = '';
        sDBname = '';
        sDescLink = '';
        sNativeLink = '';

        wfName = dbArray[0];
        dbName = dbArray[1];
        sDBDesc = dbArray[5];
        sDBname = dbName;
        sIconDesc = dbArray[9];
		
        for (l = 0 ; l < anchors.length ; l++)
        {
          if (sDBname.charAt(0) == anchors[l])
          {
            if ( anchors[l] != "*" )
            {
              text = anchors[l];
              anchors[l]= "*" ;
              letterChange = 1;
            }
          break;
          }
        }

        if (dbArray[6] != '')
        {
          sNativeLink = '<a ';
          if (letterChange == 1)
          {
            sNativeLink += 'name="' + text + '" ';
            letterChange = 0;
          }
          sNativeLink += 'class=dblink href="' + dbArray[6] + '">';
        }
		
		
		
		
		
		

		sDescLink = '<a class=detailslink href="javascript:showHelp(\'byuidahPopuphelp.html?wffield=' + wfName;
		//sDescLink += '\');">' + descriptionText + '</a>';
		sDescLink += '\');">';
        if (descriptionText == '<<database name>>')
        {
			sDescLink += sDBname;
        }
        else
        {
			sDescLink += descriptionText;
        }
        sDescLink += '</a>';
		
		

        if (sDBname.indexOf('link only') >= 0)
        {
          sCheckbox = '<input type="checkbox" disabled>';
        }
        else
        {
			sCheckbox = '<input type="checkbox" name="' + g_CheckboxName + '" value="';
			if (dbArray[3] == 1) {
				sCheckbox += wfName + '" checked>';
			} else {
				sCheckbox += wfName + '">';
			}
		}

		var wfNameDesc = wfName + "_desc";
		sTemplate = sTemplate.replace(/<<dbNameDesc>>/g, wfNameDesc);
		sTemplate = sTemplate.replace(/<<dbName>>/g, wfName);
        sTemplate = sTemplate.replace(/<<checkbox>>/g, sCheckbox);
        sTemplate = sTemplate.replace(/<<database name>>/g, sDBname);
        sTemplate = sTemplate.replace(/<<description link>>/g, sDescLink);
        sTemplate = sTemplate.replace(/<<database description>>/g, sDBDesc);
        sTemplate = sTemplate.replace(/<<native link>>/g, sNativeLink);
        
        if (dbArray[11] != 2) {
        	sTemplate = sTemplate.replace(/<<auth desc>>/g, "<br><bean:message key='searchForm.authIncomplete'/>");
        } else {
	        sTemplate = sTemplate.replace(/<<auth desc>>/g, "");
	    }                        

        for (p = 0;p < format.length ; p++)
        {
          if (sDBname.charAt(0) == format[p] && format[p]!="*")
          {
            format[p]="*";
            document.write(top);
            break;
          }
        }

        document.write(sTemplate);

        count++;

        if (offset != 0)
        {
          if (colNum == (nCols - 1))
          {
            colNum = 0;
            rowNum++;
            document.write('</tr>');

            if(currentColor == 'off')
            {
              document.write('<tr bgcolor="#FFFFFF">');
              currentColor = 'on';
            }
            else
            {
              document.write('<tr bgcolor="#EDE4C2">');
              currentColor = 'off';
            }
          }
          else
          {
            colNum++;
          }
        }
        else
        {
          document.write('</tr>');
          if(currentColor == 'off')
          {
            document.write('<tr bgcolor="#FFFFFF">');
            currentColor = 'on';
          }
          else
          {
            document.write('<tr bgcolor="#EDE4C2">');
            currentColor = 'off';
          }
        }
      }
      break;
    }
  }
}



function showDBsForCatNameBYUIDAH(GTemplate, categoryName, descriptionText)
{

	for (i=0; i< category.length; i++)
	{
		if (category[i][0][0] == categoryName)
		{
			var count = 0;
			var shouldColor = 0;
			var sTemplate = '';
		

			for (j=0; j<category[i][1].length; j++)
			{
				wfname = category[i][1][j];
				
				  

				for (k=0; k<databaseList.length; k++)
					if (databaseList[k][0] == wfname)
						dbArray = databaseList[k];
						
						//dbArray = dbArray.sort();

				if (shouldColor % 2)
				{
					sTemplate = '<tr bgcolor="#EDE4C2">';
				}
				else
				{
					sTemplate = '<tr bgcolor="#FFFFFF">';
				}
				shouldColor++;

				sTemplate += GTemplate;

				sCheckbox = '';
				sDBname = '';
				sDescLink = '';

				wfName = dbArray[0];
				dbName = dbArray[1];
				sDBDesc = dbArray[5];

				sNativeLink = '<a class=dblink href="' + dbArray[6] + '" title="You can Click to access this resource directly" TARGET="_parent">';


				sDescLink = '<a class=detailslink href="javascript:showHelp(\'http://wfsearch.webfeat.org/byuidahPopuphelp.html?wffield=';
				sDescLink += wfName;
				sDescLink += '\');">' + descriptionText + '</a>';

				sCheckbox = '<input type="checkbox" title="Check box to include in search" name="Databases" value="';
				sCheckbox += wfName + '">';
				sDBname = dbName;


				if (sDBname.indexOf('<!-- link only -->') >= 0)
				{
					sCheckbox = '<input type="checkbox" name="" disabled="disabled">';
				}

				sTemplate = sTemplate.replace(/<<checkbox>>/g, sCheckbox);
				sTemplate = sTemplate.replace(/<<database name>>/g, sDBname);
				sTemplate = sTemplate.replace(/<<description link>>/g, sDescLink);
				sTemplate = sTemplate.replace(/<<database description>>/g, sDBDesc);
				sTemplate = sTemplate.replace(/<<native link>>/g, sNativeLink);

				//alert(sTemplate);
				document.write(sTemplate);

				count++;
			}
			break;
		}
	}
}

function catFunction(index, state)
{
	//alert ("Index: " + index);
	//alert ("Length: " + document.wfform.catsel.length);
	var wfnames = new Array;
	var dbGroup = new Array;
	var subdbGroup;

	for (m=0; m<document.wfform.Databases.length; m++)
		document.wfform.Databases[m].checked = false;

	// document.wfform.catsel.length
	for (i=0; i<document.wfform.catsel.length; i++)
	{
		if (document.wfform.catsel[i].checked)
		{
			for (p=0; p<category.length; p++)
			{
				if (category[p][0][0] == document.wfform.catsel[i].value)
				{
					for (j=0; j<category[p][1].length; j++)
					{
						for (l=0; l<document.wfform.Databases.length; l++)
						{
							if (category[p][1][j] == document.wfform.Databases[l].value)
							{
								document.wfform.Databases[l].checked = true ;
								break;
							}
						}
					}
				}
			}
		}
	}
}

function showSubjectListBYUIDAH(subjectOmit, descriptionText, subjectOnly, categoryClick, nCols)
{
	// Use the default template if g_SubjectTemplate is not set
	if(typeof(g_SubjectTemplate) == 'undefined')
		g_SubjectTemplate = MAIN_SUBJECT_T;

	// Use the default template if g_SubjectDatabaseTemplate is not set
	if(typeof(g_SubjectDatabaseTemplate) == 'undefined')
		g_SubjectDatabaseTemplate = SUBJECT_DATABASE_T;

	// Use the default template if g_SubjectDatabaseTemplate is not set
	if(typeof(subjectOnly) == 'undefined')
		subjectOnly = true;

	if (typeof(subjectOmit) == 'undefined')
		subjectOnly = '';

	if (typeof(descriptionText) == 'undefined')
		descriptionText = '';

	if (typeof(nCols) == 'undefined')
		nCols = 1;

	var subSelIdx = 0;

	//Find number of categories to show
	var numCategories = category.length;
	var categoryStr ='';



	for (i=0; i<category.length; i++)
	{
		if (subjectOmit.indexOf(category[i][0][0]) >= 0)
			numCategories--;
		categoryStr += i + ':' + category[i][0][0] + ' ';
	}
	//alert(categoryStr);

	var offset = 0;
	if (typeof(nCols) != 'undefined')
		offset = Math.ceil(numCategories / nCols);
	if (nCols == 1)
		offset = 0;

	var useIdx = 0;
	var rowNum = 0;
	var colNum = 0;

	// Loop through the category array to build the page
	for (i=0; i<category.length; i++)
	{
		var subSelBegin = subSelIdx;
		var subSelEnd = subSelIdx;
		if (subjectOmit.indexOf(category[i][0][0]) >= 0)
		{
			continue;
		}

		useIdx = (colNum * offset) + rowNum;
		catName = category[useIdx][0][0];
		//alert(catName);

		wfSubNames = '';

		if (offset)
		{
			if(colNum == 0)
				document.write('<tr>');
			useIdx = (colNum * offset) + rowNum;
			//alert(useIdx + ' ' + catName);
		}
		else
		{
			useIdx = i;
			document.write('<tr>');
		}

		// Build the complete subject wf_name list first
		for (j=0; j<category[useIdx][1].length; j++)
		{
			wfSubNames += ',' + category[useIdx][1][j];
			subSelEnd++;
		}

		wfSubNames = wfSubNames.substr(1);

		sTemplate = g_SubjectTemplate;
		sCheckbox = '';

		if (subjectOnly)
		{
			if (categoryClick)
			{
				sCheckbox = '<input type="checkbox" title="Check box to include in search" name="catsel" value="';
				sCheckbox += catName + '"';
				sCheckbox += ' onClick="javascript:catFunction('+ i +',this.checked);">';
			}
			else
			{
				sCheckbox = '<input type="checkbox" title="Check box to include in search" name="Databases" value="';
				sCheckbox += wfSubNames + '">';
			}
		}
		else
		{
			sCheckbox = '<input type="checkbox" title="Check box to include in search" name="CategoryBoxes" value=""';
			sCheckbox += ' onClick="subSel(' + subSelBegin + ',' + subSelEnd;
			sCheckbox += ', this.checked)">';
		}

		// Set the Index for the next group
		subSelIdx = subSelEnd;

		sTemplate = sTemplate.replace(/<<checkbox>>/g, sCheckbox);
		sTemplate = sTemplate.replace(/<<category name>>/g, catName);
		//alert(sTemplate)

		document.write(sTemplate);

		if (offset)
		{
			if (colNum == (nCols - 1))
			{
				colNum = 0;
				rowNum++;
				document.write('</tr>');
			}
			else
				colNum++;
		}
		else
			document.write('</tr>');

	}
}

// Make sure there are no undefined variables in the list
function onLoadFormUpdateBYUIdah()
{
  if (aSearchStruct)
  {
    aSearchStruct[0] = 'wf_keyword';
    if (document.wfform.wf_term1)
    {
      aSearchStruct[1] = aSearchStruct[1].replace(/\%20/g, ' ');
      aSearchStruct[1] = aSearchStruct[1].replace(/\%22/g, '\"');
      aSearchStruct[1] = aSearchStruct[1].replace(/\%27/g, '\'');
      document.wfform.wf_term1.value = aSearchStruct[1];
    }
  }
}