// javascript support for netBook.

var a=[]; // submit array
var transID=0;

// editable table support variables
var edTableCnt=0; // number of editable table
var edTableFnArr=[];
var edTableIdArr=[];
var edTableTransIdArr=[];
var edTableRowCntArr=[];
var edTableColCntArr=[];
var edTableModFlgArr=[];
var edTableRowCntFlag=[];

// key event support variables
var edInsState=0;
var edChangeOneShot=0;
var shiftIn=false;
var ctrlIn=false;

var addModeFlag = false;  // this flag is used to disable all buttons except CANCEL and SAVE when in add mode.
var addModeErrFlag = false;  // this flag is used to disable all buttons except CONTINUE on add naming errors.

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function getEditText(o)
{  // IE5 inserts <P> for Return Key pressed in editable <DIV> and <PRE>
   return o.innerText;

   // try a multi step process, bugged
   t = o.innerHTML;
   t = t.replace(/<P>/ig,'~CR~');
   t = t.replace(/<\/P>/ig,'');
   o.innerHTML = t;
   return o.innerText.replace(/~CR~/g, '\\n');
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function go(n)
{ o = document.forms[0];
  o.shellVars.value = a[n];
  o.shellAction.value= 1; // indicate that a shell/non-former action has been invoked.

  if (addModeErrFlag)
  {
    if ( a[n].indexOf('act=addRecordErrCont') == -1 )
    { alert('You can only press the CONTINUE button.');
      return;
    }
  }
  else if (addModeFlag)
  {
    if (    a[n].indexOf('act=save')        == -1 // check for save action.
         && a[n].indexOf('act=addCanceled') == -1 // check for addCanceled action.
       )
    { alert('You can only press the SAVE or CANCEL button.');
      return;
    }
  }

  if ( a[n].indexOf('act=upload_go') > -1 ) // check for upload action.
  {
    var errFlag = false;
    if (o.userfile.value=='')    errFlag=true;
    else if (o.upDesc.value=='') errFlag=true;
    if (!errFlag) o.encoding = 'multipart/form-data'; // do file transfer.
  }
  else if ( a[n].indexOf('act=save') > -1 )
  {
    if (o._0 && o._0.value ==  '~divEditReverseTranslationRequired~' )
        o._0.value = getEditText(oEdit_0);

    if (o._1 && o._1.value ==  '~divEditReverseTranslationRequired~' )
        o._1.value = getEditText(oEdit_1);

    if (o._2 && o._2.value ==  '~divEditReverseTranslationRequired~' )
        o._2.value = getEditText(oEdit_2);
  }
  else if ( a[n].indexOf('act=editableTableSave') > -1 )
  {
    o.editableTableDataSets.value = setEdTableServerStr2();
  }

  o.submit(); // may defer this submit to former submit.
}


//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function edTabdataModified(tabNdx)
{
  edTableModFlgArr[tabNdx] = true;
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function dataModified()
{
  oEdSaveBut.style.visibility = 'visible';
  if (!edChangeOneShot)
  { edChangeOneShot++;
    oEdChangeCnt.innerText = 'Data Modified';
  }
}//dataModified


//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function evEdTabKeypress(tabNdx) // for IE5.5+, for editable tables
{
  switch (event.keyCode)
  { case 124: // dis-allow field delimiter |
    case  13: // dis-allow newlines
             event.returnValue=false;
             break;

    default: // ID the editable table and set the respective change flag.
             edTabdataModified(tabNdx);
             break;
  }//sw
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function evKeypress(crBlockflag)
{
    switch (event.keyCode)
    {
      case 13: if (crBlockflag) // handle newline key blocking (win-IE)
                 event.returnValue=false; // dis-allow newlines
               else
                 dataModified();
               break;

      default: if ( event.keyCode>31 && event.keyCode<127 )
                 dataModified();
               break;
    }//sw
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function evKeyup()
{
    switch (event.keyCode)
    { case 16: shiftIn=false; break; //   shift key released
      case 17: ctrlIn =false; break; // control key released

    }//sw
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function evKeydown()
{
    switch ( event.keyCode )
    {
      case 16: shiftIn= true; break; //   shift-key depressed
      case 17: ctrlIn = true; break; // control-key depressed

      case 45: // Insert Key depressed
               if (ctrlIn) break; // copy to clipboard
               if (shiftIn) { dataModified(); break; } // paste
               // track insert mode for user
               edInsState++;
               oInsMode.innerText = edInsState & 1 ? 'Overwrite' : 'Insert';
               break;

      case  8: dataModified(); // backspace key depressed
               break;

      case 46: dataModified(); // delete key depressed, with or without shift key
               break;
    }//sw
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function evEdTabKeydown(tabNdx)
{
    switch ( event.keyCode )
    {
      case 16: shiftIn= true; break; //   shift-key depressed
      case 17: ctrlIn = true; break; // control-key depressed

      case 45: // Insert Key depressed
               if (ctrlIn) break; // copy to clipboard
               if (shiftIn) { edTabdataModified(tabNdx); break; } // paste
               // track insert mode for user
               edInsState++;
               // oInsMode.innerText = edInsState & 1 ? 'Overwrite' : 'Insert';
               break;

      case  8: edTabdataModified(tabNdx); // backspace key depressed
               break;

      case 46: edTabdataModified(tabNdx); // delete key depressed, with or without shift key
               break;
    }//sw
}


//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function setCell (cell, value)
{ if (document.all)
    cell.innerText = value;
  else if (document.getElementById)
    cell.replaceChild(document.createTextNode(value), cell.firstChild);
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function setEdTableServerStr2()
{ var s = '';
  var cnt = 0;

  if (document.all) // for IE5.5+
  {
    for (i=0; i<edTableCnt; i++)
    {
      if (edTableModFlgArr[i]) // test: any table entry changes
      { if (cnt++) s += '~::~';
        s += edTableFnArr[i]     +'~:~'+
             edTableColCntArr[i] +'~:~'+
             edTableRowCntArr[i] +'~:~'+
             edTableTransIdArr[i]+'~:~'+
             document.getElementById(edTableIdArr[i]).outerHTML;
      }//if
    }//for
  }
  else
  {
    for (i=0; i<edTableCnt; i++)
    {
      if (edTableModFlgArr[i]) // test: any table entry changes
      {
      }//if
    }//for
  }

  return s;
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function editCell2 (cell) // used in 'information areas'
{ // used to edit 'information areas' NOT 'editable tables'
  // note: onKeypress is set forward of here.
  if (document.all) {
    cell.innerHTML =
      '<SPAN contentEditable=true ID="editCell"' +
      ' STYLE="' + hiLite_style + '"' +
      ' ONCLICK="event.cancelBubble = true;"' +
      ' ONCHANGE="setCell(this.parentElement, \'&nbsp;\'+this.innerText)"' +
      ' ONBLUR="setCell(this.parentElement, \'&nbsp;\'+this.innerText)"' +
      '>'+cell.innerText+'</span>';

    document.all.editCell.focus();
  }
  else if (document.getElementById)
  {
    cell.normalize();
    var input = document.createElement('INPUT');
    input.setAttribute('class', 's14');
    input.setAttribute('value', cell.firstChild.nodeValue);
    input.setAttribute('size', cell.firstChild.nodeValue.length);
    //input.setAttribute('size', 30);
    input.onblur=
    input.onchange = function (evt) { setCell(this.parentNode, this.value); };
    input.onclick = function (evt) { evt.cancelBubble = true;
      if (evt.stopPropagation) evt.stopPropagation(); };
    cell.replaceChild(input, cell.firstChild);
    input.focus();
    //input.select();
  }
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function edCell(cell, tabNdx) // used to edit 'editable tables'
{
  if (document.all)   //ie5.5+
  {
   cell.innerHTML =
     '<SPAN ' +
     ' contentEditable=true'+
     ' ID="editCell"' +
     ' STYLE="font-size:11px; color:blue; background:white; padding:0;"'+
     ' ONCLICK="event.cancelBubble = true;"' +
     ' ONCHANGE="setCell(this.parentElement, this.innerText)"' +
     ' ONBLUR="setCell(this.parentElement, this.innerText)"' +
     ' ONKEYUP="evKeyup();"'+
     ' ONKEYDOWN="evEdTabKeydown('+tabNdx+');"'+
     ' ONKEYPRESS="evEdTabKeypress('+tabNdx+');"'+'>'+cell.innerText+'</span>';

   document.all.editCell.focus();
   //document.all.editCell.select();
  }
  else if (document.getElementById) //ns5+,mozilla 5
  {
    cell.normalize();
    var input = document.createElement('INPUT');
    input.setAttribute('class', 's14');
    input.setAttribute('value', cell.firstChild.nodeValue);
    input.setAttribute('size', cell.firstChild.nodeValue.length);
    //input.setAttribute('size', 30);
    input.onblur=
    input.onchange = function (evt) { setCell(this.parentNode, this.value); };
    input.onclick = function (evt) { evt.cancelBubble = true;
      if (evt.stopPropagation) evt.stopPropagation(); };
    cell.replaceChild(input, cell.firstChild);
    input.focus();
    //input.select();
  }
}

var tabNdx3 = 0;
function evBlur3(event)     { this.className=''; }
function evKeyup3(event)    { evKeyup(); }
function evKeydown3(event)  { evEdTabKeydown(tabNdx3); }
function evKeypress3(event) { evEdTabKeypress(tabNdx3); }

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function edCell3(cell, tabNdx)
{
  if (document.all)   //ie5.5+
  { tabNdx3 = tabNdx;
    cell.className = 'edCell';
    cell.onblur    = evBlur3;
    cell.onkeyup   = evKeyup3;
    cell.onkeydown = evKeydown3;
    cell.onkeypress= evKeypress3;
  }
  else
  { edCell(cell, tabNdx);
  }
}



//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function goM1(offset) // menu <SELECTION> onchange event.
{ o = document.forms[0];
  i   = o.menu1.selectedIndex;
  val = o.menu1.options[i].text;
  // alert( val );
  if ( (val.indexOf('--') == -1) && // header, non-menu item
       (val.indexOf('__') == -1) )
  {
    if (i) go(offset+i-1);
  }
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function goM2(offset) // menu <SELECTION> onchange event.
{ o = document.forms[0];
  i = o.menu2.selectedIndex;
  if (i) go(offset+i-1);
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function goM3(offset) // menu <SELECTION> onchange event.
{ o = document.forms[0];
  i = o.menu3.selectedIndex;
  if (i) go(offset+i-1);
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function dhtmlObjShow(_v,id)
{
  newBrowser = (document.getElementById);

  var _a = newBrowser ? document.getElementById(id).style : document.id;

  if (_v == 0)      {  _a.visibility="hidden";  _a.display="none";  }
  else if (_v == 1) {  _a.visibility="visible"; _a.display="block"; }
}

