/*
alert(navigator.appCodeName);
alert(navigator.appName);
alert(navigator.appVersion);
alert(navigator.cookieEnabled);
alert(navigator.language);
alert(navigator.platform);
alert(navigator.userAgent);
*/

/* vim: set foldmethod=marker: */
// {{{ $RCSfile: ajax.js,v $
/**
 * goobike.com SA において Ajax 非同期接続を行う為の JavaScript
 * Opera 対応 => Opera7対応は、IFRAME 通信を行う
 *
 * @author    M.Yonebayashi    2005/03/29
 * $Date: 2005/09/13 09:43:27 $
 * $Revision: 1.1.1.1 $
 * $Author: sa_owner $
 */
// {{{ define
// HTTP Status code
var HTTP_STATUS_OK        = 200;
var HTTP_STATUS_NOT_FOUND = 404;

// Object Status
var XML_HTTP_REQUEST_STATUS_UNINITIALIZED = 0;
var XML_HTTP_REQUEST_STATUS_LOADING       = 1;
var XML_HTTP_REQUEST_STATUS_LOADED        = 2;
var XML_HTTP_REQUEST_STATUS_INTERACTIVE   = 3;
var XML_HTTP_REQUEST_STATUS_COMPLETE      = 4;

// }}}

// ActiveXObject
var axo = new Array(
    'Microsoft.XMLHTTP',
    'Msxml2.XMLHTTP.4.0',
    'Msxml2.XMLHTTP.3.0',
    'Msxml2.XMLHTTP'
);

/**
 * Ajax response 処理クラス<br>
 * 本関数を使用する際は, 必ずAjaxActionと同様に
 * doAction メソッドを実装する必要があります。
 * 本クラスを使用してオーバーライドしても可
 */
function AjaxAction(){
    this.name = "AjaxActionObject";

    /**
     * リクエストするURLを設定します<br>
     * 相対・絶対パスどちらでも構いません。
     */
    //this.url = "/php/sample/entry/ajax/ajax.php";
    this.url = "/php/client/ajax.php";

    /**
     * パラメータを設定
     * XMLHttpRequest でのリクエストパラメータに設定されます。
     *
     * 以下の連想配列の場合, 下記のようなリクエストになります。
     * var a = { "key1":"value1" , "key2":"value2" , "key3":"value3" };
     *
     * http://hoge.com?key1=value1&key2=value2&key3=value3
     */
    this.params = null;

    /**
     * Ajaxでのレスポンスを処理します
     */
    this.doAction = function(ajaxRes){
        // ここにAjaxでのresponseを処理する
        // ロジックを記述
        alert(ajaxRes);
    };
}

// {{{ function createXmlHttp
/**
 * createXmlHttp
 */
function createXmlHttp(){
    var xmlHttp = false;
    // 条件コンパイルを行う
    /*@cc_on
    @if (@_jscript_version >= 5)
    for(var i = 0; !xmlHttp && i < axo.length; i++){
        try{
            xmlHttp = new ActiveXObject(axo[i]);
        }catch(e){
            //alert(e);
        }
    }
    @else
        xmlHttp = false;
    @end @*/
    if(!xmlHttp && typeof XMLHttpRequest != 'undefined'){
        try{
            // for Firefox, safari
            xmlHttp = new XMLHttpRequest();
            //xmlHttp.overrideMimeType("text/xml");
        }catch(e){
            xmlHttp = false;
        }
    }
    // Opera 対応
    if(!xmlHttp){
        xmlHttp = new IFRAMEHttpRequest();
    }
    return xmlHttp;
}
// }}}
function counter(){
    if(!this.count)this.count = 1;
    else this.count++;

    return this.count;
}
// {{{
/**
 * IFRAME によりAjax通信を行う<br>
 * 既存の XMLHttpRequest オブジェクトのメソッドは
 * 全てオーバーライドする
 */
