// This code was written by Tyler Akins and placed in the public domain.
// Feel free to use this code if you so desire.
// It would be nice if you left this header intact.  http://rumkin.com

//Adapted for use on Purple Hell by Mage


force_run = 0;
	
function upd_method()
{
   var elem = document.getElementById('moreinput');
 var methodvalue;  
methodvalue="I_theban";

      elem.innerHTML = ShowImages(methodvalue);
  
   force_run = 1;
}


function start_update()
{
   if (! document.getElementById)
   {
      alert('Sorry, you need a newer browser.');
      return;
   }

   if ((! document.Caesar_Loaded) || (! document.Util_Loaded) ||
       (! document.getElementById('result')))
   {
      window.setTimeout('start_update()', 100);
      return;
   }
   upd_method();
   upd();
}


function upd()
{
   var alpha, elem, isunchanged, t;
   
   isunchanged = 0;
   if (isunchanged && force_run == 0)
   {
      window.setTimeout('upd()', 100);
      return;
   }
   
   force_run = 0;
	
   ResizeTextArea(document.encoder.text);

 t = document.encoder.text.value;
  
   Encode(t);
   
   window.setTimeout('upd()', 100);
}


function Encode(t)
{
   var elem,methodvalue="I_theban";
   
   elem = document.getElementById('result');

   if (t == "")
   {
      elem.style.fontSize = "20px";
      elem.innerHTML = "Type a message and it will be encoded here!";
   }
   else if (methodvalue.charAt(0) == 'I')
   {
      elem.style.fontSize = "30px";
      elem.innerHTML = EncodeImage(methodvalue, t);
   }

   else
   {
      elem.style.fontSize = "20px";
      elem.innerHTML = t;
   }
}


function EncodeImage(set, t)
{
   var pos = set.indexOf('_');
   if (pos >= 0)
      set = set.slice(pos + 1, set.length);
   
   var s = "";
   
   
   t = t.toUpperCase();

   for (var i = 0; i < t.length; i ++)
   {
      
	  var thisChar = t.charAt(i);
      var isAlpha = (thisChar >= 'A' && thisChar <= 'Z')? 1 : 0;
      var isNum = (thisChar >= '0' && thisChar <= '9')? 1 : 0;
      var validChar = isAlpha

         validChar = (isAlpha)? 1 : 0;

      if (validChar)
      {	 
       if(t.charAt(i)=='I' && (t.charAt(i+1)=='/' && t.charAt(i+2)=='J')) 
	     {		  
	     	s += "<img src=\"http://www.purplehell.com/riddletools/images/theban"  + "/" + thisChar +
	        ".gif\">";  
	 	   i=i+2; 
		 }	   
   else if(t.charAt(i)=='U' && (t.charAt(i+1)=='/' && t.charAt(i+2)=='V')) 
	     {		  
	     	s += "<img src=\"http://www.purplehell.com/riddletools/images/theban"  + "/" + thisChar +
	        ".gif\">";  
	 	   i=i+2; 
		 }	 
	    else
	    {
             s += "<img src=\"http://www.purplehell.com/riddletools/images/theban" + "/" + thisChar + 
	        ".gif\">";
	    }

      }
      else
      {
	    if (thisChar == "\n")
	    {s += "<br>\n";}
		else if(thisChar=='.')
		{s += "<img src=\"http://www.purplehell.com/riddletools/images/theban" + "/fullstop"+".gif\">";}
	    else
	    {s += HTMLEscape(thisChar);}
      }
   }
   
   return s;
}




function ShowImages(set)
{
   var pos = set.indexOf('_');
   if (pos >= 0)
      set = set.slice(pos + 1, set.length);

   var s = "Click on the images to add them to the message.<br>";
   var lett = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   

   
   for (var i = 0; i < lett.length; i ++)
   {
      
	  if(lett.charAt(i)!='J' && lett.charAt(i)!='V')
	 {
	  s += "<a href=\"#\" onclick=\"return L('" + lett.charAt(i) +
         "');\"><img src=\"http://www.purplehell.com/riddletools/images/theban" + "/" + lett.charAt(i) + 
         ".gif\" border=0></a>";  
     } 

   }  
   	  s += "<a href=\"#\" onclick=\"return L('.');\"><img src=\"http://www.purplehell.com/riddletools/images/theban" + "/fullstop" + ".gif\" border=0></a>"; 

 
   return s;
}

function L(l)
{
   var alpha;
   alpha = MakeKeyedAlphabet("");
   for (var i = 0; i < l.length; i ++)
   {
      var idx = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(l.charAt(i));
      if (idx >= 0)
      {
         idx = alpha.charAt(idx);
      }
      else
      {
         idx = l.charAt(i);
      }	
	  if(idx=='I')
	  {idx='I/J';}
	  if(idx=='U')
	  {idx='U/V';}
      document.encoder.text.value += idx;
   }
   return false;
}


window.setTimeout('start_update()', 100);