function hexify(astr) {
  astr=replaceString(' ','+',astr);
  astr=replaceString(':','%3A',astr);
  astr=replaceString(';','%3B',astr);
  astr=replaceString('"','%22',astr);
  astr=replaceString("'","%27",astr);
  astr=replaceString("&","%26",astr);
  return astr;
}

function unspace(astr) {
  astr=replaceString('+',' ',astr);
  astr=replaceString('%26','&',astr);
  astr=replaceString('%7C','|',astr);
  return astr;
}


function requote(val) {
  val=replaceString('%22','"',val);
  val=replaceString("%27","'",val);
  return val;
}

function colOf(name) {
   icol=-1;jcol=0;
   while ((icol==-1)&&(jcol<c[0].length)) {
     if (c[0][jcol].indexOf(name)==0) {icol=jcol;}
     ++jcol;
   }
   return icol;
}

function replaceString(oldS,newS,fullS) {
  if (oldS>=" ") {
    for (var ik=0; ik<fullS.length; ik++) {
      if (fullS>=" ") {
        if (fullS.substring(ik,ik+oldS.length) == oldS) {
          fullS = fullS.substring(0,ik)+newS+fullS.substring(ik+oldS.length,fullS.length);
        }
      }
    }
    return fullS;
  }
}

function unhex(val) {
  val=replaceString('%20',' ',val);
  val=replaceString('+',' ',val);
  val=replaceString('%3A',':',val);
  val=replaceString('%3B',';',val);
  val=replaceString('%22','"',val);
  val=replaceString("%27","'",val);
  val=replaceString('%26','&',val);
  val=replaceString('%0D%0A','<br>',val);
  val=replaceString('%7C','|',val);
  val=replaceString('%40','@',val);
  return val;
}

function search(fields,forstring) {
  sfieldlist=fields.split('|');
//document.write(fields,forstring,sfieldlist,sfieldlist.length,c.length,sfieldlist[0].toLowerCase(),forstring.toLowerCase());
  for (j=1;j<c.length;++j) {
//document.write(j);
    keep=0;
    for (i=0;i<sfieldlist.length;++i) {
//document.write(j,i,c[j][colOf(sfieldlist[i])],lowercase(c[j][colOf(sfieldlist[i])]));
      if ((c[j][colOf('use')]=='1')&& (c[i][colOf(user)]>' ') &&
        (c[j][colOf(sfieldlist[i])].toLowerCase().indexOf(forstring.toLowerCase())>-1)) {keep=1;}
    }
    if (keep==1) {tnlist[tnlist.length]=j;}
  }
//  tnnumperpage=0;
}

function tab(ispaces) {
  nbsp="";
  for (itab=0;itab<ispaces;++itab) {nbsp+="&nbsp;";}
  return nbsp;
}

function indent(val) {
  val=replaceString('<br>','<br>'+tab(5),val);
  return val;
}


function go2ic(qsval) {
  if (qsval>0) {newqs+='ic='+qsval;recall();}
}

function makeselect(field,label,qsvar) {
  document.write('<small><select onChange="go2ic(this.selectedIndex);">');
  document.write('<option>'+label);
  for (i=1;i<c.length;++i) {
    document.write('<option');
    document.write('>'+unhex(c[i][colOf(field)]));
  }
  document.write('</select></small>');
}

function getqs() {
  qs=window.location.search;
  pathparts=document.URL.split('?');
  if (qs.length>1) {

    qs=unhex(qs.substring(1,qs.length));
    qsargs=qs.split("&");
    for (i=0;i<qsargs.length;++i) {qsargs[i]=qsargs[i].split("=");}
  }
}

function qsOf(avar) {
//document.write(qsargs[0][0]+qsargs[0][1]+'<br>');
  aval="";
  for (i=0;i<qsargs.length;++i) {if (qsargs[i][0]==avar) {aval=qsargs[i][1];}}
  return aval;
}

function recall() {
  document.location=pathparts[0]+"?"+newqs;
}

months=new Array('January','February','March','April','May','June','July','August','September','October','November','December');

function dateOf(YYMMDD) {
  ida=Number(YYMMDD.substring(4,6));
  return ida;
}

function monthOf(YYMMDD) {
  imo=Number(YYMMDD.substring(2,4)-1);
  return months[imo];
}

function yearOf(YYMMDD) {
  return "20"+YYMMDD.substring(0,2);
}

function searchfields(formattext,fnames) {
  document.write('<form>'+formattext);
  document.write('<input type=hidden name=n value="'+fnames+'">');
  document.write('<input name=v type=text size=15>');
  document.write('<input type=submit value=Go>');
  document.write('</form>');
}


function search(fields,forstring) {
  sfieldlist=fields.split('|');
  wrote=0;
  for (j=1;j<c.length;++j) {
    keep=0;
    for (i=0;i<sfieldlist.length;++i) {
      if (c[j][colOf(sfieldlist[i])].toLowerCase().indexOf(forstring.toLowerCase())>-1){keep=1;}
    }
    if (keep==1) {show1(j);++wrote;}
  }
    if (wrote==0){document.write('<center><b><font face="Arial">Sorry, no results for: <i>'+forstring+'</i></font></b></center>');}
}

function showresults() {
  if (qs.length>0){search(unspace(qsargs[0][1]),unspace(qsargs[1][1]));}

}


