// $Revision: 4311 $

function ColorLittleFinger()
{
  return "#FDE3DE";
}

function ColorThirdFinger() 
{
  return "#FCF5E0";
}

function ColorMiddleFinger()
{
  return "#EAF4DD";
}

function ColorForeFingerLeft()
{
  return "#DDF0EC";
}

function ColorForeFingerRight()
{
  return "#DEE8F2";
}

function ColorThumb()
{
  return "#E6DFEE";
}

function ColorWhite()
{
  return "white";
}

function ColorSelected()
{
  return "#9ED7C9";
}

var CurLayout = "russianBasic";
var Keys = new Array();
Keys["russianBasic"] = new Array(); // 1st Lyout
Keys["russianTyping"] = new Array(); // 2nd Layout

Keys["russianBasic"][0] = new Array(); // 1st Lyout
Keys["russianBasic"][1] = new Array(); // 1st Lyout
Keys["russianBasic"][2] = new Array(); // 1st Lyout\n
Keys["russianBasic"][3] = new Array(); // 1st Lyout
Keys["russianBasic"][4] = new Array(); // 1st Lyout

Keys["russianTyping"][0] = new Array(); // 2nd Layout
Keys["russianTyping"][1] = new Array(); // 2nd Layout
Keys["russianTyping"][2] = new Array(); // 2nd Layout\n
Keys["russianTyping"][3]= new Array(); // 2nd Layout
Keys["russianTyping"][4]= new Array(); // 2nd Layout



