// This file contains intellectual property and artistic creations 
// by Dennis Sandow and is copyright 2000-2010 by the author. 
// For use only on websites maintained by Dennis Sandow, or with
// specific permission.  Do not remove this notice.

var URL=location.href.split('?')[0];
var isHist=( cmdLnArg && 'hXD'.indexOf(cmdLnArg) >= 0 );
var childTitle=new Array(),loYr=-1,hiYr=-1,greenbar=1,greenbar2=0,pageOrSpawn='';
function makeGeneral() { document.write( 
  ( '<form name=Form2>'+
    '<table border=0 width=95% cellpadding=1 cellspacing=0>'+
      '<tr><td align=left class=v10>Use the controls below to create any report._ The universe is all <b>39_towns in Morris County</b>, plus <b>8_surrounding towns</b> in Union and Somerset Counties.'+
      '<ul><li>Any <b>Sort</b> button will automatically select the ranking and values for the selected data.'+
          '<li>Additional <b>Rank</b> and <b>Value</b> columns may be manually added or deleted by checking or unchecking the item.'+
          '<li>Some sample reports are available at the bottom of this page, to get you started.'+
          '<li>This form has loaded '+Comma(dataCellsIn)+' input data elements._ Click these buttons to show more or fewer.'+
          '<br><input type="button" name="dummy2a" '+
                 'value="Show '+(moreDat?'MORE':'FEWER')+' data elements."'+
                 'onclick="location.href=\''+URL+(nxDat?'?'+nxDat:'')+'\'"'+
               '>_____'+
               '<input type="button" name="dummy2b" '+
                 'value="'+(moreHis?'S':'DON\'T s')+'how data history."'+
                 'onclick="location.href=\''+URL+(nxHis?'?'+nxHis:'')+'\'"'+
               '>'+
           ( ( cmdLnArg == 'X' && ! isWeb )?
               '_____<input type="button" name="dummy2c" '+
                 'value="Show Everything." onclick="location.href=\''+URL+'?D\'"'+
               '>':'') +
           '<li>There are also '+ Comma(47*derivedCells)+' derived and calculated cells available.'+
    '</ul></td></tr></table></form>'
  ).replace(/_/g,'&nbsp;') );
};  

