var eval_onload = "";
function body_onload()
{
	/*if(xmlInfo==null) {
		if(document.cookie.indexOf("info=")>=0) {
			window.location.reload();
		}
		else if(window.location.href.indexOf("dontreload")<0) {
			window.location.href = window.location.href + (window.location.href.indexOf("?")<0? "?":"&") + ("dontreload");
		}
	}*/
	if(eval_onload!="") eval(eval_onload);
}

var debug="";
var debugmode=false;
var xmlInfo = null;
var xml変換仕様 = null;
function scanCookie()
{
	var re1 = /\b([^=&;]+)=([^;]+)/g;
	var re2 = /\b([^=&;]+)=([^&;]+)/g;
	while(result=re1.exec(mycookie[0]), result!=null) {
		var cookiename  = result[1];
		var cookievalue = result[2];
		mycookie[cookiename] = new Array();
		mycookie[cookiename][0] = cookievalue;
		document.write("<tr><th rowspan=2 nowrap>"+cvmetachar(cookiename)+"</th><td>"+cvmetachar(cookievalue)+"</td></tr><tr><td><table border=1>\n");
		if(cookiename=="args") {
			while(result=re2.exec(cookievalue), result!=null) {
				var key   = result[1];
				var value = result[2];
				mycookie[cookiename][key] = value;
				document.write("<tr><th nowrap>"+cvmetachar(key)+"</th><td>"+cvmetachar(value)+"</td></tr>\n");
				if(key=="Debug") {
					debug="?"+key+"="+value;
					if(/^Y(es)?$/i.test(value)) {
						tblDebug.style.display = "block";
						debugmode=true;
					}
				}
			}
		}
		else if(cookiename=="contents") {
			xmlInfo = xmlLoadStr(unescape(cookievalue));
			if(xmlInfo==null) stop
			document.write("<tr><th nowrap>xml</th><td>"+cvmetachar(xmlInfo.xml)+"</td></tr>\n");
		}
		else if(cookiename=="style") {
			xml変換仕様 = xmlLoadStr(unescape(cookievalue));
			xml変換仕様.selectNodes('//変換仕様/スタイル/*').removeAll();
			xml変換仕様.selectNodes('//変換仕様/テキスト/*').removeAll();
			xml変換仕様.selectNodes('//変換仕様/コメント/*').removeAll();
			xml変換仕様.selectNodes('//変換仕様/ルビ/*').removeAll();
			document.write('<tr><th nowrap>xml変換仕様</th><td id="tdXml変換仕様">'+cvmetachar(xml変換仕様.xml)+'</td></tr>\n');
		}
		
		document.write("</table></td></tr>\n");
	}
	if(xml変換仕様==null || xml変換仕様.xml==null || xml変換仕様.xml=="") {
		xml変換仕様 = xmlLoadStr("<変換仕様/>");
	}
}

function writeTextOptions()
{
	if(xmlInfo!=null) {
		var textcontents = xmlInfo.documentElement.selectNodes('//contents[@type="text"]');
		var defaultPercent = (Math.floor(1000/(textcontents.length+1))+"").replace(/.$/, "\.$&");
		for(var n=0;textnode=textcontents.nextNode();++n) {
			var テキスト名 = textnode.getAttribute("name");
			var suffix = textnode.getAttribute("suffix");
			var itemname = 'text-'+suffix;
			var xmlNode = xml変換仕様.documentElement.selectSingleNode('/変換仕様/テキスト[@テキスト名="'+テキスト名+'"]');
			if(xmlNode==null) {
				var xmlInfoNode = textnode; //xmlInfo.documentElement.selectSingleNode('//contents[@name="'+テキスト名+'"]');
				xmlNode = xml変換仕様.createElement("テキスト");
				xmlNode.setAttribute("テキスト名", テキスト名);
				xmlNode.setAttribute("type", xmlInfoNode.getAttribute("type"));
				xmlNode.text = "\n";
				xml変換仕様.documentElement.appendChild(xmlNode);
			}
			writeTrTextOption(テキスト名, itemname, xmlNode, defaultPercent);
		}
		itemname = "notes";
		var name = "注釈等の記述";
		var xmlNode = xml変換仕様.documentElement.selectSingleNode('/変換仕様/スタイル[@name="'+name+'"]');
		if(xmlNode==null) {
			xmlNode = xml変換仕様.createElement("スタイル");
			xmlNode.setAttribute("name", name);
			xmlNode.text = "\n";
			xml変換仕様.documentElement.appendChild(xmlNode);
		}
		writeTrTextOption("注釈等の記述", "comment", xmlNode, defaultPercent);
	}
}