Keys["russianBasic"][0][0] = new KeyStruct(new TypingKeys('¸', '¨', 'l'), "basicTop", ColorLittleFinger(), "~", "", "`", "¨", 0, 1);
Keys["russianBasic"][0][1] = new KeyStruct(new TypingKeys('1', '!', 'l'), "basicTop", ColorLittleFinger(), "!", "", "1", "", 0, 1);
Keys["russianBasic"][0][2] = new KeyStruct(new TypingKeys('2', '\"', 'l'), "basicTop", ColorThirdFinger(), "@", "\"", "2", "", 0, 1);
Keys["russianBasic"][0][3] = new KeyStruct(new TypingKeys('3', '¹', 'l'), "basicTop", ColorMiddleFinger(), "#", "¹", "3", "", 0, 1);
Keys["russianBasic"][0][4] = new KeyStruct(new TypingKeys('4', ';', 'l'), "basicTop", ColorForeFingerLeft(), "$", ";", "4", "", 0, 1);
Keys["russianBasic"][0][5] = new KeyStruct(new TypingKeys('5', '%', 'l'), "basicTop", ColorForeFingerLeft(), "%", "", "5", "", 0,  1);
Keys["russianBasic"][0][6] = new KeyStruct(new TypingKeys('6', ':', 'l'), "basicTop", ColorForeFingerRight(), "^", ":", "6", "", 0, 1);
Keys["russianBasic"][0][7] = new KeyStruct(new TypingKeys('7', '?', 'r'), "basicTop", ColorForeFingerRight(),"&", "?", "7", "", 0, 1);
Keys["russianBasic"][0][8] = new KeyStruct(new TypingKeys('8', '*', 'r'), "basicTop", ColorMiddleFinger(), "*", "", "8", "", 0, 1);
Keys["russianBasic"][0][9] = new KeyStruct(new TypingKeys('9', '(', 'r'), "basicTop", ColorThirdFinger(), "(", "", "9", "", 0, 1);
Keys["russianBasic"][0][10] = new KeyStruct(new TypingKeys('0', ')', 'r'), "basicTop", ColorLittleFinger(),")", "", "0", "", 0, 1);
Keys["russianBasic"][0][11] = new KeyStruct(new TypingKeys('-', '_', 'r'), "basicTop", ColorLittleFinger(), "_", "", "-", "", 0, 1);
Keys["russianBasic"][0][12] = new KeyStruct(new TypingKeys('=', '+', 'r'), "basicTop", ColorLittleFinger(), "+", "", "=", "", 0, 1);
Keys["russianBasic"][0][13] = new KeyStruct(new TypingKeys('', '', ''), "backspace", ColorWhite(), "Backspace", "&nbsp;", "&nbsp;", "&nbsp;", 0, 2.5);
Keys["russianBasic"][0][14] = new KeyStruct(new TypingKeys('', '', ''), "tab", ColorWhite(), "Tab", "&nbsp;", "&nbsp;", "&nbsp;", 1, 1.5);
Keys["russianBasic"][0][15] = new KeyStruct(new TypingKeys('é', 'É', 'l'), "basic", ColorLittleFinger(), "Q", "", "", "É", 1, 1);
Keys["russianBasic"][0][16] = new KeyStruct(new TypingKeys('ö', 'Ö', 'l'), "basic", ColorThirdFinger(), "W", "", "", "Ö", 1, 1);
Keys["russianBasic"][0][17] = new KeyStruct(new TypingKeys('ó', 'Ó', 'l'), "basic", ColorMiddleFinger(), "E", "", "", "Ó", 1, 1);
Keys["russianBasic"][0][18] = new KeyStruct(new TypingKeys('ê', 'Ê', 'l'), "basic", ColorForeFingerLeft(), "R", "", "", "Ê", 1, 1);
Keys["russianBasic"][0][19] = new KeyStruct(new TypingKeys('å', 'Å', 'l'), "basic", ColorForeFingerLeft(), "T", "", "", "Å", 1, 1);
Keys["russianBasic"][0][20] = new KeyStruct(new TypingKeys('í', 'Í', 'r'), "basic", ColorForeFingerRight(), "Y", "", "", "Í", 1, 1);
Keys["russianBasic"][0][21] = new KeyStruct(new TypingKeys('ã', 'Ã', 'r'), "basic", ColorForeFingerRight(), "U", "", "", "Ã", 1, 1);
Keys["russianBasic"][0][22] = new KeyStruct(new TypingKeys('ø', 'Ø', 'r'), "basic", ColorMiddleFinger(), "I", "", "", "Ø", 1, 1);
Keys["russianBasic"][0][23] = new KeyStruct(new TypingKeys('ù', 'Ù', 'r'), "basic", ColorThirdFinger(), "O", "", "", "Ù", 1, 1);
Keys["russianBasic"][0][24] = new KeyStruct(new TypingKeys('ç', 'Ç', 'r'), "basic", ColorLittleFinger(), "P", "", "", "Ç", 1, 1);
Keys["russianBasic"][0][25] = new KeyStruct(new TypingKeys('õ', 'Õ', 'r'), "basic", ColorLittleFinger(), "{", "[", "", "Õ", 1, 1);
Keys["russianBasic"][0][26] = new KeyStruct(new TypingKeys('ú', 'Ú', 'r'), "basic", ColorLittleFinger(), "}", "]", "", "Ú", 1, 1);
Keys["russianBasic"][0][27] = new KeyStruct(new TypingKeys('\\', '/', 'r'), "basic", ColorLittleFinger(), "|", "", "", "\"", 1, 1.9);
Keys["russianBasic"][0][28] = new KeyStruct(new TypingKeys('', '', ''), "capsLock", ColorWhite(), "CapsLock", "&nbsp;", "&nbsp;", "&nbsp;", 2, 1.6);
Keys["russianBasic"][0][29] = new KeyStruct(new TypingKeys('ô', 'Ô', 'l'), "basic", ColorLittleFinger(), "A", "", "", "Ô", 2, 1);
Keys["russianBasic"][0][30] = new KeyStruct(new TypingKeys('û', 'Û', 'l'), "basic", ColorThirdFinger(), "S", "", "", "Û", 2, 1);
Keys["russianBasic"][0][31] = new KeyStruct(new TypingKeys('â', 'Â', 'l'), "basic", ColorMiddleFinger(), "D", "", "", "Â", 2, 1);
Keys["russianBasic"][0][32] = new KeyStruct(new TypingKeys('à', 'À', 'l'), "basic", ColorForeFingerLeft(), "F", "", "", "À", 2, 1);
Keys["russianBasic"][0][33] = new KeyStruct(new TypingKeys('ï', 'Ï', 'l'), "basic", ColorForeFingerLeft(), "G", "", "", "Ï", 2, 1);
Keys["russianBasic"][0][34] = new KeyStruct(new TypingKeys('ð', 'Ð', 'r'), "basic", ColorForeFingerRight(), "H", "", "", "Ð", 2, 1);
Keys["russianBasic"][0][35] = new KeyStruct(new TypingKeys('î', 'Î', 'r'), "basic", ColorForeFingerRight(), "J", "", "", "Î", 2, 1);
Keys["russianBasic"][0][36] = new KeyStruct(new TypingKeys('ë', 'Ë', 'r'), "basic", ColorMiddleFinger(), "K", "", "", "Ë", 2, 1);
Keys["russianBasic"][0][37] = new KeyStruct(new TypingKeys('ä', 'Ä', 'r'), "basic", ColorThirdFinger(), "L", "", "", "Ä", 2, 1);
Keys["russianBasic"][0][38] = new KeyStruct(new TypingKeys('æ', 'Æ', 'r'), "basic", ColorLittleFinger(), ":", "", ";", "Æ", 2, 1);
Keys["russianBasic"][0][39] = new KeyStruct(new TypingKeys('ý', 'Ý', 'r'), "basic", ColorLittleFinger(), "\"", "", "\'", "Ý", 2, 1);
Keys["russianBasic"][0][40] = new KeyStruct(new TypingKeys('\n', '\n', ''), "enter", ColorWhite(), "Enter", "&nbsp;", "&nbsp;", "&nbsp;", 2, 2.5);
Keys["russianBasic"][0][41] = new KeyStruct(new TypingKeys('', '', ''), "lshift", ColorWhite(), "Shift", "&nbsp;", "&nbsp;", "&nbsp;", 3, 2.5);
Keys["russianBasic"][0][42] = new KeyStruct(new TypingKeys('ÿ', 'ß', 'l'), "basic", ColorLittleFinger(), "Z", "", "", "ß", 3, 1);
Keys["russianBasic"][0][43] = new KeyStruct(new TypingKeys('÷', '×', 'l'), "basic", ColorThirdFinger(), "X", "", "", "×", 3, 1);
Keys["russianBasic"][0][44] = new KeyStruct(new TypingKeys('ñ', 'Ñ', 'l'), "basic", ColorMiddleFinger(), "C", "", "", "C", 3, 1);
Keys["russianBasic"][0][45] = new KeyStruct(new TypingKeys('ì', 'Ì', 'l'), "basic", ColorForeFingerLeft(), "V", "", "", "Ì", 3, 1);
Keys["russianBasic"][0][46] = new KeyStruct(new TypingKeys('è', 'È', 'l'), "basic", ColorForeFingerLeft(), "B", "", "", "È", 3, 1);
Keys["russianBasic"][0][47] = new KeyStruct(new TypingKeys('ò', 'Ò', 'r'), "basic", ColorForeFingerRight(), "N", "", "", "Ò", 3, 1);
Keys["russianBasic"][0][48] = new KeyStruct(new TypingKeys('ü', 'Ü', 'r'), "basic", ColorForeFingerRight(), "M", "", "", "Ü", 3, 1);
Keys["russianBasic"][0][49] = new KeyStruct(new TypingKeys('á', 'Á', 'r'), "basic", ColorMiddleFinger(), "&lt;", "", ",", "Á", 3, 1);
Keys["russianBasic"][0][50] = new KeyStruct(new TypingKeys('þ', 'Þ', 'r'), "basic", ColorThirdFinger(), "&gt;", "", ".", "Þ", 3, 1);
Keys["russianBasic"][0][51] = new KeyStruct(new TypingKeys('.', ',', 'r'), "basic", ColorLittleFinger(), "?", "", "/", ".", 3, 1);
Keys["russianBasic"][0][52] = new KeyStruct(new TypingKeys('', '', ''), "rshift", ColorWhite(), "Shift", "&nbsp;", "&nbsp;", "&nbsp;", 3, 3);
Keys["russianBasic"][0][53] = new KeyStruct(new TypingKeys('', '', ''), "lCtrl", ColorWhite(), "Ctrl", "&nbsp;", "&nbsp;", "&nbsp;", 4, 1.2);
Keys["russianBasic"][0][54] = new KeyStruct(new TypingKeys('', '', ''), "start", ColorWhite(), "Start", "&nbsp;", "&nbsp;", "&nbsp;", 4, 1);
Keys["russianBasic"][0][55] = new KeyStruct(new TypingKeys('', '', ''), "alt", ColorWhite(), "Alt", "&nbsp;", "&nbsp;", "&nbsp;", 4, 1);
Keys["russianBasic"][0][56] = new KeyStruct(new TypingKeys(' ', '', ''), "space", ColorWhite(), "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", 4, 4.2);
Keys["russianBasic"][0][57] = new KeyStruct(new TypingKeys('', '', ''), "alt", ColorWhite(), "Alt", "&nbsp;", "&nbsp;", "&nbsp;", 4, 1);
Keys["russianBasic"][0][58] = new KeyStruct(new TypingKeys('', '', ''), "contextMenu", ColorWhite(), "Menu", "&nbsp;", "&nbsp;", "&nbsp;", 4, 1);
Keys["russianBasic"][0][59] = new KeyStruct(new TypingKeys('', '', ''), "start", ColorWhite(), "Win", "&nbsp;", "&nbsp;", "&nbsp;", 4, 1);
Keys["russianBasic"][0][60] = new KeyStruct(new TypingKeys('', '', ''), "rCtrl", ColorWhite(), "Ctrl", "&nbsp;", "&nbsp;", "&nbsp;", 4, 1.4);

