<!--
// Login functions
function pg_login(objU, objP, objC, xml_script) {
  user_cleanup(logReply,objU, objP)
  cmdstr = xml_script+'?user='+objU.value+'&pass='+objP.value+'&remember='+objC.checked
  user_get(cmdstr)
}

function logReply(reply) {
  var stat = (xml_tag_value(reply,"logstat"))
  if ( (stat==null) || (stat=='') ) {
    var msgO = document.getElementById('logMsg')
    EnterCheck = log_ckF
    msgO.style.display = 'inline'
    if (params.args) {
      params.args[2].value = ''; // clear password field
      ErrorFlag(params.args[1], true)
      ErrorFlag(params.args[2], true)
    }
  } else {
    params.chain = function() {html_get('acct_list.php',div_load);};
  }
}

function log_ckF () {
  var pwd = document.getElementById("upwd");
  var ubox = document.getElementById("unm")
  if (ubox.value.length==0) {
    if (pwd.value.length > 0) {ubox.focus();}
    return false
  } else {
    if (pwd.value.length == 0) {
      var err = ''
      try { pwd.focus(); }
      catch(err) {alert(err)}
      return false
    } else {
      EnterCheck = void 0
      document.getElementById("signinButton").click()
      return true
    }
  }
}

function logout() {
  html_get('logout.php', div_load)
}

//DIV loading functions
function div_load(reply) {
  var stat,dest,sp,lp,up,ap,cp
  lp = document.getElementById("logout")
  sp = document.getElementById("status")
  up = document.getElementById("users")
  ap = document.getElementById("accts")
  cp = document.getElementById("customers")

  if ((stat=xml_tag_value(reply,'logstat'))!=null) {
    if (stat=='') {
      lp.style.display = 'none'
      sp.innerHTML = ''
    } else {
      lp.style.display='inline'
      sp.innerHTML = stat
    }
    if (stat=='Administrator') {
      up.style.display = 'inline'
      ap.style.display = 'inline'
      cp.style.display = 'inline'
    } else {
      up.style.display = 'none'
      ap.style.display = 'none'
      cp.style.display = 'none'
    }
  }
  if ((dest=xml_tag_value(reply,'target'))==null) dest = 'main'
  stat=strip_tags(http_req.responseText,"markup")
  if (stat.length>0)  document.getElementById(dest).innerHTML = stat
  if ((stat=xml_tag_value(reply,'js1'))!=null) eval(stat)
}

//This function pair activates a small info <div>, and fills it with user info.
function show_info(aObj,tid,e) {
  user_cleanup(show_res,aObj)
  cmdstr = 'tx_descript.php?tid='+tid
  user_get(cmdstr)
}

function show_res(reply) {
  var p = params.args[1]
  while (!(p.previousSibling) && p.nodeType!=9) p = p.parentNode
  while (p != null) {
    if ((p.nodeType==1) && (p.className =="hidBox")) break;
    p = p.previousSibling
  }
  if (p) {
    params.args[0] = p
    p.innerHTML = strip_tags(http_req.responseText,"markup")
    p.style.display = 'block'
  } else  alert('No DIV found to put the data') 
}

//These functions show and hide objects.

function showObj(obj,how) {
  obj.style.display = how
}

function hideObj(obj) {
  obj.style.display = 'none'
}

function swapObj(objShow,objHide,how) {
  showObj(objShow, how)
  hideObj(objHide)
}


//Page-specific functions.  We need to load them all at the outset.
// index.php
// This checkin function queries the server to see if there is stuff to do.
//  if so, checkin.php chains to another page for the reply.  So far, this is only
//  used at initial index page load, and after a browser POST operation (trx_edit.php)
function checkCont() {
  html_get('checkin.php',div_load)
}

// acct_list.php
function aEdit(aid,aname) {
  html_get('acct_edit.php',div_load,['aid','aname'],[aid,aname])
}

function acctDel(aid,flag) {
  html_post('acct_delete.php',div_load,['aid','delF'],[aid,flag]);
}

// acct_edit.php

function aEdit_qual(reply) {
  if (xml_tag_value(reply,"allOK")==1) {
    params.chain = function() {html_get('acct_list.php',div_load);};
  } else {
  var p = document.AcctF
  ErrorFlag(p.aname,Eget(reply,"aname"))
  ErrorFlag(p.acurrency,Eget(reply,"acurrency"))
  ErrorFlag(p.adescript,Eget(reply,"adescript"))
  document.getElementById("errors").innerHTML = strip_tags(http_req.responseText,"err_rec")
  }
}

