// Next 2 routines for backwards compatibility with the older DKWebServe_Adv.exe
function clrzero(qq)
{
  var fmel=document.dkwsaqscr["z0op"+qq];
  if (fmel) if (fmel.checked) fmel.checked=false;
  return true;
}

function clrsetzero(qq,rr)
{
  var ii,fmel;
  for (ii=1;ii<=rr;++ii)
  {
    fmel=document.dkwsaqscr["z"+String(ii)+"op"+qq];
    if (fmel) if (fmel.checked) fmel.checked=false;
  }
  return true;
}

function getintnlval(el)
{
  var att=el.getAttribute("MJVAL");
  return (att)? att: el.value;
}

function mjdspchstypd(qq,rr)
{
  var el=document.getElementById("CHSTYPD_"+qq);
  chty=document.dkwsaqscr[qq].value;
  if (rr<chty.length)
  {
    chty=chty.substr(0,rr); document.dkwsaqscr[qq].value=chty;
    alert("Maximum Length Reached");
  }
  if (el) el.innerHTML="Characters Remaining = "+String(rr-chty.length)+"<BR>";
  if (!document.dkwsaqscr[qq].disabled) document.dkwsaqscr[qq].focus();
  return userinput(qq);
}

function clch(qq)
{
  var ii,el=document.getElementById("NICEWINANLYS2_BRITE"),dspel=document.getElementById("colrchngr");
  if (el && dspel) for (ii=0;ii<el.length;++ii)
    if (el.options[ii].selected) { dspel.style.backgroundColor=el.options[ii].style.backgroundColor; break;}
  el=document.getElementById("NICEWINANLYS2_BRITE2"); dspel=document.getElementById("colrchngr2");
  if (el && dspel) for (ii=0;ii<el.length;++ii)
    if (el.options[ii].selected) { dspel.style.backgroundColor=el.options[ii].style.backgroundColor; break;}
  return userinput(qq);
}

function clrchks(qq,hw,vl)
{
  var jj=-1,el=document.dkwsaqscr[qq],elop,isin;
  if (el) if (el[0]) while (true)
  {
    elop=el[++jj]; if (!elop) break;
    if (elop.checked)
    {
      isin=(vl.indexOf(","+elop.value+",")>=0); if ((!hw && isin) || (hw && !isin)) elop.checked=false;
    }
  }
  return userinput(qq);
}

function changeop(qq,op,chg)
{
  var jj=-1,el=document.dkwsaqscr[qq],elop,lel;
  if (op.charAt(0)!=",") op=","+op;
  if (op.charAt(op.length-1)!=",") op+=",";
  if (el) if (el[0]) while (true)
  {
    if (chg==7 || chg==8) elop=el.options[++jj]; else elop=el[++jj];
    if (!elop) break;
    if (op.indexOf(","+getintnlval(elop)+",")>=0)
    {
      if (chg==1) elop.disabled=true;
      else if (chg==2) { elop.disabled=false; lel=document.getElementById("LBL"+elop.id); if (lel) lel.disabled=false;}
      else if (chg==3) elop.checked=true;
      else if (chg==4) elop.checked=false;
      else if (chg==5) document.getElementById("TD"+elop.id).style.visibility="hidden";
      else if (chg==6) document.getElementById("TD"+elop.id).style.visibility="visible";
      else if (chg==7) elop.selected=true;
      else if (chg==8) elop.selected=false;
    }
  }
  return true;
}

function getqlbl(qid)
{
  if (qid.charAt(0)=="z" && qid.substr(4,2)=="op") return qid.substr(6);
  return qid;
}

function createCookie(name,value,days)
{
  var expires="",date;
  if (days)
  {
    date=new Date(); date.setTime(date.getTime()+days*24*60*60*1000);
    expires="; expires="+date.toGMTString();
  }
  document.cookie=name+"="+value+expires+"; path=/";
  return true;
}

function readCookie(name)
{
  var nameEQ=name+"=",ca=document.cookie.split(";"),ii,cc;
  for(ii=0;ii<ca.length;++ii)
  {
    cc=ca[ii]; while (cc.charAt(0)==" ") cc=cc.substr(1);
    if (cc.indexOf(nameEQ)==0) return cc.substr(nameEQ.length);
  }
  return "";
}

function eraseCookie(name)
{
  createCookie(name,"",-1);
  return true;
}

function getonscreencode(qlabel)
{
  return getintansw(qlabel,false);
}

function getanswer(qlabel)
{
  return getintansw(qlabel,true);
}

function getintansw(qlabel,intnl)
{
  var jj=-1,anw="",el=document.dkwsaqscr[qlabel],elop;
  if (!el) el=document.dkwsaqscr[qlabel.toUpperCase()];
  if (!el) return ""; else if (!el[0]) return el.value;
  if (el.options) while (true) { elop=el.options[++jj]; if (!elop) break; else if (elop.selected) anw+=","+elop.value;}
  else while (true)
  {
    elop=el[++jj]; if (!elop) break;
    if (elop.checked) { if (intnl) anw+=","+elop.getAttribute("MJVAL"); else anw+=","+elop.value;}
  }
  if (anw.length>0) { if (el[0].type=="checkbox") anw+=","; else anw=anw.substr(1);}
  return anw;
}

