
wdcntr=0;pplctr=0

function parsehtm(){
  maxpg=1;linstr="";cplctr=0;
  linectr=1                                     // otherwise won't do pagebreak on first page unless it has a <p>
  header=makehdr()                              // for page 2 -- create its hdr before each page is parsed

  fulltext=iframetext('loadframe');
  if(!ns)fulltext=fulltext.substring(0,5)+chr(13)+chr(10)+fulltext.substring(6,fulltext.length)  // ie chops off lead cr-lf
  totchrs=fulltext.length;
  for(i=0;i<totchrs;i++){                  // there's no <pre> in an htm file so don't have to skip over it
    curchrcode=fulltext.charCodeAt(i)
    curchr=fulltext.charAt(i)
    nxtchr=fulltext.charAt(i+1);
    nxt2chr=fulltext.charAt(i+2);
    prvchr=fulltext.charAt(i-1);
    prv2chr=fulltext.charAt(i-2);

    if(curchrcode==32||curchr=="—"){lastsp=i;wdcntr++;if(curchr=="—"){cplctr++;cplctr++}}
    if(ns&&curchrcode==10){} //|| (curchrcode==13)){}   // skip 10's so don't mess cplctr
      else if(!ns&&curchrcode==13){i++}
        else if(curchr=="&"){
           if(nxtchr=="n"){i=i+5;cplctr++;linstr+="&nbsp;"}
            else if(nxtchr=="r"){i=i+5;cplctr++;linstr+="&rarr;"}
             else if(nxtchr=="l"){i=i+5;cplctr++;linstr+="&larr;"}
         }
         else if(curchr=="<"){
           if(nxtchr=="p"||nxtchr=="P"){
              if(nxt2chr==">"){lnwdth=0;indnt0=30;indnt1=0;findterm()}
                else {findterm();parsep();}
              crline(parseInt(lnwdth)); // sets cplctr to 0
              if(parseInt(indnt0)){
                 lnwdth=parseInt(indnt0+indnt1+indnt2)/6
                 for(jj=1;jj<lnwdth;jj++){linstr+="&nbsp;";
                 if(lnwdth<5)cplctr=lnwdth; else cplctr=3   // nxt lin st w/cplctr=indent not 0, b4 limit to 5, crashed mfu
               }
            }
          }
         else if(nxtchr=="/" && (nxt2chr=="p"||nxt2chr=="P") ){i=i+3;}
         else if(nxtchr=="m"  && fulltext.charAt(i+5)=="n"){i=i+7}    // <mbp:nu>
         else if(nxtchr=="M"  && fulltext.charAt(i+5)=="N"){i=i+7}
         else if(nxtchr=="m"){// <mbp:pagebreak />
            i=i+14;
            htmflg=1
            newpage();
            cplctr=0;
          }
         else if(nxtchr=="M"){                    // <mbp:pagebreak />
            i=i+16;
            htmflg=1
            newpage();
            cplctr=0;pplctr=0
          }
         else if(nxtchr=="/" && nxt2chr=="m" && fulltext.charAt(i+6)=="n"){i=i+8;}   // </mbp:nu>
         else if(nxtchr=="/" && fulltext.charAt(i+2)=="m"){i=i+15;}   // </mbp:pagebreak />

         else if(nxtchr=="/" && (nxt2chr=="i"||nxt2chr=="I")){i=i+3;linstr+="</i>"}
         else if((nxtchr=="i"||nxtchr=="I") && nxt2chr==">"){i=i+2;linstr+="<i>"}

         else if(nxtchr=="/" && (nxt2chr=="b"||nxt2chr=="B") && fulltext.charAt(i+3)==">"){i=i+3;linstr+="</b>"}
         else if((nxtchr=="b"||nxtchr=="B") && nxt2chr==">"){i=i+2;linstr+="<b>"}

//         else if(nxtchr=="b" && nxt2chr=="r"){i=i+3;linstr+="<br>";cplctr=0;}
         else if(nxtchr=="b" && nxt2chr=="r"){i=i+3;crline(0);}
         else if(nxtchr=="B" && nxt2chr=="R"){i=i+3;crline(0);}

         else if(nxtchr=="b" && nxt2chr=="l"){linstr+=findterm();linectr++;linectr++;cplctr=0}
         else if(nxtchr=="B" && nxt2chr=="L"){linstr+=findterm();linectr++;linectr++;cplctr=0}

         else if(nxtchr=="/" && nxt2chr=="b"){i=i+12;linstr+="</blockquote>";linectr++;linectr++;cplctr=0;}
         else if(nxtchr=="/" && nxt2chr=="B"){i=i+12;linstr+="</blockquote>";linectr++;linectr++;cplctr=0;}

         else if(nxtchr=="/"&& nxt2chr=="e"){i=i+4;linstr+="</em>"}
         else if(nxtchr=="e" && nxt2chr=="m"){i=i+3;linstr+="<em>"}

         else if(nxtchr=="/"&& nxt2chr=="E"){i=i+4;linstr+="</EM>"}
         else if(nxtchr=="E" && nxt2chr=="M"){i=i+3;linstr+="<EM>"}

         else if(nxtchr=="f" )linstr+=findterm();  //so cplctr not bumped
         else if(nxtchr=="F" )linstr+=findterm();  //so cplctr not bumped

         else if(nxtchr=="/" && nxt2chr=="f" ){i=i+6;linstr+="</font>"}
         else if(nxtchr=="/" && nxt2chr=="F" ){i=i+6;linstr+="</FONT>"}

         else if(nxtchr=="a"||nxtchr=="A" ){linstr+=findterm();}
         else if(nxtchr=="/" && (nxt2chr=="a"||nxt2chr=="A") ){i=i+3;}

         else if(nxtchr=="c"||nxtchr=="C"){fixcenter();}

//          else if(nxtchr=="c"||nxtchr=="C"){linstr+=findterm();}
//          else if(nxtchr=="/" && (nxt2chr=="c"||nxt2chr=="C") ){i=i+8;linstr+="</center>";linectr--}

         else if(nxtchr=="/" && (nxt2chr=="d"||nxt2chr=="D")){i=i+5;linstr+="</div>"}
         else if(nxtchr=="d"||nxtchr=="D"){linstr+=findterm()}
         else if(fulltext.charAt(i)=="<"  && fulltext.charAt(i+1)=="!" )commentout()

         else if(nxtchr=="x"||nxtchr=="X"){dynchcpl(100);i=i+2}
         else if(nxtchr=="y"){dynchcpl(0);i=i+1} // shld be i+2 but seems to chew extra char & i+1 works
         else if(nxtchr=="Y"){dynchcpl(0);i=i-1} // 
//           else if(nxtchr=="/"&&nxt2chr=="x"){i=i+3}
//           else if(nxtchr=="/"&&nxt2chr=="y"){i=i+3}

         else linstr+="<";
       }
      else{
        linstr+=curchr    // (chr(curchrcode));
        if(++cplctr>=maxcpl)newlin();
       }
  }
  if(chflg==2)finupdate()
   else if(!chflg)postparse() 
}
function parsep(){
  str=colltag
  lnwdth=str.search(/height/i)
  if(str.charAt(lnwdth+7)=='"')lnwdth=str.charAt(lnwdth+8); else lnwdth=str.charAt(lnwdth+7)
  ix=str.search(/width/i)
  if(str.charAt(ix+6)=='"'){
      indnt0=str.charAt(ix+7);
      if(!isNumeric(indnt1=str.charAt(ix+8)))indnt1="";
      if(!isNumeric(indnt2=str.charAt(ix+9)))indnt2="";
      if(str.charAt(ix+9)=="%"){indnt0="2";indnt1="2";indnt2="8";}
    }
    else{
      indnt0=str.charAt(ix+6);
      if(!isNumeric(indnt1=str.charAt(ix+7)))indnt1="";
      if(!isNumeric(indnt2=str.charAt(ix+8)))indnt2="";
      if(str.charAt(ix+8)=="%"){indnt0="2";indnt1="2";indnt2="8";}
     }
   if(str.search(/ALIGN="CENTER"/i)!=-1||str.search(/ALIGN=CENTER/i)!=-1){
      for(kk=i+1;kk<i+100;kk++)if(fulltext.charCodeAt(kk)==10)break;
      ctrstr=fulltext.substring(i+1,kk-1)
      ctrstr=ctrstr.replace(/<.*?>/gi,"")
      toindent=""+((maxcpl-ctrstr.length*.7)/2)*9      // .7 & 9 empirically determined for centering MFU chapter numbers
//      linstr+=toindent/6
      indnt0=toindent.charAt(0);indnt1=toindent.charAt(1);indnt2=toindent.charAt(2);
    }
}
function isNumeric(str){return(("" + parseInt(str)) == str)}

