var hMainWnd=AkelPad.GetMainWnd();
var hEditWnd=AkelPad.GetEditWnd();
var oSys=AkelPad.SystemFunction();
var pScriptName=WScript.ScriptName;
var pSelText;
var pLinesArray;
var pTopL="庚";
var pTopR="庖";
var pBotL="弩";
var pBotR="彼";
var pHori="岸";
var pVert="岫";
var pGridHeader=pTopL;
var pGridFooter=pBotL;
var bColSel;

var nGridLength=0;

if (hMainWnd)
{
  if (AkelPad.IsAkelEdit())
  {
    if (AkelPad.SendMessage(hEditWnd, 3125 /*AEM_GETSEL*/, 0, 0))
    {
      bColSel = AkelPad.SendMessage(hEditWnd, 3127 /*AEM_GETCOLUMNSEL*/, 0, 0);

      if (!bColSel)
      {
        nSelStart=AkelPad.GetSelStart();
        nSelEnd  =AkelPad.GetSelEnd();

        SelCompliteLine(hEditWnd, nSelStart, nSelEnd);
      }

      pSelText=AkelPad.GetSelText();

      if (pLinesArray=pSelText.split("\r"))
      {
        // Support DoubleByteCharacter, Such as Chinese, Janpanese, Korean...
        String.prototype.getBytesLength = function()
        {
          return this.replace(/[^\u0000-\u00ff]/gi, "--").length;
        }

        for (i=0; i<pLinesArray.length; ++i)
        {
          if (pLinesArray[i].getBytesLength() > nGridLength)
            nGridLength=pLinesArray[i].getBytesLength();
        }

        if (nGridLength==Math.floor(nGridLength/2)*2)
        {
          for (i=0; i<pLinesArray.length; ++i)
          {
            for (n=pLinesArray[i].getBytesLength(); n<nGridLength; ++n)
            {
              pLinesArray[i]+=" ";
            }
            pLinesArray[i]=pVert+pLinesArray[i]+pVert;
          }
        }
        else
        {
          for (i=0; i<pLinesArray.length; ++i)
          {
            for (n=pLinesArray[i].getBytesLength(); n<nGridLength; ++n)
            {
              pLinesArray[i]+=" ";
            }
            pLinesArray[i]=pVert+pLinesArray[i]+" " + pVert;
          }
        }

        pSelText=pLinesArray.join("\r");

        for (i=0; i<nGridLength; i=i+2)
        {
          pGridHeader=pGridHeader+pHori;
          pGridFooter=pGridFooter+pHori;
        }
        pGridHeader=pGridHeader+pTopR;
        pGridFooter=pGridFooter+pBotR;

        pSelText=pGridHeader+"\r"+pSelText+"\r"+pGridFooter+"\r";

        AkelPad.ReplaceSel(pSelText);
      }
    }
    else AkelPad.MessageBox(hMainWnd, "帤恁隅恅掛﹝", pScriptName, 48 /*MB_ICONEXCLAMATION*/);
  }
  else AkelPad.MessageBox(hMainWnd, "剒猁 AkelPad 4.x.x 麼載詢唳掛", pScriptName, 48 /*MB_ICONEXCLAMATION*/);
}


//滲杅
function SelCompliteLine(hWnd, nMinSel, nMaxSel)
{
  var nMinLine;
  var nMaxLine;
  var nMinLineIndex;
  var nMaxLineIndex;
  var nMaxLineLength;

  if (nMinSel < nMaxSel)
  {
    nMinLine=AkelPad.SendMessage(hWnd, 1078 /*EM_EXLINEFROMCHAR*/, 0, nMinSel);
    nMaxLine=AkelPad.SendMessage(hWnd, 1078 /*EM_EXLINEFROMCHAR*/, 0, nMaxSel);
    nMinLineIndex=AkelPad.SendMessage(hWnd, 187 /*EM_LINEINDEX*/, nMinLine, 0);
    nMaxLineIndex=AkelPad.SendMessage(hWnd, 187 /*EM_LINEINDEX*/, nMaxLine, 0);
    nMaxLineLength=AkelPad.SendMessage(hWnd, 193 /*EM_LINELENGTH*/, nMaxSel, 0);

    if (nMaxLineIndex == nMaxSel) --nMaxLine;
    else if (nMaxLineLength) nMaxSel=nMaxLineIndex + nMaxLineLength + 1;

    nMinSel=nMinLineIndex;
    AkelPad.SetSel(nMinSel, nMaxSel);
    return nMaxLine - nMinLine + 1;
  }

  return 0;
}