// This file was written in UTF-8.

var num = 1;

// IE でのメモリリークを防ぐための空の関数
function empty_func()
{
}

// Ajax によるリクエスト処理
function do_request(id)
{
    // XMLHttpRequest インスタンスをアロケート
    var client;
    try{
	// 今は MSXML2.XMLHTTP.3.0 が最新なのかな?
	// http://msdn.microsoft.com/en-us/library/ms759148.aspx
	client = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    } catch (e) {
	try {
	    client = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	    try {
		client = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e) {
		client = new XMLHttpRequest();
	    }
	}
    }

    // XMLHttpRequest インスタンスの onreadystatechange ハンドラに
    // 無名関数をセットする
    client.onreadystatechange = function() {
	// あ…ありのまま 今　起こった事を話すぜ！
	// 『IE だと XMLHttpRequest オブジェクトは this を持たない』
	// な…　何を言ってるのか　わからねーと思うが
	// おれも何をされたのかわからなかったry
	if (client.readyState != 4)
	    return;

	// IE だと onreadystatechange にセットされた
	// 無名関数 (この関数自身) がメモリリークするので
	// グローバルスコープの関数 empty_func で上書きする
	client.onreadystatechange = empty_func;

	if (client.status != 200)
	    throw("Got " + client.status + " error");

	var res = client.responseXML;
	if (res.parseError != null && res.parseError.errorCode != 0) {
	    throw(res.parseError);
	} else if ((res.documentElement.tagName == 'parsererror' &&
		    res.documentElement.namespaceURI ==
		    "http://www.mozilla.org/newlayout/xml/parsererror.xml") ||
		   (res.documentElement == null)) {
	    throw("Failed to parse document");
	}

	var s = "" + (num++) + ": " + res.documentElement.firstChild.data;
	var div = document.createElement("div");
	var text = document.createTextNode(s);
	div.appendChild(text);
	var attach = document.getElementById(id);
	attach.appendChild(div);
    }

    client.open("GET", "rand.php", true);
    client.send(null);
}

// フォームに渡された文字列を数値に変換して do_request を呼び出す
function do_action()
{
    var form = document.getElementById('testform');

    var n = parseInt(form.count.value);
    if (isNaN(n) || n <= 0) {
	n = 1;
    }

    for (var i = 0; i < n; ++i) {
	do_request('attach');
    }
}
