// same as graphical version, but we need it here because of how we include
// js files

var naicArray = new Array(50);
var naicIndex = 0;
var sicArray = new Array(50);
var sicIndex = 0;
var tickerArray = new Array(50);
var tickerIndex = 0;
var subjectArray = new Array(50);
var subjectIndex = 0;
var meshArray = new Array(50);
var meshIndex = 0;
var meshIndicatorArray = new Array(50);
var paperKeywordArray = new Array(50);
var paperKeywordIndex = 0;
var advisorArray = new Array(50);
var advisorIndex = 0;
var generateMoreLikeThis = false;
var absoluteDatabaseName = null;
var requestString = null;
var naicsExist = false;
var sicExist = false;
var tickerExist = false;
var tickerName = ' ';
var naicsName = ' ';
var sicName = ' ';
var subjectName = ' ';
var meshName = ' ';

function setTickerName(name) {
   tickerName = name;
}

function setNaicsName(name) {
   naicsName = name;
}

function setSicName(name) {
   sicName = name;
}

function setSubjectName(name) {
   subjectName = name;
}

function setMeshName(name) {
    meshName = name;
}

function addSubject(sub) {
  //alert('adding subject '+sub+' Index '+subjectIndex);
  // Check to see if there is any repetition
  for ( i =0 ; i < subjectIndex; i++ ) {
      //alert('ARRAY VALUE ['+subjectArray[i]+']  sub value ['+sub+']');
      if ( subjectArray[i] == sub )
        return;
  }
  subjectArray[subjectIndex++] = sub;

}

function addMesh(sub, ind) {
  for ( i =0 ; i < meshIndex; i++ ) {
      if ( meshArray[i] == sub )
        return;
  }
  meshArray[meshIndex++] = sub;  
  meshIndicatorArray[meshIndex] = ind;
}

function addPaperKeyword(sub) {
  for ( i =0 ; i < paperKeywordIndex; i++ ) {
      
      if ( paperKeywordArray[i] == sub )
        return;
  }
  paperKeywordArray[paperKeywordIndex++] = sub;
}

function addAdvisor(sub) {
  for ( i =0 ; i < advisorIndex; i++ ) {
      
      if ( advisorArray[i] == sub )
        return;
  }
  advisorArray[advisorIndex++] = sub;
}

function addNaic(sub) {
  //alert('adding naic '+sub+' Index '+naicIndex);
  // Check to see if there is any repetition
  for ( i =0 ; i < naicIndex; i++ ) {
      //alert('ARRAY VALUE ['+naicArray[i]+']  sub value ['+sub+']');
      if ( naicArray[i] == sub )
        return;
  }
  naicArray[naicIndex++] = sub;
}

function addSic(sub) {
  //alert('adding sic '+sub+' Index '+sicIndex);
  for ( i =0 ; i < sicIndex; i++ ) {
      if ( sicArray[i] == sub )
        return;
  }
  sicArray[sicIndex++] = sub;
}

function addTicker(sub) {
  //alert('adding ticker '+sub+' Index '+tickerIndex);
  for (i =0 ; i < tickerIndex; i++ ) {
      if ( tickerArray[i] == sub )
        return;
  }
  tickerArray[tickerIndex++] = sub;
}

function generateSubjectMLT() {
    var str = ' ';
    if ( subjectIndex > 0 )  {
       str = str + '<tr>';	
       str = str + '<td valign="top" class="textMedium" nowrap="nowrap">' + subjectName + '</td>';
       str = str + '<td valign="top" class="textMedium">';
       for (i = 0 ; i < subjectIndex ; i++ ) {
          str = str + '<input id="sub'+i+'" type="checkbox" name="moreLikeThis" value=" SUB('+subjectArray[i]+') " />';
          str = str + '<label for="sub'+i+'">'+subjectArray[i] + '</label>';
		  if (i != subjectIndex) {
			  str = str + '&nbsp;';
		  }
       }
       str = str + '</td></tr>' ;
     }  // end Subject

  //alert(str);
  document.write(str);
}