function makeDataElementTable(){
  colSpan = isHist?6:4;
  oneMneListA=new Array(); oneMneListA=threeMneLstsA[flavor].split('|'); isBunch=0; 

  document.write('<hr align="center" width="60%">'+
    '<table border=0 cellpadding=0 cellspacing=0>');

  for (x=1; x<oneMneListA.length; x++){
    Mne=oneMneListA[x];
    MneRoot=stripMne(Mne,'m43');

// Grey section separator
    if ( typeof CB[MneRoot] == udf ) {
      document.write(('<tr '+(isPrint?'':'bgcolor="#708090"')+'><td align=center><font color=white><b>'+Mne+'</b><font></td><td colspan='+colSpan+'>_</td></tr>').replace(/_/g,HS));
      continue;
    };
    
    rootMneCtl=new Array(); rootMneCtl=(CB[MneRoot]+'||||||||').split('|')

    A2=rootMneCtl[2]; A20=(A2+' ').split(' ')[0]
    okSrt = ( A20.indexOf('-S') <0 ) ; 
    okVal = ( A20.indexOf('-V') <0 ) ; 
    okRk = ( okSrt && A20.indexOf('-R') <0 ) ; 
    defSort = ( (A2+' ').split(' ')[1].indexOf('defaultSort') >=0 ); 
    okP = ( A20.indexOf('P') >=0 ) ; 
    okC = ( A20.indexOf('C') >=0 ) ; 
    okc = ( A20.indexOf('c') >=0 ) ; 
    okv = ( A20.indexOf('v') >=0 ) ; 
    oka = ( A20.indexOf('a') >=0 ) ; 

    neatNameRaw=nnr=rootMneCtl[3];
    hLabel0=hLabel=nnr.replace(/.*>/g,'');
    if ( nnr.indexOf('<B>') >= 0 ) hLabel='<b>'+hLabel+'</b>';
    if ( ! nnr.indexOf('>') ) hLabel='__'+hLabel;
    popWrap=popThings(Mne,hLabel0);
    
    dotGreen=(isPrint || Math.floor(++greenbar)%2)?'':'bgcolor="#c0ffc0"';
    bunchCtl = rootMneCtl[6] ;
    curYr = rootMneCtl[7] ;

// pre-current year is allowed and available
    if ( isHist && bunchCtl ) { // 60 lines

      lStr='<tr valign=center><td align=left '+dotGreen+' class=v10 rowspan=2>';
      lStr+=(
        popWrap?'<a href="javascript:void(0)" onMouseover="return HintPop(\''+Mne+'\',\'Discussion of '+hLabel0+'\',\'-450,R,A\')" onMouseOut="xnd()" class=h>':'');
      lStr+=hLabel+(
        popWrap?'_<script type="text/javascript">Wingding(\'mouse\',0)</script></a>':'')+
      '</td>'+
      '<td align=center '+dotGreen+'>_--_</td>';
      
      lStr+='<td colspan='+colSpan+' class=v10>'+
        '<table width=100% border=0 cellpadding=0 class=v8>'+
        '<tr align=center><td '+dotGreen+'>'+
         '<b>allV</b><input type="checkbox" name="ckbxTogVal'+Mne+
           '" onclick="toggleRow(\'Val'+Mne+'\')">'+
         (okRk?'<br><b>allR</b><input type="checkbox" name="ckbxTogRk'+Mne+
           '" onclick="toggleRow(\'Rk'+Mne+'\')"></td>':'');
      greenbar2=Math.floor(greenbar);

      if ( bunchCtl.indexOf('-') > 0 ) {
        loYr=parseInt( bunchCtl.split('-')[0] );
        hiYr=parseInt( bunchCtl.split('-')[1] );
        bunchCtl='';
        for (xYr=loYr; xYr<=hiYr; xYr++) bunchCtl+=''+xYr;
      };

// loop years
      for (xYr=0; xYr<parseInt(curYr); xYr++) {
        yr4 = 2000+xYr; yr2=Yr2(xYr);
        if ( bunchCtl.indexOf(xYr) < 0 ) {
          lStr+='<td bgcolor=pink>____<b>'+yr2+'</b>____</td>';
          continue;
        };

        popThings(Mne+yr2,yr4+' '+hLabel0);
        childTitle[Mne+yr2] = yr4+' '+hLabel0;

        sortBut = ''; onKl='';My2=Mne+yr2;
        if (okVal) onKl='ckbxVal'+My2+'.checked=true;';
        if (okRk)  onKl+='ckbxRk'+My2+'.checked=true;';
        if ( okSrt ) {
          sortBut += '<br><input type="radio" name=sort1 value="'+My2+'" '+
              'onclick="' + onKl + 'reTitleTA(\''+Mne+'\',\''+yr4+'\',\'\');">' ;
        };
        dotGreen2=(isPrint || ++greenbar2%2)?'':'bgcolor="#c0ffc0"';
        lStr+=(sortBut?'<td '+dotGreen2+'><b>'+yr2+'</b>S'+sortBut+'</td>':'')+
          '<td align=left '+dotGreen2+'>'+
          (okRk?'':'<b>'+yr2+'</b>')+
          'V<input type="checkbox" name=ckbxVal'+My2+'>'+
          ( okRk?'<br>R<input type="checkbox" name=ckbxRk'+My2+'>':'_' )+
          '</td>';
      }; // end of loop years

      document.write( lStr.replace(/_/g,HS)+'</tr></table></td></tr>' );
    }; // end of ( isHist && bunchCtl )

    if ( curYr ) {
      yr4 = 2000+parseInt(curYr); yr2=Yr2( parseInt(curYr) );
    } else { yr4 = ''; yr2=''; };

    hLabel0=hLabel=((yr4?yr4+' ':'')+hLabel0).replace(/ /g,HS);
    if ( nnr.indexOf('<B>') >= 0 ) hLabel='<b>'+hLabel+'</b>';
    if ( ! nnr.indexOf('>') ) hLabel='__'+hLabel;

    popWrap=popThings(Mne+yr2,hLabel0);
    childTitle[Mne+yr2] = hLabel0;

    lStr='<tr valign=top>'+
      ( isHist && bunchCtl
          ?'<td align=center '+dotGreen+'>'+yr4+'_</td>'
          :('<td align=left '+dotGreen+' class=v10 colspan=2>'+
            (popWrap?'<a href="javascript:void(0)" onMouseover="return HintPop(\''+Mne+'\',\'Discussion of '+hLabel0+'\',\'-450,R,A\')" onMouseOut="xnd()" class=h>':'')+
            hLabel+'_'+
            (popWrap 
              ?'_<script type="text/javascript">Wingding(\'mouse\',0)</script></a>'
              :''
            )+
         '</td>')
      );
    onKl='';onKl='';My2=Mne+yr2;
    if (okVal) onKl='ckbxVal'+My2+'.checked=true;';
    if (okRk)  onKl+='ckbxRk'+My2+'.checked=true;';
    if ( okSrt ) {
      sortBut = '<input type="radio" name=sort1 value="'+My2+'" '+
        (defSort?' checked ':' ')+
        'onclick="' + onKl + 'reTitleTA(\''+Mne+'\',\''+yr4+'\',\'\');">Sort_' ;
    } else sortBut='_' ;

    lStr+='<td align=left '+dotGreen+' class=v10>'+ sortBut +'</td>';

    lStr+='<td align=left '+dotGreen+' class=v10>'+
      (okVal?'<input type="checkbox" name=ckbxVal'+My2+'>Value':'_')+'</td>';
    lStr+='<td align=left '+dotGreen+' class=v10>'+ 
      ( okRk?'<input type="checkbox" name=ckbxRk'+My2+'>Rank_':'_' )+'</td>';

    if ( isHist && ( okP || oka ) ) {
      lStr+='<td align=center bgcolor=lightgrey class=v10>';
      if ( okP ) {
        childTitle['PctChg'+Mne] = 'Percent Change in '+CB[Mne].split('|')[3].replace(/.*>/g,'') +' SUBSTYRS';
        lStr+='_%&#916;'+
          ( okSrt ? '_S<input type="radio" name="sort1" value="PctChg'+Mne+'" '+
            'onclick="if ( isTwoYears(\''+Mne+'\') ) { '+
              'ckbxPctChgVal'+Mne+'.checked=true; '+
              'ckbxPctChgRk'+Mne+'.checked=true; '+
              'reTitleTA(\''+Mne+'\',\'\',\'PctChg\');}">' :'' )+
          '__V<input type="checkbox" name=ckbxPctChgVal'+Mne+
            ' onclick="return isTwoYears(\''+Mne+'\');">'+
          ( okRk ? '__R<input type="checkbox" name=ckbxPctChgRk'+Mne+
            ' onclick="return isTwoYears(\''+Mne+'\');">' :'' );
      };
      if (okP && oka) lStr+='<br>';
      if ( oka ) {
        childTitle['Avgg'+Mne] = 'Average '+CB[Mne].split('|')[3].replace(/.*>/g,'') +' SUBSTYRS';
        lStr+='Avg'+
          ( okSrt ? '_S<input type="radio" name="sort1" value="Avgg'+Mne+'" '+
            'onclick="if ( isTwoYears(\''+Mne+'\') ) { '+
              'ckbxAvggVal'+Mne+'.checked=true; '+
              'ckbxAvggRk'+Mne+'.checked=true; '+
              'reTitleTA(\''+Mne+'\',\'\',\'Avgg\');}">' :'' )+
          '__V<input type="checkbox" name=ckbxAvggVal'+Mne+
            ' onclick="return isTwoYears(\''+Mne+'\');">'+
          ( okRk ? '__R<input type="checkbox" name=ckbxAvggRk'+Mne+
            ' onclick="return isTwoYears(\''+Mne+'\');">' :'' );
      };
      lStr+='</td>';
    } else if (isHist) lStr+='<td '+dotGreen+'>_</td>';

    if ( isHist && ( okC || okc || okv || oka ) ) {
      lStr+='<td align=left bgcolor=silver class=v10>';

      if ( okC ) {
      childTitle['ChgRnk'+Mne] = 'Change of Rankings for '+CB[Mne].split('|')[3].replace(/.*>/g,'') +' SUBSTYRS';
      lStr += '_Rank&#916;'+
        '_S<input type="radio" value="ChgRnk'+Mne+'" name="sort1" '+
          'onclick="if ( isTwoYears(\''+Mne+'\') ) { '+
            'ckbxChgRnkVal'+Mne+'.checked=true; '+
            'reTitleTA(\''+Mne+'\',\'\',\'ChgRnk\'); }">'+
        '__V<input type="checkbox" name=ckbxChgRnkVal'+Mne+
          ' onclick="return isTwoYears(\''+Mne+'\');">';
      } else if ( okc ) {
      childTitle['ChgVl'+Mne] = 'Change of Ranking for '+CB[Mne].split('|')[3].replace(/.*>/g,'') +' SUBSTYRS';
      lStr += '_Rank&#916;'+
        '_S<input type="radio" value="ChgVl'+Mne+'" name="sort1" '+
          'onclick="if ( isTwoYears(\''+Mne+'\') ) { '+
            'ckbxChgVlVal'+Mne+'.checked=true; '+
            'reTitleTA(\''+Mne+'\',\'\',\'ChgVl\'); }">'+
        '__V<input type="checkbox" name=ckbxChgVlVal'+Mne+
          ' onclick="return isTwoYears(\''+Mne+'\');">';
      } else if ( okv ) {
      childTitle['ChgVvl'+Mne] = 'Change of Value for '+CB[Mne].split('|')[3].replace(/.*>/g,'') +' SUBSTYRS';
      lStr += '_Value&#916;'+
        '_S<input type="radio" value="ChgVvl'+Mne+'" name="sort1" '+
          'onclick="if ( isTwoYears(\''+Mne+'\') ) { '+
            'ckbxChgVvlVal'+Mne+'.checked=true; '+
            'reTitleTA(\''+Mne+'\',\'\',\'ChgVvl\'); }">'+
        '__V<input type="checkbox" name=ckbxChgVvlVal'+Mne+
          ' onclick="return isTwoYears(\''+Mne+'\');">';
      };
      
      if ( ( okC || okc || okv ) && oka) lStr+='<br>';
      if ( oka ) {
        lStr+='Suppress Limits'+
          '<input type="checkbox" name=ckbxSuppressVal'+Mne+'>';
      };
      
      
      lStr+='</td>';
    } else if (isHist) lStr+='<td '+dotGreen+'>_</td>';

    document.write( lStr.replace(/_/g,HS)+'</tr>' );
  };
  document.write('</table>');
};