// acct_stat.php
// on Details button
function acList() {
  var url = ''
  url = 'txm_list.php?'+args_prep();
  window.open(url,"listing")
}

// on Date change, Page request, or Show button
function acStat(tagstr) {
  var rqstr
  rqstr = args_prep()
  if (tagstr.length>0) rqstr += ((rqstr.length>0)? '&':'') + tagstr
  user_cleanup(div_load)
  user_get('acct_stat.php?'+rqstr)
}

// on Page link
function acPage(page) {
  acStat('page='+page)
}

// on 'Show Selected' or 'Show all' button
function acSel(onoff) {
  acStat('sel='+onoff)
}

// called to display or clear field error indicators
function acStat_qual(reply) {
  var p = document.getElementById('aFirst')
  ErrorFlag(p,Eget(reply,"aFirst"))
  p = document.getElementById('aLast')
  ErrorFlag(p,Eget(reply,"aLast"))
  document.getElementById("errors").innerHTML =  strip_tags(http_req.responseText,"err_rec")
}

// generate ON/OFF and date parameter list
function args_prep() {
  var str='',p,aFirst,aLast
  if (p = document.getElementById('aFirst')) aFirst = p.value
  else aFirst = '';
  if (p = document.getElementById('aLast')) aLast = p.value
  else aLast = ''
  str = aListSel('acctsel')
  if (aFirst.length>0) str = nvAdd('aFirst',aFirst,str)  
  if (aLast.length>0) str = nvAdd('aLast',aLast,str)  
  return str
}

// returns ON/OFF list from checkboxes
function aListSel(fname) {
  var chks, on, off, p, str
  str = ''
  chks = document.getElementsByName(fname)
  for (i=0;i<chks.length; i++) {
    p = chks[i]
    n2 = (p.checked)? 'on[]': 'off[]'
    str =  nvAdd(n2,p.value,str)
  }
  return str
}

// returns str, extended by one name/value pair
function nvAdd(nam,val,str) {
  var regexp = /%20/g
  var lstr = str
  if (lstr.length>0) lstr += '&'
  lstr += nam + '=' + encodeURIComponent(val).replace(regexp,'+')
  return lstr
}
// user_list.php

function userDel(uid,flag) {
  html_post('user_delete.php',div_load,['uid','delF'],[uid,flag]);
}

// user_edit.php

function uEdit_qual(reply) {
  if (xml_tag_value(reply,"allOK")==1) {
    params.chain = function() {html_get('user_list.php',div_load);};
  } else {
  var p = document.UserF
  ErrorFlag(p.loginName,Eget(reply,"loginName"))
  ErrorFlag(p.access,Eget(reply,"access"))
  ErrorFlag(p.realname,Eget(reply,"realname"))
  ErrorFlag(p.pw1,Eget(reply,"passwd"))
  p.pw1.value=''
  ErrorFlag(p.pw2,Eget(reply,"passwd"))
  p.pw2.value=''
  document.getElementById("errors").innerHTML =  strip_tags(http_req.responseText,"err_rec")
  }
}

// cust_list.php

function custDel(cid,flag) {
  html_post('cust_delete.php',div_load,['cid','delF'],[cid,flag]);
}

// List recent transactions
// trx_edit.php, trx_delete.php, acct_list.php, trx_list.php
function trxList(aid,aname,order,lDateID) {
  var lDate
  if (lDateID) {
    if ((p=document.getElementById(lDateID))!=null) lDate = p.value
    else lDate = lDateID;
  } else lDate = ''
  html_get('trx_list.php',div_load,['aid','aname','order','lDate'],[aid,aname,order,lDate])
}

// trx_list.php
function trxEdit(tid,aid,aname,ttype,lDate) {
  html_get('trx_edit.php',div_load,['tid','aid','aname','txType','lDate'],[tid,aid,aname,ttype,lDate])
}

function trxList_qual(reply) {
  var p = document.getElementById('lDate')
  ErrorFlag(p,Eget(reply,"lDate"))
  document.getElementById("errors").innerHTML =  strip_tags(http_req.responseText,"err_rec")
}

