///////////////////////
// Script Constants:
//

var delay=      2000;   // How soon is the first timer set to trigger.
var recycle=    "Off";  // Do we recycle?  (i.e. Reset the timer each time)
var refresh=    60000;  // Refresh timer.  If never set, it's an hour
var timer=      false;  // This is the active timer handle;
var msgTimer=   false;  // This is the timer handle for messages
var hurry=      true;   // This is set when we want to process right away.
var lastCode=   "";     // This is the last biff Code received.
var keepLast=   false;  // Keep the last received code.
var helpIsOn=   false;  // Is help being shown?


//////////////////////////////////////
// Get the value of a field by name:

function gVal(frm, ele){
  return frm.elements[ele].value;
}


////////////////////////////////////////////////////////////////////////////////
// Get text from a text field, add to listbox, delete text field, and refocus:
//

function addOptionFromField(frm, selw, txtw){
  var t=document.forms[frm].elements[txtw].value;
  if(t!=""){
    addOption(frm, selw, t, t, 0, 0);
    document.forms[frm].elements[txtw].value="<email>";
    document.forms[frm].elements[txtw].style.color="#bbbbbb";
//  document.forms[frm].elements[txtw].insert=0;
    
  }
  document.forms[frm].elements[txtw].focus();
}


////////////////////////////////////// 
// Add an option to a select field:
//

function addOption(frm, wid, t, v, d, s){
  var w=document.forms[frm].elements[wid];
  var l=w.length;
  
  if(v=='') v=t;
  w[l]=new Option(t, v, d, s);
}


//////////////////////////////////////////
// Delete a selected item in a listbox:
//

function delSelectedOption(frm, selw){
  var w=document.forms[frm].elements[selw];
  var s=w.selectedIndex;
  var l=w.length;
  if(s>-1) w.remove(s);
  if(s>=l-1) s--;
  if(s>-1) w[s].selected=true;
}


///////////////////////////////////////////////////////////////////////////
// Check for new mail, and replace the indDiv portion of the document:
//

function check4Mail() {
  stopTimer();
  stopMsgTimer();
  if(recycle=="On"){
    buildBiffArgs("beMode", "check");
    buildBiffArgs("hurry", hurry);
    ajaxPost(indDiv, backend+"?ck", true, argBuf);
    hurry=false;
    timer=setTimeout("check4Mail()", refresh);
  }
}


//////////////////////////////////////////////
// Start the Event timer to check for mail:
//

function startTimer() {
  stopTimer();
  if(recycle=="On") timer=setTimeout("check4Mail()", delay);
}


//////////////////////////////////////////
// Stop the timer while editing things:
//

function stopTimer() {
  if(timer) clearTimeout(timer);
  timer=false;
}

function stopMsgTimer(){
  if(msgTimer) clearTimeout(msgTimer);
  msgTimer=false;
}

function clearBiffDiv(){
  lastCode="";
  document.getElementById(indDiv).innerHTML=lastCode;
  setBrowserTitleInd('');
}

function setBrowserTitleInd(str){
  var title=document.title.replace(new RegExp('^('+titleRe+') '), '');
  if(str=='') document.title=title;
  else document.title=str+' '+title;
}


/////////////////////////////////////////////////////////////////////
// Get the login page from the server, and plug it into loginDiv:
//

function showLogin() {
  if(gateClosed==true) return;
  stopMsgTimer();
  buildBiffArgs("beMode", "showLogin");
  document.body.style.cursor="wait";
  ajaxPost(loginDiv, backend+"?shL", false, argBuf);
  startTimer();
}


function renderHelp(helpMode) {
  if(gateClosed==true) return;
  helpIsOn=helpMode;
  backend2=(helpIsOn==true) ? helpBackend : xHelpBackend;

  if(helpMethod=="replace") {
    buildBiffArgs("beMode", "showHelp");
    document.body.style.cursor="wait";
    ajaxPost(helpDiv, backend2, false, argBuf);
    return false;
  } else {
    //  popup a window
    if(helpIsOn==true) window.open('','biffHelp', 'top=0,left=0,width=400,height=250,directories=0,hotkeys=0,personalbar=0,toolbar=0,menubar=0,resizable=1,scrollbars=1,status=0,location=0');
    return true;
  }
}

/////////////////////////////////////////////////////////
// Build a buffer to hold the post field values:
//

function buildBiffArgs(field, val){
  if(field=="beMode") buildArgs("", "");
  
  buildArgs(field, val);

  if(field=="beMode") buildArgs("biffId", biffId);
  return val;
}


//////////////////////////////////////////////////////////////
// Get the Biff Settings page, replace document loginDiv:
//

function showConfig() {
  if(gateClosed==true) return;
  stopTimer();
  stopMsgTimer();
  clearBiffDiv();

  var frm=document.forms['implogin'];
  buildBiffArgs("beMode", "showConfig");
  buildBiffArgs("account", gVal(frm, "imapuser"));
  buildBiffArgs("password", gVal(frm, "pass"));
  document.body.style.cursor="wait";
  ajaxPost(loginDiv, backend+"?shB", false, argBuf);
}


///////////////////////////////////////////////
// Get the changes made to the biff Settings:
//

function saveBiff() {
  if(gateClosed==true) return;
  var frm = document.forms['biffForm'];
  buildBiffArgs("beMode", "save");
  buildBiffArgs("account",  gVal(frm, "account"));
  buildBiffArgs("password", gVal(frm, "password"));
  refresh=buildBiffArgs("refresh", gVal(frm, "refresh"));
  buildBiffArgs("keepPswd", gVal(frm, "keepPswd"));

  // Formulate listbox items into a delimited string:
  var spec="";
  var w=frm.elements['specific'];
  var l=w.length;
  for(i=0; i<w.length; i++) spec+=w[i].value+"|";
  buildBiffArgs("specific", spec);

  var uspe = (frm.elements['useSpec'].checked==true) ? 1 : 0;
  if(l==0) uspe=0; // If there is no listbox items, then turn off using it:
  buildBiffArgs("useSpec", uspe);

  actn=buildBiffArgs("action", gVal(frm, "action"));

  recycle=(actn=="Set") ? "On" : actn;
  stopMsgTimer();
  if(recycle=="Off") lastCode="";

  buildBiffArgs("makeDft", gVal(frm, "makeDft"));
  document.body.style.cursor="wait";
  ajaxPost(loginDiv, backend+"?svB", false, argBuf);
  startTimer();
}


function toggle(){
  if(gateClosed==true) return;
  var frm = document.forms['implogin'];
  buildBiffArgs("beMode", "toggle");
  buildBiffArgs("account", gVal(frm, "imapuser"));
  buildBiffArgs("password", gVal(frm, "pass"));
  buildBiffArgs("curr", recycle);
  document.body.style.cursor="wait";
  ajaxPost(loginDiv, backend+"?toggl", false, argBuf);
  stopTimer();
}