function extractYr2(aax2,bbm){
  return aax2.replace( 'ckbxVal'+bbm,'').replace('ckbxRk'+bbm,'');
};

function isTwoYears(baseMne){
  loYr=-1; hiYr=-1; aa=document.forms.FZ
  for (x2=0;x2< aa.length;x2++){
    if ( aa[x2].type != 'checkbox' || 
         ( aa[x2].name.indexOf('ckbxVal'+baseMne) &&
           aa[x2].name.indexOf('ckbxRk'+baseMne) ) ||
         ! aa[x2].checked ) continue
    if(loYr == -1) loYr = extractYr2(aa[x2].name, baseMne);
    hiYr = extractYr2(aa[x2].name, baseMne);
  };
  if ( loYr != -1 && loYr != hiYr ) return true;
 
  alert ( 'You must first select two Values or Ranks for "'+
          ( CB[baseMne].split('|')[3] ).replace(/.*>/,'')+
          '" to compare different years.' );
  for (x=0; x < document.forms.FZ.sort1.length ; x++){
    document.forms.FZ.sort1[x].checked = false; 
  };
  return false;
};

function reTitleTA(Rmne,Ryr4,Rpref){
// when a sort button is manually clicked, Force the col title into the title textarea and clear name  Footnote is cleared .
  tWord=CB[stripMne(Rmne,'m276')].split('|')[3].replace(/.*>/,'') ;
  if ( Rpref == 'PctChg') {
    tWord = '% Change '+ tWord+' SUBSTYRS';
  } else if ( Rpref == 'PctChg') {
    tWord = 'Average '+ tWord+' SUBSTYRS';
  } else if ( Rpref == 'ChgRnk') {
    tWord = 'Change '+ tWord+' Ranking SUBSTYRS';
  } else if ( Rpref == 'ChgVl') {
    tWord = 'Change '+ tWord+' Rank SUBSTYRS';
  } else if ( Rpref == 'ChgVvl') {
    tWord = 'Change '+ tWord+' SUBSTYRS';
  } else if ( Ryr4 ) tWord = Ryr4 +' '+ tWord;
  document.forms.FZ['reportTitle'].value=tWord;
  document.forms.FZ['reportFootnote'].value='';
  document.forms.FZ['pistName'].value='NewName';
};