Keys["russianBasic"][1][0] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 0, 0.5);
Keys["russianBasic"][1][1] = new KeyStruct(new TypingKeys('', '', ''), "insert", ColorWhite(), "Ins", "", "&nbsp;", "", 0, 1);
Keys["russianBasic"][1][2] = new KeyStruct(new TypingKeys('', '', ''), "home", ColorWhite(), "Ho", "", "&nbsp;", "", 0, 1);
Keys["russianBasic"][1][3] = new KeyStruct(new TypingKeys('', '', ''), "pageUp", ColorWhite(), "Pa", "", "Up", "", 0, 1);
Keys["russianBasic"][1][4] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 0, 0.6);
Keys["russianBasic"][1][5] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 1, 0.5);
Keys["russianBasic"][1][6] = new KeyStruct(new TypingKeys('', '', ''), "delete", ColorWhite(), "Del", "", "&nbsp;", "", 1, 1);
Keys["russianBasic"][1][7] = new KeyStruct(new TypingKeys('', '', ''), "end", ColorWhite(), "End", "", "&nbsp;", "", 1, 1);
Keys["russianBasic"][1][8] = new KeyStruct(new TypingKeys('', '', ''), "pageDown", ColorWhite(), "Pa", "", "Do", "", 1, 1);
Keys["russianBasic"][1][9] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 1, 0.6);
Keys["russianBasic"][1][10] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 2, 1);
Keys["russianBasic"][1][11] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 2, 1);
Keys["russianBasic"][1][12] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 2, 1);
Keys["russianBasic"][1][13] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 2, 1);