function quesasval(lbl)
{
  var vl=parseInt(getanswer(lbl));
  if (isNaN(vl)) vl=0;
  return vl;
}

function codetest(lbl,cdes,allin)
{
  var karns=getanswer(lbl.toUpperCase());
  if (cdes.length==0 || karns.length==0) return false;
  var rng,arans=karns.split(','),artst=cdes.split(','),zq='',ii,jj,kk,mm;
  if (!allin) allin=false; else allin=true;
  for (ii=0;ii<artst.length;++ii)
  {
    if (artst[ii].length==0) continue;
    rng=artst[ii].split('-'); kk=parseInt(rng[0]); if (isNaN(kk)) continue;
    mm=kk; if (rng.length>1) { mm=parseInt(rng[1]); if (isNaN(mm)) continue;}
    if (mm>=kk) for (jj=kk;jj<=mm;++jj) zq+=','+String(jj);
  }
  if (zq.length==0) return false; else zq+=',';
  for (ii=0;ii<arans.length;++ii)
  {
    if (arans[ii].length==0) continue;
    if (zq.indexOf(','+arans[ii]+',')>-1) { if (!allin) return true;}
    else if (allin) return false;
  }
  return allin;
}

function formcooky(lbls)
{
  var ckstr="",ii,splt=lbls.split(",");
  for (ii=0;ii<splt.length;++ii) ckstr+="&"+splt[ii]+"="+getanswer(splt[ii]);
  return createCookie("DKWSA_ANSWS",ckstr.substr(1),1);
}

function strit(str)
{
  if (str.indexOf("'")==-1) return "'"+str+"'";
  if (str.indexOf("\"")==-1) return "\""+str+"\"";
  return "["+str+"]";
}

function subxp(fd,nt,st,vl1,vl2)
{
  var trv="",l1=vl1.length,l2=vl2.length;
  if (!nt && l1==0 && l2==0) return "";
  if (nt && l1==0 && l2==0) return " .AND. !EMPTY("+fd+")";
  if (st==2 || st==4 || st==5) { vl1=strit(vl1); vl2=strit(vl2);}
  if (st==2 || st==3 || (st==4 && fd.toUpperCase().indexOf("TIME")>-1))
  {
    if (l1>0 && l2>0 && vl1==vl2) trv=fd+"="+vl1;
    else
    {
      if (l1>0) trv=fd+">="+vl1;
      if (l1>0 && l2>0) trv+=" AND ";
      if (l2>0) trv+=fd+"<="+vl2;
    }
  }
  else
  {
    if (l1>0)
    {
      if (st==5 || vl1.indexOf(" OR ")>=0 || vl1.indexOf("*")>=0 || vl1.indexOf("?")>=0 || vl1.indexOf("mjre:")>=0)
        trv="''!=MJFINDTEXT("+fd+","+vl1+")";
      else trv=vl1.toLowerCase()+" $ LOWER("+fd+")";
    }
    if (l1>0 && l2>0) trv+=" AND ";
    if (l2>0)
    {
      if (st==5 || vl2.indexOf(" OR ")>=0 || vl2.indexOf("*")>=0 || vl2.indexOf("?")>=0 || vl2.indexOf("mjre:")>=0)
        trv+="''!=MJFINDTEXT("+fd+","+vl2+")";
      else trv+=vl2.toLowerCase()+" $ LOWER("+fd+")";
    }
  }
  if (nt) trv="!("+trv+")";
  return " .AND. "+trv;
}

function getxp()
{
  if (!document.dkwsaqscr.elements) return "";
  var ii,el,rv="",st,fd,cl,lnt,lvl1;
  for (ii=0;ii<document.dkwsaqscr.elements.length;++ii)
  {
    el=document.dkwsaqscr.elements[ii];
    if (el.name=="DBEXPRESSION")
    {
      if (el.value.length>0) rv+=" .AND. "+el.value;
      continue;
    }
    if (el.name.substr(0,3)!="FLD" || el.name.charAt(4)!="-") continue;
    st=parseInt(el.name.charAt(3)); if (st<1 || st>5) continue;
    fd=el.name.substr(5);
    if (st==1)
    {
      if (el.checked) { if (el.value=="1") rv+=" AND "+fd; else rv+=" AND !"+fd;}
      continue;
    }
    cl=parseInt(fd.charAt(fd.length-1)); fd=fd.substr(0,fd.length-2); if (cl<1 || cl>3) continue;
    if (cl==3) lnt=el.checked;
    else if (cl==1) lvl1=el.value;
    else rv+=subxp(fd,lnt,st,lvl1,el.value);
  }
  return rv.substr(7);
}