function toggleRow(arg){
  aa=document.forms.FZ; gotoState='fault'
  for (x2=0;x2< aa.length;x2++){
    if (aa[x2].name == 'ckbxTog'+arg) gotoState= aa[x2].checked
  };
  for (x2=0;x2< aa.length;x2++){
    name2=aa[x2].name.replace(/ckbx/,'')
    if ( name2.indexOf(arg) <0 ) continue;
    if ( ! name2.indexOf(arg+'0') ||
         name2 == 'PctChg'+arg ||
         name2 == 'ChgRnk'+arg ||
         name2 == 'ChgVl'+arg ||
         name2 == 'ChgVvl'+arg ||
         name2 == 'Avgg'+arg 
       ) { document.forms.FZ[x2].checked = gotoState; };
  };
};

function makeTownsTable(){
  allTowns = '<tr><td colspan=5 class=v10>'+
    'You may use these buttons to preset towns for "Include Only":<div align=center>'+
    '<input type=button name=pistnA Value="5_surrounding_towns" '+
      'onclick="preSetTowns(5)">'+
    '_____<input type=button name=pistnB Value="Nearest_12" '+
      'onclick="preSetTowns(12)">'+
    '_____<input type=button name=pistnC Value="Morris County" '+
      'onclick="preSetTowns(\'Morris\')">'+
    '_____<input type=button name=pistnD Value="DFG &#148;I/J&#148;" '+
      'onclick="preSetTowns(\'DFGIJ\')" '+
      'onMouseover="return HintPop(\'DFG2\',\'Discussion of DFG\',\'-450,L\')" '+
      'onMouseOut="xnd()" class=h>'+
    '_____<input type=button name=pistnE Value="L8R12" '+
      'onclick="preSetTowns(\'L8R12\')" '+
      'onMouseover="return HintPop(\'SchType2\',\'Discussion of School Type\',\'-450,L\')" '+
      'onMouseOut="xnd()" class=h>'+
    '_____<input type=button name=pistnF Value="INVERT selection" '+
      'onclick="preSetTowns(\'Invert\')" class=invers>'+
  '</div></td></tr>';
  allTowns+='<tr valign="top">'; ord=0;
  for (twn in ma['Towns'] ){
    tag1=('|0|10|20|29|38|'.indexOf('|'+(ord++)+'|')<0)?'<br>':'<td class="v10">'; 
    tag2=('|0|10|20|29|38|'.indexOf('|'+(ord)+'|')<0)?'':'</td>';
    isLH=(twn == 'Long Hill')
    allTowns+=tag1+'_<input type="checkbox" name="Twn'+twn+'" '+(isLH?'checked':'')+
    ' onclick="exportTownList=\'\'" >_'+
    (isLH?'<b>':'')+twn.replace(/ /g,HS)+(isLH?'</b>':'')+'_'+tag2;
  };
  allTowns+='</tr>';
  document.write( ('<table width=100% border=1 cellpadding=5 cellspacing=0>'+
    allTowns+'</table>').replace(/_/g,HS) );
};

