/**
 *  Author: Robin Pan (htmlor [at] gmail.com)
 *
/*----------------------------------------------------------------*/


/* 
 *  XmlHttpRequest() [class]
 *
 */

function XmlHttpRequest(bAsync){
    $async = (bAsync != null ? bAsync : true);
    
    this.send = function(sURL, sMethod, oData, fCallback){
        var req = null;
        // support XMLHttpRequest
        if(window.XMLHttpRequest){
            req = new XMLHttpRequest();
        }
        // support ActiveX
        else if(window.ActiveXObject){
            try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e){
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        if(req == null){
            return false;
        }
        
        req.onreadystatechange = function(){
            // complete
            if(req.readyState == 4){
                if(req.status == 200){
                    fCallback("ok", req);
                }
                else{
                    fCallback("ex", req);
                }
            }
            // loading
            else{
                fCallback("ing", req);
            }
        }
        
        req.open(sMethod, sURL, $async);
        // send header(post)
        if(sMethod == "post"){
            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        }
        req.send((sMethod == "post") ? oData : null);
        
        return true;
    };
}


/* 
 * InstSubmit [class]
 *
 * @param string sUrl 提交地址
 * @param object oParams 要提交的参数
 * @param element eTrigger 提交动作触发元件
 * @param function fCallback 回调函数
 *
 */

function InstSubmit(sUrl, oParams, eTrigger, fCallback){
    var $url = sUrl,
    $params = oParams,
    $trigger = eTrigger,
    $callback = fCallback,
    $method = "post";
    
    var ME = this;
    var $xmlhttp = null;
    
    // 设置参数
    this.setParams = function(oParams){
        $params = oParams;
    };
    // 设置某个参数
    this.setPara = function(sName, sValue){
        $params[sName] = sValue;
    };
    // 设置参数提交方法
    this.setMethod = function(sType){
        $method = sType;
    };
    
    // 执行提交动作
    this.execute = function(){
        /*
        if($params == null){
            window.alert("no params!");
            return false;
        }
        */
        var paramstr = "";
        for(var param in $params){
            paramstr += (paramstr == "" ? "" : "&") + param + "=" + $params[param];
        }
        var urlstr, data;
        if($method == "post"){
            urlstr = $url;
            data = paramstr;
        }
        else{
            urlstr = $url + (paramstr == "" ? "" : "?" + paramstr);
            data = null;
        }
        if($xmlhttp == null){
            $xmlhttp = new XmlHttpRequest();
        }
        return $xmlhttp.send(urlstr, $method, data, this.response);
    };
    
    // 响应提交结果
    this.response = function(sFlag, oReq){
        switch(sFlag){
            case "ing":
                if($trigger != null){
                    ME.weakenTrigger();
                }
                ME.resLoading(oReq);
                break;
            case "ex":
                if($trigger != null){
                    ME.weakenTrigger(0);
                }
                ME.resException(oReq);
                break;
            case "ok":
                if($trigger != null){
                    ME.weakenTrigger(0);
                }
                ME.resComplete(oReq);
                break;
        }
    };
    this.resLoading = function(oReq){
        this.onLoading(oReq);
    };
    this.resException = function(oReq){
        this.onFailure(oReq);
    };
    this.resComplete = function(oReq){
        if(oReq.responseText == "success"){
            this.onSuccess(oReq);
            // 响应成功后执行回调函数
            if($callback != null){
                $callback($params, $trigger);
            }
        }
        else{
            this.onFailure(oReq);
        }
    };
    
    this.onLoading = function(oReq){
    };
    this.onSuccess = function(oReq){
    };
    this.onFailure = function(oReq){
        window.alert("系统错误："+oReq.responseText);
    };
    
    this.weakenTrigger = function(iStatus){
        if(iStatus == null){
            iStatus = 1;
        }
        $trigger.disabled = (iStatus == 1 ? true : false);
    };
}


/* 
 * InstQuery [class]
 *
 * @param string sUrl 提交地址
 * @param element eTrigger 提交动作触发元件
 * @param function fCallback 回调函数
 *
 */

function InstQuery(sURL, eTrigger, fCallback){
    var $url = sURL,
    $trigger = eTrigger,
    $callback = fCallback,
    $params = null,
    $method = "get";
    
    var ME = this;
    var $submitter;
    
    this.setParams = function(oParams){
        $params = oParams;
    };
    this.setPara = function(sName, oValue){
        $params[sName] = oValue;
    };
    this.setMethod = function(sType){
        $method = sType;
    };
    
    this.execute = function(){
        /*
        if($params == null){
            window.alert("no params");
            return false;
        }
        */
        if($submitter == null){
            createSubmitter();
        }
        return $submitter.execute();
    };
    this.onLoading = function(oReq){
    };
    this.onFailure = function(oReq){
        window.alert("系统错误："+oReq.responseText);
        //$callback($params, oReq);
    };
    this.onSuccess = function(oReq){
        if($callback != null){
            $callback($params, oReq);
        }
    };
    
    var createSubmitter = function(){
        $submitter = new InstSubmit($url, $params, $trigger);
        $submitter.setMethod($method);
        $submitter.resComplete = function(oReq){
            var hasBadXml = false;
            if(oReq.responseXML){
                // in ie responseXML be never null
                if(oReq.responseText.indexOf("<?xml") != -1){
                    // if parse error occurs, in ie documentElement is null, in ff documentElement.nodeName is parsererror
                    try{
                        hasBadXml = (oReq.responseXML.documentElement.nodeName == "parsererror");
                    }
                    catch(e){
                        hasBadXml = true;
                    }
                }
            }
            if(oReq.responseText == "" || hasBadXml){
                this.onFailure(oReq);
            }
            else{
                this.onSuccess(oReq);
            }
        };
        $submitter.onLoading = function(oReq){
            ME.onLoading(oReq);
        };
        $submitter.onFailure = function(oReq){
            ME.onFailure(oReq);
        };
        $submitter.onSuccess = function(oReq){
            ME.onSuccess(oReq);
        };
    };
}
