title: Caps Lock script cleanup
body: <p>I was wondering if someone could help me clean up this code if possible; including merging the two functions into one. I'm new to javascript so I could really use some help.</p>
  <p>I was able to get this script working quite well, I just want to know if it can be refined and/or if there are any problems with it that I overlooked.</p>
  <p>The script first determines whether caps lock is on or off based on keypresses. Once caps lock state is determined, pressing the caps lock key simply toggles the caps lock warning message on or off.</p>
  <pre><code>&lt;script language=&quot;javascript&quot;&gt;
  
  //Caps Lock Warning
  var onOff = &quot;&quot;;
  function capLock(e){
  var kc = e.keyCode?e.keyCode:e.which;
  var sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
  var cl = document.getElementById('cl');
  
  if(onOff == &quot;&quot;){
  
  //Checks if caps lock is on.
  if(((kc &gt;= 65 &amp;&amp; kc &lt;= 90) &amp;&amp; !sk)||((kc &gt;= 97 &amp;&amp; kc &lt;= 122) &amp;&amp; sk))
  {
  cl.style.visibility = 'visible'; 
  onOff = &quot;on&quot;; 
  } 
  //Checks if caps lock is off.    
  else if(((kc &gt;= 65 &amp;&amp; kc &lt;= 90) &amp;&amp; sk)||((kc &gt;= 97 &amp;&amp; kc &lt;= 122) &amp;&amp; !sk))
  {
  cl.style.visibility = 'hidden'; 
  onOff = &quot;off&quot;; 
  }  
  else
  {
  cl.style.visibility = 'hidden';  
  }
  }  
  };
  
  //Hides/shows Caps Lock warning when Caps Lock key is pressed once Caps Lock
  //state is determined.
  function hideMsg(e){
  var cl = document.getElementById('cl');    
  var KeyID = (window.event) ? event.keyCode : e.keyCode; 
  
  if(KeyID==20 &amp;&amp; onOff == &quot;on&quot;)
  {
  cl.style.visibility = 'hidden';
  onOff = &quot;off&quot;;
  }
  else if(KeyID==20 &amp;&amp; onOff == &quot;off&quot;)
  {
  cl.style.visibility = 'visible';
  onOff = &quot;on&quot;;
  }
  }
  document.onkeydown=hideMsg
  
  &lt;/script&gt;
  
  
  &lt;body OnLoad=&quot;document.form1.tb1.focus();&quot;&gt;
  &lt;form name=&quot;form1&quot;&gt;
  &lt;input name=&quot;tb1&quot; onkeypress=&quot;capLock(event)&quot; type=&quot;text&quot; onLoad=&quot;javascript:this.focus&quot; /&gt;&lt;p /&gt;
  &lt;input name=&quot;tb2&quot; onkeypress=&quot;capLock(event)&quot; type=&quot;password&quot; /&gt;&lt;p /&gt;
  &lt;input id=&quot;Reset1&quot; type=&quot;reset&quot; value=&quot;reset&quot; /&gt;&lt;p /&gt;
  &lt;span id=&quot;cl&quot; class=&quot;hint&quot; style=&quot;visibility:hidden;&quot;&gt;Caps Lock is On!
  &lt;p&gt;Having Caps Lock on may cause you to enter your password incorrectly.&lt;/p&gt;
  &lt;p&gt;You should press Caps Lock to turn it off before entering your password.&lt;p/&gt;
  &lt;/span&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>