Mesage['textareaNote'] = 'Use normal HTML markup to format the footnote text. &lt;p&gt; = new paragraph;  &lt;br&gt; = new line;  &lt;b&gt;<b>bold</b>&lt;/b&gt; etc.<p>_ is shortcut for hard space (&amp;nbsp;).';

function makeBoilerplateBlock(){
  document.write('<table border=0 cellpadding=0 cellspacing=0><tr><td class=v10>'+
    '<tr valign=top><td align=center class=v8>'+
    ( '<input type=button value="Clear Form" onclick="clearEm(\'textarea\')">___'+
    '<input type="checkbox" name=BiggerFont>Use a larger font size in the report_'+
    '<input type="checkbox" name=BiggestFont>Even larger than that_'+
    '</td></tr>').replace(/_/g,HS)+
    '</table>');
};

function makeCustomize(){
  dsWd=mySpecial?'inline':'none'
  document.write('<div align=left class=v8>'+
    '<a href="javascript:divControl(-1,\'customeyes\')" onmouseout="" ><i>Customize Report (optional)?</i></a></div>'+
    
  '<div id=customeyes style="display:'+dsWd+';">'+
    '<table border=0 align=center width=90% cellpadding=1 cellspacing=0>'+

    ('<tr valign="top"><td width=15% align=right class=v10>Title:_</td>'+
       '<td><textarea name="reportTitle" cols=60 rows=1></textarea>'+
       '_ <input type="button" value = "Clr" onclick="document.forms.FZ[\'reportTitle\'].value=\'\';">'+
       '</td></tr>'+
    '<tr valign="top"><td width=15% align=right class=v10>'+
      '<a href="javascript:void(0)" onmouseout="xnd()" '+
      'onMouseover="return HintPop(\'textareaNote\',\'\')" class=h>'+
      'Footnote:</a>_'+
      '</td><td>'+
    '<textarea name="reportFootnote" cols=60 rows=4"></textarea>'+
      '_ <input type="button" value = "Clr" '+
      'onclick="document.forms.FZ[\'reportFootnote\'].value=\'\';">'+
      '</td></tr>').replace(/_/g,HS)+
    '</td></tr></table>'+
  (mySpecial?'<div align=left class=v8>'+
      '<a href="javascript:divControl(-1,\'stor1\',\'stor2\')" onmouseout="" ><i>Store Report (optional)?</i></a></div>':'')+
      
  '<div id=stor1 align=center style="display:'+dsWd+';">'+
      '<table border=0 width=90% cellpadding=1 cellspacing=0>'+
      ( '<tr valign="top"><td width=15% align=right class=v10>Name:_</td>'+
        '<td><input type="text" name="pistName" size=70>'+
        '_ <input type="button" value = "Clr" onclick="document.forms.FZ[\'pistName\'].value=\'\';"></td></tr>' ).replace(/_/g,HS)+
      '</table>'+
    '</div>' +

  '<div id=stor2 align=center style="display:'+dsWd+';">'+
      '<table border=0 width=90% cellpadding=1 cellspacing=0>'+
      ( '<tr valign="top"><td width=15% align=right class=v10>Piston:_</td>'+
        '<td><textarea name="pistContent" cols=60 rows=10></textarea>'+
        '_ <input type="button" value = "Clr" onclick="document.forms.FZ[\'pistContent\'].value=\'\';"><br>_'+
      '</td></tr>').replace(/_/g,HS)+
      '</table>'+
    '</div>' +
  '</div>');

};


