//ajax
//usage:
/*
new ajax({
    method: 'get',
    href  : 'test.php',
    onComplete : function(text){
        alert(text);
    },
    onLoading : function(){
        alert('loading..');
    },
    asyn : 'true',
    parameters : {
        id : 12,
        name : 'test',
        age : 17
    },
    type : 'xml',
    cache : true
});

*/

ajax = function($)
{
    var _self           = this;
    this.href           = $.href        || alert('AJAX Error : Page isn\'t given!');
    this.method         = $.method      || 'post';
    this.asyn           = $.asyn;       if(typeof(this.asyn) === 'undefined')   this.asyn = true ;
    this.type           = $.type        || 'default';
    this.parameters     = $.parameters  || false;
    this.cache          = $.cache;      if(typeof(this.cache) === 'undefined')   this.cache = true ;
    this.onComplete     = $.onLoaded  || false;
    this.onLoading      = $.onLoading   || false;
    this.ajaxRequest    = null;
    //Ajaxobject

    try{ this.ajaxRequest = new XMLHttpRequest(); }catch(e){
  	    try{ this.ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){
  		    try{ this.ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
  	}}
    
    var ajaxReq = this.ajaxRequest;

    if(this.ajaxRequest == null)
    {   alert('AJAX Error:  no AJAXRequest avaible!');
        return false;   }
    //***************

    if(eval(this.asyn)){
          if(this.onComplete)
          {
              this.ajaxRequest.onreadystatechange = function()
              {
                  if(ajaxReq.readyState == 4)
                  {
                      if(_self.type.toUpperCase() == 'XML')
                          _self.onComplete(ajaxReq.responseXML);
                      else
                          _self.onComplete(ajaxReq.responseText);
                  }else{
                      if(_self.onLoading)
                          _self.onLoading();
                 
                  }
              }
          }
    }

    if(this.method.toUpperCase() == 'GET')
    {
        var paramString = "?";
        if(this.cache)
        {
            var time = new Date();
            var time = time.getTime();
            paramString += "cache_="+time+"&";
        }


        if(this.parameters){
            for(var p in this.parameters)
                paramString += p+"="+this.parameters[p]+"&";
        }

        this.ajaxRequest.open('GET', this.href+paramString, eval(this.asyn));
        this.ajaxRequest.send(null);
    }

    if(this.method.toUpperCase() == 'POST')
    {
        var paramString = "";
        if(this.parameters){
            for(var p in this.parameters)
                paramString += p+"="+this.parameters[p]+"&";
        }
        this.ajaxRequest.open('POST', this.href, eval(this.asyn));
        this.ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        this.ajaxRequest.send(paramString);
    }

    if(!eval(this.asyn)){
        if(this.type.toUpperCase() == 'XML')
            this.onComplete(this.ajaxRequest.responseXML);
        else
            this.onComplete(this.ajaxRequest.responseText);
    }
}



