function processSearch(theForm) {
  var wf_term1 = '';
  wf_term1 = theForm.q.value;
  if (wf_term1 == '')
  {
    alert('Please enter a search term!')
    return false;
  }
  wf_term1 = wf_term1.replace(/\"/g, '');
  wf_term1 = wf_term1.replace(/\'/g, '');

  var nexturl = '';
  
  nexturl = 'http://search3.webfeat.org/cgi-bin/WebFeat.Dll?Command=Search&Client_ID=novel&BypassSessData=yes&noserial=1';
  nexturl += '&format=JS&rtmpl=js&wf_field1=wf_keyword&wf_term1=' + wf_term1;
  nexturl += '&Databases=' + theForm.Databases.value;
  nexturl = nexturl.replace(/\&/g, '%26');
  
  document.location = "http://search3.webfeat.org/novelauth.asp?nexturl=" + nexturl;
  //document.location = nexturl;
  return false;
}

function useWF(catname) {
	

  document.write('<form id="quickform" method="post" name="quickform" onSubmit="return processSearch(this);">');
  document.write('<input type="text" name="q" id="q" size="30" maxlength="255" class="input_textfield" tabindex="15" value="">');
  document.write('<input name="Submit" type="submit" class="go_green" tabindex="16" value="GO" />');
  document.write('<input type="hidden" name="Databases" value="' + formCategoryList(catname) + '">');
  document.write('</form>');  
}



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++)
			{
			//alert(i);
				//dbValue += ',' + category[i][1][j];
				dbValue += category[i][1][j] + ',';
			}
			break;
		}
	}
	return dbValue;
}


function checkDBsNew()
{
  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;
  }
  if (document.wfform.wf_term2)
  {
    if (document.wfform.wf_term2.value.length > 0)
    {
      missing = 0;
    }
  }
  if (document.wfform.wf_term3)
  {
    if (document.wfform.wf_term3.value.length > 0)
    {
      missing = 0;
    }
  }

  if (missing == 1)
  {
  	alert('Please enter a search term!');
  	return false;
  }

  if (interfaceType == 'a')
  {
    // Set the wf_search_structure cookie so we can display the text search string on the result page.
    var sCookieText = '';
    sCookieText += document.wfform.wf_field1.value + ":";
    sCookieText += document.wfform.wf_term1.value + ":";
    sCookieText += document.wfform.wf_op2.options[document.wfform.wf_op2.selectedIndex].value + ":";
    sCookieText += document.wfform.wf_field2.options[document.wfform.wf_field2.selectedIndex].value + ":";
    sCookieText += document.wfform.wf_term2.value+ ":";
    sCookieText += document.wfform.wf_op3.options[document.wfform.wf_op3.selectedIndex].value + ":";
    sCookieText += document.wfform.wf_field3.options[document.wfform.wf_field3.selectedIndex].value + ":";
    sCookieText += document.wfform.wf_term3.value+ ":";
  }
  else
  {
    var sCookieText = '';
    sCookieText += document.wfform.wf_field1.value + ":";
    sCookieText += document.wfform.wf_term1.value + ":";
  }

  if (document.wfform.wf_all_years)
  {
    if (document.wfform.wf_all_years.value != null)
    {
      sCookieText += document.wfform.wf_all_years.value + ':0:0:';
    }
    else
    {
      if (document.wfform.wf_all_years[0].value == 'yes')
      {
        sCookieText += 'yes:0:0:';
      }
      else
      {
        sCookieText += document.wfform.wf_all_years[1].value + ':';
        sCookieText += document.wfform.wf_from_year.selectedIndex + ':';
        sCookieText += document.wfform.wf_to_year.selectedIndex + ':';
      }
    }
  }
  if (document.wfform.wf_ftonly)
  {
    sCookieText += document.wfform.wf_ftonly.checked + ':';
  }
  if (document.wfform.wf_peeronly)
  {
    sCookieText += document.wfform.wf_peeronly.checked + ':';
  }

  sCookieText = sCookieText.substr(0, sCookieText.length - 1);
  document.cookie = 'wf_search_structure=' + sCookieText + '; Path=/';

  for (i = 0; i < document.wfform.Databases.length; i++)
  {
    if (document.wfform.Databases[i].checked == true)
    {
      noneSelected = false;
    }
  }
  if (noneSelected == true)
  {
    //alert('Trying to set kids: ' + kidsPos);
    document.wfform.Databases[GenPos].checked = true;
  }

  for (i=0; i< document.wfform.Databases.length; i++)
  {
    if (document.wfform.Databases[i].checked == true)
    {
      sDatabaseCookie += document.wfform.Databases[i].value + ',';
    }
  }

  sDatabaseCookie = sDatabaseCookie.substr(0, sDatabaseCookie.length - 1);
  alert(sDatabaseCookie);
  document.cookie = 'wf_selected_databases=' + sDatabaseCookie + '; Path=/';
  return true;
}