function dtasval(dtstr)
{
  var dd=parseInt(dtstr.split("/")[0],10),mm=parseInt(dtstr.split("/")[1],10),yy=parseInt(dtstr.split("/")[2],10);
  if (isNaN(dd) || isNaN(mm) || isNaN(yy) || dd>31 || mm>12 || dd<1 || mm<1 || yy<1 || yy>9999) return -1;
  return yy*10000+mm*100+dd;
}

function mouseX(evt)
{
  if (evt.pageX) return evt.pageX;
  if (evt.clientX) return evt.clientX+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft);
  return 0;
}

function mouseY(evt)
{
  if (evt.pageY) return evt.pageY;
  if (evt.clientY) return evt.clientY+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop);
  return 0;
}

var TimeToFade=700.0,efwd=0,efht=0,efcts="",dsttop=0,orgtop=0;

function usingie()
{
  return (typeof(window.opera)=='undefined' && navigator.appName.toLowerCase().indexOf("internet explorer")>=0);
}

function fade(eid,efx,tmy,gvw,gvh)
{
  var el=document.getElementById(eid),wsv;
  if (!el) return false;
  if (!el.FadeState)
  {
    if (!gvw || !gvh)
    {
      wsv=el.style.display; el.style.visibility="hidden"; el.style.display="block";
      efwd=el.offsetWidth+16; efht=el.offsetHeight; el.style.display=wsv; el.style.visibility="visible";
    }
    else { efwd=gvw+16; efht=gvh;}
    if (!usingie()) efht-=12;
    efcts=efx.toLowerCase(); TimeToFade=tmy; el.FadeState=(el.style.display=="none")? -2 : 2;
  }
  else { el.style.height=efht+"px"; el.style.width=efwd+"px";}
  if (el.FadeState==1 || el.FadeState==-1) { el.FadeState=-el.FadeState; el.FadeTimeLeft=TimeToFade-el.FadeTimeLeft;}
  else
  {
    el.FadeState=(el.FadeState==2) ? -1 : 1; el.FadeTimeLeft=TimeToFade;
    setTimeout("animateFade("+new Date().getTime()+",'"+eid+"')",33);
  }
  return true;
}

function animateFade(lastTick,eid)
{  
  var curTick=new Date().getTime(),newOpVal;
  var elapsedTicks=curTick-lastTick,el=document.getElementById(eid);
  if (el.FadeTimeLeft<=elapsedTicks)
  {
    el.style.display=(el.FadeState==1) ? "block" : "none";
    if (efcts.indexOf("opacity")>=0)
    {
      el.style.opacity=(el.FadeState==1) ? "1" : "0"; el.style.filter="alpha(opacity = "+((el.FadeState==1) ? "100" : "0")+")";
    }
    if (efcts.indexOf("size")>=0)
    {
      el.style.height=(el.FadeState==1) ? efht+"px" : "0px"; el.style.width=(el.FadeState==1) ? efwd+"px" : "0px";
    }
    if (efcts.indexOf("slide")>=0) el.style.top=(el.FadeState==1) ? dsttop+"px" : 0-efht+"px";
    scrollwindel(el); el.FadeState=(el.FadeState==1) ? 2 : -2; return;
  }
  el.FadeTimeLeft-=elapsedTicks; newOpVal=el.FadeTimeLeft/TimeToFade; if (el.FadeState==1) newOpVal=1-newOpVal;
  if (efcts.indexOf("opacity")>=0) { el.style.opacity=newOpVal; el.style.filter="alpha(opacity = "+(newOpVal*100)+")";}
  if (efcts.indexOf("size")>=0) { el.style.height=(newOpVal*efht)+"px"; el.style.width=(newOpVal*efwd)+"px";}
  if (efcts.indexOf("slide")>=0) el.style.top=(newOpVal*(dsttop-orgtop))+orgtop+"px";
  if (el.FadeState==1) el.style.display="block";
  setTimeout("animateFade("+curTick+",'"+eid+"')",33);
  return true;
}

function getwindowwidth()
{
  if (window.innerWidth) return window.innerWidth;
  return document.getElementsByTagName("body")[0].clientWidth;
}

function getwindowheight()
{
  if (window.innerHeight) return window.innerHeight;
  return document.getElementsByTagName("body")[0].clientHeight;
}

function scrollwindel(el)
{
  var ii=parseInt(el.style.left)+parseInt(el.style.width)-document.body.scrollLeft-getwindowwidth();
  if (ii>0) document.body.scrollLeft=document.body.scrollLeft+ii+40;
  return true;
}

