/*
* AjaxService
*/
function AjaxService()
{
  /*
  * コネクション生成
  */
 this.conn = function()
 {
    xmlhttp = false;
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        xmlhttp = false;
      }
    }
    if (!xmlhttp && typeof XMLHttpRequest  != 'undefined') {
      xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp ;
 }
 
  /*
  * 実行
  */
  this.execute = function( url, param, Listener )
  {
    //var xmlhttp = this.conn();
    xmlhttp = this.conn();
    
    if (xmlhttp)
    {
      xmlhttp.onreadystatechange = function()
      {
        switch( xmlhttp.readyState )
        {
          case 0 : // UNINITIALIZED(openが呼ばれてない)
            if( null != Listener.onUninitialized )
              Listener.onUninitialized();
            break;
          case 1 : // LOADING(sendが呼ばれてない) 
            if( null != Listener.onLoading )
              Listener.onLoading();
            break;
          case 2 : // LOADED(responseがまだ) 
            if( null != Listener.onLoaded )
              Listener.onLoaded();
            break;
          case 3 : // INTERACTIVE(部分的に応答着たよ)
            if( null != Listener.onInteractive )
              Listener.onInteractive();
            break;
          case 4 : // COMPLETED(完了) 
            if( 200 == xmlhttp.status )
              Listener.onCompleted( xmlhttp.responseText );
            else // エラー
              Listener.onError( xmlhttp.status );
            break;

        }
      }
      
      switch( typeof(param) )
      {
      	case 'object':
          xmlhttp.open('POST', url, true);
          if( null == param )
          {
            xmlhttp.send(null);
          }
          else
          {
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            var seri_str = '';
            for( i=0 ; i<param.length ; i++ )
              seri_str += param[i].serialize() + '&';
            xmlhttp.send(seri_str);
          }
          break;
        case 'string':
        case 'number':
          xmlhttp.open('GET', url+"?"+param, true);
          xmlhttp.send(null);
          break;
        default :
          xmlhttp.open('GET', url, true);
          xmlhttp.send(null);
          break;
      }
    }
  }
}


/*
* リスナー
*/
function Listener()
{
  /*
  * UNINITIALIZED(openが呼ばれてない)
  */
  this.onUninitialized = function() { }

  /*
  * LOADING(sendが呼ばれてない)
  */
  this.onLoading = function() { }

  /*
  * LOADED(responseがまだ) 
  */
  this.onLoaded = function() { }

  /*
  * INTERACTIVE(部分的に応答着たよ)
  */
  this.onInteractive = function() { }

  /*
  * COMPLETED(完了) 
  */
  this.onCompleted = function( responseText ) { }

  /*
  * エラー発生
  */
  this.onError = function( status ) { }
}

/*
* 連想キー
*/
function AssocKey( key, val )
{
  this.key = key;
  this.val = val;
  
  this.serialize = function()
  {
    return this.key +'='+ this.val;
  }
}
