/**********************************************************************

  チャット

**********************************************************************/

//設定項目
var scriptPath = './chat.cgi';
var filePath   = './chat.log';
var pastDir    = './past/';

//記事一覧作成
function getArticle(response) {
	var list = response.split('\n');

	var article = '<ul>';

	for (var i = 0; i < list.length; i++) {
		if (list[i]) {
			var data = list[i].split('\t');

			var date  = data[0].replace(/\d\d\d\d-(\d\d)-(\d\d)\s(\d\d):(\d\d):\d\d/, '$1/$2 $3:$4');
			var name  = data[1];
			var text  = data[2];
			var color = data[3];
			var host  = data[4];

			article += '<li style="color:' + color + ';">' + name + ' &gt; ' + text + ' （' + date + '）</li>';
		}
	}

	article += '</ul>';

	return article;
}

//記事投稿
function registData(form) {
	//入力内容チェック
	if (!form.name.value) {
		alert('名前が入力されていません。');
		form.name.focus();
		return false;
	}
	if (!form.text.value) {
		alert('メッセージが入力されていません。');
		form.text.focus();
		return false;
	}

	//XMLHttpRequest作成
	var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new function() {
		try {
			return new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			return new ActiveXObject('Microsoft.XMLHTTP');
		}
	};

	//イベントハンドラ設定
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				//投稿完了
				if (xmlhttp.responseText == 'complete') {
					form.text.value = '';
					form.text.focus();
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert('記事の投稿に失敗しました。');
			}
		}
	};

	//通信開始
	xmlhttp.open('post', scriptPath, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	xmlhttp.send('mode=regist&name=' + encodeURIComponent(form.name.value) + '&text=' + encodeURIComponent(form.text.value) + '&color=' + encodeURIComponent(form.color.value));

	delete xmlhttp;

	return false;
}

//記事表示
function showData(state) {
	var screenNode = document.getElementById('screen');

	//処理中メッセージ表示
	if (state == 'init') {
		screenNode.innerHTML = '<ul><li>Now Loading ...</li></ul>';
	}

	//XMLHttpRequest作成
	var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new function() {
		try {
			return new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			return new ActiveXObject('Microsoft.XMLHTTP');
		}
	};

	//イベントハンドラ設定
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				screenNode.innerHTML = getArticle(xmlhttp.responseText);
			} else {
				screenNode.innerHTML = '<ul><li>Error : ' + xmlhttp.status + '</li></ul>';
			}
		}
	};

	//通信開始
	xmlhttp.open('get', filePath + '?' + new Date().getTime(), true);
	xmlhttp.send('');

	delete xmlhttp;

	return;
}

//過去ログ一覧
function getList() {
	var screenNode = document.getElementById('past_list');

	//処理中メッセージ表示
	screenNode.innerHTML = 'Now Loading ...';

	//XMLHttpRequest作成
	var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new function() {
		try {
			return new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			return new ActiveXObject('Microsoft.XMLHTTP');
		}
	};

	//イベントハンドラ設定
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var list = xmlhttp.responseText.split('\n');

				//過去ログ一覧作成
				var options = '';

				for (var i = 0; i < list.length; i++) {
					if (list[i]) {
						options += '<option value="' + pastDir + list[i] + '">' + list[i] + '</option>';
					}
				}

				//情報反映
				screenNode.innerHTML = '<select name="file"><option value="" selected="selected">選択してください</option>' + options + '</select>';
			} else {
				screenNode.innerHTML = '<select name="file"><option>Error : ' + xmlhttp.status + '</option></select>';
			}
		}
	};

	//通信開始
	xmlhttp.open('get', scriptPath + '?mode=past&' + new Date().getTime(), true);
	xmlhttp.send('');

	delete xmlhttp;

	return;
}

//過去ログ表示
function showPast(file) {
	var screenNode = document.getElementById('screen');

	if (!file) {
		return false;
	}

	//XMLHttpRequest作成
	var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new function() {
		try {
			return new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			return new ActiveXObject('Microsoft.XMLHTTP');
		}
	};

	//イベントハンドラ設定
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				screenNode.innerHTML = getArticle(xmlhttp.responseText);
			} else {
				screenNode.innerHTML = '<ul><li>Error : ' + xmlhttp.status + '</li></ul>';
			}
		}
	};

	//通信開始
	xmlhttp.open('get', file + '?' + new Date().getTime(), true);
	xmlhttp.send('');

	delete xmlhttp;

	return false;
}

/**********************************************************************

  処理開始

**********************************************************************/

//読み込み完了時
window.onload = function() {
	var node_form = document.getElementById('form');
	var node_past = document.getElementById('past');

	if (node_form) {
		node_form.onsubmit = function() {
			return registData(node_form);
		};
		showData('init');
		setInterval('showData()', 1000);
	}
	if (node_past) {
		node_past.onsubmit = function() {
			return showPast(node_past.file.value);
		};
		getList();
	}
};
