/* budget form has controls for:
   a# - acct names, c# - acct amts, r# - % in each acct
   ct - amount to be spent, rt - total % allocated */
/* checked with jslint 2009 02 22 */
// set globals here
var maxbud=100;var bct;var brt;var ac=[];var bc=[];var br=[];
var to=99;var max=4; //cookie time(days), # of accts
function isDig(entry){
  validChar='0123456789';strlen=entry.length;
  if(strlen<1){alert('Enter Something!');return false;}
  for(idx=0;idx<strlen;idx++){
    if(validChar.indexOf(entry.charAt(idx))<0){
      alert("Use digits only!");return false;} }
  return true;}
function budPrint(el){
  budObj=document.getElementById(el).innerHTML;
  win=window.open();self.focus();win.document.open();
  win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>');
  win.document.write('body, td {font-family:Verdana;font-size:10pt}');
  win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');
  win.document.writeln(budObj);
  win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
  win.document.close();win.print();win.close();}
function bakeCookie(name,value){
  args=arguments;argc=args.length;expires=(argc>2)?args[2]:null;
  path=(argc>3)?args[3]:null;domain=(argc>4)?args[4]:null;
  secure=(argc>5)?args[5]:false;expDate=new Date();day=24*60*60*1000;
  if(expires){expDate.setTime(expDate.getTime()+expires*day);}
  document.cookie=name+"="+escape(value)+
    ((expires===null)?"":("; expires="+expDate.toUTCString()))+
    ((path===null)?"":("; path="+path))+
    ((domain===null)?"":("; domain="+domain))+
    ((secure===true)?"; secure":"");}
function eatCookieVal(name){
  endstr=document.cookie.indexOf(";",name);
  if(endstr===-1){endstr=document.cookie.length;}
  return unescape(document.cookie.substring(name,endstr));}
function eatCookie(name){
  arg=name+"=";alen=arg.length;clen=document.cookie.length;idx=0;
  while(idx<clen){j=idx+alen;
    if(document.cookie.substring(idx,j)===arg){return eatCookieVal(j);}
    idx=document.cookie.indexOf(" ",idx)+1;if(idx===0){break;}
  }}
function budAbt(){
  alert('Budget.js 2.0 written by John Russell.\n'+
  'It apportions an amount into several accounts.\n'+
  'Feel free to copy for your own use.\n'+
  'Contact ve3ll@rac.ca for more info.');}
function budFocus(){document.getElementById('ct').focus();}
function budReadCookies(){
  c=eatCookie("bct");if(c===undefined){budClr();}
  for(i=1;i<=max;i++){
    ac[i]=eatCookie('a'+i);br[i]=parseFloat(eatCookie('r'+i),10);}
  bct=parseFloat(eatCookie("bct"),10);}
function budWriteCookies(){bakeCookie("bct",bct,to);
  for(i=1;i<=max;i++){
    bakeCookie('a'+i,ac[i],to);bakeCookie('r'+i,br[i],to);} }
function budRead(){
  for(i=1;i<=max;i++){
    ac[i]=document.getElementById('a'+i).value;
    ival=document.getElementById('r'+i).value;
    if(!isDig(ival)){ival=0;}
    br[i]=parseFloat(ival,10);}
  ival=document.getElementById('ct').value;
  if(!isDig(ival)){ival=maxbud;}
  bct=parseFloat(ival,10);}
function budWrite(){ // sets form display
  for(i=1;i<=max;i++){
    document.getElementById('c'+i).style.background='yellow';
    document.getElementById('a'+i).value=ac[i];
    document.getElementById('c'+i).value=bc[i];
    document.getElementById('r'+i).value=br[i];}
  document.getElementById('ct').value=bct;
  document.getElementById('rt').value=brt;}
function budRound(x,y){s=Math.pow(10,y);x=Math.round(x*s)/s;
  x+='';s=x.indexOf('.');if(s==-1){x+=".000";}else{x+="000";}
  s=x.indexOf('.');x=x.substr(0,s+3);return x;}
function budCalc(){budRead();brt=0;
  for(i=1;i<=max;i++){brt+=br[i];bc[i]=budRound(bct*br[i]/100,2);}
  back='yellow';if(brt>100){back='red';}
  document.getElementById('rt').style.background=back;
  budWriteCookies();budWrite();}
function budClr(){for(i=1;i<=max;i++){bc[i]=0;br[i]=0;}
  bct=maxbud;brt=100;br[1]=40;br[2]=30;br[3]=15;br[4]=15;
  budWrite();budWriteCookies();}
function budReset(){for(i=1;i<=max;i++){ac[i]="Account Name";}
  budWrite();budWriteCookies();}
function budInit(){budReadCookies();budWrite();budCalc();}
