var opened = false, vkb = null;

   // This function is the "connector" from arbitrary length
   // units to "px" world. Parameters are:
   //
   // 'obj' - object, in which context we should determine
   //         the px value from the size given in 'value';
   //
   // 'value' - string, consisting of the numeric value and
   //           the length unit designator.
   //
   function convert_to_px(obj, value)
   {
     // Inner function - retrieves the (default) font size
     // for the given object.
     function get_font_size(obj)
     {
       // Standard way:
       if(window.getComputedStyle)
         return window.getComputedStyle(obj, "").getPropertyValue("font-size");

       // MS IE way:
       if(obj.currentStyle)
         return obj.currentStyle.fontSize;

       return "16px";
     }

     var unit, valu, last = value.length ? value.substr(value.length - 1, 1) : "";

     if(isNaN(last))
     {
       var def = get_font_size(document.body);
       unit = def.substr(def.length - 2, 2), valu = def.substr(0, def.indexOf(unit));

       switch(value)
       {
         case "xx-small": valu -= 3; break;
         case "x-small":  valu -= 2; break;
         case "small":    valu -= 1; break;
         case "medium":              break; // already set
         case "large":    valu += 1; break;
         case "x-large":  valu += 2; break;
         case "xx-large": valu += 3; break;
         default:
           if(value.length)
           {
             unit = (last == "%") ? "%" : value.substr(value.length - 2, 2);
             valu = value.substr(0, value.indexOf(unit));
           }
       }
     }
     else
       return value;

     if(unit == "px")
       return valu;

     // Below we have the main problem with the code. The problem is -
     // only MS IE has the facility ('window.screen.logicalXDPI' property)
     // to determine current screen DPI, which we need to convert values
     // from absolute length units (pt, pc, in, cm, mm) to pixels.
     //
     // Currently, no other browser has the way to retrieve the DPI,
     // so we have to use the "usual" value of 96 DPI (== 1.3333 pixels
     // per point), which is quite common for Windows machines.
     //
     // With browsers other than MS IE, only relative length units
     // (em, ex, % or px) can be used safely.

     // Pixels per point:
     var px_per_pt = window.screen.logicalXDPI ? (window.screen.logicalXDPI / 72.0) : 1.3333;

     // 'base_u' - base unit for current hierarchy level;
     // 'base_v' - base value for current hierarchy level;
     //
     var base_u, base_v = 1, obj_ = obj;

     do
     {
       var base = get_font_size(obj_);
       if(String(base).length)
       {
         var tmp = base.substr(base.length - 1, 1);

         if(isNaN(tmp))
         {
           base_u  = (tmp == "%") ? "%" : base.substr(base.length - 2, 2);
           base_v *= base.substr(0, base.indexOf(base_u));

                if(base_u == "%")  { base_v /= 100.0; }
           else if(base_u == "ex") { base_v /= 2.0; }
           else if(base_u == "em") { }
           else break;
         }
         else  
         {
           base_u  = "px";
           base_v *= base;
           break;
         }
       }

       obj_ = obj_.parentNode;
     }
     while(obj_ != document.documentElement);

     if(!base_v) { base_v = 16; base_u = "px"; }

     switch(unit)
     {
       case "%":  valu /= 50.0;
       case "ex": valu /= 2.0;
       case "em": valu *= base_v, unit = base_u;
     }

     switch(unit)
     {
       case "mm": valu *= 0.1;
       case "cm": valu *= 0.3937007874015748031496062992126;
       case "in": valu *= 6
       case "pc": valu *= 12;
       case "pt": valu *= px_per_pt;
     }

     return valu;
   }

   function keyb_change(size)
   {
     document.getElementById("switch").innerHTML = (opened ? "<img src=\"images/keyboard_up.jpg\" border=0 valign=absmiddle vspace=0 height=65 width=61>" : "<img src=\"images/keyboard_dn.jpg\" border=0 valign=absmiddle vspace=0 height=65 width=61>");
     opened = !opened;

     if(opened && !vkb)
     {
       var obj = document.getElementById("keyboard");

       vkb = new VKeyboard("keyboard",    // container's id
                           keyb_callback, // reference to the callback function
                           true,          // create the numpad or not? (this and the following params are optional)
                           "",            // font name ("" == system default)
   convert_to_px(obj,size) + "px",        // font size in px
                           "#000",        // font color
                           "#F00",        // font color for the dead keys
                           "#FFF",        // keyboard base background color
                           "#FFF",        // keys' background color
                           "#DDD",        // background color of switched/selected item
                           "#777",        // border color
                           "#CCC",        // border/font color of "inactive" key (key with no value/disabled)
                           "#FFF",        // background color of "inactive" key (key with no value/disabled)
                           "#F77",        // border color of the language selector's cell
                           true,          // show key flash on click? (false by default)
                           "#CC3300",     // font color during flash
                           "#FF9966",     // key background color during flash
                           "#CC3300",     // key border color during flash
                           false);        // embed VKeyboard into the page?
     }
     else
       vkb.Show(opened);
       if(opened){
     	document.getElementById("keyboard_td").style.height = "180px";
      	
      }
      else{
      	document.getElementById("keyboard_td").style.height = "0px";
      }
   }

   // Callback function:
   function keyb_callback(ch)
   {
   
     //var text = document.getElementById("x_word"), val = text.value;
      var text = document.getElementById("general"), val = text.value;
     /*if(document.srch_frm.srch_hidden.value==""){
     var text = document.getElementById("general"), val = text.value;
     }
     else if(document.srch_frm.srch_hidden.value==1){
     var text = document.getElementById("hw_name"), val = text.value;
     }
     else if(document.srch_frm.srch_hidden.value==2){
     var text = document.getElementById("vf_name"), val = text.value;
     }
     else if(document.srch_frm.srch_hidden.value==3){
     var text = document.getElementById("comp_name"), val = text.value;
     }
     else if(document.srch_frm.srch_hidden.value==4){
     var text = document.getElementById("phr_name"), val = text.value;
     }
     else if(document.srch_frm.srch_hidden.value==5){
     var text = document.getElementById("idm_name"), val = text.value;
     }
     else if(document.srch_frm.srch_hidden.value==6){
     var text = document.getElementById("prov_name"), val = text.value;
     }
     else{
       var text = document.getElementById("general"), val = text.value;
     }*/
     count=1;
    if(ch!=""){
    	count=count+1;
    	if(count==1 || count==2){
    		document.getElementById("keyboarduse").value=count;
    	}	
    
    	
    }
     switch(ch)
     {
       
       case "BackSpace":
         var min = (val.charCodeAt(val.length - 1) == 10) ? 2 : 1;
         text.value = val.substr(0, val.length - min);
         break;

       case "Enter":
         //text.value += "\n";
        document.getElementById("myform").submit();
         break;

       default:
         text.value += ch;
     }

     text.focus();

     // The following piece of code is intended to
     // correctly position the insertion marker for
     // both left-to-right and right-to-left languages:
     //
     var l = text.value.length;
     if(text.setSelectionRange)
     {
       text.setSelectionRange(l, l);
     }
     else if(document.selection)
     {
       var Sel = document.selection.createRange();

       if(Sel.moveToElementText)
       {
         Sel.moveToElementText(text);
         Sel.collapse(true);
         Sel.move('character', l);
         Sel.select();
       }
     }
   }
