var persDocTitle = 'Your Title'

/*
  This function handles the full screen formatting.  Assumes keys and values from the full screen
  are set up.  Assumes that commonfunctions.js has been included in the calling page.
  In:
     inKeyId: the base key Id 
     inValueId: the base value Id
     inCounter: the total number of full-999-body lines added
*/
function format999(inKeyId, inValueId, inTrId, inCounter)
{
   var counter = 1, keyText = '', valueText = '';
   var inISSN = '', ssnValue = '', isISSN = false, isPeriodical = false;
   var locationOutput = '', locCount = 0, allLink = '';
   var lastMatch = '';
   while ((counter <= inCounter) && (document.getElementById(inKeyId + counter + ''))) {
      var linkURL = '', linkText = '';
      keyText = getElementHTML(inKeyId + counter + '');
      
      // The full text screen will continue with blank keys if the value side needs to continue
      if (keyText == '') {
         keyText = lastMatch;
      } else {
         lastMatch = keyText;
      }
      
      if (keyText.match(/link/i)) {
         // formats the Link line
         valueText = getElementHTML(inValueId + counter + '');

         var linkListText = valueText.split('\&nbsp\;');
         linkText = linkListText[0];
         if (linkListText.length > 1) {
            if (linkListText[0].match(/href/i)) {
               // ebooks
               linkURL = linkListText[0];
               linkText = linkListText[1];
            } else {
               // all other links
               linkURL = linkListText[1];
               linkText = linkListText[0];
            }
            linkURL = getDescFromHref(linkURL.replace(new RegExp( "\<BR\>", "gi" ),""));
         } else if (linkListText.length > 0) {
//            linkURL = getDescFromHref(linkListText[0]);
//            linkText = linkURL;
              linkURL = linkListText[0].replace(new RegExp("\<BR\>","gi"),"");

         }
         linkText = linkText.replace(new RegExp( "\<BR\>", "gi" ),"");
         linkURL = linkURL.replace(new RegExp( "\<BR\>", "gi" ),"");
         // Done this way because IE was having problems with some of the parts of this construct
         linkText = "<a href=\'" + "javascript" + ":cclaWindow" + "(\"" + linkURL + "\",\"Resource\")\' title=\'" + linkText + "\'>" + linkText + "<" + "/a>";
         document.getElementById(inValueId + counter + '').innerHTML = linkText;
      } 
      
      else if (keyText.match(/\?pst|\?all/i)) {
         hideElement(inTrId + counter + '');
         // formats the locations (pst)
         valueText = getElementHTML(inValueId + counter + '');
         if (valueText != '') {
            linkURL = getUrlFromHref(valueText);
            linkURL = linkURL.replace(/\&amp\;/gi,'&').replace(/\"/g,'');
            if ((allLink == '') && (keyText.match(/\?all/i))) {
               allLink = linkURL;
            } else {
//            } else if (locCount < 4) {               
               linkText = getDescFromHref(valueText);
               locationOutput += splitLocStr(linkURL, linkText,'~[0-9]');
            }
         }
         locCount++;
      } 
      
      else if (keyText.match(/description/i)) {
         valueText = getElementHTML(inValueId + counter + '');
         getElement(inValueId + counter + '').innerHTML = valueText.replace(/p\./g,"pages ").replace(/col\./g,"color ").replace(/ill\./g,"illustrations ").replace(/cm\./g,"centimeters ").replace(/ports./g,"portraits "); 
      }
      
      // If this is course reserves, change the Course Number tag to Course Info
      else if (keyText.match(/course number/i)) {
         getElement(inKeyId + counter + '').innerHTML = getElementHTML(inKeyId + counter + '').replace(/number/i, "Info");
      }
      
      // If this is course reserves, hide the course name and instructor (it is contained in the course number(info) tag)
      else if (keyText.match(/course name|instructor/i)) {
         hideElement(inTrId + counter + '');
      }
      
      // If there's an ISSN, add the sfx button in the divSFX.
      else if (keyText.match(/issn/i)) {
         if (inISSN = getElementHTML(inValueId + counter + '').match(/([0-9]{4}-[0-9]{3}[0-9x])/i) ) {
            issnValue = inISSN[0].split(',');
            isISSN = true;
            
            // also need to handle the open window
            valueText = getOpenWindowValue(getElementHTML(inValueId + counter + ''), '');
            getElement(inValueId + counter + '').innerHTML = valueText;
         }
      }
      
      else if (keyText.match(/Cover Image/i)) {
         // place the bookjacket in the div for it.
         valueText = getElementHTML(inValueId + counter + '');
         var regEx = /isbn=(\d){9,12}[\dx]/i;
         var arIsbn = valueText.match(regEx);
         var sLink = '';
         var imgLink = '';
         if (arIsbn) {
            sLink = lwServer + '/search/SyndeticsDetails.aspx?' + arIsbn[0];
            valueText = '<a href="javascript:cclaWindow(\'' + sLink + '\', \'syndetics\');" title="Click for more details" alt="Book Cover"><img src="http://syndetics.com/index.aspx?' + arIsbn[0] + '/SC.GIF&client=ccla&type=xw12" border=0></a>';
         } else {
            valueText = '';   
         }
         document.getElementById('bookCover').innerHTML = valueText;
         document.getElementById('bookCover').innerHTML += '<br><a class="hlbnormal" style="font-weight:bold;text-decoration:underline;" href="javascript:cclaWindow(\'' + sLink + '\', \'syndetics\');" title="More details">More details</a>';
         hideElement(inTrId + counter + '');
      }
      
      else if (keyText.match(/Format/i)) {
         var formatText = getElementHTML(inValueId + counter + '').match(/Periodical|Newspaper/i);
         if (!isPeriodical) {
            isPeriodical = (formatText != '');
         }
      }
      
      // reformat table of contents; replace all | with '', all '--' and || with <div> 
      else if (keyText.match(/contents/i)) {
         valueText = getElementHTML(inValueId + counter + '');
         document.getElementById(inValueId + counter + '').innerHTML = "<div class='tblcnts'>" + valueText.replace(/\| \|/g,"</div><div class='tblcnts'>").replace(/--/g,"</div><div class='tblcnts'>").replace(/\|/g,"") + "</div>"; 
      }
      
      // This deals with the window opens.and saves the title
      else {
         valueText = getElementHTML(inValueId + counter + '');
         // Save the title element
         if (keyText.match(/title/i)) {
            persDocTitle = getTitleFromFull(valueText);
         }

         if (valueText.match(/open_window_ext/i)) {
            valueText = getOpenWindowValue(valueText, 'catalog');
         
         } else if (valueText.match(/open_window/i)) {
            valueText = getOpenWindowValue(valueText, '');
         }
         getElement(inValueId + counter + '').innerHTML = valueText;

      }
      
      
      
      counter++;
   }
   
   // If ISSN and is a periodical, display SFX button
   if (isISSN && isPeriodical) {
      var outSFX = '<a href="javascript\:cclaWindow(\'' + lwServer + '/index.asp?screen=citation&issn=' + issnValue + '\',\'sfx\');" title="Full Text">'
      outSFX += '<img border="0" alt="Find Text" src="' + lwServer + '/images/sfx.gif"></a>';
      getElement('divSFX').innerHTML = outSFX;
            
   }
   
   // If there are locations available, output them here.
   if (locCount > 0) {
      document.write ('<tr class="tr1"><td align="left"><b>Campus</b></td><td align="left"><b>Location</b></td><td align="left"><b>Call Number</b></td></tr>');
      document.write(locationOutput);
      if (allLink != '')
      {
         document.write('<tr><td colspan="3" align="left"><a href="' + allLink + '" title="Full List of Holdings">Full List of Holdings</a></td></tr>');
      }
      document.write('<tr><td colspan="3" style="font-size:small;" align="left">Click on College/Campus for availability.</td></tr>');
   }
}; // format999

// replaces the open_window valuetext
function getOpenWindowValue(inText, newWinName) {
   if (inText.match(/open_window_ext/i)) {
      inText = inText.replace('open_window_ext', 'cclaWindow').replace('\")\;','\"\,\"' + newWinName + '\")\;')

   } else if (inText.match(/open_window/i)) {
      inText = inText.replace(/javascript\:open_window(\d)*\(\"/i, '').replace('\")\;', '');
   }
   return inText;
}; getOpenWindowValue

// The full document has at least the link around it and possibly a span, returns just the title.
function getTitleFromFull(inTD) {
   var regEx = /HREF\s*=(\")?/i;
   var ar = inTD.match(regEx);
   var strConv = 'Your Title';
   if (ar) {
      strConv = inTD.substring(ar.index + ar[0].length);
      strConv = strConv.substring(strConv.search(/\>/) + 1);
      strConv = strConv.substring(0, strConv.search(/\<\/a\>/i));
      // Now strip out the <span> that can occur
      strConv = strConv.replace(/<span(.*?)>|<\/span\>|\&nbsp\;/gi,'');
   }
   return strConv;
}; // getTitleFromFull