Keys["russianBasic"][2][0] = new KeyStruct(new TypingKeys('', '', ''), "numLock", ColorWhite(), "Num", "", "Lock", "", 0, 1);
Keys["russianBasic"][2][1] = new KeyStruct(new TypingKeys('/', '/', 'r'), "numpad", ColorWhite(), "/", "", "&nbsp;", "", 0, 1);
Keys["russianBasic"][2][2] = new KeyStruct(new TypingKeys('*', '*', 'r'), "numpad", ColorWhite(), "*", "", "&nbsp;", "", 0, 1);
Keys["russianBasic"][2][3] = new KeyStruct(new TypingKeys('7', '7', 'r'), "numpad", ColorForeFingerRight(), "7", "", "Home", "", 1, 1);
Keys["russianBasic"][2][4] = new KeyStruct(new TypingKeys('8', '8', 'r'), "numpad", ColorMiddleFinger(), "8", "", "&nbsp;", "", 1, 1);
Keys["russianBasic"][2][5] = new KeyStruct(new TypingKeys('9', '9', 'r'), "numLock", ColorThirdFinger(), "9", "", "PgUp", "", 1, 1);
Keys["russianBasic"][2][6] = new KeyStruct(new TypingKeys('4', '4', 'r'), "numpad", ColorForeFingerRight(), "4", "", "&nbsp;", "", 2, 1);
Keys["russianBasic"][2][7] = new KeyStruct(new TypingKeys('5', '5', 'r'), "numpad", ColorMiddleFinger(), "5", "", "&nbsp;", "", 2, 1);
Keys["russianBasic"][2][8] = new KeyStruct(new TypingKeys('6', '6', 'r'), "numpad", ColorThirdFinger(), "6", "", "&nbsp;", "", 2, 1);
Keys["russianBasic"][2][9] = new KeyStruct(new TypingKeys('1', '1', 'r'), "numpad", ColorForeFingerRight(), "1", "", "&nbsp;", "", 3, 1);
Keys["russianBasic"][2][10] = new KeyStruct(new TypingKeys('2', '2', 'r'), "numpad", ColorMiddleFinger(), "2", "", "&nbsp;", "", 3, 1);
Keys["russianBasic"][2][11] = new KeyStruct(new TypingKeys('3', '3', 'r'), "numpad", ColorThirdFinger(), "3", "", "&nbsp;", "", 3, 1);
Keys["russianBasic"][2][12] = new KeyStruct(new TypingKeys('0', '0', 'r'), "numpad", ColorThumb(), "0", "", "Ins", "", 4, 2);
Keys["russianBasic"][2][13] = new KeyStruct(new TypingKeys('.', '.', 'r'), "numpad", ColorWhite(), ".", "", "Del", "", 4, 1);

Keys["russianBasic"][3][0] = new KeyStruct(new TypingKeys('-', '-', 'r'), "numpadvertical", ColorWhite(), "-", "", "&nbsp;", "", 0, 1, 1); // also specify height there
Keys["russianBasic"][3][1] = new KeyStruct(new TypingKeys('+', '+', 'r'), "numpadvertical", ColorWhite(), "+", "", "&nbsp;", "", 1, 1, 2);
Keys["russianBasic"][3][2] = new KeyStruct(new TypingKeys('', '', 'r'), "numpadvertical", ColorWhite(), "enter", "", "&nbsp;", "", 2, 1, 2);

Keys["russianBasic"][4][0] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 0, 1.5);
Keys["russianBasic"][4][1] = new KeyStruct(new TypingKeys('', '', ''), "width/3", ColorWhite(), "Up", "", "&nbsp;", "", 0, 1);
Keys["russianBasic"][4][2] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 0, 1.5);
Keys["russianBasic"][4][3] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 1, 0.5);
Keys["russianBasic"][4][4] = new KeyStruct(new TypingKeys('', '', ''), "arrowpad", ColorWhite(), "Left", "", "&nbsp;", "", 1, 1);
Keys["russianBasic"][4][5] = new KeyStruct(new TypingKeys('', '', ''), "arrowpad", ColorWhite(), "Do", "", "&nbsp;", "", 1, 1);
Keys["russianBasic"][4][6] = new KeyStruct(new TypingKeys('', '', ''), "arrowpad", ColorWhite(), "Rig", "", "&nbsp;", "", 1, 1);
Keys["russianBasic"][4][7] = new KeyStruct(new TypingKeys('', '', ''), "empty", ColorWhite(), "&nbsp;", "", "&nbsp;", "", 1, 0.6);

var KeyHeight = 0;

function TypingKeys(notShiftedKey, shiftedKey, side)
{
  this.NotShiftedKey = notShiftedKey;
  this.ShiftedKey = shiftedKey; 
  this.Side = side;
}

