var b_m0;
var b_m1;
var b_m2;
var b_m3;
var b_m4;
var b_p0;
var b_p1;
var b_p2;
var b_p3;
var b_p4;

var b_n_p0;
var b_n_p1;
var b_n_p2;
var b_n_p3;
var b_n_p4;

function initHTML()
{
 clearValues();
 updateEquation('','','','','','','','','','');
}

function clearValues()
{
 m_x0.value = "0";
 p_x0.value = "0";
 m_x1.value = "0";
 p_x1.value = "0";
 m_x2.value = "0";
 p_x2.value = "0";
 m_x3.value = "0";
 p_x3.value = "0";
 m_x4.value = "0";
 p_x4.value = "0";
}

function updateEquation( m0, m1, m2, m3, m4, p0, p1, p2, p3, p4 )
{
 document.fourier.get_m0();
 document.fourier.get_m1();
 document.fourier.get_m2();
 document.fourier.get_m3();
 document.fourier.get_m4();
 document.fourier.get_p0();
 document.fourier.get_p1();
 document.fourier.get_p2();
 document.fourier.get_p3();
 document.fourier.get_p4();

 document.fourier.get_n_p0();
 document.fourier.get_n_p1();
 document.fourier.get_n_p2();
 document.fourier.get_n_p3();
 document.fourier.get_n_p4();

 var newEq = "";
 var numTerms = 0;

 if ( b_m0 || b_m1 || b_m2 || b_m3 || b_m4 )
{

 if ( b_m0 )
{
 if ( b_p0 )
  newEq += "<m:apply><m:times/>";
 if ( !b_p0 && m0 == '<m:cn> </m:cn>' )
  newEq += "<m:cn> 1 </m:cn>";
 else
  newEq += m0;
 if ( b_p0 )
 {
  newEq += "<m:apply><m:exp/><m:apply><m:times/>";
  if ( b_n_p0 )
   newEq += "<m:cn>-</m:cn>";
  newEq += "<m:ci>j</m:ci>";
  newEq += p0;
  newEq += "</m:apply></m:apply></m:apply>";
 }
 numTerms++;
}

 if ( b_m1 )
{
 newEq += "<m:apply><m:times/>";
 newEq += m1;
 newEq += "<m:apply><m:exp/><m:apply><m:times/><m:ci>j</m:ci>";
 if ( b_p1 )
  if ( b_n_p1 )
   newEq += "<m:apply><m:minus/>";
  else
   newEq += "<m:apply><m:plus/>";
 newEq += "<m:apply><m:times/><m:ci><m:msub><m:mi>&omega;</m:mi><m:mi>o</m:mi></m:msub></m:ci><m:ci>n</m:ci></m:apply>";
 if ( b_p1 )
 {
  newEq += p1;
  newEq += "</m:apply>";
 }
 newEq += "</m:apply></m:apply></m:apply>";
 numTerms++;
}

 if ( b_m2 )
{
 newEq += "<m:apply><m:times/>";
 newEq += m2;
 newEq += "<m:apply><m:exp/><m:apply><m:times/><m:ci>j</m:ci>";
 if ( b_p2 )
  if ( b_n_p2 )
   newEq += "<m:apply><m:minus/>";
  else
   newEq += "<m:apply><m:plus/>";
 newEq += "<m:apply><m:times/><m:cn>2</m:cn><m:ci><m:msub><m:mi>&omega;</m:mi><m:mi>o</m:mi></m:msub></m:ci><m:ci>n</m:ci></m:apply>";
 if ( b_p2 )
 {
  newEq += p2;
  newEq += "</m:apply>";
 }
 newEq += "</m:apply></m:apply></m:apply>";
 numTerms++;
}

 if ( b_m3 )
{
 newEq += "<m:apply><m:times/>";
 newEq += m3;
 newEq += "<m:apply><m:exp/><m:apply><m:times/><m:ci>j</m:ci>";
 if ( b_p3 )
  if ( b_n_p3 )
   newEq += "<m:apply><m:minus/>";
  else
   newEq += "<m:apply><m:plus/>";
 newEq += "<m:apply><m:times/><m:cn>3</m:cn><m:ci><m:msub><m:mi>&omega;</m:mi><m:mi>o</m:mi></m:msub></m:ci><m:ci>n</m:ci></m:apply>";
 if ( b_p3 )
 {
  newEq += p3;
  newEq += "</m:apply>";
 }
 newEq += "</m:apply></m:apply></m:apply>";
 numTerms++;
}

 if ( b_m4 )
{
 newEq += "<m:apply><m:times/>";
 newEq += m4;
 newEq += "<m:apply><m:exp/><m:apply><m:times/><m:ci>j</m:ci>";
 if ( b_p4 )
  if ( b_n_p4 )
   newEq += "<m:apply><m:minus/>";
  else
   newEq += "<m:apply><m:plus/>";
 newEq += "<m:apply><m:times/><m:cn>4</m:cn><m:ci><m:msub><m:mi>&omega;</m:mi><m:mi>o</m:mi></m:msub></m:ci><m:ci>n</m:ci></m:apply>";
 if ( b_p4 )
 {
  newEq += p4;
  newEq += "</m:apply>";
 }
 newEq += "</m:apply></m:apply></m:apply>";
 numTerms++;
}

 if ( numTerms > 1 )
  newEq = "<m:math><m:apply><m:plus/>" + newEq + "</m:apply></m:math>";
 else
  newEq = "<m:math>" + newEq + "</m:math>";

}

 newEq = "<i>x</i>[<i>n</i>] = " + newEq;
 document.all.eq2.innerHTML = newEq;

}

function redMessage( message )
{
 messagebox.writeln( "ERROR:  " + message );
}

function greenMessage( message )
{
 messagebox.writeln( message );
}