function makecategoryselect(label) {
  allf=new Array();
  for (i=1;i<c.length;++i) {
//    if (c[i][colOf('itemID')].indexOf("VB")>-1) {
      allf[allf.length]=c[i][colOf('category')].split('^');
//    }
  }
  allf2=new Array();
  for (fi=0;fi<allf.length;++fi) {
    for (fi2=0;fi2<allf[fi].length;++fi2) {
      allf2[allf2.length]=allf[fi][fi2];
    }
  }
  allf2.sort();
  allf3=new Array();
  allf3[0]=allf2[0];
  for (fi=1;fi<allf2.length;++fi) {
    if (allf2[fi]!=allf2[fi-1]) {
      allf3[allf3.length]=allf2[fi];
    }
  }
/*  document.write('<form>'+formattext);
  document.write('<input type=hidden name=n value="'+field+'">');
  document.write('<input name=v type=text size=15>');
*/
  document.write('<small><select onChange="go2category(this.selectedIndex-1);">');
  document.write('<option>'+label);
  for (i=0;i<allf3.length;++i) {
    document.write('<option');
    document.write('>'+unhex(allf3[i]));
  }
  document.write('</select></small>');
//  document.write('<input type=submit value=Go>');
  document.write('</form>');
}

function go2category(iallf3) {
  if (iallf3>=0) {newqs='n=category&v='+hexify(allf3[iallf3]);recall();}
}

function YYMMDD() {
  now=new Date();
  YY=String(Math.floor(now.getFullYear() % 100));if (YY.length<2){YY='0'+YY;}
  MM=String((1+now.getMonth()) % 12); if (MM=="0") {MM="12";};if (MM.length<2){MM='0'+MM;}
  DD=String(now.getDate());if (DD.length<2){DD='0'+DD;}
  YYMMDD=String(YY)+String(MM)+String(DD);
  return YYMMDD;
}

function currentIC(aday) {
  dsort=new Array();idsort=-1
  for (i=1;i<c.length;++i) {dsort[++idsort]=c[i][colOf('YYMMDD')]+'|'+i;}
  dsort.sort();

//  aday=YYMMDD();
  pick=1;
  idsort=0;
  while (idsort<dsort.length) {
    dsorts=dsort[idsort].split('|');
    if (today>=dsorts[0]) {pick=dsorts[1];}
    ++idsort;
  }
  return pick;
}

function listValues(afield,mid,issorted,go2page) {
// lists unique values from one afield, with links to
//   call go2page, appending a search for selected value in afield
//   NOTE: search uses function show1(ic), where ic is the row number
// issorted=0or1; if unsorted, uses order from catalog, using only first of ^-delimited list
//   if sorted, uses all values, including from ^-delimited list
// mid is written after all linked values except the last one;
  allf=new Array();
  for (i=1;i<c.length;++i) {
      allf[allf.length]=c[i][colOf(afield)].split('^');
  }
  allf2=new Array();
  for (fi=0;fi<allf.length;++fi) {
    if (issorted) {
      for (fi2=0;fi2<allf[fi].length;++fi2) {
        allf2[allf2.length]=allf[fi][fi2];
      }
    } else {allf2[allf2.length]=allf[fi][0];}
  }
  if (issorted) {allf2.sort();}
  allf3=new Array();
  allf3[0]=allf2[0];
  for (fi=1;fi<allf2.length;++fi) {
    if (allf2[fi]!=allf2[fi-1]) {
      allf3[allf3.length]=allf2[fi];
    }
  }
/*
  sorts[++isorts]=new Array();
  sorts[isorts]=allf3;
  sortnames[isorts]=afield;
*/
  for (i=0;i<allf3.length;++i) {
    document.write('<a href="'+go2page+'?n='+afield+'&v='+hexify(allf3[i])+'">');
    document.write(requote(allf3[i])+'</a>');
    if (i<allf3.length-1) {document.write(mid);}
  }
}

function USD(anumber) {
  pennies=String(Math.round(100*Number(anumber)));
  astring=pennies.substring(0,pennies.length-2)+"."+pennies.substring(pennies.length-2,pennies.length);
  if (anumber==0) {astring="0.00";}
  return astring;
}
sorts=new Array();sortnames=new Array();isorts=-1;

function makefieldselect(field,label,action) {
  allf=new Array();
  for (i=1;i<c.length;++i) {
      allf[allf.length]=c[i][colOf(field)].split('^');
  }
  allf2=new Array();
  for (fi=0;fi<allf.length;++fi) {
    for (fi2=0;fi2<allf[fi].length;++fi2) {
      allf2[allf2.length]=allf[fi][fi2];
    }
  }
  allf2.sort();
  allf3=new Array();
  allf3[0]=allf2[0];
  for (fi=1;fi<allf2.length;++fi) {
    if (allf2[fi]!=allf2[fi-1]) {
      allf3[allf3.length]=allf2[fi];
    }
  }
  sorts[++isorts]=new Array();;
  sorts[isorts]=allf3;
  sortnames[isorts]=field;
  sfieldnames=field;
  document.write('<form><small><select onChange="'+action+'('+isorts+',this.selectedIndex-1);">');
  document.write('<option>'+label);
  for (i=0;i<allf3.length;++i) {
    document.write('<option');
    document.write('>'+requote(allf3[i]));
  }
  document.write('</select></small>');
  document.write('</form>');
}

function go2search(sfields,sfor) {
var sfields,sfor;
//  if (sfor>=" ") {
document.location="category.htm?search^0^"+sfields+"^"+unspace(sfor);
//}
}

function select2search(isort,iallf3) {
//note sfieldnames read from sortnames[isort]
//alert(isort+' '+iallf3);
  sfieldnames=sortnames[isort];
  if (iallf3>=0) {sfor=sorts[isort][iallf3];}
//  go2search(sfieldnames,sfor);
  document.location=pathparts[0]+'?n='+sfieldnames+'&v='+hexify(sfor);
//    document.write('<a href="'+go2page+'?n='+afield+'&v='+hexify(allf3[i])+'">');

}