function KeyStruct(typingKeys, type, color, leftTop, rightTop, leftBottom, rightBottom, row, width, height)
{
  if(type == undefined)
  {
    alert("Exception: {type = undefined} at KeyStruct(...)");
    return;
  }
  this.TypingKeys = typingKeys;
  this.Type = type;
  this.InitKeyPress = InitKeyPress;
  if(color == undefined) this.Color = "white"; else this.Color = color; 
  if(leftTop == undefined) this.LeftTop = ''; else   this.LeftTop = leftTop;
  if(rightTop == undefined) this.RightTop = ''; else   this.RightTop = rightTop;
  if(leftBottom == undefined)  this.LeftBottom = ''; else  this.LeftBottom = leftBottom;
  if(rightBottom == undefined)  this.RightBottom = ''; else  this.RightBottom = rightBottom;
  if(row == undefined) this.Row = 0; else this.Row = row; 
  if(width == undefined)  this.Width = 1;  else  this.Width = width;
  if(height == undefined) this.Height = 1;  else  this.Height = height; 
}

function AlertKeyboardTable()
{
  alert('Keys[0][0].Type=' + Keys["russianBasic"][3][0].Type + 
      '; Keys[0][0].leftTop=' + Keys["russianBasic"][3][0].LeftTop +
      '; Keys[0][0].rightTop=' + Keys["russianBasic"][3][0].RightTop +
      '; Keys[0][0].leftBottom=' + Keys["russianBasic"][3][0].LeftBottom +
      '; Keys[0][0].rightBottom=' + Keys["russianBasic"][3][0].RightBottom +
      '; Keys[0][0].width=' + Keys["russianBasic"][3][0].Width +
      '; Keys[0][0].heigth=' + Keys["russianBasic"][3][0].Height);
}

function CalcWeightedTotal(KeyArray, isCalcWidth, begIndex, endIndex)
{
  if(KeyArray == undefined  ||  begIndex == undefined)
  {
    alert("Exception: {type = undefined} at CalcWeightedTotal(...)");
    return;
  }
  
  var sum = 0.0;
  if(endIndex == undefined)
  {
    var curRow = KeyArray[begIndex].Row;
    if(isCalcWidth)  for(i = begIndex; i > 0 && curRow == KeyArray[i].Row; i--);
    else ;
    
    if(isCalcWidth)
    {
      if(curRow != KeyArray[i].Row) i++;
      for(; i < KeyArray.length && curRow == KeyArray[i].Row; i++)
      {
        sum += KeyArray[i].Width;
      }
    }
    else
      for(i = 0; i < KeyArray.length; i++)
      {
        sum += KeyArray[i].Height;
      }
  }
  else
  {
    for(var i = begIndex; i < endIndex; i++)
    {
      if(isCalcWidth)  sum += KeyArray[i].Width;
      else  sum += KeyArray[i].Height;
    }
  }
  return sum;
}

function CalcWeightedTotalWidth(KeyArray, begIndex, endIndex)
{
  return CalcWeightedTotal(KeyArray, true, begIndex, endIndex);
}

function CalcWeightedTotalHeight(KeyArray, begIndex, endIndex)
{
  return CalcWeightedTotal(KeyArray, false, begIndex, endIndex);
}

function GetPercentageWidth(KeyArray, index)
{
// grupp - not using here
  return Math.round( KeyArray[index].Width * 100.0 / CalcWeightedTotalWidth(KeyArray, index));
}

function GetAbsoluteWidth(KeyArray, index, srcWidth)
{
  return Math.round(srcWidth * KeyArray[index].Width / CalcWeightedTotalWidth(KeyArray, index));
}

function GetPercentageHeight(KeyArray, index, grupp)
{
  if(grupp == undefined)
  {
    alert("Exception: GetPercentageHeight(...): parameter grupp not specified");
    return;
  }
  switch(grupp)
  {
    case 0:
      return 20.0; 
      break;
    case 1:
      return 50.0;
    case 2:
      return 20.0;
    case 3:
      return Math.floor(KeyArray[index].Height * 100.0 / CalcWeightedTotalHeight(KeyArray, index) * 100) / 100.0;
    case 4:
      return 50.0;
    default:
      alert("Exception: wrong grupp argument passing to GetPercentageHeight(...)");
  }

  return Math.floor(KeyArray[index].Height * 100.0 / CalcWeightedTotalHeight(KeyArray, index) * 100) / 100.0;
}