function IFRAMEHttpRequest(){
//alert(window.opera);
    this.ifm = document.createElement("IFRAME");
    this.ifm.id = "load_" + counter();
/***********
    with(this.ifm.style){
        width="400";
        height="400";
    }
***********/

    this.div = document.createElement("DIV");
    with(this.div.style){
        visibility="hidden";
        position="absolute";
        left="-10";
        top="-10";
        width="100";
        height="100";
    }
    this.div.appendChild(this.ifm);

    this.onreadystatechange = function(){
        //alert("iframe onreadystatechange");
    };

    this._header = new Object();
    this.getResponseHeader = function(key){
        return this._header[key];
    };

    this.open = function(method, url, sync){
        this.method = method;
        this.requestURL = url + "&div=ifm&time=" + new String(new Date());
    };
    this.setRequestHeader = function(header){
        //alert(header);
    };
    this.sleep = function(ms){
        var t1 = new Date().getTime();
        var t2 = new Date().getTime();
        while(t2 < t1 + ms){
            t2 = new Date().getTime();
        }
        return;
    }
    this.resAjax = function(){
        try{
        while(!document.frames[this.ifm.id]){
            this.sleep(100);
        }
        this.responseText = "\n\n" + document.frames[this.ifm.id].document.body.innerHTML;
        this.readyState = XML_HTTP_REQUEST_STATUS_COMPLETE;
        this.status = HTTP_STATUS_OK;
        this.onreadystatechange();
        this.div.removeChild(this.ifm);
        document.body.removeChild(this.ifm.div);
        }catch(e){
            alert("処理中...");
            this.resAjax();
        }
    };

    this.send = function(query){
        this._GET();
    };

    this._GET = function(){
        this.ifm.src = this.requestURL;
        document.body.appendChild(this.div);
        this.resAjax();
    };
    return this;
}
// }}}

// {{{ function execAjax
/**
 * Ajax を使用しRequest & get Response
 *
 * @param    object    doAjax レスポンス処理クラス(doActionメソッドを必ず実装)
 *
 * @author   M.Yonebayashi    2005/03/28
 */
function execAjax(doAjax){
    var xmlHttp = createXmlHttp();
    if(!document.getElementById) return;
    var sURL = doAjax.url + "?time=";
    var sParam = "";
    if(null != doAjax.params){
        // set parameter
        for(var key in doAjax.params){
            sParam += "&" + key + "=" + doAjax.params[key];
        }
//alert(sParam);
    }
    sURL += sParam;
    if(xmlHttp){
        // ======================================================
        // event Handler 登録
        // object の状態が変化した際に実行されるイベントハンドラ
        // Java Applet の eventListnerと同様
        // ex) xmlHttp.onreadystatechange = 関数名;
        // ======================================================
        ajax_event(xmlHttp, doAjax);

        xmlHttp.open("GET", sURL, true);
        xmlHttp.setRequestHeader("If-Modified-Since", new String(new Date()));
        xmlHttp.send(null);
    }
}
// }}}

/**
 * onreadystatechange event handler
 *
 * @param    XMLHttpRequest    req
 * @param    object            doAjax
 */
function ajax_event(req, doAjax){
    req.onreadystatechange = function(){
        // HTTP status が正常でかつ、XMLHttpRequest の状態が完了の場合
        if(XML_HTTP_REQUEST_STATUS_COMPLETE == req.readyState
                && HTTP_STATUS_OK == req.status){
            doAjax.doAction(req.responseText);
        }
    };
}


function go(key, val, opt){
    var params = new Array(val);
    //var data = execAjax(opt, key, params);
    execAjax(opt, key, params);

//  createDynamicList(data, opt);
}

// 排気区分を選択された場合
function selectHaiki(key, val, opt){
    var maker = document.frm.maker;
    if(-1 == maker.selectedIndex){
        //alert("メーカを選択してよ");
        //maker.focus();
        return;
    }
    var params = new Array("", maker.value, val);
    execAjax(opt, key, params);
}
function selectColor(key, val, opt){

    // 先に色系統の文字列
    var params = new Array("", val);

    var params = new Array("", val);
    execAjax(opt, key, params);
}

function sjis(){
    execAjax(null, "T", null);
}