function generateMeshMLT() {
    var str = ' ';
    var temp = '';
    if ( meshIndex > 0 )  {
       str = str + '<tr>';
       str = str + '<td valign="top" class="textMedium" nowrap="nowrap">' + meshName + '</td>';
       str = str + '<td valign="top" class="textMedium">';
       for (i = 0 ; i < meshIndex ; i++ ) {
          temp = meshArray[i];
          temp =stringReplace(temp,'&lt;hHl&gt;','<font color=red><b>');
          temp =stringReplace(temp,'&lt;/hHl&gt;','</b></font>');
          temp =stringReplace(temp,'<hHl>','<font color=red><b>');
          temp =stringReplace(temp,'</hHl>','</b></font>');
          //alert('After ='+temp);
          str = str + '<input id="mesh'+i+'" type="checkbox" name="moreLikeThis" value=" MESH('+meshArray[i]+') " />';
          str = str + '<label for="mesh'+i+'">' + temp + '</label>';
                  if (i != meshIndex) {
                          str = str + '&nbsp;';
                  }
       }
       str = str + '</td></tr>' ;
     }  // end Mesh
  document.write(str);
}

function generateSubjectTerms() {
    var str = ' ';
    if ( subjectIndex > 0 )  {
       for (i = 0 ; i < subjectIndex ; i++ ) {
          str = str + '<a href="javascript:searchSideWays(';
          str = str + "'SUB', '"+ subjectArray[i] + "')";
          str = str + '">' + subjectArray[i]  +'</a>';
		  if (i != (subjectIndex -1)) {
			  str = str + ',&nbsp;&nbsp;';
		  }
       }
     }  // end Subject

  //alert(str);
  document.write(str);
}

function generateMeshTerms() {
    var str = ' ';
    if ( meshIndex > 0 )  {
       for (i = 0 ; i < meshIndex ; i++ ) {
          temp = meshArray[i];
          while(temp.indexOf('<hHl>') >= 0 || temp.indexOf('</hHl>') >= 0 || temp.indexOf('&lt;hHl&gt;') >= 0 || temp.indexOf('&lt;/hHl&gt;') >= 0 )
          {
              temp =stringReplace(temp,'&lt;hHl&gt;','<font color=red><b>');
              temp =stringReplace(temp,'&lt;/hHl&gt;','</b></font>');
              temp =stringReplace(temp,'<hHl>','<font color=red><b>');
              temp =stringReplace(temp,'</hHl>','</b></font>');
          }
          //alert('After ='+temp);
          
          str = str + '<a href="javascript:searchSideWays(';
          str = str + "'MESH', &quot;"+ meshArray[i] + "&quot;)";
          str = str + '">' + temp +'</a>';
          if(meshIndicatorArray[i+1] == true) {
              str = str + " " + majorIndicatorString;
          }
                  if (i != (meshIndex -1)) {
                          str = str + ',&nbsp;&nbsp;';
                  }
       }
     }  // end Mesh
  
  while(str.indexOf('<hHl>') >= 0 || str.indexOf('</hHl>') >= 0)
  {
      str = stringReplace(str, '<hHl>','');
      str = stringReplace(str, '</hHl>','');
  }

  //alert(str);
  document.write(str);
}

function generatePaperKeywords() {
    var str = ' ';
    if ( paperKeywordIndex > 0 )  {
       for (i = 0 ; i < paperKeywordIndex ; i++ ) {
          temp = paperKeywordArray[i];
          //alert('Before ='+temp);
          while(temp.indexOf('<hHl>') >= 0 || temp.indexOf('</hHl>') >= 0 || temp.indexOf('&lt;hHl&gt;') >= 0 || temp.indexOf('&lt;/hHl&gt;') >= 0 )
          {
              temp =stringReplace(temp,'&lt;hHl&gt;','<font color=red><b>');
              temp =stringReplace(temp,'&lt;/hHl&gt;','</b></font>');
              temp =stringReplace(temp,'<hHl>','<font color=red><b>');
              temp =stringReplace(temp,'</hHl>','</b></font>');
          }
          //alert('After ='+temp);
          
          str = str + '<a href="javascript:searchSideWays(';
          str = str + "'IF', &quot;"+ paperKeywordArray[i] + "&quot;)";
          str = str + '">' + temp +'</a>';
                  if (i != (paperKeywordIndex -1)) {
                          str = str + ',&nbsp;&nbsp;';
                  }
       }
     }  // end Subject
  
  while(str.indexOf('<hHl>') >= 0 || str.indexOf('</hHl>') >= 0)
  {
      str = stringReplace(str, '<hHl>','');
      str = stringReplace(str, '</hHl>','');
  }

  //alert(str);
  document.write(str);
}

