﻿var gTextControl; // Stores global reference to text control, used for IE only.
function storeCurrentTextPosition()
{
      // This is IE specific. Required (I believe) to support accelerator keys on the toolbar buttons. Using the
      // mouse alone, they're ok, but if you use an accelerator key in IE, focus changes to the button and
      // apparently the selection is lost. So this keeps the current selection in a global reference
      // to the text control, as a custom property (currentSelection).
      if(document.selection)
      {
         gTextControl = document.getElementById("textComment");
         gTextControl.currentSelection = document.selection.createRange().duplicate();
      }
}

function displayCommentCharCount()
{

   var textComment = document.getElementById("textComment")
   var hiddenCharCount = document.getElementById("hiddenCharCount");
   hiddenCharCount.value = textComment.value.length; // Stores value so it will survive postback
   var spanCharCount = document.getElementById("spanCharCount")
   spanCharCount.innerHTML = textComment.value.length;
}

function checkEmailFollowup_CheckChanged() {
    // Conditionally enables client-side validation (required validator) if
    // the checkEmailFollowup box is checked.
    ValidatorEnable(validateRequiredFieldTextEmail, event.srcElement.status);
}

function InsertAtCursor(stringToInsert)
{
   var textField = document.getElementById("textComment")
   if (document.selection)
   {
      // IE specific
      textField.focus();
      if(gTextControl.currentSelection)
      {
         gTextControl.currentSelection.text = stringToInsert;
      }
      else
      {
         sel = document.selection.createRange();
         sel.text = stringToInsert;
      }
   }
   else if (textField.selectionStart || textField.selectionStart == 0)
   {
      // Firefox
       textField.focus();
       var startPos = textField.selectionStart;
       var endPos = textField.selectionEnd;
       textField.value = textField.value.substring(0, startPos) + stringToInsert + textField.value.substring(endPos, textField.value.length);
   }
}

function InsertFormat(format)
{
  	var formattedText = "<" + format + ">" + GetSelection() + "</" + format + ">"
  	InsertAtCursor(formattedText);
  	return false;
}

function InsertList(tag)
{
   listItems = GetSelection();
   if(listItems == "")
   {
      alert("No selection!");
      return false;
   }
   
   // If there's a trailing CRLF, strip it
   if(listItems.charAt(listItems.length+1) == "\n")
   {
      listItems = listItems.substr(0, listItems.length-1);
   }
   listItems = listItems.replace(/\n/gi, "\n<li>");
   listItems = "<" + tag + "><li>" + listItems + "</" + tag + ">";
   InsertAtCursor(listItems);
}

function GetSelection()
{
   var curSel = "";
   var ta = document.getElementById("textComment");
   if(document.selection)
   {
	  // IE
	  ta.focus()
	  if(gTextControl.currentSelection)
	  {
	     // If the selection was stored globally before.
	     curSel = gTextControl.currentSelection.text;
	  }
	  else
	  {
	    curSel = document.selection.createRange().duplicate().text;
	  }  
   }
   else
   {
	  // Firefox
	  sPos = ta.selectionStart;
	  ePos = ta.selectionEnd;
	  sLen = ePos - sPos;
	  if(sLen > 0)
	  {
		 curSel = ta.value.substring(sPos, ePos);
	  }
   }
   return curSel;
}


function InsertLink()
{
   var ta = document.getElementById("textComment");
   var urlText = prompt("URL", "")
   if(urlText == null || urlText == "")
   {
      ta.focus();
   }
   else
   {
      InsertAtCursor("<a href=\"" + urlText + "\" target=\"_blank\">" + GetSelection() + "</a>");
   }
   return false;
}

function InsertIndent()
{
   InsertAtCursor("<div style=\"margin-left:50px\">" + GetSelection() + "</div>");
}

function InsertNBSpace()
{
   InsertAtCursor("&nbsp;");
}


function InsertImage()
{
   InsertAtCursor("<img src=\"images/.jpg\" width='' height='' />");
}


function InsertImageLinked()
{
   var urlText = prompt("Image URL", "");
   var width = prompt("width", "");
   var height = prompt("height", "");
   
   var smallUrlText = "";
   if(urlText == null || urlText == "")
   {
      urlText = ".jpg";
      smallUrlText = "_sm.jpg"
   }
   else
   {
      // inserts "_sm" into graphics file name in front of extension
      lastDotPos = urlText.lastIndexOf(".");
      if(lastDotPos > -1)
      {
         smallUrlText = urlText.substring(0, lastDotPos) + "_sm" + urlText.substr(lastDotPos);
         
      }
      else
      {
         smallUrlText = urlText + "_sm.jpg";
      }
   }
   var imageText = 
   "<a target=\"_blank\" href=\"images/" 
      + urlText 
      + "\"><img src=\"images/" 
      + smallUrlText
      +  "\" width=\"" + width + "\" height=\"" + height + "\" /></a>"
   InsertAtCursor(imageText)
}


var footnoteCounter;
function InsertFootnote()
{
   if(footnoteCounter == null)
   {
      footnoteCounter = 1
   }
   else
   {
     footnoteCounter += 1
   }
   
   var ta = document.getElementById("textComment");
   var title = document.form1.textTitle.value.toLowerCase();
   // remove spaces from title
   var titleWithoutSpaces;
   var okChars = "abcdefghijklmnopqrstuvwxyz_"
   titleWithoutSpaces = "";
   var nextChar;
   for(i = 0; i < title.length; i++)
   {
      nextChar = title.substr(i, 1);
      if ( okChars.indexOf(nextChar) >= 0)
         titleWithoutSpaces += nextChar;
   }
   footnoteID = titleWithoutSpaces + footnoteCounter

   // Insert footnote marker
   footnoteLink = "[<a href='#" + footnoteID  + "'>"  + footnoteCounter + "</a>]"
   InsertAtCursor(footnoteLink)

   // Move to end of entry, add actual footnote
   newText = "\n\n<span class='footnote'><a name='" + footnoteID + "'>[" + footnoteCounter + "]</a> </span>"
   ta.value += newText;
}

function HtmlEncode()
{
   InsertAtCursor(DoHtmlEncoding(GetSelection()));
}

function DoHtmlEncoding(t)
{
   var i;
   var nextChar;
   var newText = "";
   for(i=0;i<t.length;i++)
   {
      nextChar = t.substr(i, 1);
      switch(nextChar)
      {
         case "&":
            newText += "&amp;"
            break;

//         case '"':
//          newText += "&quote;"
//            break;

         case "<":
             nextNextChar = t.substr(i+1,1);
             if(nextNextChar == " " || nextNextChar == "=")
                {newText += "<";}
             else
                {newText += "&lt;";}
             break;

         case ">":
             prevChar = t.substr(i-1,1)
             nextNextChar = t.substr(i+1,1);
             //if(nextNextChar == " " || prevChar == " " || nextNextChar == "=" || prevChar == "=")
             if(nextNextChar == " " || nextNextChar == "=")
                {newText += ">";}
             else
                {newText += "&gt;";}
             break;

         default:
            newText += nextChar;
      }
   }
   return newText;
}