function validateTermsNew()
{
	boolDBs = checkDBsNew();
	if (boolDBs == false)
	{
	  return false;
	}
	return true;
}


// Function to trap the enter key
function checkSubmitNew(event, oForm)
{
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13)
  {
    if (checkDBsNew())
    {
      oForm.submit();
      return false;
    }
  }
  return true;
}



function showBasicSearchGale(selectName, categoryOmit, omitDatabase, nCols)
{
  if (typeof(selectName) == 'undefined')
  {
    selectName = 'Databases';
  }

  if (typeof(categoryOmit) == 'undefined')
  {
    categoryOmit = '';
  }

  if (typeof(omitDatabase) == 'undefined')
  {
    omitDatabase = '';
  }

  endIndex = 0;
  rowNum = 0;
  dbCount = 0;
  curCol = 1;

  var offset = Math.ceil(category.length / nCols);
  var count = 1;

  document.write('<tr>');
  for (i=0; i< category.length; i++)
  {
    if (categoryOmit.indexOf(category[i][0][0]) >= 0)
    {
      continue;
    }

    if (count % offset == 1)
    {
      document.write('<td valign="top" width="50%">');
    }

    var sDatabases='';
    sOptionValue = '';
    sOptionText = category[i][0][0];

    for (j = 0; j < category[i][1].length; j++)
    {
      wfname = category[i][1][j];

      for (k=0; k<databases.length; k++)
      {
        if (databases[k][0] == wfname)
        {
          dbArray = databases[k];
        }
      }

      sDatabases += '&nbsp;&nbsp;&nbsp;' + dbArray[1] + ' ';
      sDatabases += '<a href="javascript:showHelp(\'/help.html?wffield=';
      sDatabases += dbArray[0]+'\')">' + dbArray[9] + '</a><br>\n';

      if (dbArray[1].indexOf('<!-- link only -->') >= 0)
      {
        continue;
      }

      sOptionValue += ',' + category[i][1][j];
    }

    sOptionValue = sOptionValue.substr(1);


		//Here, we are going to create a blank space where a specific cat would be
	if (sOptionText == "NonGale" || sOptionText == "Gale")
	{
document.write('');
	}

	else

	{
    document.write('&nbsp;<input type="checkbox" name="Databases" id="' + sOptionValue + '" value=' + sOptionValue + '> </font>');
    document.write('<label for="' + sOptionValue + '">' + sOptionText + '</label><BR>\n');
	}

    count++;
    if (count % offset == 0)
    {
      count = 1;
      document.write('</td>');
    }
  }
  document.write('</tr>');
}






