var timeoutSec = 20;

var httpObj = false;
var timerID;
var timerCounter = timeoutSec;
var putResult;

function setHttpTimeout(sec) {

    if (sec > 0) {
	timeoutSec = sec;
    }
}

function httpRequest(URL, functionReference) {

    if (httpObj) {
	return (false);
    }

    putResult = functionReference;
    timerCounter = timeoutSec;

    try {
    	if (window.XMLHttpRequest) {
	    httpObj = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
	    httpObj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
	    httpObj = false;
	}
    }
    catch (e) {
    	httpObj = false;
    }
    if (!httpObj) {
	putResult('httpRequest: このブラウザはサポートされていません。');
	return (false);
    }

    timerID = setInterval('timeoutCheck()', 1000);

    httpObj.open("GET", URL, true);
    httpObj.onreadystatechange = function() {
    	if (httpObj.readyState == 4) {
	    clearInterval(timerID);
	    if (httpObj.status == 200) {
	    	putResult(httpObj.responseText);
	    }
	    else {
	    	putResult(httpObj.status + ": " + httpObj.statusText);
	    }
	    httpObj = false;
	}
    }
    httpObj.send('');

    return (true);
}

function timeoutCheck() {

    timerCounter--;
    if (timerCounter <= 0) {
    	clearInterval(timerID);
	httpObj.abort();
	putResult('サーバへの要求がタイムアウトしました。');
	httpObj = false;
	return (false);
    }
}