function AddLetterTable(LeftTop, RightTop, LeftBottom, RightBottom, tableID, width, color, empty)
{
  var res = "";
  
  if(color == undefined) color="white";
  
  res += BeginCenter();
  if(RightTop == ""  &&  LeftBottom == ""  &&  RightBottom == "")
  {
    TableBuilder.ResetT();
    
//    alert(tableID);
    TableBuilder.SetTID(tableID);
    TableBuilder.SetTWidth(width);
    TableBuilder.SetTHeight("100%");
    if(empty == "empty")  TableBuilder.SetTClass("letterTableEmpty");  
    else  TableBuilder.SetTClass("letterTable");
    res += TableBuilder.BeginTable();
    TableBuilder.ResetT();
    res += TableBuilder.BeginTr();
    
    TableBuilder.SetTID(tableID + String(" 0"));
    res += "<div id=\'" + (tableID + String(" 1")) + "\'>";
    res += "<div id=\'" + (tableID + String(" 2")) + "\'>";
    res += "<div id=\'" + (tableID + String(" 3")) + "\'>";
    
    TableBuilder.SetTClass("letterTableTd");
    TableBuilder.SetTColor(color);
    
    res += TableBuilder.BeginTd();
    
    res += LeftTop;
      
    res += TableBuilder.EndTd();
    res += TableBuilder.EndTr();      
    res += TableBuilder.EndTable();
  }
  else
  {
    TableBuilder.ResetT();
  
    TableBuilder.SetTID(tableID);
    TableBuilder.SetTHeight("100%");
    TableBuilder.SetTWidth(width);
    if(empty == "empty")  TableBuilder.SetTClass("letterTableEmpty");  
    else  TableBuilder.SetTClass("letterTable");
    BeginCenter();
    res += TableBuilder.BeginTable();
    
    TableBuilder.ResetT();
    res += TableBuilder.BeginTr();
    TableBuilder.ResetT();
    TableBuilder.SetTClass("letterTableTd");
    TableBuilder.SetTColor(color);
    TableBuilder.SetTID(tableID + String(" 0"));
    res += TableBuilder.BeginTd();
    res += LeftTop;
    res += TableBuilder.EndTd();
    TableBuilder.SetTID(tableID + String(" 1"));
    res += TableBuilder.BeginTd();
    res += RightTop;
    res += TableBuilder.EndTd();
    res += TableBuilder.EndTr();
  
    TableBuilder.ResetT();
    res += TableBuilder.BeginTr();
    TableBuilder.SetTClass("letterTableTd");
    TableBuilder.SetTColor(color);
    TableBuilder.SetTID(tableID + String(" 2"));
    res += TableBuilder.BeginTd();
    res += LeftBottom;
    res += TableBuilder.EndTd();
    TableBuilder.SetTID(tableID + String(" 3"));
    res += TableBuilder.BeginTd();
    res += RightBottom;
    res += TableBuilder.EndTd();
    res += TableBuilder.EndTr();  
    
    res += TableBuilder.EndTable();
  }  
  res += EndCenter();
  return res;
}

function SetWidthWithRegardToEmptyCells(keys, srcWidth)
{
  var numOfCells = 0;
  var numOfNotEmptyCells = 0;
  var curRow = keys[0].Row;
  for(var i = 0; i < numOfCells  &&  keys[i].Row == curRow; i++)
  {
    if(keys[i].Type != "empty")  numOfNotEmptyCells++;
    numOfCells++;
  }
 
  
  width = Math.floor(numOfNotEmptyCells * srcWidth / numOfCells);
  
  return width;
}



function InitKeyHeight(val)
{
  if(KeyHeight == 0)
  {
    KeyHeight = val;
  }
}

function GenerateKeyboardSegment(SectionID, Keys, grupp, width, height)
{
  var res = "";
  var rowHeight = 0;
//  var res = "";
  
  var tdClass = "keyboardInner", tdClassEmpty = "keyboardInnerEmpty";

  var id;

  for(var i = 0, j = 0; i < Keys.length; j++)
  {
    curRow = Keys[i].Row;
    id = "key" + String(grupp) + String(j);
    TableBuilder.ResetT();
    TableBuilder.SetTID(id);
    
    var width_ = width;
/*    if(Keys[i].Type == "empty")  
    {
      width_ /= 3;
      width += 1;
    }*/
    
    TableBuilder.SetTWidth(width_);
    if(height == undefined) TableBuilder.SetTHeight(String(GetPercentageHeight(Keys, i, grupp)) + "%");
    else
      TableBuilder.SetTHeight(String(height * 5 * GetPercentageHeight(Keys, i, grupp) / 100.0));
    TableBuilder.SetTClass("keyboardTable");
    res += BeginCenter();
    res += TableBuilder.BeginTable();
    for(;i < Keys.length && curRow == Keys[i].Row;i++)
    {
      var id1 = "key" + String(grupp) + String(" ") + String(i);
      
      if(i < Keys.length - 1)
      {
        if(curRow != Keys[i + 1].Row)
        {
          TableBuilder.ResetT();
          TableBuilder.SetTID(id1);
          if(Keys[i].Type == "empty")  TableBuilder.SetTClass(tdClassEmpty);
          else  TableBuilder.SetTClass(tdClass);
          res += TableBuilder.BeginTd();
        }
        else
        {
          TableBuilder.ResetT();
          TableBuilder.SetTID(id1);
          if(Keys[i].Type == "empty")  TableBuilder.SetTClass(tdClassEmpty);
          else  TableBuilder.SetTClass(tdClass);
//          TableBuilder.SetTWidth(document.getElementById(id).width * GetPercentageWidth(Keys, i));
          TableBuilder.SetTWidth(GetAbsoluteWidth(Keys, i, width));
//          alert(TableBuilder.BeginTd());
          res += TableBuilder.BeginTd();
        }
      }
      else
      {
        TableBuilder.ResetT();
        TableBuilder.SetTID(id1);
        if(Keys[i].Type == "empty")  TableBuilder.SetTClass(tdClassEmpty);
        else  TableBuilder.SetTClass(tdClass);     
        res += TableBuilder.BeginTd();
      }
      
      if(Keys[i].Type == "empty")  
      {
        res += AddLetterTable(Keys[i].LeftTop, Keys[i].RightTop, Keys[i].LeftBottom, Keys[i].RightBottom, id1+" Inner",
                "100%", Keys[i].Color, "empty");
      }
      else
        res += AddLetterTable(Keys[i].LeftTop, Keys[i].RightTop, Keys[i].LeftBottom, Keys[i].RightBottom, id1+"Inner",
                "100%", Keys[i].Color);
            

      res += TableBuilder.EndTd();
    }
    res += TableBuilder.EndTr() + TableBuilder.EndTable();
    res += EndCenter();
  }
  
  return res;
} 

