/*==========================================
 Xhr.js
 XmlHttpRequestクラス
 -------------------------------------------
 初版 2007-11-20 by crosshatch.jp
==========================================*/
function Xhr()
{
    this.name = "Xhr";
    this.xhr;

    var tid;

    try
    {
        this.xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            this.xhr = false;
        }
    }
    
    if (!this.xhr && typeof XMLHttpRequest != undefined)
    {
        this.xhr = new XMLHttpRequest();
    }
    
    //------------------------------------------
    //void request()
    //リクエスト開始
    // 引数
    // 	url			リクエスト先uri
    // 	params		パラメータ(連想配列）
    // 	onSuccess	正常終了時のコール先
    // 	onFailure	異常終了時(200以外)のコール先
    // 	async		非同期/同期
    // 	sec			タイムアウト秒
    // 	onTimeout	タイムアウトコール先
    //------------------------------------------
    this.request = function(url, params, onSuccess, onFailure, async, sec, onTimeout)
    {
        var xhr = this.xhr;
        var requesturl = url;
        if (async == undefined) async=true;
        
        if(sec == undefined) sec=40;
        
        tid = setTimeout(
                function(){
                    xhr.abort();
                    if (onTimeout!=undefined)
                    {
                        onTimeout();
                    }
                    else
                    {
                        alert("Xhr timeout:"+url);
                    }
                },
                sec*1000
        );

        xhr.open("POST", url, async);
		try
		{
        	xhr.onload = function()
        	{
            	if (xhr.readyState == 4)
	            {
    	            if(tid) clearTimeout(tid);
        	        
            	    if (xhr.status==200)
                	{
                    	if(onSuccess!=null) onSuccess(xhr.responseText);
                	}
                	else
                	{
                    	if(onFailure!=null) onFailure(xhr.status);
                	}
            	}
        	}
        }
        catch(e)
        {
	        xhr.onreadystatechange = function()
    	    {
        	   	if (xhr.readyState == 4)
				{
    	        	if(tid) clearTimeout(tid);
                
           	    	if (xhr.status==200)
               		{
                   		if(onSuccess!=null) onSuccess(xhr.responseText);
               		}
               		else
               		{
                   		if(onFailure!=null) onFailure(xhr.status);
               		}
           		}
	        }
        }
        xhr.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");

        var wk = new Array();
        for (var key in params)
        {
            if (typeof params[key]=="object")
            {
                for (param in params[key])
                {
                    if ( params[key][param] && (typeof params[key][param] == "string" || typeof params[key][param] == "number") )
                        wk.push( encodeURIComponent(key) + "=" + encodeURIComponent(params[key][param]) );
                }
            }
            else
            {
                if ( params[key]  && (typeof params[key] == "string" || typeof params[key] == "number") )
                    wk.push( encodeURIComponent(key) + "=" + encodeURIComponent(params[key]) );
            }
        }
        param = wk.join("&");
        xhr.send(param);
    }
}
