/* integrate.js - a command to integrate expressions in terms of the variable x */


CmdUtils.CreateCommand({
  name: "integrate",
  author: { name: "Tom Wright", email: "tom@tat.wright.name"},
  license: "BSD",
  description: "Integrates an expression in terms of x",
  help: "Displays the integral of an expression in terms of x",
  takes: {'integrand in terms of x': noun_arb_text},
  preview: function(pblock, integrandObj) {
    integrandString = integrandObj.text;
    if (integrandString.length == 0)
    {
         pblock.innerHTML = "Integrate an expression in terms of x";
         return
    }
    pblock.innerHTML = "Integrating " + integrandString + "...";
    var baseUrl = "http://integrals.wolfram.com/index.jsp";
    var params = {expr: integrandString};
    jQuery.get( baseUrl, params, function(responseText) {
      /* This is a very bad way to parse ajax responses - do not copy this */
        if (responseText.indexOf('Sorry') != -1)
        { 
            pblock.innerHTML = "Cannot integrate " + integrandString;
            return;
        }  
      before = responseText.indexOf('id="inputForm"');
      start = responseText.indexOf('<br/>', before) + 5;
      end = responseText.indexOf('</p>', before);
      answer = responseText.slice(start, end);
      template = '<p style="margin-left:20px"> <b>${answer}</b></p> <p style="font-size:small;text-align:right"> Powered by <a style="color:blue" href="http://integrals.wolfram.com/index.jsp">The Wolfram Integrator</a></p>'
      pblock.innerHTML = CmdUtils.renderTemplate(template, {answer: answer});
    })
  }
})