//This custom function is being used to eliminate the Gale categories from the list.  Since we want to omit more than 1 category from the list, this may be the best option.
function showSubjectListNoGale(subjectOmit, descriptionText, subjectOnly, categoryClick, nCols)
{
  var subSelIdx = 0;
  //Find number of categories to show
  var numCategories = category.length;
  var categoryStr ='';
  var offset = 0;
  var useIdx = 0;
  var rowNum = 0;
  var colNum = 0;

  // 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;
  }

  for (i=0; i<category.length; i++)
  {
    if (subjectOmit.indexOf(category[i][0][0]) >= 0)
    {
      numCategories--;
    }
    categoryStr += i + ':' + category[i][0][0] + ' ';
  }
  if (typeof(nCols) != 'undefined')
  {
    offset = Math.ceil(numCategories / nCols);
  }
  if (nCols == 1)
  {
    offset = 0;
  }

  document.write('<td class="arrowcell" colspan="2" rowspan="2" width="50%">');
  // 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;
    }

    catName = category[i][0][0];

    wfSubNames = '';

    if (rowNum == 0)
    {
      if (i > 0)
      {
        document.write('</td>');
        document.write('<td class="arrowcell" colspan="2" rowspan="2" width="50%">');
      }
    }
    else
    {
      // document.write('<br><div class=wfcatbot></div>');
      //document.write('<br>');
    }

    // Build the complete subject wf_name list first
    for (j=0; j<category[i][1].length; j++)
    {
      wfSubNames += ',' + category[i][1][j];
      subSelEnd++;
    }

    wfSubNames = wfSubNames.substr(1);

    sTemplate = g_SubjectTemplate;
    sCheckbox = '';

    if (subjectOnly)
    {
      if (categoryClick)
      {
        // Get the subcategory and checkbox
        var subcat_array;
        var subcat = catName;

        // Setup the array variable the first time through
        // Use the first 3 characters from each word and remove the spaces
        subcat = subcat.replace(/&/g,'');
        subcat = subcat.replace(/-/g,'');
        subcat = subcat.replace(/\?/g,'');
        subcat = subcat.replace(/,/g,'');
        subcat = subcat.replace(/'/g,'');
        subcat = subcat.replace(/\!/g,'');
        subcat = subcat.replace(/\./g,'');
        subcat = subcat.replace(/\(/g,'');
        subcat = subcat.replace(/\)/g,'');
        subcat_array = subcat.split(" ");
        subcat = "";
        for (p=0; p<subcat_array.length; p++)
        {
          if (!subcat_array[p])
          {
            continue;
          }
          if (subcat_array[p].length < 3)
          {
            subcat += subcat_array[p].substring(0, subcat_array[p].length);
          }
          else
          {
            subcat += subcat_array[p].substring(0, 3);
          }
        }
        subcat = subcat.replace(/\s/g,'');
        subcat_array="";

        eval("if (typeof(" + subcat + ") != 'undefined')" + "	subcat_array = " + subcat);

        sCheckbox = '<input type="checkbox" title="Check box to include in search" name="CheckAll" value="';
        sCheckbox += wfSubNames + '"';
        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;

    eval("WFSign" + i + "='+'");
    var noSubcats = 0;
    eval("if (typeof(" + subcat + ") == 'undefined')" + "  noSubcats = 1");
    if (noSubcats == 0)
    {
      sTemplate = sTemplate.replace(/<<category image>>/g,
        "<td width=\"10%\" align=\"right\"><a href=\"JavaScript:showHideLayer('WFDB" + i + "');JavaScript:swap('WFSign" + i + "')\">" +
        "<IMG class=\"arrowbutton\"" +
        "src=\"/images/plus_on.gif\" name=\"WFSign" + i + "\"></a></td>");
    }
    else
    {


	//Here, we are going to create a blank space where a specific cat would be
	if (catName == "NonGale" || catName == "Gale")
	{
  sTemplate = "";
	}

	else

	{


	    sTemplate = sTemplate.replace(/<<category image>>/g, "<td width=\"10%\">&nbsp;</td>");
    }
    sTemplate = sTemplate.replace(/<<checkbox>>/g, sCheckbox);
    sTemplate = sTemplate.replace(/<<category name>>/g, "<font class=\"categorytitle\">" + catName + "</font>");
	}




    document.write(sTemplate);

    for (p=0; p<subcat_array.length; p++)
    {
      sSubTemplate = MAIN_SUBSUBJECT_T;
      sSubTemplate = sSubTemplate;
      sSubCheckbox = '<input type="checkbox" title="Check box to include in search" name="CheckSubAll" value="';
      sSubCheckbox += p + ':::' + subcat + '"';
      sSubCheckbox += ' onClick="javascript:catFunction('+ p +', this.checked, '+ subcat +');">';

      sSubTemplate = sSubTemplate.replace(/<<subcheckbox>>/g, sSubCheckbox);
      sSubTemplate = sSubTemplate.replace(/<<subcategory name>>/g, subcat_array[p][0][0]);
      document.write("<div class=\"categorytitle\" id=\"WFDB" + i + "\">" + sSubTemplate + "</div>");
    }

    if (rowNum == (offset - 1))
    {
      rowNum = 0;
    }
    else
    {
      rowNum++;
    }
  }
  document.write('</td>');
}





function showDBsForCatNameGale(categoryName, descriptionText, nCols, omitDatabase, format,top)
{
  var wf_access = 0;

  // Use the default template if g_CheckboxTemplate is not present
  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 = '';
  }

  for (i=0; i< category.length; i++)
  {
    if (category[i][0][0] == categoryName)
    {
      var offset = 0;
      var dbCount = category[i][1].length;

      for(j=0; j<category[i][1].length; j++)
      {
        wfname = category[i][1][j];
        for (k = 0; k < databases.length; k++)
        {
          if (databases[k][0] == wfname)
          {
            dbArray = databases[k];
            if ((wf_access == 1 && dbArray[2] != 2) || dbArray[0] == 'wf_iefbr14' || dbArray[1] == '')
            {
              dbCount--;
            }
          }
        }
      }
      if (typeof(nCols) != 'undefined')
      {
        offset = Math.ceil((dbCount) / nCols);
      }
      if (nCols == 1)
      {
        offset = 0;
      }
      var rowNum = 0;
      var colNum = 0;
      var count = 0;
      var anchors = omitDatabase;
      var text="";
      var textanchor="";
      var isTop = 0;

      // Emergency fix due to misuse of omitDatabase
      if (omitDatabase.length >= 0)
      {
        omitDatabase = '';
      }

      for (j=0; j<category[i][1].length; j++)
      {
        wfname = category[i][1][count];
        for (k=0; k<databases.length; k++)
        {
          if (databases[k][0] == wfname)
          {
            dbArray = databases[k];
          }
        }

        if ((wf_access == 1 && dbArray[2] != 2) || dbArray[0] == 'wf_iefbr14' || dbArray[1] == '' || (omitDatabase.indexOf(dbArray[0]) >= 0))
        {
          count++;
          continue;
        }

        isTop = 0;
        if ((rowNum == 0) && (colNum < nCols))
        {
          if(count > 0)
          {
            //document.write(top);
            document.write('</td>');
          }


		            document.write('<td>');
		 // document.write('<input type="checkbox" name="select" onClick="changeSelect(true)">Gale Virtual Reference Library (All)<BR><BR>');
		 document.write('<input type="checkbox" name="CheckAll" onClick="javascript:catFunction('+ i +',this.checked);">Gale Virtual Reference Library (All)<BR><BR>');
          colNum ++;
        }

        sTemplate = g_CheckboxTemplate;

        sCheckbox = '';
        sDBname = '';
        sDescLink = '';

        wfName = dbArray[0];
        dbName = dbArray[1];
        sDBDesc = dbArray[5];
        sDBname = dbName;

        for (p = 0;p < format.length ; p++)
        {
          if (sDBname.charAt(0) == format[p] && format[p]!="*")
          {
            format[p]="*";
            document.write(top);
            isTop = 1;
            j--;
            break;
          }
        }

        if (isTop == 0)
        {
          if (dbArray[6] != '')
          {
            sNativeLink =   dbArray[6];
            sNativeLink = '<a class=dblink href="' + sNativeLink + '" title="Click to access this resource directly">';
          }
          else
          {
            sNativeLink = '';
          }
          sDescLink = '<a href="javascript:showHelp(\'/help.html?wffield=';
          sDescLink += wfName;
          sDescLink += '\');">' + descriptionText + '</a>';

          sCheckbox = '<input type="checkbox" title="Check box to include in search" name="' + g_CheckboxName + '" value="';
          sCheckbox += wfName + '">';

          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);

          for (l = 0 ; l < anchors.length ; l++)
          {
            if (sDBname.charAt(0) == anchors[l])
            {
              if ( anchors[l] != "*" )
              {
                text = anchors[l].toLowerCase();
                anchors[l]= "*" ;
                textanchor = "<a name='#"+text+"'></a>";
              }
              break;
            }
          }
          document.write(textanchor);
          document.write(sTemplate);

          count++;
          if (rowNum == (offset - 1))
          {
            rowNum = 0;
          }
          else
          {
            rowNum++;
          }
        }
      }
      break;
    }
  }
  //document.write(top);
}
function hidemail(emailname,emaildomain,emailext) {
document.write(' <a href="mailto:' + emailname + '@' +
emaildomain + '.' + emailext + ' " >' +  emailname + '&#64' + emaildomain + '.' + emailext + '</a>' ); 
}

