  // tp (c) 2010 Konstantin Kirillov. Landkey.net.

  // ajax.js - simple wrapper for AJAX.
  // Tries to create $tj.ajy.xml - ajax object when this file is loaded.
  // If fails, will repeat this attempt at first call to set.
  // ajy has three methods set, request, and set_and_request:
  // set opens xml, request makes a request.
  // onreadystatechange callcack is suppied to xml as a parameter of set.

  tj$ = window.tj$ || {};
  tj$.ajy = new ( function () { 
       that = this;
       // credits: 
       // https://developer.mozilla.org/en/ajax:getting_started
       // Ilya Cantor: http://javascript.ru/ajax/intro#chto-takoe-ajax-primer-realizacii
       var xml = null;

       var trier = function() {
          if( typeof XMLHttpRequest !== 'undefined' ) {
              xml = new XMLHttpRequest();
              if(xml.overrideMimeType) {
                 xml.overrideMimeType('text/xml'); //for quirky FF.
              }
          }else{
             try{ xml = new ActivXObject("Msxml2.XMLHTTP");
             }catch(e){
                 try{ xml = new ActiveXObject("Microsoft.XMLHTTP");
                 } catch (e) { 
                    //Give up:
                    widnow.status = 'no ajax object exist';
                    //console.log('initial ajax failure' + e);
                 }
             }
          }
       };
       trier();
       this.xml = xml;
       this.set = function (method, feeder, flag, onchange, request) { 
            if( !xml ) {
                trier();
                if( !xml ) {
                    //console.log('second attempt failure');
                    return;
                }
            }
            xml.onreadystatechange = function() { onchange(xml); };
            request_count = 0;
            that.request = function () {
              request_count++;
              //xml.open('GET', 'http://localhost/ajy_feedback.txt', true);
              //3rd par. true => asyn. 
              try{ 
                 xml.open( method, feeder, flag);  
              }catch(e){
                 //console.log( e);
                 //Give up.
              }
              xml.send(null);
              if( request ) request(request_count);
            };
       };
       this.set_and_request = function(method, feeder, flag, onchange, request){
            that.set(method, feeder, flag, onchange, request);
            that.request();
       };
     } )();