cntrfc=.75

function fixcenter(){
  findterm();
  if(colltag.search(/height="1%"/i)!=-1){crline(1);linectr++;linectr++;linectr++} // for pp 112-3 mfu1-17, 3 lctr++ empiric
  if(colltag.search(/HEIGHT="1%"/i)!=-1){crline(1);linectr++;linectr++;linectr++} // for pp 112-3 mfu1-17, 3 lctr++ empiric
  startoftxt=i+1
  for(i=i+1;i<i+300;i++)if(cha(i)=="<"&&cha(i+1)=="/"&&(cha(i+2)=="c"||cha(i+2)=="C"))break
  endoftxt=i;endcntr=i+8
  cntrtxt=fulltext.substring(startoftxt,endoftxt)

  if(i>startoftxt+maxcpl){
     collctr="";cplqctr=0;
     for(q=startoftxt;q<endoftxt;q++){
       if(fulltext.charCodeAt(q)==10||fulltext.charCodeAt(q)==13){q++}
       if(fulltext.charCodeAt(q)==32)lastqsp=q
       if(cha(q)=="<"&&cha(q+1)=="p"){
           q++;q++;q++
           linstr+=dosps((maxcpl-collctr.length)*cntrfc)+collctr+"<p style='margin-top:0px;'>";
           cplqctr=0;collctr=""
        }
        else{
          collctr+=cha(q);cplqctr++
          if(cplqctr>=maxcpl){
             if(lastqsp!=q){collctr=collctr.substring(0,collctr.length-(q-lastqsp));q=lastqsp;}
             linstr+=dosps((maxcpl-collctr.length)*cntrfc)+collctr+"<p style='margin-top:0px;'>";
             cplqctr=0;collctr=""
           }
         }
      }
     if(collctr)linstr+=dosps((maxcpl-collctr.length)*cntrfc)+collctr
     i=endcntr
   }
    else{
       cntrtxt=cntrtxt.replace(/<.*?>/gi,"")
       numsps=((maxcpl-cntrtxt.length)*cntrfc)
       i=endcntr
       linstr+=dosps(numsps)+fulltext.substring(startoftxt,endoftxt)
 //      linectr--
     }
//  comm(cntrtxt+" "+cntrtxt.length+" "+endcntr)
}

function findterm(){
   colltag=""
   for(i=i;i<i+100;i++){
     colltag+=fulltext.charAt(i)
     if(fulltext.charAt(i)==">"){return(colltag)}
    }
}
function dynchcpl(newcpl){                          // use this to respond to changes made by html font size
  if(!newcpl){maxcpl=defaultcpl;i=i+3;cplctr=0;}    // changes cpl but somehow changes back???
   else {maxcpl=newcpl; i=i+2;}
}
function commentout(){
  for(i=i+1;i<totchrs;i++){
    if(ishtm){
      if(fulltext.charAt(i)=="-" && fulltext.charAt(i+1)=="-" && fulltext.charAt(i+2)==">"){i=i+2;return(0)}
     }
      else 
      if(fulltext.charAt(i)=="-"&&fulltext.charAt(i+1)=="-"&&fulltext.charAt(i+2)=="&" && fulltext.charAt(i+3)=="g"){i=i+5;linectr--;return(0)}
   }
}