// trx_edit.php
function trxEdit_qual(reply) {
  var p = document.TrxF
  ErrorFlag(p.txType,Eget(reply,"txType"))
  ErrorFlag(p.tdate,Eget(reply,"tdate"))
  ErrorFlag(p.amount,Eget(reply,"amount"))
  ErrorFlag(p.vat_amt,Eget(reply,"vat_amt"))
  ErrorFlag(p.vat_rate,Eget(reply,"vat_rate"))
  ErrorFlag(p.trans_amt,Eget(reply,"trans_amt"))
  ErrorFlag(p.custName,Eget(reply,"custN"))
  ErrorFlag(p.cid,Eget(reply,"custS"))
  ErrorFlag(p.tCustContact,Eget(reply,"cContact"))
  var cClr = Eget(reply,"cLoc")
  ErrorFlag(p.tLocation,cClr)
  if (cClr) document.TrxF.tLocation.selectedIndex = -1
 document.getElementById("errors").innerHTML =  strip_tags(http_req.responseText,"err_rec")
}

function trxDel(tid,aid,aname) {
  html_post('trx_delete.php',div_load,['tid','aid','aname'],[tid,aid,aname]);
}

function trxFileSel(flg) {
  var fsel = document.getElementById('file_in').style
  var fstat = document.getElementById('file_stat').style
  if (flg) {
    fsel.display = 'block'
    fstat.display = 'none'
  } else {
    fsel.display = 'none'
    fstat.display = 'block'
  }
}

function custInfo(selObj,newF) {
  if (newF>0) {
    var cid = selObj.value
    if (cid>0) {
      user_cleanup(custInfoRet)
      user_get('cust_info.php?cid='+cid);
    } else {
      var p = document.TrxF
      p.tCustContact.value=''
      p.tComment.value = ''
      p.tLocation.selectedIndex = -1
    }
  }
}

function cInputEnable() {
  swapObj(document.getElementById('custSel'), document.getElementById('custInp'),'block')
  document.TrxF.tLocation.disabled = false
}

function custInfoRet(reply) {
  var p = document.TrxF
  p.tCustContact.value = xml_tag_value(reply,"txCustContact")
  p.tComment.value = xml_tag_value(reply,"tComment")
  selInit(p.tLocation,xml_tag_value(reply,"tLoc"))
}

function selInit(obj,opt) {
  var lp = obj.firstChild
  var i = 0
  while (lp !=null) {
    if (lp.nodeType==3) {
      lp = lp.nextSibling
    } else {
      if (lp.value == opt) {
        obj.selectedIndex = i
        break
      }
      lp = lp.nextSibling
      i++
    }
  }
}

function cVis(cTy,clrF) {
  var tc = document.TrxF.tCustContact
  var cnam = document.getElementById('custInp')
  var csel = document.getElementById('custSel')
  switch (cTy) {
  case "0": // Existing
    swapObj(csel,cnam,'block')
    showObj(tc,'block')
break;
  case "1": // New
    swapObj(cnam,csel,'block')
    showObj(tc,'block')
    break;
  case "2": // No Info
    hideObj(csel)
    hideObj(cnam)
    hideObj(tc)
  }
  if (clrF) {
    document.getElementById('cidSel').selectedIndex = -1
    document.getElementById('cNam').value = ''
    tc.value = ''
    document.TrxF.tComment.value=''
    document.TrxF.tLocation.selectedIndex = -1
  }
}

function custSrch(obj) {
  var regexp = /%20/g
  user_cleanup(cSrchReply)
  user_get('cust_info.php?srch='+encodeURIComponent(obj.value).replace(regexp,'+'))
}

function cSrchReply (reply) {
  document.TrxF.cid.value = xml_tag_value(reply,'cid')
  custInfoRet(reply)
}

//cust_edit.php
function ceInit(cid) {
 if (cid==0) document.CustF.cLoc.selectedIndex=-1
}

function cEdit_qual(reply) {
  if (xml_tag_value(reply,"allOK")==1) {
    params.chain = function() {html_get('cust_list.php',div_load);};
  } else {
  var p = document.CustF
  ErrorFlag(p.cName,Eget(reply,"cName"))
  ErrorFlag(p.cLoc,Eget(reply,"cLoc"))
  ErrorFlag(p.cContact,Eget(reply,"cContact"))
  document.getElementById("errors").innerHTML =  strip_tags(http_req.responseText,"err_rec")
  }
}

//-->

