/* newURIHTML.js
	Public Domain (Programmed by RobaQ[private@robaq.info] in 2003-2005.)

	作者メモ
		将来多国語対応するためにメッセージはすべて外出しして (xml:)lang 属性をみてスイッチする用にすること。
*/

window.addEventListener('load', newURI, false);

function newURI(){

	if(
		(
			document.implementation.hasFeature("HTML", "1.0") === false
			|| document.implementation.hasFeature("HTML", "1.0") === undefined
		) && (
			document.implementation.hasFeature("HTML", "2.0") === false
			|| document.implementation.hasFeature("HTML", "2.0") === undefined
		)
	){
		return;
	}
	if(document.URL === undefined){
		return;
	}
	if(document.getElementById("newURL") === false){
		return;
	}

	// 仕様
	// 1997年12月から1998年03月、及び1998年10月以降に対応。
	// 暫定 9999年12月まで正規表現にてマッチング (Y10K 問題は考慮しない)。
	var url = document.URL.replace(/%7E/,"~");
	var targetReg = /http:\/\/www\.netlaputa\.ne\.jp\/~robaq\/(199712)|(1998(0[1-3]|1[0-2]))|(1999(0[1-9]|1[0-2]))|([2-9][0-9][0-9][0-9](0[1-9]|1[0-2]))\.html?/;
	if(targetReg.test(url) === false){
		return;
	}

	var newURL = document.URL.replace(/\.html?/, ".xhtml")
	var body = getBodyElement();

	var newUrlSection = document.createElement("div");
	newUrlSection.className = "section";
	newUrlSection.setAttribute("id", "newURL");

	var heading  = document.createElement("h2");
	heading.appendChild(document.createTextNode("予想される正しい URL"));
	newUrlSection.appendChild(heading);

	var p0  = document.createElement("p");
	p0.appendChild(document.createTextNode("以下に javascript による正しい URL の予想結果を出力します (間違っている可能性もありますので予めご承知ください)。"));
	newUrlSection.appendChild(p0);

	var dl  = document.createElement("dl");
	var dt1 = document.createElement("dt");
	var dd1 = document.createElement("dd");
	var p1  = document.createElement("p");
	var a1  = document.createElement("a");

	dt1.appendChild(document.createTextNode("予想される最新版 (application/xhtml+xml) URL"));
	a1.setAttribute("href", newURL);
	a1.className = "newurl";
	a1.appendChild(document.createTextNode(newURL));
	p1.appendChild(a1);
	dd1.appendChild(p1);

	dl.appendChild(dt1);
	dl.appendChild(dd1);

	// 仕様
	// 1997年12月から1998年03月、及び1998年10月から2002年12月までの場合、凍結版に対応する。
	// 暫定 9999年12月まで正規表現にてマッチング (Y10K 問題は考慮しない)。
	if(document.URL.match(/http:\/\/www\.netlaputa\.ne\.jp\/~robaq\/(199712)|(1998(0[1-3]|1[0-2]))|(1999(0[1-9]|1[0-2]))|(200[0-2](0[1-9]|1[0-2]))/i)){
		var frozenURL = document.URL.replace(/http:\/\/robaq\.info\//, "http://robaq.info/frozenDocument/");
		var dt2 = document.createElement("dt");
		var dd2 = document.createElement("dd");
		var p2  = document.createElement("p");
		var a2  = document.createElement("a");

		dt2.appendChild(document.createTextNode("予想される更新凍結版 (text/html) URL"));
		a2.setAttribute("href", frozenURL);
		a2.className = "frozenurl";
		a2.appendChild(document.createTextNode(frozenURL));
		p2.appendChild(a2);
		dd2.appendChild(p2);
		dl.appendChild(dt2);
		dl.appendChild(dd2);
	}

	newUrlSection.appendChild(dl);

	document.getElementById("newURL").parentNode.replaceChild(newUrlSection, document.getElementById("newURL"));

	function getBodyElement(){
		if(document.documentElement.namespaceURI){
			switch(document.documentElement.namespaceURI){
				case namespace.xhtml:
					switch(document.doctype.publicId){
						case publicId.xhtmlbacis10:
						case publicId.xhtml10Strict:
						case publicId.xhtml10Transitional:
						case publicId.xhtml11:
							return document.getElementsByTagNameNS(namespace.xhtml, "body")[0];
							break;
						case publicId.xhtml10Frameset:
							return document.getElementsByTagNameNS(namespace.xhtml, "noframes")[0];
							break;
						default:
							return document.documentElement;
							break;
					}
					break;
				default:
					return document.documentElement;
					break;
			}
		} else {
			if(document.getElementsByTagName("BODY").length === 1){
				return document.getElementsByTagName("BODY")[0];
			} else {
				return document.documentElement;
			}
		}
	}
}