function crinitbox(mode,timems,hrf)
{
  var cow,coh,el=document.getElementById("crslidein");
  if (!el) return false;
  el.style.visibility="hidden"; el.style.display="block"; cow=el.offsetWidth; coh=el.offsetHeight;
  el.style.left=(getwindowwidth()-cow-16)/2+"px"; dsttop=(getwindowheight()-coh)/2; orgtop=-15;
  if (mode.indexOf("slide")<0) orgtop=dsttop; else if (mode.indexOf("size")<0) orgtop=0-coh;
  el.style.display="none"; el.style.visibility="visible"; el.style.top=orgtop+"px";
  fade("crslidein",mode,timems,cow,coh); createCookie("Critical_Slide_In",hrf,99);
  return true;
}

function mjsetinhtmexec(eln,tset)
{
  var xx,ii,odv=document.getElementById(eln);
  if (!odv) return false;
  odv.innerHTML=tset; xx=odv.getElementsByTagName("script"); for (ii=0;ii<xx.length;++ii) eval(xx[ii].text);
  return true;
}

function postxmlhttp(url,lbls,resel)
{
  var self=this;
  if (window.XMLHttpRequest) self.xhreq=new XMLHttpRequest();
  else if (window.ActiveXObject) self.xhreq=new ActiveXObject("Microsoft.XMLHTTP");
  self.xhreq.open("POST",url,true); self.xhreq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  self.xhreq.onreadystatechange=function() { if (self.xhreq.readyState==4) mjsetinhtmexec(resel,self.xhreq.responseText);}
  self.xhreq.send(getquerystring(lbls));
  return true;
}

function postxmlhttp2(url,lbls,resel)
{
  var self=this;
  if (window.XMLHttpRequest) self.xhreq2=new XMLHttpRequest();
  else if (window.ActiveXObject) self.xhreq2=new ActiveXObject("Microsoft.XMLHTTP");
  self.xhreq2.open("POST",url,true); self.xhreq2.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  self.xhreq2.onreadystatechange=function() { if (self.xhreq2.readyState==4) mjsetinhtmexec(resel,self.xhreq2.responseText);}
  self.xhreq2.send(getquerystring(lbls));
  return true;
}

function postxmlhttp3(url,lbls,resel)
{
  var self=this;
  if (window.XMLHttpRequest) self.xhreq3=new XMLHttpRequest();
  else if (window.ActiveXObject) self.xhreq3=new ActiveXObject("Microsoft.XMLHTTP");
  self.xhreq3.open("POST",url,true); self.xhreq3.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  self.xhreq3.onreadystatechange=function() { if (self.xhreq3.readyState==4) mjsetinhtmexec(resel,self.xhreq3.responseText);}
  self.xhreq3.send(getquerystring(lbls));
  return true;
}

function postxmlhttp4(url,lbls,resel)
{
  var self=this;
  if (window.XMLHttpRequest) self.xhreq4=new XMLHttpRequest();
  else if (window.ActiveXObject) self.xhreq4=new ActiveXObject("Microsoft.XMLHTTP");
  self.xhreq4.open("POST",url,true); self.xhreq4.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  self.xhreq4.onreadystatechange=function() { if (self.xhreq4.readyState==4) mjsetinhtmexec(resel,self.xhreq4.responseText);}
  self.xhreq4.send(getquerystring(lbls));
  return true;
}

function postxmlhttp5(url,lbls,resel)
{
  var self=this;
  if (window.XMLHttpRequest) self.xhreq5=new XMLHttpRequest();
  else if (window.ActiveXObject) self.xhreq5=new ActiveXObject("Microsoft.XMLHTTP");
  self.xhreq5.open("POST",url,true); self.xhreq5.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  self.xhreq5.onreadystatechange=function() { if (self.xhreq5.readyState==4) mjsetinhtmexec(resel,self.xhreq5.responseText);}
  self.xhreq5.send(getquerystring(lbls));
  return true;
}

function getquerystring(lbls)
{
  if (lbls.length==0) return "";
  var rvstr="",ii,splt=lbls.split(",");
  for (ii=0;ii<splt.length;++ii) rvstr+="&"+splt[ii]+"="+encodeURIComponent('%'+getanswer(splt[ii]));
  return rvstr.substr(1);
}

function rankupdate(qq)
{
  if (!document.getElementById(qq)) return false;
  window.setTimeout("rnkup('"+qq+"');",70);
  return true;
}

function rnkup(qq)
{
  var ii=0,str="",el,jj,vl,dlm;
  el=document.getElementById(qq+"_RANK_"+String(++ii));
  while (el)
  {
    str+="|"; dlm=""; jj=-1;
    while (el.childNodes[++jj]) if (el.childNodes[jj].getAttribute)
    {
      vl=el.childNodes[jj].getAttribute("MJVL"); if (vl) { str+=dlm+vl; dlm=";";}
    }
    el=document.getElementById(qq+"_RANK_"+String(++ii));
  }
  document.dkwsaqscr[qq].value=str.substr(1); rnkanot(qq);
  return userinput(qq);
}

function rnkanot(qq)
{
  var el=document.getElementById(qq+"_cntrtd"),ii;
  if (el) { ii=el.offsetHeight; el=document.getElementById(qq+"_anottbl"); if (el) el.style.height=ii-2+"px";}
  return true;
}

