////////////////////////////////////////////////////////////
// $Id$
////////////////////////////////////////////////////////////

// XMLHttpRequestオブジェクト生成
function createHttpRequest()
{
  var xmlhttp = null;
  if(window.ActiveXObject){
    try {
      // MSXML2以降用
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        // 旧MSXML用
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {

      }
    }
  } else if(window.XMLHttpRequest){
    // Win Mac Linux m1,f1,o8 Mac s1 Linux k3用
    xmlhttp = new XMLHttpRequest();
  } else {

  }
  if (xmlhttp == null) {
    alert("Can not create an XMLHTTPRequest instance");
  }
  return xmlhttp;
} 

// // ファイルにアクセスし受信内容を確認します
// function sendRequest (method, url, data, async, callback)
// {
//     // XMLHttpRequestオブジェクト生成
//     var xmlhttp = createHttpRequest();
    
//     // 受信時に起動するイベント
//     xmlhttp.onreadystatechange = function() { 
//         // readyState値は4で受信完了
//         if (xmlhttp.readyState == 4) { 
//             // コールバック
//             callback(xmlhttp);
//         }
//     }
//     // open メソッド
//     xmlhttp.open(method, url, async);
//     // 無いとPOSTデータが送れない
//     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//     // send メソッド
//     xmlhttp.send(data);
// }


// ファイルにアクセスし受信内容を確認します
function sendRequest (method, url, data, async, callback)
{
    // XMLHttpRequestオブジェクト生成
    var xmlhttp   = createHttpRequest();
    var safari    = navigator.userAgent.indexOf("Safari")!=-1;
    var konqueror = navigator.userAgent.indexOf("Konqueror")!=-1;
    var mozes     = ((a = navigator.userAgent.split("Gecko/")[1] )
                     ? a.split(" ")[0] : 0) >= 20011128 ;

    if(window.opera || safari || mozes){
        xmlhttp.onload = function ()
        {
            callback(xmlhttp);
        }
    } else {

        // 受信時に起動するイベント
    xmlhttp.onreadystatechange = function ()
        {
            // サーバーからの応答判定
            //   0 : 初期化されていません
            //   1 : 読み込み中です
            //   2 : 読み込み完了
            //   3 : 双方向に扱えます
            //   4 : すべて完了しました
            if (xmlhttp.readyState == 4) {
                
                // サーバーの応答コード判定
                //   200 : OK
                if (xmlhttp.status == 200) {
                    callback(xmlhttp);
                } else {
                    alert("There was a problem with the request.");
                }
            }
        }
    }
    
    // open メソッド
    xmlhttp.open(method, url, async);
    // 無いとPOSTデータが送れない
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    // send メソッド
    xmlhttp.send(data);
}

