﻿// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function Trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function getExcerpt(pageResponse, data)
{
   var HandleSrc, HandleDest;
   var textLength;
   var textToMove, tempString;
   var IdSource, IdDestination;
   var retrievedText;
   var idString;
   var regExpression, whiteSpaceExpression;
   var i, j, k;
   var tag, endTag; 
   var re, re2, re3;

   IdSource = data[0];
   IdDestination = data[1];
   excerptLength = data[2]; 
   tagOnText = data[3];
   
   regExpression = "id\\s*=\\s*(\\\"|')" + IdSource + "(\\\"|')";
   whiteSpaceExpression = "\\s+";
   lastWordExpression = "\\s+[^\\s]+$";
   retrievedText = pageResponse.responseText;
   
   re = new RegExp(regExpression);
   i = retrievedText.search(re);
 
   if (i != -1)      
   {
      for (j=i-1; j >= 0; j--)
      {
           if (retrievedText.substr(j,1) == "<")
           {
              tag = retrievedText.substring(j+1);
              re2 = new RegExp(whiteSpaceExpression);
              k = tag.search(re2);
              tag = tag.substring(0, k);
              endTag = "</" + tag + ">"
              break;
           }
           
      }
     
      if (j >= 0)
      {
         textToMove = retrievedText.substring(i);        // Take everything from the id= onward
         
         k = textToMove.indexOf(">") + 1;                // Find the end of the opening tag
         textToMove = textToMove.substring(k);           //    and remove it from the string
         
         k = textToMove.indexOf(endTag);                 // Find the end tag
         textToMove = textToMove.substring(0, k);        // Take everything before it
         
         textToMove = Trim(textToMove);                  // Trim leading and trailing spaces
         textLength = textToMove.length;                 // Determine the length of the string
         if (textLength > excerptLength)                 // Check to see if it exceeds the requested length
         {
            tempString = Trim(textToMove.substring(0, excerptLength + 1));  // Look one character beyond cutoff
            textLength = excerptLength;                            // Length exceeded to cut it back to the requested length
            textToMove = textToMove.substring(0, excerptLength);   //    so cut it down to the requested length 

            if (tempString.length != excerptLength)                // Different lengths mean we hit mid-word
            {                                       
               re3 = new RegExp(lastWordExpression);               // Find the word boundary
               i = textToMove.search(re3);
               if (i != -1)
                 textToMove = textToMove.substring(0, i);          // Cut off partial word
            }
         }
       
         HandleDest = document.getElementById(IdDestination);     // Get the destination element
         HandleDest.innerHTML = textToMove + tagOnText;           // Write the excerpt into the destination
      }   
   }
}