function docclik(e)
{
  var trg;
  if (!e) e=window.event;
  if (e.which) if (e.which==3) return true;
  if (e.button) if (e.button==2) return true;
  if (e.target) trg=e.target; else if (e.srcElement) trg=e.srcElement;
// mitigate Safari text node problem
  if (trg.nodeType==3) trg=trg.parentNode;
  if (trg.nodeName=="IMG" && trg.parentNode.parentNode.parentNode.parentNode.parentNode.className=="pix")
    window.setTimeout("clikdoc('"+getqlbl(trg.parentNode.parentNode.id.substr(2)).toUpperCase()+"');",100);
  else if ((trg.nodeName=="LABEL" || trg.nodeName=="INPUT") && trg.parentNode.parentNode.parentNode.parentNode.className=="pix")
    window.setTimeout("clikdoc('"+getqlbl(trg.parentNode.id.substr(2)).toUpperCase()+"');",100);
// To make TD "live"
//  else if (trg.nodeName=="TD" && trg.parentNode.parentNode.parentNode.className=="pix")
//    window.setTimeout("document.getElementById('"+trg.id.substr(2)+"').click(); /*clikdoc('"+getqlbl(trg.id.substr(2)).toUpperCase()+"');*/",100);
  return true;
}

function sortsldsel(ov,nv,el,lab,dk)
{
  if (ov!=nv) return true;
  if (ov==-9 && lab) { if (document.dkwsaqscr[lab].value==dk || document.dkwsaqscr[lab].value=="") el.style.visibility="hidden";}
  else if (el.style.visibility=="hidden") el.style.visibility="visible";
  else { el.style.visibility="hidden"; document.dkwsaqscr[lab].value=dk;}
  return true;
}

function addnewchel(towhat,adel)
{
  if (!REDIPS || !towhat) return false;
  if (REDIPS.drag.drop_option=='bucket' && towhat.hasChildNodes()) towhat.insertBefore(adel,towhat.firstChild);
  else towhat.appendChild(adel);
  return true;
}

function mjinnertext(el,vl)
{
  if (!el) return "";
  if (!vl) { if (usingie()) return el.innerText; else return el.textContent;}
  if (usingie()) el.innerText=vl; else el.textContent=vl;
  return true;
}

function patchup()
{
  if (!usingie() || navigator.appVersion.toLowerCase().indexOf("windows")<0) return false;
  var ii,jj,kk,aa=document.getElementsByTagName("LABEL"),onc;
  for (ii=0,jj=aa.length;ii<jj;++ii)
  {
    if (aa[ii].hasChildNodes()) if (aa[ii].childNodes[0].tagName=="IMG")
    {
      aa[ii].childNodes[0].forid=aa[ii].htmlFor; onc=String(document.getElementById(aa[ii].htmlFor).onclick);
      kk=onc.indexOf("{"); mm=onc.lastIndexOf("}"); if (kk<0 || mm<0) onc=""; else onc=onc.substring(kk+1,mm);
      aa[ii].childNodes[0].id=onc;
      aa[ii].childNodes[0].onclick=function(){
        var ee=document.getElementById(this.forid);
        if (this.id.length>0) eval(this.id);
        switch(ee.type)
        {
          case "radio": ee.checked|=1; break;
          case "checkbox": ee.checked=!ee.checked; break;
          case "text": case "password": case "textarea": ee.focus(); break;
        }
      }
    }
  }
  return true;
}

window.setTimeout("patchup();",350);

function fixtblhdr(tbl,ht)
{
  var tbhel=document.getElementById(tbl),cntnr,dv;
  if (!ht) ht=Math.max(getwindowheight()-250,250);
  if (!tbhel) alert("FIXTBLHDR - Table "+tbl+" does not exist."); else if (tbhel.offsetHeight<=ht+50) return false;
  dv=document.createElement("div"); dv.style.height=String(ht)+"px"; dv.style.overflow="auto"; dv.id="div_"+tbl;
  cntnr=tbhel.cloneNode(false); cntnr.id="cntnr_"+tbl; cntnr.style.position='absolute';
  tbhel.parentNode.insertBefore(cntnr,tbhel); tbhel.parentNode.insertBefore(dv,tbhel); dv.appendChild(tbhel);
  window.setTimeout("fxhdsetwids('"+tbl+"');",100);
  return true;
}

