<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="Cezar's Calculator" height="275" width="345" 
author="Cezar A." author_email="xmlbeans+calculator@gmail.com" 
author_affiliation="XMLBeans" 
description="Cezar's Calculator, started after Goggle Calculator failed to work.
For simple expressions like 1+1 or (5*10 + 20)/10 works directly in your browser, 
for more complicated expressions calls into Google Search." 
title_url="http://code.google.com/p/cezarscalculator/wiki/Home" 
thumbnail="http://hosting.gmodules.com/ig/gadgets/file/104369655353720335001/cezars_calculator_thumb.PNG" 
screenshot="http://hosting.gmodules.com/ig/gadgets/file/104369655353720335001/cezars_calculator.PNG">
 
  <Require feature="analytics"/>
</ModulePrefs>
<Content type="html"><![CDATA[

  
<script type="text/javascript">

function cc_compute (form)
{
  var input = form.inputbox.value;
  var message = "";
  var evalResult = "";

  try
  {
    evalResult = eval(input);
  }
  catch (ex)
  {
    cc_callGoogle(input);
    return;
  }

  if ( evalResult==null || evalResult.length==0 || evalResult=="undefined")
  {
    cc_callGoogle(input);
    return;
  }

  message = input + " = " + evalResult;

  cc_addMessageBlue(message);
}

function cc_addMessageBlue(message)
{
  cc_addMessage("<span style='color:blue;'>" + message + "</span>");
}

function cc_addMessage(message)
{
  _gel('result').innerHTML = "<p style='margin-bottom:2px; margin-top:2px;'>" + message + "</p>" +
    _gel('result').innerHTML;
}

function cc_callGoogle(input)
{
    try
    {
      input = encodeURIComponent(input);

      var url = "http://www.google.com/search?q=" + input;
      _IG_FetchContent(url, cc_requestReady);
      //cc_addMessage("Calling google ...");
    }
    catch (ex)
    {
       cc_addMessage("Exception: " + ex);
    }
}

function cc_requestReady (response)
{
    if ( response == "" )
        response = "No response!";
    else
        response = cc_findRelevant(response);

   cc_addMessage(response);
   _IG_Analytics('UA-1852382-6', '/cezars_calculator/click_callGoogle');
}

function cc_findRelevant (response)
{
    var indx1 = -1;
    var indx2 = -1;

    //cc_addMessage("  response.search indx: " + response.search(/\/images\/calc_img.gif/));
  
    if (response.search(/\/images\/calc_img.gif/)>=0)
    {
        indx1 = response.search(/\/images\/calc_img.gif/);
        //cc_addMessage("  indx1   : " + indx1 ); 
        response = response.substring(indx1);
  
        indx1 = response.search(/<b>/);
        //cc_addMessage("  indx1.1 : " + indx1 ); 
        response = response.substring(indx1);
        
        indx2 = response.search(/<\/b>/);
        //cc_addMessage("  indx2   : " + indx2 ); 

        var rezult = response.substring(3, indx2);

        //cc_addMessage( rezult ); 
  
        return rezult; 
    }
    else
    {
        return "No Google Calculator result."
    }
}

function cc_clear (form)
{
  _gel('result').innerHTML = "";
  _gel('inputbox').value = "";
  form.inputbox.value = "";
  _IG_Analytics('UA-1852382-6', '/cezars_calculator/click_clear');
}

function cc_help (form)
{
  var headerStyle = "style='margin-bottom:2px; margin-top:2px; padding-bottom:2px; padding-top:2px;font-size:1.2em; font-family:verdana,arial,sans-serif;'";
  cc_addMessage("<a href='http://code.google.com/p/cezarscalculator/wiki/Home' target='_blank'>Cezar's Calculator &copy; 2007</a>");
  cc_addMessage("&nbsp;&nbsp;  1 + 1 =");
  cc_addMessage("&nbsp;&nbsp;  PI * e");
  cc_addMessage("&nbsp;&nbsp;  160 pounds * 4000 feet in Calories");
  cc_addMessage("&nbsp;&nbsp;  half a cup in teaspoons");
  cc_addMessage("&nbsp;&nbsp;  60 miles/hour = ? km/h");
  cc_addMessage("<span " + headerStyle + ">Google Calculator</h4>");
  cc_addMessageBlue("&nbsp;&nbsp;  ((123 - 1)/2 - 59) *3");
  cc_addMessageBlue("&nbsp;&nbsp;  1 + 1");
  cc_addMessageBlue("<span " + headerStyle + ">Local</span>");

  _IG_Analytics('UA-1852382-6', '/cezars_calculator/click_help');
}
</script>

<div id="input">
  <a name="cc_calculator"></a>
  <form action="#cc_calculator" method="get" onSubmit="return false">
    Exp: <input type="text" name="inputbox" value="" onChange="cc_compute(this.form)" />
    <input type="button" value="Go" onClick="cc_compute(this.form)"/>
    <input type="button" value="Clear" onClick="cc_clear(this.form)"/>
    <input type="button" value="Help" onClick="cc_help(this.form)"/>
  </form>
</div>
<div id="result"></div>
 
  <div style="padding:0; margin:0; overflow:hidden;">
    <iframe width="200" scrolling="no" height="200" frameborder="0" allowtransparency="true"
            hspace="0" vspace="0" marginheight="0" marginwidth="0" 
            src='http://xmlbeans.org/ad1.html'
            style='padding:0; margin:0; overflow:hidden; border-width:0; '></iframe>
  </div>
 
   <script>
     // Track this gadget using Google Analytics.
     _IG_Analytics("UA-1852382-6", "/cezars_calculator");
   </script>
   
]]></Content>
</Module>