/*
  AJAX objects
*/
function ajaxObject(url, callbackFunction, targetId) {
  var that=this;      
  if (url == undefined){url = '/ajax'}
  if (callbackFunction == undefined){callbackFunction = renderWidgets}
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null; 
    }
  }
  this.update = function(passData, postMethod) { 
    if (postMethod == undefined) {postMethod = "POST"}
    if (passData == undefined) {passData = ""}
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML, that.targetId);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall     //unique request id:    +'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.overrideMimeType( 'text/xml'); // get xml
        that.AJAX.setRequestHeader("Content-type", 'application/x-www-form-urlencoded'); // send form data
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        //alert('sending: ' + passData)
        that.AJAX.send(passData);
      } 
      else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url; 
  this.targetId = targetId;
  this.callback = callbackFunction || function () { };
}
function update(id, ps) {
  /* 
    update makes server send widget update
    echo overrules update and tells server just to loopback value of echo for element update
  */
  // update('news2title', ['action=save', 'kind=News', 'id=2', 'title=New News', 'echo=New News'])
  
  //if not echo in params, add update
  var data = '';
  for ( var i=0; i< ps.length; i++) {
    if (ps[i].indexOf('=') >= 0) {
      var t = ps[i].indexOf( '=');
      var att = ps[i].substring( 0, t);
      var val = encodeURIComponent(ps[i].substring( t + 1));
      var tokenizer = '';
      if (i) tokenizer = '&';
      data += tokenizer + att + '=' + val;
    }  
  }
  A = new ajaxObject(undefined, refreshElements, id);
  A.update(data)
}  
setValue = function(id, text) {
  var node = $(id); 
  if (node != undefined) 
    if (node.nodeName == 'input') 
      {node.value = text}
    else 
      {node.innerHTML = text}
}
refreshElements = function( responseText, responseStatus, responseXML, echoTargetId) {
  if ( responseStatus==200) {
    var update = '';
    var echo = responseXML.getElementById(echoTargetId)
		if (echo != undefined) {
		  update = echo.innerHTML;
		  setValue(echoTargetId, update); 
      evalScripts($(echoTargetId));
      echo.remove();
    }
    var response = responseXML.getElementById('response');
    if (response != undefined) {
      var widgets = response.childElements();
      for (var i=0; i<widgets.length; i++) {
        var widgetId = widgets[i].identify();
        // widget contained by document
        if ($(widgetId) != undefined) {
          alert('eval?');
          setValue(widgetId, widgets[i].innerHTML);
          evalScripts($(widgetId));
        }        
      }
    }
    
	}	
}
evalScripts = function(tag) {
  var scripts = tag.getElementsBySelector('script');
  for (var i=0; i < scripts.length; i++) {
    eval( scripts[i].innerHTML);
  }  
}