var Word2003Colors = "black,red,green,darkred,blue,magenta,darkblue,darkmagenta,darkgreen,darkcyan,yellow,cyan,lightgrey,white".split(",");
var reverseColors = ",black,red,green,darkred,blue,magenta,darkblue,darkmagenta,darkgreen,darkcyan,";

function changeStyle(itemname, xpath)
{
	// todo: スタイルの参照関係を追跡して、実際のサンプルを表示するように修正要
	var spanItemName = document.getElementById(itemname+'-name');
	var xmlNode = xml変換仕様.documentElement.selectSingleNode(xpath);
	if(xmlNode==null) stop;

	try {
		var color = document.getElementById(itemname+'-color').value;
		spanItemName.style.color = color;
		if(color=="") xmlNode.removeAttribute("color");
		else xmlNode.setAttribute("color",color);
	} catch(e) { }
	
	try {
		var backgroundColor = document.getElementById(itemname+'-bgcolor').value;
		spanItemName.style.backgroundColor = backgroundColor;
		if(backgroundColor=="") xmlNode.removeAttribute("backgroundColor");
		else xmlNode.setAttribute("backgroundColor",backgroundColor);
	} catch(e) { }
	
	try {
		if(this!=null && this.name==(itemname+'-bold')) this.value="clicked";
		var elem = document.getElementById(itemname+'-bold');
		var fontWeight = spanItemName.style.fontWeight = (elem.checked? "bold": "normal");
		if(fontWeight=="normal" && elem.value=="Default") xmlNode.removeAttribute("fontWeight");
		else xmlNode.setAttribute("fontWeight",fontWeight);
	} catch(e) { }
	
	try {
		if(this!=null && this.name==(itemname+'-italic')) this.value="clicked";
		var elem = document.getElementById(itemname+'-italic');
		var fontStyle  = spanItemName.style.fontStyle  = (elem.checked? "italic": "normal");
		if(fontStyle=="normal" && elem.value=="Default") xmlNode.removeAttribute("fontStyle");
		else xmlNode.setAttribute("fontStyle",fontStyle);
	} catch(e) { }
	
	try {
		var fontFamily = document.getElementById(itemname+'-fontface').value;
		if(fontFamily=="") xmlNode.removeAttribute("fontFamily");
		else {
			spanItemName.style.fontFamily = fontFamily;
			xmlNode.setAttribute("fontFamily",fontFamily);
		}
	} catch(e) { }
	
	try {
		var fontSize  = document.getElementById(itemname+'-fontsize').value;
		if(fontSize=="") xmlNode.removeAttribute("fontSize");
		else {
			spanItemName.style.fontSize = fontSize+"pt";
			xmlNode.setAttribute("fontSize",fontSize);
		}
	} catch(e) { }
	
	try {
		document.cookie = "style="+escape(xml変換仕様.xml.replace(/\n|\r/g,""))+";path=/xml/;expires="+cookie_expires.toGMTString();
		document.getElementById("tdXml変換仕様").innerHTML = cvmetachar(xml変換仕様.xml);
	} catch(e) { }
}