var ActiveKeys = new Array(0);

var PrevKey = new KeyDefinition(-1, 0, 0);

function KeyDefinition(grupp, num, shift)
{
  this.Grupp = grupp;
  this.Num = num;
  this.Shift = shift;
}


function SetActiveKey(keyDef, bActive)
{
  /*for(var i = 0; i < 4; i++)
  {
//    alert("key" + String(keyDef.Grupp) + " " + String(keyDef.Num) + String("Inner ") + String(i));
    var obj = document.getElementById("key" + String(keyDef.Grupp) + " " + String(keyDef.Num) + String("Inner ") + String(i));
    
// this (obj = NULL) if not from global space... Why?
//    alert('SetActiveKey1: obj = ' + obj);
    if(bActive)  
    {
      obj.style.backgroundColor = ColorSelected();
      ActiveKeys[ActiveKeys.length] = keyDef;
//      alert('SetActiveKey2');
    }
    else   obj.style.backgroundColor = Keys[CurLayout][keyDef.Grupp][keyDef.Num].Color;
//    alert('SetActiveKey3');
  }*/
//  alert("OK, length = " + ActiveKeys.length);
}

function SetActiveKeyLeft(keyDef, bActive)
{
    var obj = document.getElementById("key" + String(keyDef.Grupp) + " " + String(keyDef.Num) + String("Inner ") + String(0));
    var obj1 = document.getElementById("key" + String(keyDef.Grupp) + " " + String(keyDef.Num) + String("Inner ") + String(2));
    if(bActive)  
    {
      obj.style.backgroundColor = ColorSelected();
      obj1.style.backgroundColor = ColorSelected();
      ActiveKeys[ActiveKeys.length] = keyDef;
    }
    else   SetActiveKey(keyDef, false);  
}

function SetActiveKeyRight(keyDef, bActive)
{
    var obj = document.getElementById("key" + String(keyDef.Grupp) + " " + String(keyDef.Num) + String("Inner ") + String(1));
    var obj1 = document.getElementById("key" + String(keyDef.Grupp) + " " + String(keyDef.Num) + String("Inner ") + String(3));
    if(bActive)  
    { 
      obj.style.backgroundColor = ColorSelected();
      obj1.style.backgroundColor = ColorSelected();
      ActiveKeys[ActiveKeys.length] = keyDef;
    }
    else   SetActiveKey(keyDef, false);  
}

function ResetActiveKeys()
{
  if(ActiveKeys.length < 1) return;
//  alert('ResetActiveKeys1');
  for(var i = 0; i < ActiveKeys.length; i++)
    this.SetActiveKey(ActiveKeys[i], false);
//  alert('ResetActiveKeys2');
  ActiveKeys.splice(0, ActiveKeys.length);
//  alert("OK, after deleting length = " + ActiveKeys.length);
}

function FindKeyDefinition(symbol)
{
  var keyDef = new KeyDefinition();

  switch(symbol)
  {
  case '\n':
  
  for(var j = 0; j < Keys[CurLayout].length; j++)
  {
    for(var i = 0; i < Keys[CurLayout][j].length; i++)
    {
      if(Keys[CurLayout][j][i].Type == "enter")
      {
        keyDef.Grupp = j;
        keyDef.Num = i;      
        keyDef.Shift = '';  
        return keyDef;
      } 
    }
  }
    
  default:
  for(var j = 0; j < Keys[CurLayout].length; j++)
  {
    for(var i = 0; i < Keys[CurLayout][j].length; i++)
    {
      if(Keys[CurLayout][j][i].TypingKeys.NotShiftedKey == symbol  ||  Keys[CurLayout][j][i].TypingKeys.ShiftedKey == symbol)
      {
        keyDef.Grupp = j;
        keyDef.Num = i;
        if(Keys[CurLayout][j][i].TypingKeys.ShiftedKey == symbol)
        {
          if(Keys[CurLayout][j][i].TypingKeys.Side == 'r')  keyDef.Shift = 'l';
          else   keyDef.Shift = 'r';
          
          return keyDef;  
        }  
        keyDef.Shift = '';
        return keyDef;      
      }
    }
  }
  
  
  }
}

function FindKeyWithType(type)
{
  var keyDef = new KeyDefinition();
  for(var j = 0; j < Keys[CurLayout].length; j++)
  {
    for(var i = 0; i < Keys[CurLayout][j].length; i++)
    {
      if(Keys[CurLayout][j][i].Type == type)
      {
        keyDef.Grupp = j;
        keyDef.Num = i;
        if(Keys[CurLayout][j][i].TypingKeys.Side == 'r')  keyDef.Shift = 'l';
        else
        if(Keys[CurLayout][j][i].TypingKeys.Side == 'l')  keyDef.Shift = 'r';
        else
        keyDef.Shift = '';
        return keyDef;
      }
    }
  }  
}