function fxhdsetwids(tblid)
{
  var ii,jj,adj,trel,trnod,noddy,thdel=null,tbhel=document.getElementById(tblid),cntnr=document.getElementById("cntnr_"+tblid);
  for (jj=0;jj<tbhel.childNodes.length;++jj) if (tbhel.childNodes[jj].tagName=="THEAD") { thdel=tbhel.childNodes[jj]; break;}
  if (thdel==null) { alert("FIXTBLHDR - Table "+tblid+" needs a THEAD section."); return false;}
  noddy=thdel.cloneNode(true); cntnr.appendChild(noddy);
  for (jj=0;jj<thdel.childNodes.length;++jj) if (thdel.childNodes[jj].tagName=="TR")
  {
    trel=thdel.childNodes[jj]; trnod=noddy.childNodes[jj];
    for (ii=trel.childNodes.length-1;ii>=0;--ii) if (trel.childNodes[ii].tagName=="TD" || trel.childNodes[ii].tagName=="TH")
    {
      adj=mjcomputedstyle(trel.childNodes[ii],"border-right-width",true)+
          mjcomputedstyle(trel.childNodes[ii],"padding-left",true)+
          mjcomputedstyle(trel.childNodes[ii],"padding-right",true);
      trnod.childNodes[ii].style.width=trel.childNodes[ii].offsetWidth-adj+'px';
    }
  }
  ii=(usingie()?0:1); cntnr.style.width=tbhel.offsetWidth+ii+'px';
  return true;
}

function rmvtblfix(tbl)
{
  var cntnr=document.getElementById("cntnr_"+tbl),dv=document.getElementById("div_"+tbl),tbhel=document.getElementById(tbl);
  if (cntnr && dv && tbhel) { cntnr.parentNode.removeChild(cntnr); dv.parentNode.replaceChild(tbhel,dv);}
  return true;
}

function mjwopen(url,innw)
{
  if (!usingie()) { if (innw) window.open(url,''); else window.location=url;}
  else
  {
    var rfr=document.createElement('a');
    rfr.href=url; if (innw) rfr.target="_blank";
    document.body.appendChild(rfr); rfr.click();
  }
  return true;
}

var nwa=new Date(),calinst,xdttm,lasdtpvl='',dvmjcalctl,curcalctl,lsclktp=-999,lsclklf=-999,dtsepa="/";
var mthnamz=["January","February","March","April","May","June","July","August","September","October","November","December"];
var wkdynamz=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

function NewCal(pctl,pfmt)
{
  var lf,tp,sp1,sp2,strd,strm,stry,intm,inty;
  curcalctl=document.getElementById(pctl); if (curcalctl.disabled) return false;
  calinst=new mjcalndor(nwa); calinst.ctl=pctl; calinst.fmt=pfmt.toUpperCase();
  xdttm=curcalctl.value; if (lasdtpvl.length>4 && xdttm.length==0) xdttm=lasdtpvl;
  if (xdttm!="")
  {
    sp1=xdttm.indexOf(dtsepa,0); sp2=xdttm.indexOf(dtsepa,(parseInt(sp1)+1));
    if (calinst.fmt=="DDMMYYYY" || calinst.fmt=="DDMMMYYYY")
    {
      strm=xdttm.substring(sp1+1,sp2); strd=xdttm.substring(0,sp1);
    }
    else if (calinst.fmt=="MMDDYYYY" || calinst.fmt=="MMMDDYYYY")
    {
      strm=xdttm.substring(0,sp1); strd=xdttm.substring(sp1+1,sp2);
    }
    if (isNaN(strm)) intm=calinst.gtmthidx(strm); else intm=parseInt(strm,10)-1;
    if (parseInt(intm,10)>=0 && parseInt(intm,10)<12) calinst.moth=intm;
    if (parseInt(strd,10)<=calinst.gtdysmth() && parseInt(strd,10)>=1) calinst.dyt=strd;
    stry=xdttm.substring(sp2+1,sp2+5); inty=parseInt(stry,10); if (inty>0) calinst.yur=inty;
  }
  if (!document.getElementById('mjcalctl'))
  {
    dvmjcalctl=document.createElement("div");
    dvmjcalctl.style.display="none"; dvmjcalctl.style.filter="alpha(opacity=0)"; dvmjcalctl.style.opacity="0";
    dvmjcalctl.style.backgroundColor="#99F0C0";
    dvmjcalctl.style.border="3px ridge #917431"; dvmjcalctl.style.margin="1px"; dvmjcalctl.style.padding="1px";
    dvmjcalctl.style.position="absolute"; dvmjcalctl.style.overflow="hidden"; dvmjcalctl.style.whiteSpace="nowrap";
    dvmjcalctl.id="mjcalctl"; document.body.appendChild(dvmjcalctl);
  }
  lf=curcalctl.offsetLeft; tp=curcalctl.offsetTop; sp1=curcalctl.offsetParent;
  while (sp1) { lf+=sp1.offsetLeft; tp+=sp1.offsetTop; sp1=sp1.offsetParent;}
  dvmjcalctl.style.left=lf+curcalctl.offsetWidth+26+'px'; dvmjcalctl.style.top=tp+3+'px';
  if (dvmjcalctl.style.display=="none" || (Math.abs(lf-lsclklf)<15 && Math.abs(tp-lsclktp)<15))
    fade('mjcalctl','opacity size',200,150,150);
  lsclklf=lf; lsclktp=tp; dvmjcalctl.innerHTML=""; drawmjcal();
  return true;
}