function addFontItems(str, itemname, xmlNode, onchange, defaultColor, defaultBackgroundColor, defaultFontWeight, defaultFontStyle, defaultFontFamily, defaultFontSize)
{
	var color = xmlNode.getAttribute("color");
	var backgroundColor = xmlNode.getAttribute("backgroundColor");
		if(color==null && backgroundColor==null) {
			color = defaultColor;
			backgroundColor = defaultBackgroundColor;
		}
	var fontWeight = xmlNode.getAttribute("fontWeight");
		if(fontWeight==null && defaultFontWeight!="normal") fontWeight = defaultFontWeight;
	var fontStyle  = xmlNode.getAttribute("fontStyle");
		if(fontStyle==null && defaultFontStyle!="normal") fontStyle = defaultFontStyle;
	var fontFamily = xmlNode.getAttribute("fontFamily");
		if(fontFamily==null) fontFamily = defaultFontFamily;
	var fontSize   = xmlNode.getAttribute("fontSize");
		if(fontSize==null) fontSize = defaultFontSize;
	if(itemname=="standard") {
		str = str + "<td>&nbsp</td>"
		          + "<td>&nbsp</td>"
		          + "<td>&nbsp</td>"
		          + "<td>&nbsp</td>"
	}
	else {
		str = str
			/*文字色*/ +'\t<td><select name="'+itemname+'-color" onchange='+onchange+'>\n'
			           +'\t\t<option value="" ></option>\n'
							for(var i in Word2003Colors) {
								var fgcolor = Word2003Colors[i];
								var bgcolor = (reverseColors.indexOf(","+fgcolor+",")>=0? "white": "gray");
								str = str + ('\t\t<option style="color:'+fgcolor+';background-color:'+bgcolor+';font-weight:bold;" value="'+fgcolor+'" '
								          + (fgcolor==color? ' selected': '')
								          + '>'+fgcolor+'</option>\n');
							}
							str = str + "\t\t</select></td>\n"
			/*背景色*/ +'\t<td><select name="'+itemname+'-bgcolor" onchange='+onchange+'>\n'
			           +'\t\t<option value="" ></option>\n'
							for(var i in Word2003Colors) {
								var bgcolor = Word2003Colors[Word2003Colors.length-1-i];
								var fgcolor = (reverseColors.indexOf(","+bgcolor+",")>=0? "white": "black");
								str = str + ('\t\t<option style="color:'+fgcolor+';background-color:'+bgcolor+';" value="'+bgcolor+'" '
								          + (bgcolor==backgroundColor? ' selected': '')
								          + '>'+bgcolor+'</option>\n');
							}
							str = str + "\t\t</select></td>\n"
			/*太字*/   +'\t<td align=center><input type=checkbox name="'+itemname+'-bold"   value='+(fontWeight==null? '"Default"': '"Yes"')+(fontWeight=='bold'?   ' checked': '')+' onclick='+onchange+'></td>\n'
			/*斜体*/   +'\t<td align=center><input type=checkbox name="'+itemname+'-italic" value='+(fontStyle ==null? '"Default"': '"Yes"')+(fontStyle =='italic'? ' checked': '')+' onclick='+onchange+'></td>\n'
	}
	str = str
		/*フォント*/   +'\t<td><input type=text     name="'+itemname+'-fontface"   onchange='+onchange+' size="30" value="'+fontFamily+'">'
		/*サイズ*/     +'\t<td><input type=text     name="'+itemname+'-fontsize"   onchange='+onchange+' size="4"  value="'+fontSize+'" style="text-align:right;">pt</td>\n'
	return str;
}

function changeSpecialStyle(id)
{
	changeStyle(id, '/変換仕様/スタイル[@id="'+id+'"]');
}

function setStyle(name, id, defaultFontWeight, defaultFontSize)
{
	var xmlNode = xml変換仕様.documentElement.selectSingleNode('/変換仕様/スタイル[@name="'+name+'"]');
	if(xmlNode==null) {
		xmlNode = xml変換仕様.createElement("スタイル");
		xmlNode.setAttribute("name", name);
		xmlNode.text = "\n";
		xml変換仕様.documentElement.appendChild(xmlNode);
	}
	xmlNode.setAttribute("id", id);
	var onchange   =   "\"return changeSpecialStyle('"+id+"');\"";
	eval_onload = eval_onload + "changeSpecialStyle('"+id+"');";
	var str = addFontItems("", id, xmlNode, onchange, "", "", defaultFontWeight, "normal", "", defaultFontSize);
	return str;
}

function changeTextStyle(suffix, テキスト名)
{
	changeStyle('text-'+suffix, '/変換仕様/テキスト[@テキスト名="'+テキスト名+'"]');
}
function writeFontOptions4Text()
{
	if(xmlInfo!=null) {
		var textcontents = xmlInfo.documentElement.selectNodes('//contents[@type="text"]');
		for(var n=0;textnode=textcontents.nextNode();++n) {
			var テキスト名 = textnode.getAttribute("name");
			var suffix = textnode.getAttribute("suffix");
			var itemname = 'text-'+suffix;
			var xmlNode = xml変換仕様.documentElement.selectSingleNode('/変換仕様/テキスト[@テキスト名="'+テキスト名+'"]');
			if(xmlNode==null) {
				var xmlInfoNode = textnode; // xmlInfo.documentElement.selectSingleNode('//contents[@name="'+テキスト名+'"]');
				xmlNode = xml変換仕様.createElement("テキスト");
				xmlNode.setAttribute("テキスト名", テキスト名);
				xmlNode.setAttribute("type", xmlInfoNode.getAttribute("type"));
				xmlNode.text = "\n";
				xml変換仕様.documentElement.appendChild(xmlNode);
			}
			var onchange   =   "\"return changeTextStyle('"+suffix+"', '"+テキスト名+"');\"";
			eval_onload = eval_onload + "changeTextStyle('"+suffix+"', '"+テキスト名+"');";

			var str ="<tr>"
					/*テキスト名*/ +'\t<td nowrap><div id="'+itemname+'-name" width="100%">'+テキスト名+'</div></td>\n'

			str = addFontItems(str, itemname, xmlNode, onchange, "", "", "normal", "normal", "", "12")

			str = str + "</tr>";
			document.write(str);
		}
	}
}