function test(key, val){
    var maker = document.frm.maker;
    var hkbn  = document.frm.haikikbn;
    if(-1 == maker.selectedIndex || -1 == hkbn.selectedIndex){
        alert("メーカ・排気を選択して下さい。");
        return false;
    }
    var params = new Array(val, maker.value, hkbn.value);

    execAjax(null, key, params, true);
}

function goSyasyu(key, val, opt){
    //if("" == val) return;
    var maker = document.frm.maker;
    var hkbn  = document.frm.haikikbn;
    if(-1 == maker.selectedIndex || -1 == hkbn.selectedIndex){
        alert("メーカ・排気を選択して下さい。");
        return false;
    }

    // 選択状態の場合は, Ajaxしない
    // あくまで選択支援機能
    if(opt.selectedIndex != -1){
        return false;
    }

    var params = new Array(val, maker.value, hkbn.value);
    execAjax(opt, key, params);
}

/**
 * 指定文字列よりリスト生成
 *
 * @param   string    ajax_res    Ajax により取得したデータ
 */
function createDynamicList(ajax_res, opt){
    var tmp = ajax_res.split("\\n\\n");
    data = tmp[1];
    var iListCnt = opt.length;
    // 既存リスト削除
    for(i = iListCnt - 1; i >= 0; i--){
        opt.options[i] = null;
    }
    if(ajax_res == "") return;
    // リスト生成
    var work = data.split("\\n");
    for(j = 0; j < work.length - 1; j++){
        var tmp = work[j].split("\\t");
        id  = tmp[0];
        txt = tmp[1];
        opt.options[j] = new Option(txt, id);
    }
}

function createDynamicListEx(ajax_res, opt){
    var iListCnt = opt.length;
    // 既存リスト削除
    for(i = iListCnt - 1; i >= 0; i--){
        opt.options[i] = null;
    }
    if(ajax_res == "") return;

    // リスト生成(色の場合は２つ)
    var tmp = ajax_res.split("\\t\\t");
    var tmp1 = tmp[0];
    var tmp2 = tmp[1];
    var work = tmp1.split("\\n");
    for(j = 0; j < work.length - 1; j++){
        var tmp = work[j].split("\\t");
        id  = tmp[0];
        txt = tmp[1];
        opt.options[j] = new Option(txt, id);
    }
    document.frm.color_idx_string.value = tmp2;
}

/**
 * メーカ選択時
 */
function selectMaker(key, val, opt){
    //clearAll();
}








function hoge(obj){
    // go メソッドを実行
    obj.go(obj.name);
}

function Test(){

    this.go = function(txt){
        alert("Test:" + txt);
    };
    this.name = "Test";
}
function Foo(){
    this.go = function(txt){
        alert("Foo:" + txt);
    }
    this.name = "Foo";
}








/**
 * 値をクリアする
 *
 * @author    M.Yonebayashi    2005/03/29
function clearAll(){
    var frm = document.frm;
    frm.haikikbn.selectedIndex = -1;
    frm.color.selectedIndex    = -1;

    // リストを動的に削除
    createDynamicList("", frm.syasyu);
    createDynamicList("", frm.color_idx);

    frm.syasyu_name.value      = "";
    frm.color_idx_string.value = "";
    frm.color_idx_name.value   = "";
    //frm.year.value             = "";
    //frm.year_from.value        = "";
    //frm.year_to.value          = "";

    document.getElementById("haikikbn_str").innerHTML  = "";
    document.getElementById("syasyu_str").innerHTML    = "";
    document.getElementById("color_str").innerHTML     = "";
    document.getElementById("color_idx_str").innerHTML = "";
}
 */

/**
 * 車種選択時処理
 *
 * @param    object    opt    SELECT object
 * @param    object    txt    TEXT object
 */
function selectSyasyu(opt, txt){
    var idx = opt.selectedIndex;

    var text = opt.options[idx].text;

    txt.value = text;
}
function selectColorIdx(frm, opt){
    var idx = opt.selectedIndex;
    var color_array = (frm.color_idx_string.value).split(",");
    frm.color_idx_name.value = color_array[idx];

}

/**
 * encodeURL
 */
function encode(val){
    if(encodeURIComponent) return encodeURIComponent(val);
    if(escape)return escape(val);
}
// }}}


