﻿// JScript 檔
var _httpRequest = getHTTPObject();

var _isWorking = false;

var _functionName = 'doWork';

/**
 * MyHttpRequest
 * $Id: MyHttpRequest.js,v 1.4 2005/04/27 15:09:49 jaceju Exp $
 **/
MyHttpRequest = function() {

    this.params = new Array();

    this.headers = new Array();

    this.method = 'POST';

    this.async = true;

    this.init = function() {
        this.params = new Array();
        this.headers = new Array();
        this.method = 'POST';
    }

    this.setMethod = function(m) {
        m = m.toUpperCase();
        if (m == 'GET' || m == 'POST') {
            this.method = m;
        }
    }

    this.addParam = function(n, v) {
        this.params.push(n + '=' + escape(v));
    }

    this.getParam = function() {
        var var_body = this.params.join('&');
        if (var_body.length != 0) {
            if (this.method == 'GET') {
                var_body = '?' + var_body;
            }
            return var_body;
        }
        return '';
    }

    this.setRequestHeader = function(n, v) {
        this.headers.push(n);
        this.headers.push(v);
    }

    this.setHandleFunction = function(fn) {
        if (typeof fn == 'string' && fn.length > 0) {
            _functionName = fn;
        }
    }

    this.open = function(url) {
		
        _isWorking = false;
        if (!_isWorking && (typeof(_httpRequest) == 'object')) {
            if (this.method == 'GET') {
                url += this.getParam();
            }
            _httpRequest.open(this.method, url, this.async);
            for (var i = 0; i < this.headers.length; i = i + 2) {
                _httpRequest.setRequestHeader(this.headers[i], this.headers[i + 1]);
            }
            _httpRequest.onreadystatechange = function() {
                if (_httpRequest.readyState == 4) {
                    eval(_functionName + '()');
                    _isWorking = false;
                }
            }
            _isWorking = true;
            if (this.method == 'GET') {
                _httpRequest.send(null);
            }
            else {
                _httpRequest.send(this.getParam());
            }
        }
    }

    this.getResponseText = function() {
        return _httpRequest.responseText;
    }

    this.getResponseXML = function() {
        return _httpRequest.responseXML;
    }
}

/**
 * 取得 XmlHttpRequest 物件
 **/
function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
    @else
	    xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
            xmlhttp.overrideMimeType("text/xml"); 
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
