var mkw_Ajax_Url =   'ajax.html';
var http =           null;
var destName =       'ajax';

function Loading()
{
var dest = document.getElementById(destName);

   dest.innerHTML = '<div class="a1" style="color: #AAAAAA; height: 372px; margin: 8px; text-align: center;">Proszę <img class="vm" src="_d/ico/ml.gif" alt=""/> czekać</div>';
}

function Complete()
{
var dest = document.getElementById(destName);
   
   res = http.ResponseText;
   if (typeof(res) == 'string')
      dest.innerHTML = res;
   else
      dest.innerHTML = '';
}

function ErrorHandler(i)
{
	try
	{
	   var dest = document.getElementById(destName);
	
	   dest.innerHTML = '<div class="a1" style="margin: 20px; text-align: center;"></div>';
	}
	catch(e){}

}

function AjaxRequest(url)
{
   if (url)
   {
      http = new mkwHTTP();
      http.Get({
      'url' : url,
      'onLoading':   Loading,
      'onComplete':  Complete,
      'onError':     ErrorHandler
      });
   }
}

function mkwHTTP(vl)
{
   var o           = new Object();
   o.method        = true;
   o.MimeType      = "text/html";

   o.ErrorMsg      = false;
   o.HttpRequest   = null;
   o.requestTimer  = null;

   o.rqTimeOut     = 10000;
   o.ResponseText  = null;
   o.UserObj		 = null;
   o.ResponseXml   = null;
   o.ResponseError = null;
   o.onLoad        = null;
   o.onComplete    = null;
   o.ononSuccess   = null;
   o.onError       = null;
   o.onLoading     = null;
   o.vl            = 1;
   if (vl!='undefined')
   	o.vl = vl;
   o.ch            = 0;
   
   o.createHTTP = function()
   {
      try{   return new XMLHttpRequest();   }
      catch(Err1)
      {
         try{return new ActiveXObject("Msxml2.XMLHTTP");   }
         catch(Err2){
            try{   return new ActiveXObject("Microsoft.XMLHTTP");}
            catch(Err3){ return null; }
         }
      }
   }

   o.SetMime = function(Mime)
   {
      o.MimeType = Mime;
   }

   o.isFunction   = function(Val)
   {
      if(typeof Val == "function")
         return 1;
      return 0;
   }

   o.PrepareArgs = function(args)
   {
		o.UserObj = args.UserObj;
		
      for(parm in args)
      {
         switch(parm)
         {
            case 'onLoad':
            case 'onLoading':
            case 'onComplete':
            case 'onSuccess':
            case 'onError':
               if(o.isFunction(args[parm]))
                  eval('o.'+parm+'= args.'+parm+';');
               break;
         }
      }
   }

   o.RemoveHTTP = function()
   {
      delete o.HttpRequest["onreadystatechange"];
      o.HttpRequest = null;
   }

   o.mkwHTTPConstruct = function()
   {

      if(o.HttpRequest != null)
         o.RemoveHTTP();

      o.HttpRequest=o.createHTTP();

      if(o.HttpRequest==null)
      {
         o.ErrorMsg = true;
         return 0;
      }

      if(typeof o.HttpRequest.overrideMimeType == 'function')
         o.HttpRequest.overrideMimeType(o.MimeType);

      o.HttpRequest.onreadystatechange = o.ReadyState;
		o.requestTimer = setTimeout(function() {
			o.HttpRequest.abort();
			if(o.isFunction(o.onError))
            o.onError(o);
		}, o.rqTimeOut);
   }


   o.vlf = function(i)
   {
      if(o.isFunction(o.onError))
      {
         switch(o.vl)
         {
            case 1:
               o.onError();
               break;
            case 2:
               o.onError(o.HttpRequest.status);
               break;
            case 3:
               o.onError(o.HttpRequest.getAllResponseHeaders());
               break;
            
         }
      }
      else
      {
         o.ErrorMsg = (i) ? i : o.HttpRequest.status;
      }

   }
   o.ReadyState = function()
   {

      if(o.HttpRequest!=null)
      {
      	if (o.HttpRequest.readyState != 4)
      	{
      		if (typeof o.requestTimer != 'undefined')
      		{
      			clearTimeout(o.requestTimer)
      		}
      	}
         switch(o.HttpRequest.readyState)
         {
            case 0:
            case 1:
            case 2:
               if(o.isFunction(o.onLoading))
                  o.onLoading(o);
               break;
            case 3:
               if(o.isFunction(o.onLoad))
                  o.onLoad(o);
               break;
            case 4:
               if(o.HttpRequest.status==200)
               {
                  if(o.MimeType.indexOf('html')) {
                     o.ResponseText=""+o.HttpRequest.responseText+"";
                     o.ResponseText = o.ResponseText.replace(/<!-- onet:.*-->/, '');
                  }
                  else if(o.MimeType.indexOf('xml'))
                     o.ResponseXml=o.HttpRequest.responseXML;
                  if(o.isFunction(o.onSuccess))
	                  o.onSuccess(o);
               }
               else
               {
                   o.vlf();
               }

               if(o.isFunction(o.onComplete))
                  o.onComplete(o);
               break;
                  default:
                  o.vlf();
               break;
         }
      }
   }

   o.setCh = function()
   {
      o.HttpRequest.setRequestHeader("Accept-Charset","ISO-8859-2,utf-8;q=0.7,*;q=0.7")
   }

   o.Get = function(args)
   {
      o.mkwHTTPConstruct();
      o.PrepareArgs(args);

      if(typeof args=='object' && args.url != null)
      {
         if(!args.user)
            args.user = null;
         if(!args.passwd)
            args.passwd = null;
         try
         {
            o.HttpRequest.open("GET", args.url, o.method,args.user,args.passwd );
            if(o.ch)
               o.setCh();

            o.HttpRequest.send(null);
         }
         catch(e)
         {  o.ErrorMsg=e;
            return 0;
         }
      }
   }

   o.Post = function(args)
   {
      o.mkwHTTPConstruct();
      o.PrepareArgs(args);

      if(typeof args=='object' && args.url != null)
      {
         if(!args.user)
            args.user = null;
         if(!args.passwd)
            args.passwd = null;

         if(!args.post)
         {
            o.vlf('Empty post arg');
         }

         try
         {
            o.HttpRequest.open("POST", args.url, o.method,args.user,args.passwd );
            o.HttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            if(o.ch)
               o.setCh();

            o.HttpRequest.send(args.post);
         }
         catch(e)
         {
            o.ErrorMsg=e;
            return 0;
         }
      }
   }

   return o;
}
