function ajax(url, method, parameters, doneFunction) {
  url=url+((parameters!=null && parameters.length>0)?('?'+parameters):(''));
  //console.log(doneFunction);
  (new AjaxObj()).callServerMigration(url,method,doneFunction);
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

/* ------------------------------------------------------------------ */

function AjaxObj() {}

AjaxObj.prototype.callServer=function(url,methodname) {
 try {
    //window.eval("new Ajax('"+url+"', {method: 'get',onComplete: function(text, xml) {this."+methodname+"(text,xml)}.bind(this)}).request();").bind(this);
    new Ajax(url, {
          method: 'get',
          onComplete: function(text, xml) {
            this[methodname](text,xml);
          }.bind(this)
    }).request();
  } catch(e) {}
}

AjaxObj.prototype.callServerMigration=function(url, httpmethod, methodname) {
  new Ajax(url, {method: httpmethod,onComplete: function(text, xml) { methodname(text); }.bind(this) }).request();
}

AjaxObj.prototype.callbackFromServer=function(text,xml) {}

AjaxObj.prototype.strToXml=function(str) {
  var xml = null;
  if(window.ActiveXObject) {
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.loadXML(str);
  } else {
    var parser = new DOMParser();
    xml = parser.parseFromString(str, "text/xml");
  }
  return xml;
}

/* ------------------------------------------------------------------ */
/**
 * Ajax request which sets the response into a target div container
 */
AjaxObj.prototype.ajaxRequest = function(url, targetDivId, show, oncomplete, onerror) {
  var mbAjax = new Ajax(url,{
    method: 'post',
    evalScripts: true,
    update: targetDivId,
    onRequest: function() {
      AjaxObj.prototype.showLoading(targetDivId, true);
    },
    onComplete: function() {
      AjaxObj.prototype.showLayer(targetDivId, show);
      AjaxObj.prototype.showLoading(targetDivId, false);
      if(oncomplete)
            oncomplete();
    }, 
    onFailure: function() {
      AjaxObj.prototype.showLoading(targetDivId, false);
      if(onerror)
            onerror();
    }
  }).request();
}

/**
 * Ajax form submit which sets the response into a target div container
 */
AjaxObj.prototype.ajaxFormSubmit = function(formId, targetDivId, show, validator, oncomplete) {
  newsubmit = function(e) {
    if(e)
      new Event(e).stop();
    if(!validator || (validator && validator.validate(true))) {
      this.send({
        evalScripts: true,
        update: targetDivId,
        onRequest: function() {
          AjaxObj.prototype.showLoading(targetDivId, true);
        },
        onComplete: function() {
          AjaxObj.prototype.showLayer(targetDivId, show);
          AjaxObj.prototype.showLoading(targetDivId, false);
          if(oncomplete)
            oncomplete();
        }, 
        onFailure: function() {
          AjaxObj.prototype.showLoading(targetDivId, false);
          if(oncomplete)
            oncomplete();
        }
      });
    }
  }
  var form = $(formId);
  form.addEvent('submit', newsubmit);
  form._submit = form.submit;
  form.submit = newsubmit;
}

/**
 * Shows or hides a given div container by id
 */
AjaxObj.prototype.showLayer = function(targetDivId, show) {
  AjaxObj.prototype.positionContainer(targetDivId);

  if(show){
    $(targetDivId).removeClass('hideout');
    window.addEvent('resize', AjaxObj.prototype.positionContainer.bind(this, targetDivId));
  }
  else
    $(targetDivId).addClass('hideout');
}

AjaxObj.prototype.showLoading = function(targetDiv, show) {
  if(show)
    $(targetDiv).setStyles({'background-image': 'url(/wcsstore/ErnstingStore/images/ajax_loader.gif)', 'background-position': 'center', 'background-repeat': 'no-repeat'}); 
  else
    $(targetDiv).setStyles({'background-image': '', 'background-position': '', 'background-repeat': ''}); 
}

/**
 * position of a container according current position of anchor container
 */
AjaxObj.prototype.positionContainer = function (targetDivId) {
  if($(targetDivId + "_anchor")){
    var anchorDiv = $(targetDivId + "_anchor");
    $(targetDivId).setStyles({top: (anchorDiv.getPosition().y + 20) + 'px', left: anchorDiv.getPosition().x  + 'px'});
  }
}  