function drawmjcal()
{
  var celstr,vclhd,vcldt,ii,vdc=0,vfd,mysty;
  mysty="font-size:10pt; font-family:calibri; font-weight:bold; color:black; padding:0px; margin:0px; border:0px; border-collapse:collapse; overflow:hidden; white-space:nowrap;";
  vclhd="<table width=\"100%\" align=\"center\" valign=\"middle\" style=\""+mysty+"\">";
//Month Selector
  vclhd+="<tr><td colspan=7><table width=\"100%\" style=\""+mysty+"\"><tr><td align=\"left\">";
  vclhd+="<span style=\"cursor:pointer; color:blue;\""+hilt("#99F0C0")+" onClick=\"calinst.Decmonth(); drawmjcal();\">&larr;</span>"+calinst.getmthnamz()+"<span style=\"cursor:pointer; color:blue;\""+hilt("#99F0C0")+" onClick=\"calinst.Incmonth(); drawmjcal();\">&rarr;</span></td>";
//Year selector
  vclhd+="<td align=\"right\"><span style=\"cursor:pointer; color:red;\""+hilt("#99F0C0")+" onClick=\"calinst.DecYear(); drawmjcal();\">&larr;</span>"+calinst.yur+"<span style=\"cursor:pointer; color:red;\""+hilt("#99F0C0")+" onClick=\"calinst.IncYear(); drawmjcal();\">&rarr;</span></td></tr></table></td></tr>";
//Week day header
  vclhd+="<tr bgcolor=\"#00BCCD\">";
  for (ii=0;ii<7;++ii) { vclhd+="<td align=\"center\" width=\"14%\">"+wkdynamz[ii].substr(0,2)+"</td>";}
  vclhd+="</tr>";
//Dates
  celstr=new Date(calinst.yur,calinst.moth); celstr.setDate(1); vfd=celstr.getDay(); vcldt="<tr>";
  for (ii=0;ii<vfd;++ii) { vcldt+="<td></td>"; ++vdc;}
  for (ii=1;ii<=calinst.gtdysmth();++ii)
  {
    ++vdc;
    if (ii==nwa.getDate() && calinst.moth==nwa.getMonth() && calinst.yur==nwa.getFullYear())
      celstr=mkcel(ii,true,"#FFFF33");
    else if (ii==calinst.dyt) celstr=mkcel(ii,true,"#FFFF99");
    else if (vdc%7==0) celstr=mkcel(ii,false,"#CCCCFF");
    else if (vdc%7==1) celstr=mkcel(ii,false,"#66CCFF");
    else celstr=mkcel(ii,false,"white");
    vcldt=vcldt+celstr; if (vdc%7==0 && ii<calinst.gtdysmth()) vcldt=vcldt+"</tr><tr>";
  }
  dvmjcalctl.innerHTML=vclhd+vcldt+"</tr></table>";
  return true;
}

function populcalctl(pvl)
{
  lasdtpvl=calinst.fmtdatz(pvl); curcalctl.value=lasdtpvl; fade('mjcalctl','opacity size',200,150,150);
  if (typeof hilitecell=='function') hilitecell(curcalctl.id);
  return true;
}

function mkcel(pvl,phl,pcl)
{
  var colr=(phl)?"black":"navy";
  return "<td bgcolor=\""+pcl+"\" align=\"center\" style=\"cursor:pointer; color:"+colr+";\""+hilt(pcl)+" onClick=\"populcalctl("+pvl+");\">"+pvl+"</td>";
}

function hilt(pcl)
{
  return " onmouseover=\"style.backgroundColor='burlywood';\" onmouseout=\"style.backgroundColor='"+pcl+"';\"";
}

function mjcalndor(pdt,pctl)
{
  this.dyt=pdt.getDate(); this.moth=pdt.getMonth(); this.yur=pdt.getFullYear(); this.ctl=pctl; this.fmt="DDMMYYYY";
  return true;
}

function gtmthidx(shortmthnamz)
{
  var ii;
  for (ii=0;ii<12;++ii) if (mthnamz[ii].substring(0,3).toUpperCase()==shortmthnamz.toUpperCase()) return ii;
  return -1;
}
mjcalndor.prototype.gtmthidx=gtmthidx;

function IncYear()
{
  ++calinst.yur;
  return true;
}
mjcalndor.prototype.IncYear=IncYear;

function DecYear()
{
  --calinst.yur;
  return true;
}
mjcalndor.prototype.DecYear=DecYear;

function Incmonth()
{
  ++calinst.moth; if (calinst.moth>11) { calinst.moth=0; ++calinst.yur;}
  return true;
}
mjcalndor.prototype.Incmonth=Incmonth;

