function createRequestObject() {
  var anfrage = null;
  try {
    anfrage = new XMLHttpRequest();
  } catch (versuchmicrosoft) {
    try {
      anfrage = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (anderesmicrosoft) {
      try {
	anfrage = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (fehlschlag) {
	anfrage = null;
      }
    }
  }
  
  if (anfrage == null)
    alert("Fehler beim Erzeugen des Anfrage-Objekts!");
  return anfrage;
}


function BuildCalculator()
{
     var oDiv = document.getElementById("calcscreen");
     while(oDiv.firstChild) 
        oDiv.removeChild(oDiv.firstChild);


     var img = new Image();
     img.src = "/calctool.jpg";
     img.heigth = "20px";
     img.style.height = "200px";

     var div = document.createElement('div');
     div.appendChild(img);   
     oDiv.appendChild(div); 

     oDiv.appendChild(document.createTextNode('\u000D')); 
     oDiv.appendChild(document.createTextNode('\u000D')); 
     oDiv.appendChild(document.createTextNode('\u000D')); 
     oDiv.appendChild(document.createTextNode('\u000D')); 
     oDiv.appendChild(document.createTextNode('\u000D')); 
     oDiv.appendChild(document.createTextNode('\u000D')); 
     oDiv.appendChild(document.createTextNode('\u000D')); 
     oDiv.appendChild(document.createTextNode('\u000D')); 
     oDiv.appendChild(document.createTextNode('\u000D')); 


     var oEntry = document.getElementById("calcentry");
     oEntry.onkeypress = calcCheck4Return;  
}

function calcCheck4Return(evt)
{      
   try
   {
      var itext = document.getElementById('calcentry');
      saveit(itext.value);
      var kc = -1;
      try
	{
	  //Mozilla
	  kc = evt.keyCode;
	}
      catch(e1)
	{
	  try
	    { 
	      //windows
	      kc = window.event.keyCode;
	    }
	  catch(e2)
	    {
	      alert("could not read keycode: "+e2);
	    } 
	}
    
      if(kc == 13)
	{
	  SendCalcRequest();
	}  
   } catch(exc)
     {
       alert("calcCheck4Return : "+exc);
     } 
}
function rep(text,org,ers)
{
    while(text.indexOf(org) >= 0)
        text =  text.replace(org,ers);
    return text;
}

function addTextObject(text)
{
      text = rep(text,'&sup2;','\u00B2');
      text = rep(text,'&sup3;','\u00B3');
      text = rep(text,'&middot;','\u00B7');
      text = rep(text,'&pi;','\u03C0');
      text = rep(text,'&alpha;','\u03B1');
      text = rep(text,'&beta;','\u03B2');
      text = rep(text,'&gamma;','\u03B3');
      text = rep(text,'&delta;','\u03B4');
      text = rep(text,'&epsilon;','\u03B5');
      text = rep(text,'&lambda;','\u03BB');
      text = rep(text,'&mu;','\u03BC');
      text = rep(text,'&infin;','\u221e');
     
      var div = document.createElement('div');
      div.appendChild(document.createTextNode('\u000D'));   
      div.appendChild(document.createTextNode(text)); 
      addObject(div); 
}
function addImageObject(image,part,factor,anz)
{
  var quality      = 1.0;// can change if want
  var height       = 25; //please don't change
  var width        = factor * height;
  var img          =  new Image();
  img.src          =  image+'?p='+part+'&a='+anz+'&w='+width+'&h='+height+'&q='+quality+'&d='+new Date().getTime();;
  //img.id           =  id;
  var oattr        = document.createAttribute('class');
  oattr.value      = 'none';    
  img.setAttributeNode(oattr);   

  img.style.width  = width+"px";
  img.style.height = height+"px";
  
  //img.onclick      = clickCard;
  var div = document.createElement('div');
  div.appendChild(img);   
  addObject(div); 
}
        
function addObject(div)
{
      var calcscreen = document.getElementById('calcscreen');
      if(calcscreen.firstChild) calcscreen.removeChild(calcscreen.firstChild);
      else alert('can not delte child');
      var divCLASS = document.createAttribute('class');
      divCLASS.value = 'classdivobject';    
      div.setAttributeNode(divCLASS);   


      calcscreen.appendChild(div);  
}
var url ="/math.xml";
var sid = new Date().getTime();     
function SendCalcRequest()
{
        /* Send text an clear */
        var itext = document.getElementById('calcentry');
        var request = createRequestObject();

	var getreg  = url + "?req=" + escape(itext.value)+"&sid="+sid+"&dummy="+new Date().getTime();
        //alert(getreg);
	request.open('get', getreg ,true);
	request.onreadystatechange = function () { handleState(request) };
	request.send(null);
  
}
function handleState(req) {
        if(req.readyState != 4){
		return;
	}
	var response = req.responseXML;
        try
	  {
	    var data   =  response.getElementsByTagName('data')[0];
	    //alert('data = '+data);
	    var child  =  data.childNodes;
	    
	    if(child.length <= 0)
	      alert('child.length == '+child.length+' data = '+data);


	    for (var i = 0; i < child.length; i++) 
	      {
		if(child[i].nodeName == 'text')
		  {
		    addTextObject(child[i].firstChild.nodeValue);
		  }
		else if(child[i].nodeName == 'image')
		  {
		    var anz   = child[i].getAttribute('parts');
		    var image = child[i].firstChild.nodeValue;
		    for(var x = 0;x < anz;x++)
		      addImageObject(image,x,20,anz);
		  }
		else if(child[i].nodeName == '#text')
		  {
		    //ignore
		  }
		else
		  alert(child[i].nodeName+' not supported');
	      }
	    
	    //if set itext without errors
	    var itext = document.getElementById('calcentry');
	    saveit(itext.value);
	    itext.value = '';
	    itext.focus();
	  } 
        catch(exc)
	  {
	    alert('handleState: '+exc);
	  }
}

function AddVal(val)
{
   var itext = document.getElementById('calcentry');
   saveit(itext.value);
   itext.value += val;
   itext.focus();
}
function CallDirect(val)
{
   var itext = document.getElementById('calcentry');
   saveit(itext.value);
   itext.value = val + ' '+itext.value;
   itext.focus();
   SendCalcRequest();
}
function Enclose(val)
{
   var itext = document.getElementById('calcentry');
   saveit(itext.value);
   itext.value = val + '('+itext.value+')';
   itext.focus();
}
function RightEnclose(val)
{
   var itext = document.getElementById('calcentry');
   saveit(itext.value);
   itext.value = '('+itext.value+')'+val;
   itext.focus();
}
function ClearAll()
{
  var itext = document.getElementById('calcentry');
  saveit(itext.value);
  itext.value = '';
  itext.focus();
}


var save   = new Array(100);
var is     = 0;
var ls     = 0;
function saveit(val)
{
   if(val != '')
   {
        if(ls < save.length) ls++;
        save[is] = val;
        is++;
        if(is > save.length -1) is = 0;
   }
}

function Undo()
{
  /* should be undo function */
  if(ls > 0)
  {
     is--;
     ls--;
     if(is < 0) is =  save.length -1;
     var itext = document.getElementById('calcentry');
     itext.value = save[is];
     itext.focus();
  }
}

