function Ajax()
{

   this.synchronous = false;


   this.getRequestObject = function()
   {
      var req = null;
      if (typeof XMLHttpRequest != "undefined") req = new XMLHttpRequest();
      if (!req && typeof ActiveXObject != "undefined")
      {
         try {req = new ActiveXObject("Msxml2.XMLHTTP");}
         catch(e)
         {
            try {req = new ActiveXObject("Microsoft.XMLHTTP");}
            catch (e2)
            {
               try {req = new ActiveXObject("Msxml2.XMLHTTP.4.0");}
               catch (e3) {req = null;}
            }
         }
      }
      if(!req && window.createRequest) req = window.createRequest();
      if (!req) alert("Request Object Instantiation failed.");
      return req;
   }


   this.sendRequest = function(url,loc, params, httpMethod)
   {
       
	  if (url.charAt(url.length-1) == '#') url = url.substr(0, url.length-1);
      if (!httpMethod) httpMethod = "post";
      var req = this.getRequestObject();
      if (req)
      {
        ajax.showAjaxLoader(loc);
         req.open(httpMethod, url, !this.synchronous);
         req.setRequestHeader("Content-Type",
                        "application/x-www-form-urlencoded; charset=UTF-8");
         req.onreadystatechange = function()
         {
            if (req.readyState != 4) return;
            if (req.status == 200 || req.status == 0)
            {
               //alert(req.responseText);
			   eval(req.responseText);
               //ajax.hideAjaxLoader();
               delete req;
            }
            else
            {
              // ajax.hideAjaxLoader();
               alert("Error: the server returned the following HTTP status: " + req.status);
               return false;
            }
         }
         req.send(params);
         return true;
      }
      return false;
   }


   this.call = function(func)
   {
     
	  var url = new String(window.location);
      var params = "", i = 1;
      var args = arguments;	   	  
	
	 if (arguments.length == 2 && typeof(arguments[1]) == 'object')
      {
        args = arguments[1];
		i = 0;
      }
	  loc=args[1];
	
	  params += "ajaxfunc=" + encodeURIComponent(func);	
		  
      for (; i<args.length; i++)	  
	     params += "&ajaxargs[]=" + encodeURIComponent(ajax.encodeObj(args[i]));	 
      return this.sendRequest(url,loc,params);
   }


   this.encodeObj = function(param)
   {
      if (typeof(param) == 'object')
      {
         var obj = "<ajaxarray>";
         for (i in param)
         {
             if (i == 'constructor' || param[i] && typeof(param[i]) == 'function') continue;
             obj += "<k>"+i+"</k><v>"+ajax.encodeObj(param[i])+"</v>";
         }
         obj += "</ajaxarray>";
         return obj;
      }
      return param;
   }


  this.showAjaxLoader = function(loc)
   {
   
	 $('#'+loc).html('<div style="text-align:center;"><img src="/images/common/ajaxLoader.gif" style="border:1px solid #b7007b;background: #fff;"></div>');
   }


}

var ajax = new Ajax();

