/*
 * This is function to check all letters in string is in uppercase or not
 */
function IsUppercase(str)
{
  var regexp = new RegExp("[a-z]");
  if(str.match(regexp)) {
     upper = false
  } 
  else {
     upper = true
  }
  return upper;
}


/*
 * This is doHighlight function.
 */

function doHighlight(NIbodyText, searchTerm, highlightStartTag, highlightEndTag)
{
  var portal_url = document.getElementById('portal_url').value;
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<span>"
    //highlightEndTag = "<img class='no_pb no_mb' id='pos_right' onClick=initHelp(this,'"+ searchTerm + "') src='"+portal_url+"/word_icon.gif' title='Klik for at se beskrivelse af ord' alt='Klik for at se beskrivelse af ord'></span>";
  }

  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;

  if (searchTerm){
     if (IsUppercase(searchTerm)) {
        var lcSearchTerm = searchTerm;
        var lcBodyText = NIbodyText;
     }
     else {
        var lcSearchTerm = searchTerm.toLowerCase();
        var lcBodyText = NIbodyText.toLowerCase();
     }
  }

  while (NIbodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += NIbodyText;
      NIbodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (NIbodyText.lastIndexOf(">", i) >= NIbodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {

          func_name = "initHelp(this,'"+ searchTerm + "')";
          highlightEndTag = '<img class="no_pb no_pt no_mb no_mt" style="cursor:pointer;" id="pos_right_'+i+'" onClick="' + func_name + '" onMouseOver="' + func_name + '" onMouseOut="closeHelp()" src="'+portal_url+'/marking-question.gif" alt="" /></span>';

          //condition for exact match of word
          var chr_before_word = NIbodyText.substr(i-1, 1);
          var chr_after_word = NIbodyText.substr(i + searchTerm.length, 1);
          var start_char_regexp = new RegExp("[\[\\]]+|[,.?;:!<>(){}\\s\\t\\r\\n]+");      
          var end_char_regexp = new RegExp("[\[\\]]+|[,.?;:!<>(){}\\s\\t\\r\\n]+"); 
          if ((chr_before_word.match(start_char_regexp)) && (chr_after_word.match(end_char_regexp)))
          {
             newText += NIbodyText.substring(0, i) + highlightStartTag + NIbodyText.substr(i, searchTerm.length) + highlightEndTag;
             NIbodyText = NIbodyText.substr(i + searchTerm.length);

             // below two lines are added for highlighting searchterm once only inside body text. 
             // If you want to highlighlight all occurances of searchterm then comment these two lines, as a result all the searchterm occurances in body text will be highlighted.
             newText += NIbodyText;
             break;
          }
          else
          {
             newText += NIbodyText.substring(0, i + searchTerm.length);
             NIbodyText = NIbodyText.substr(i + searchTerm.length);
          }
          lcBodyText = NIbodyText;
          i = -1;
        }
      }
    }
  }
  return newText;
}




function doHighlightForSynonyms(NIbodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  var tempNIbodyText = NIbodyText;
  var portal_url = document.getElementById('portal_url').value;
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {             
    highlightStartTag = "<span>"
    //highlightEndTag = "<img class='no_pb no_mb' id='pos_right' onClick=initHelp(this,'"+ searchTerm + "') src='"+portal_url+"/word_icon.gif' title='Klik for at se beskrivelse af ord' alt='Klik for at se beskrivelse af ord'></span>";
  }

  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  //var newText = "";
  var newText = "";
  var flag = "";
  var origSearchTerm = searchTerm.replace(':=',',').split(',');
 
  if (searchTerm){
     if (IsUppercase(searchTerm)) {
        var lcSearchTerm = searchTerm;
        var lcBodyText = NIbodyText;
     }
     else {
        var lcSearchTerm = searchTerm.toLowerCase();
        var lcBodyText = NIbodyText.toLowerCase();
     }
  }

  lcSearchTerm = lcSearchTerm.replace(':=',',').split(',');

  for (var k = 0; k < lcSearchTerm.length; k++){
	var newText = "";
	if (flag=="gotit"){break;}
	searchTerm = lcSearchTerm[k];
	var regexp = eval("/\\b"+searchTerm+"(?!([^<]+)?>)\\b/gi");
	if(lcBodyText.search(regexp) == -1){
		 //alert('not found');
		 flag="no";
	    }
	else{
	      i = lcBodyText.search(regexp); //lcBodyText.indexOf(searchTerm, i+1);
	      // skip anything inside an HTML tag
	      if (NIbodyText.lastIndexOf(">", i) >= NIbodyText.lastIndexOf("<", i)) {
	        // skip anything inside a <script> block
	        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
	          func_name = "initHelp(this,'"+ origSearchTerm[0] + "')";
	          highlightEndTag = '<img class="no_pb no_pt no_mb no_mt" style="cursor:pointer;" id="pos_right_'+i+'" onClick="' + func_name + '" onMouseOver="' + func_name + '" onMouseOut="closeHelp();" src="'+portal_url+'/marking-question.gif" alt="" /></span>';
	          //condition for exact match of word
	          var chr_before_word = NIbodyText.substr(i-1, 1);
	          var chr_after_word = NIbodyText.substr(i + searchTerm.length, 1);
	          var start_char_regexp = new RegExp("[\[\\]]+|[,.?;:!<>(){}\\s\\t\\r\\n]+");      
	          var end_char_regexp = new RegExp("[\[\\]]+|[,.?;:!<>(){}\\s\\t\\r\\n]+"); 
	          if ((chr_before_word.match(start_char_regexp)) && (chr_after_word.match(end_char_regexp)))         
	          {
	             newText += NIbodyText.substring(0, i) + highlightStartTag + NIbodyText.substr(i, searchTerm.length) + highlightEndTag;
	             NIbodyText = NIbodyText.substr(i + searchTerm.length);
	             // below two lines are added for highlighting searchterm once only inside body text. 
	             // If you want to highlighlight all occurances of searchterm then comment these two lines, as a result all the searchterm occurances in body text will be highlighted.
	             newText += NIbodyText;
		     //alert('found and appended');
	      	     flag = 'gotit';
		     break;
	          }
	          else 
	          {
		     newText = NIbodyText;
		     flag="";
	          }
        	  lcBodyText = tempNIbodyText.toLowerCase();  
	        }
      	      }
	 }  
	}
  if (flag=="no"){return NIbodyText}
  else{ return newText;}
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined" || !document.getElementById('esfBody')) {
     return false;
  }

  if (searchText.length > 0) {
      var searchArray = searchText.split("||");
  }

  var NIbodyText = document.getElementById('esfBody').innerHTML;
  //var bodyText = document.body.innerHTML;

  if (searchText.length > 0) {
     for (var i = 1; i < searchArray.length; i++) {
	 if (searchArray[i].search(':=') == -1){
         	NIbodyText = doHighlight(NIbodyText, searchArray[i]);
	 }
	 else{
		search_arr = searchArray[i].replace(/[\'^\s+|\s+$]/g, "");
		NIbodyText = doHighlightForSynonyms(NIbodyText, search_arr);
	 }
     }
  }

  document.getElementById('esfBody').innerHTML = NIbodyText;
  return true;
}