function generateAdvisors() {
    var str = ' ';
    if ( advisorIndex > 0 )  {
       for (i = 0 ; i < advisorIndex ; i++ ) {
          temp = advisorArray[i];
          //alert('Before ='+temp);
          while(temp.indexOf('<hHl>') >= 0 || temp.indexOf('</hHl>') >= 0 || temp.indexOf('&lt;hHl&gt;') >= 0 || temp.indexOf('&lt;/hHl&gt;') >= 0 )
          {
              temp =stringReplace(temp,'&lt;hHl&gt;','<font color=red><b>');
              temp =stringReplace(temp,'&lt;/hHl&gt;','</b></font>');
              temp =stringReplace(temp,'<hHl>','<font color=red><b>');
              temp =stringReplace(temp,'</hHl>','</b></font>');
          }
          //alert('After ='+temp);
          
          str = str + '<a href="javascript:searchSideWays(';
          str = str + "'AD', &quot;"+ advisorArray[i] + "&quot;)";
          str = str + '">' + temp +'</a>';
                  if (i != (advisorIndex -1)) {
                          str = str + ',&nbsp;&nbsp;';
                  }
       }
     }  // end Subject
  
  while(str.indexOf('<hHl>') >= 0 || str.indexOf('</hHl>') >= 0)
  {
      str = stringReplace(str, '<hHl>','');
      str = stringReplace(str, '</hHl>','');
  }

  //alert(str);
  document.write(str);
}

function generateSubjectTermsPrint() {
//alert('here');
    var str = ' ';
    if ( subjectIndex > 0 )  {
    //alert('here');
       for (i = 0 ; i < subjectIndex ; i++ ) {
           //alert('here1');
          str = str + subjectArray[i];
             // alert('here2');
		  if (i != (subjectIndex - 1)) {
			  str = str + ',&nbsp;&nbsp;';
		  }
       }
     }  // end Subject

  //alert(str);
  document.write(str);
}

function generateNSTMLT() {
	var str = ' ';
	if ( tickerIndex > 0 )  {
       str = str + '<tr>';	
       str = str + '<td valign="top" class="textMedium" nowrap="nowrap">' + tickerName + '</td>';
       str = str + '<td valign="top" class="textMedium">';
       for (i = 0 ; i < tickerIndex ; i++ ) {
          str = str + '<input id="NSTtick'+i+'" type="checkbox" name="moreLikeThis" value=" TS('+tickerArray[i]+') " />';
          str = str + '<label for="NSTtick'+i+'">' + tickerArray[i] + '</label>';
		  if (i != tickerIndex) {
			  str = str + '&nbsp;';
		  }
       }
       str = str + '</td></tr>' ;
     }  // end ticker 
	if ( naicIndex > 0 )  {
       str = str + '<tr>';	
       str = str + '<td valign="top" class="textMedium" nowrap="nowrap">' + naicsName + '</td>';
       str = str + '<td valign="top" class="textMedium">';
       for (i = 0 ; i < naicIndex ; i++ ) {
          str = str + '<input id="NSTnaic'+i+'" type="checkbox" name="moreLikeThis" value=" NAICS('+naicArray[i]+') " />';
          str = str + '<label for="NSTnaic'+i+'">' + naicArray[i] + '</label>';
		  if (i != tickerIndex) {
			  str = str + '&nbsp;';
		  }
       }
       str = str + '</td></tr>' ;
     }  // end NAICS 
	if ( sicIndex > 0 )  {
       str = str + '<tr>';	
       str = str + '<td valign="top" class="textMedium" nowrap="nowrap">' + sicName + '</td>';
       str = str + '<td valign="top" class="textMedium">';
       for (i = 0 ; i < sicIndex ; i++ ) {
          str = str + '<input id="NSTsic'+i+'" type="checkbox" name="moreLikeThis" value=" SIC('+sicArray[i]+') " />';
          str = str + '<label for="NSTsic'+i+'">' + sicArray[i] + '</label>';
		  if (i != tickerIndex) {
			  str = str + '&nbsp;';
		  }
       }
       str = str + '</td></tr>' ;
     }  // end SIC

  //alert(str);
  document.write(str);
}


var ns6=document.getElementById&&!document.all?1:0

var head="display:''"
var folder=''

function expandit(curobj){
folder=ns6?curobj.nextSibling.nextSibling.style:document.all[curobj.sourceIndex+1].style
if (folder.display=="none")
folder.display=""
else
folder.display="none"
}


function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

function trim ( s )
{
	return rtrim(ltrim(s));
}

function setRequestString(str) {
   requestString = str;
}