function changeCommentStyle(suffix, コメント名)
{
	changeStyle('comment-'+suffix, '/変換仕様/コメント[@コメント名="'+コメント名+'"]');
}

var ignoreNames = " 朗読 朗読と本文の対応関係 朗読用文節情報 渋谷栄一訳の文節情報 ";
function writeFontOptions4Comment()
{
	if(xmlInfo!=null) {
		var commentcontents = xmlInfo.documentElement.selectNodes('//contents[@type="comment"]');
		for(var n=0;commentnode=commentcontents.nextNode();++n) {
			var コメント名 = commentnode.getAttribute("name");
			var suffix = commentnode.getAttribute("suffix");
			var itemname = 'comment-'+suffix;
			var xmlNode = xml変換仕様.documentElement.selectSingleNode('/変換仕様/コメント[@コメント名="'+コメント名+'"]');
			if(xmlNode==null) {
				var xmlInfoNode = commentnode; //xmlInfo.documentElement.selectSingleNode('//contents[@name="'+コメント名+'"]');
				xmlNode = xml変換仕様.createElement("コメント");
				xmlNode.setAttribute("コメント名", xmlInfoNode.getAttribute("name"));
				xmlNode.setAttribute("type", xmlInfoNode.getAttribute("type"));
				xmlNode.text = "\n";
				xml変換仕様.documentElement.appendChild(xmlNode);
			}
			if(ignoreNames!=null && ignoreNames.indexOf(" "+コメント名+" ")>=0) continue;
			var onchange   =   "\"return changeCommentStyle('"+suffix+"', '"+コメント名+"');\"";
			eval_onload = eval_onload + "changeCommentStyle('"+suffix+"', '"+コメント名+"');";

			var str ="<tr>"
				/*コメント名*/ +'\t<td nowrap><div id="'+itemname+'-name" width="100%">'+コメント名+' の見出し</div></td>\n'

				str = addFontItems(str, itemname, xmlNode, onchange, Word2003Colors[(n+1)%Word2003Colors.length], "", "bold", "normal", "", "")

				str = str + "<td nowrap><small>(注1)</small></td></tr>";
			document.write(str);
		}

		var rubycontents = xmlInfo.documentElement.selectNodes('//contents[@type="ruby"]');
		var xmlInfoNode;
		for(var n=0;xmlInfoNode=rubycontents.nextNode();++n) {
			var ルビ名 = xmlInfoNode.getAttribute("name");
			var xmlNode = xml変換仕様.documentElement.selectSingleNode('/変換仕様/ルビ[@ルビ名="'+ルビ名+'"]');
			if(xmlNode==null) {
				xmlNode = xml変換仕様.createElement("ルビ");
				xmlNode.setAttribute("ルビ名", ルビ名);
				xmlNode.text = "\n";
				xml変換仕様.documentElement.appendChild(xmlNode);
			}
		}
	}
}

function setCookie()
{
	if(xml変換仕様!=null) {
		var textNodes = xml変換仕様.documentElement.selectNodes('//変換仕様/テキスト');
		for(var i=0;xmlNode=textNodes.nextNode();++i) {
			var テキスト名 = xmlNode.getAttribute("テキスト名"); // 2007/12/1現在 未使用
			var xmlInfoNode = xmlInfo.documentElement.selectSingleNode('//contents[@name="'+テキスト名+'"]');
			if(xmlInfoNode==null) continue;
			var suffix = xmlInfoNode.getAttribute("suffix");
			var itemname = 'text-'+suffix;
			setCookie4TextOption(テキスト名, itemname, xmlNode);
		}
		var xmlNode = xml変換仕様.documentElement.selectSingleNode('/変換仕様/スタイル[@name="注釈等の記述"]');
		setCookie4TextOption("注釈等の記述", "comment", xmlNode);
		var str = escape(xml変換仕様.xml.replace(/\n|\r/g,""));
		if(str.length>4096) stop
		document.cookie = "style="+str+";path=/xml/;expires="+cookie_expires.toGMTString();
	}
}

function OK_onclick()
{
	setCookie();
	document.cookie = "contents=;path=/xml/;expires=Fri, 31 Dec 1999 23:59:59 GMT";
	if(xmlInfo==null) stop
	window.location.href=xmlInfo.documentElement.getAttribute("homepage")+debug;
}

