﻿var maxTextLength = 2000;
var messageText = 'Your issue text cannot exceed ' + maxTextLength + ' symbols.';

function isAlphaNumericKey(keyCode)
{
   if ((keyCode > 47 && keyCode < 58) || (keyCode > 64 && keyCode < 91))
   {
       return true;
   }
   return false;
}

function LimitCharacters(editor)
{
  editor.AttachEventHandler ("onkeydown", function (e)
  {
      e = (e == null)? window.event : e;
      if (isAlphaNumericKey(e.keyCode))
      {
          textLength = editor.GetText().length;
          if (textLength >= maxTextLength)
          {
               alert(messageText);
               e.returnValue = false;
               return false;
          }
      }
   });
  editor.AttachEventHandler ("onpaste", function (e)
  {
      var textLength = CalculateLength (editor);
      if (textLength >= maxTextLength)
      {
          alert(messageText);
          e.returnValue = false;
          e.cancelBubble = true;
          return false;
      }
  });
}

function CalculateLength(editor)
{
  var textLength = editor.GetText().length;


      //Add the length of the text from the clipboard to the current length!
      //textLenght += 1;

  var clipboardLength = window.clipboardData.getData("Text").length;
  textLength += clipboardLength;
  return textLength;
}

function OnClientCommandExecuting(editor, commandName, oTool)
{
 if (commandName == "PasteFromWord"
     || commandName == "PasteFromWordNoFontsNoSizes"
     || commandName == "PastePlainText"
     || commandName == "PasteAsHtml"
     || commandName == "Paste" )
 { 


//Only if IE!!! 

     if (document.all)
     {
         var textLength = CalculateLength (editor);
         if (textLength >= maxTextLength)
         {
           alert(messageText);
           return false;
         }
     }
     else // not IE
     {
		if (commandName == "Paste")            
		{            
			editor.Fire("PastePlainText");   //you can also fire PasteFromWord and PasteAsHtml, etc         
			return false;            
		}                
	} 
 }
} 
function searchLength(val, args)
{
	args.IsValid = args.Value.length >= 3;
}
function passLength(val, args)
{
	args.IsValid = args.Value.length >= 6 && args.Value.length <= 25;
}
function termsChecked(val, args)
{
	var agree = document.getElementById(chkAgree);
	args.IsValid = agree.checked;
}
function ageChecked(val, args)
{
	var agree = document.getElementById(chkAge);
	args.IsValid = agree.checked;
}
function validateQuestion(val, args)
{
	var a = document.getElementById(edtAnswer);
	var q = document.getElementById(edtQuestion);
	args.IsValid = !(q.value.length == 0 && a.value.length > 0)
}
function validateAnswer(val, args)
{
	var a = document.getElementById(edtAnswer);
	var q = document.getElementById(edtQuestion);
	args.IsValid = !(a.value.length == 0 && q.value.length > 0)
}

function OnClientCommandExecuting(editor, commandName, oTool)            
{            
    if (commandName == "Paste")            
    {            
        editor.Fire("PastePlainText");   //you can also fire PasteFromWord and PasteAsHtml, etc         
        return false;            
    }                
}  