function searchMoreLikeThis() {
	var isFirst = true;
	var qString = ' ';
	if ( document.article.moreLikeThis.length ) {
    for ( i =0 ; i <document.article.moreLikeThis.length  ; i++ )
    {
        if (document.article.moreLikeThis[i].checked)
        {
            if (!isFirst)
            {
                qString = qString + ' AND ';
            }
            isFirst = false;
            qString = qString + stripStrings(document.article.moreLikeThis[i].value, false); 
        }
    }
    } else {
       // only one in the list
       if ( document.article.moreLikeThis.checked ) {
              isFirst = false;
            qString = qString + stripStrings(document.article.moreLikeThis.value, false); 
       }
    }

    if ( isFirst)
    { 
        alert("Please select a term to search.");
        return;
    }
    // escape not needed
	document.article.SQ.value = qString;
	document.article.submit();
    return;
}

function resetMoreLikeThis() {
	if ( document.article.moreLikeThis.length ) {
	    for ( i =0 ; i <document.article.moreLikeThis.length  ; i++ )
        {
            document.article.moreLikeThis[i].checked = false;
        }
	} else {
         document.article.moreLikeThis.checked = false;
	}
}

function stripStrings(str, isO) {
   
   if ( isO) {
       str = stringReplace(str, '(', ' ');
       str = stringReplace(str, ')', ' ');
   }
   
   if ( !isO) {
      //tempString = 'NA(abcded(def))';
      // check for double 
      index = str.indexOf('(');
      if ( index != -1 ) {
          //alert('Index ='+index);
          substr = str.substring(index+1, str.length);
          //alert('Substring ='+substr);
          subindex = substr.indexOf('(');
          if ( subindex != -1) {
             // We have multiple paranthesis
             //alert('Subindex ='+subindex);
             temp1 = str.substring(0, subindex+index+1);
             temp2 = str.substring(subindex+index+2, str.length);
             //alert('TEMP1 ='+temp1);
             //alert('TEMP2 ='+temp2);
             //alert(temp1+temp2);
             str = temp1 + temp2;
             endIndex = str.indexOf(')');
             if ( endIndex != -1 ) {
                // we are assuming we have two end )
                str = str.substring(0, endIndex) + str.substring(endIndex+1, str.length);
             }
             //alert(tempString);
          }
      }
   }
   
   str = stringReplace(str, '[', ' ');
   str = stringReplace(str, ']', ' ');
   str = stringReplace(str, '{', ' ');
   str = stringReplace(str, '}', ' ');
   str = stringReplace(str, ';', ' ');
   
   while(str.indexOf('<hHl>') >= 0 || str.indexOf('</hHl>') >= 0)
   {
      str = stringReplace(str, '<hHl>','');
      str = stringReplace(str, '</hHl>','');
   }
   str = stringReplace(str, '<', ' ');
   str = stringReplace(str, '>', ' ');
   str = stringReplace(str, 'hHl', '  ');
   str = stringReplace(str, '/hHl', '');
   str = stringReplace(str, ',,', ',');
   str = stringReplace(str, ', ,', ',');
   str = stringReplace(str, '  ', ' ');
   // Do it twice just so that if we have "  ,"
   str = stringReplace(str, ' ,',',');
   str = stringReplace(str, ' ,',',');
   //alert(str);
   return str;
}


function searchSideWays(sf, str) {
   str = stripStrings(str, true);

   document.article.SQ.value = sf + '(' + str + ')';
   document.article.submit();
   return false;
}

function stringReplace(str1, str2, str3) { 
 str1 = str1.split(str2).join(str3);  //<-- SLOW
 // This doesn't work in netscape
 //while(str1.indexOf(str2) != -1) {
 //  str1 = str1.replace(str2, str3);
 //} 
 return str1;     
} 

function doTranslation(baseLocation)
{
    var translationLang = document.article.translang.options[document.article.translang.selectedIndex].value;
    if(translationLang == "#")
    {
        return;
    }    
    var transLocation = baseLocation + "&tw=1";
    transLocation = transLocation + "&arttran=" + translationLang;  
    document.location = transLocation;    
}

function doTranslationLink(baseLocation, langPair)
{    
    var transLocation = baseLocation + "&tw=1";
    transLocation = transLocation + "&arttran=" + langPair;
    document.location = transLocation;
}

function fixURL(sID, oldURL) {
	var oSpan = document.getElementById(sID);
	if (oSpan) {
		oSpan.innerHTML = oldURL;
	}
}