function Type(e)
{
  var obj = document.getElementById("typer");
  this.TypeFunc(obj.value.charAt(obj.value.length - 1));
}

function OnBlur()
{
//  return false;
  if(keyboard.IsFocus)
  {
    document.getElementById("typer").focus();
    document.getElementById("typer").value = document.getElementById("typer").value;
  }
}


function OnFocus()
{
  //alert("OK");
  //return true;
}

//var i = 0;
//var userInput = new TInputField('input1', 60, 6, 3);

function TKeyboardInput(typeFunc)
{
  this.TypeFunc = typeFunc;
  this.PlaceKeyboard = placeKeyboard;
//  this.InitKeyInput = InitKeyInput;
  this.InitKeyboard = InitKeyboard;
  this.IsFocus = false;
  
//  this.SetFocus = SetFocus;
  this.ResetActiveKeys = ResetActiveKeys;
  this.SetActiveKey = SetActiveKey;
  
  this.SetTypeBlock = SetTypeBlock;
  this.GetTypeBlock = GetTypeBlock;
  this.TypeBlocked = false;
  this.SetInputFocus = SetInputFocus;
  
/*  this.Show = function(bShow)
  {
    var obj1 = document.getElementById('flashKeyboardSpan');
    var obj2 = document.getElementById('flashErrorSpan');
    if(bShow == false)
    {
      obj1.style.display="none";
      obj2.style.dislay="none";
      //obj1.style.visibility = "hidden";
      //obj2.style.visibility = "hidden";
    }
    else
    {
      obj1.style.display="";
      obj2.style.dislay="";    
    }
  }*/  
}

function SetTypeBlock(bBlock)
{
  this.TypeBlocked = bBlock;
}

function GetTypeBlock()
{
  return this.TypeBlocked; 
}

function SetInputFocus()
{
  if(document.getElementById('IDInputRadio') != undefined)
  {  
//    alert("SetInputFocus()");
    if(document.getElementById('IDInputRadio').style.display != "none")
    {
      if(document.getElementById("Exercise").style.display != "none")
      {
        if(navigator.appName == "Netscape")
        {
          setTimeout(function(){document.getElementById('IDInputRadio').focus();},10);
        }
        else  
        document.getElementById('IDInputRadio').focus();
      }
    }
  }
}

function OnFocus()
{
  
}

function InitKeyPress()
{

}

function flashKillFocus()
{
 //document.getElementById('IDInputRadio').focus();
// FlashInput.ShowTypeHere();
}

function flashKeyboardLoaded()
{
//   alert("point 2"); 
// alert("flashKeyboardLoaded");

//  if(IsKeyboard) SetCurKeyboardKey();
//  alert("KeyboardInfo.Layout="+KeyboardInfo.Layout);
  
//  alert("keyboard loaded");	
	
  if(!isIe())
  {
    if(UserCaption.GetUserName() == "")
    {
       KeyboardInfo.Layout = "rus";
    } 
  }
  
/*  if(Context.IsError() || FlashKeyboard.GetParamsLoaded()) // if user made an error then we init keyboard one more time
  {
	  if(!isIe()) 
	  {	
          FlashKeyboard.Activate();
          FlashKeyboard.InitLanguage();          
//          FlashInput.SetLayout(KeyboardInfo.Layout);
          SetCurKeyboardKey();
	  }
	  Context.SetError(false);
  }*/
}

function inputFlashLoaded()
{

}

function placeKeyboard(width)
{
  
//  this.Width = width;
  
  var res = "";
  
/*  res += "<span id='flashKeyboardSpan'><OBJECT codebase='' ID='flashKeyboard'>" +
  "<PARAM NAME='allowScriptAccess' VALUE='" + Environment.getScriptAccess() + "' />" +
	"<PARAM NAME='movie' VALUE='" + Environment.getResourceDirectory() + "flash/onlineKeyboard.swf'>" +
	"<EMBED WIDTH='911' HEIGHT='240' play='true' swliveconnect='true' name='flashKeyboard' src='" + Environment.getResourceDirectory() + "flash/onlineKeyboard.swf' quality='high' bgcolor='#FFFFFF' allowScriptAccess='" + Environment.getScriptAccess() + "' TYPE='application/x-shockwave-flash'></EMBED>" +
  "</OBJECT></span>";
  res += "<span id='flashErrorSpan' style='display: none'>" +
  FlashError.Place() + "</span>";*/
  
/*  var classForInput = "";
  if(navigator.appName == "Microsoft Internet Explorer")
   classForInput = "hiderRadioExplorer";
  else
  if(navigator.appName == "Netscape")
   classForInput = "hiderRadioNetscape";
  else
   classForInput = "hiderRadioOtherBrowsers";
   
  res += "<input id='IDInputRadio' type='radio' class='" + classForInput + "' onKeyPress='Type(event);' onblur='SetInputFocus_();'>"; 
  
  InitKeyPress();*/

  return res;
}

/*function ResetKeyboard()
{
  document.getElementById('IDKeyboard').innerHTML = keyboard.PlaceKeyboard();
}*/

function InitKeyboard()
{

}