function Decmonth()
{
  --calinst.moth; if (calinst.moth<0) { calinst.moth=11; --calinst.yur;}
  return true;
}
mjcalndor.prototype.Decmonth=Decmonth;

function getmthnamz()
{
  return mthnamz[this.moth];
}
mjcalndor.prototype.getmthnamz=getmthnamz;

function gtdysmth()
{
  var dysmth=[31,28,31,30,31,30,31,31,30,31,30,31];
  if (this.islpyr()) dysmth[1]=29;
  return dysmth[this.moth];
}
mjcalndor.prototype.gtdysmth=gtdysmth;

function islpyr()
{
  if (this.yur%4==0)
  {
    if (this.yur%100==0 && this.yur%400!=0) return false;
    return true;
  }
  return false;
}
mjcalndor.prototype.islpyr=islpyr;

function fmtdatz(pdt)
{
  if (this.fmt=="DDMMYYYY") return (pdt+dtsepa+(this.moth+1)+dtsepa+this.yur);
  else if (this.fmt=="DDMMMYYYY") return (pdt+dtsepa+this.getmthnamz().substr(0,3)+dtsepa+this.yur);
  else if (this.fmt=="MMDDYYYY") return ((this.moth+1)+dtsepa+pdt+dtsepa+this.yur);
  else if (this.fmt=="MMMDDYYYY") return (this.getmthnamz().substr(0,3)+dtsepa+pdt+dtsepa+this.yur);
  return true;
}
mjcalndor.prototype.fmtdatz=fmtdatz;

function resztxa(txaid)
{
  var txa=document.getElementById(txaid);
  if (!txa) return false;
  var aa=txa.value.split("\n"),bb=0,xx;
  for (xx=0;xx<aa.length;++xx) if (aa[xx].length>=txa.cols) bb+=Math.floor(aa[xx].length/txa.cols);
  txa.rows=bb+aa.length;
  return true;
}

function enabrow(rwid)
{
  var arry,el=document.getElementById(rwid);
  if (!el) return false;
  arry=el.getElementsByTagName('INPUT'); for (ii=0;ii<arry.length;++ii) arry[ii].disabled=false;
  arry=el.getElementsByTagName('SELECT'); for (ii=0;ii<arry.length;++ii) arry[ii].disabled=false;
  arry=el.getElementsByTagName('TEXTAREA'); for (ii=0;ii<arry.length;++ii) arry[ii].disabled=false;
  return true;
}

function camelise(str)
{
  var ii,aa=str.split("-"),rv;
  rv=aa[0];
  for (ii=1;ii<aa.length;++ii) rv+=aa[ii].substr(0,1).toUpperCase()+aa[ii].substr(1);
  return rv;
}

function mjcomputedstyle(el,prop,nmr)
{
  var sty,rv="undefined",dfv=document.defaultView;
  if (dfv && dfv.getComputedStyle) { sty=dfv.getComputedStyle(el,''); if (sty) rv=sty.getPropertyValue(prop);}
  else if (el.currentStyle) rv=el.currentStyle[camelise(prop)];
  else return null;
  return nmr ? (parseInt(rv) || 0) : rv;
}

function mothnumb(mstr)
{
  var bij=mstr.toLowerCase().substr(0,3);
  if (bij=='jan') return 0;
  if (bij=='feb') return 1;
  if (bij=='mar') return 2;
  if (bij=='apr') return 3;
  if (bij=='may') return 4;
  if (bij=='jun') return 5;
  if (bij=='jul') return 6;
  if (bij=='aug') return 7;
  if (bij=='sep') return 8;
  if (bij=='oct') return 9;
  if (bij=='nov') return 10;
  if (bij=='dec') return 11;
  return -1;
}

function isitaleapyear(yrry)
{
  var yry=parseInt(yrry);
  if (isNaN(yry)) return false;
  if (yry%4==0)
  {
    if (yry%100==0 && yry%400!=0) return false;
    return true;
  }
  return false;
}

function numbdaysinmoth(mttm,yrry)
{
  var mtm=parseInt(mttm),dyms=[31,28,31,30,31,30,31,31,30,31,30,31];
  if (isNaN(mtm)) return 0;
  if (mtm<0 || mtm>11) return 0;
  if (isitaleapyear(yrry)) dyms[1]=29;
  return dyms[mtm];
}

function thedayofweek(dyyd,mttm,yrry)
{
  var dyd=parseInt(dyyd),mtm=parseInt(mttm),yry=parseInt(yrry),ii,dtfmd;
  if (isNaN(dyd) || isNaN(mtm) || isNaN(yry)) return '';
  dtfmd=new Date(yry,mtm,dyd); ii=dtfmd.getDay();
  if (ii==0) return 'Sunday';
  if (ii==1) return 'Monday';
  if (ii==2) return 'Tuesday';
  if (ii==3) return 'Wednesday';
  if (ii==4) return 'Thursday';
  if (ii==5) return 'Friday';
  if (ii==6) return 'Saturday';
  return '';
}
