/* 検索機能 */
/* All Rights Reserved,Copyright(C) FUJITSU LIMITED 2006 */

/* ---------------------------検索前処理--------------------------- */
function searchExec(){
	var strSearchWord=document.form1.input1.value;
	var strSearchWordSplit=strSearchWord.split(" ");
	var intCounter=null;

	var objUserAgent=navigator.userAgent;
	if(objUserAgent.indexOf("MSIE 6.") != -1 || objUserAgent.indexOf("MSIE 7.") != -1){
		support(objUserAgent);
	}
	else if(objUserAgent.indexOf("Gecko/") != -1){
		if(objUserAgent.indexOf("Netscape6/6.")!= -1){
			notSupport(objUserAgent);
		}
		else{
			support(objUserAgent);
		}
	}
	else{
		notSupport(objUserAgent);
	}

	function notSupport(objUserAgent){
		if(objUserAgent.indexOf("MSIE 5.") != -1){
			document.all?document.all('resultOut').style.display="block":document.getElementById('resultOut').style.display="block";
			getElementById("resultOut").innerHTML="<div class='close'>［<a href='javascript:searchResultOnOff(\"bodyOut\")'>検索結果を閉じる</a>］</div>";
			resultOut.innerHTML+="<div class='result_title'>お使いのブラウザでは検索機能は使用できません。</div><br>";
			resultOut.innerHTML+="検索機能を使用できるブラウザは、InternetExplorer 6.0/7.0、Netscape 7.1、FireFox 1.0/1.5です。";
			searchResultOnOff('resultOut');
		}
		else if(objUserAgent.indexOf("Mozilla/4.") != -1){
			with(document){
				write("<p><b>お使いのブラウザでは検索機能は使用できません。</b></p>");
				write("<p>検索機能を使用できるブラウザは、InternetExplorer 6.0/7.0、Netscape 7.1、FireFox 1.0/1.5です。<br>（「戻る」ボタンで元の画面に戻ってください。）</p>");
				close();
			}
		}
		else{
			document.all?document.all('resultOut').style.display="block":document.getElementById('resultOut').style.display="block";
			with(document){
				getElementById("resultOut").innerHTML="<div class='close'>［<a href='javascript:searchResultOnOff(\"bodyOut\")'>検索結果を閉じる</a>］</div>";
				getElementById("resultOut").innerHTML+="<div class='result_title'>お使いのブラウザでは検索機能は使用できません。</div><br>";
				getElementById("resultOut").innerHTML+="検索機能を使用できるブラウザは、InternetExplorer 6.0/7.0、Netscape 7.1、FireFox 1.0/1.5です。";
			}
			searchResultOnOff('resultOut');
		}
		return;
	}

	function support(objUserAgent){
		if(strSearchWord==""){
			document.all?document.all('resultOut').style.display="block":document.getElementById('resultOut').style.display="block";
			with(document){
				getElementById("resultOut").innerHTML="<div class='close'>［<a href='javascript:searchResultOnOff(\"bodyOut\")'>検索結果を閉じる</a>］</div>";
				getElementById("resultOut").innerHTML+="<div class='result_title'>検索語がありません</div><br>";
				getElementById("resultOut").innerHTML+="検索語を入力してください。";
				form1.input1.focus();
			}
			searchResultOnOff('resultOut');
			return;
		}
		else{
			for(var i=0;i<strSearchWordSplit.length;i++){
				if(strSearchWordSplit[i].length>31){
					intCounter++;
				}
			}
			if(intCounter==null){
				document.getElementById("resultOut").innerHTML="<span style='color:#999999'>検索中です......</span>";
				if(objUserAgent.indexOf("MSIE 6.") != -1 || objUserAgent.indexOf("MSIE 7.") != -1){
					loadXMLFileIE();
				}
				else{
					loadXMLFileGK();
				}
			}
			else if(intCounter!=null){
				document.all?document.all('resultOut').style.display="block":document.getElementById('resultOut').style.display="block";
				with(document){
				getElementById("resultOut").innerHTML="<div class='close'>［<a href='javascript:searchResultOnOff(\"bodyOut\")'>検索結果を閉じる</a>］</div>";
					getElementById("resultOut").innerHTML+="<div class='result_title'>検索語が長すぎます</div><br>";
					getElementById("resultOut").innerHTML+="検索語1つにつき、30文字以内で入力してください。";
					form1.input1.focus();
				}
				searchResultOnOff('resultOut');
				return;
			}
		}
	}

	function loadXMLFileIE(){
		var domDocument=new ActiveXObject("Microsoft.XMLDom");
		domDocument.async=false;
		domDocument.load("search.xml");

		var err=domDocument.parseError;
		if(err.errorCode!=0){
			var strErr="<span style='font-weight:bold'>エラー！</span><br>XML文書を検証した結果、以下のエラーが見つかりました。<br><br>" + err.reason + "(line " + err.line+ " , pos " + err.linepos + ")";
			document.getElementById("resultOut").innerHTML=strErr;
		}
		else{
			searchXml(domDocument);
		}
	}

	function loadXMLFileGK(){
		var domDocument = document.implementation.createDocument("", "", null);
		domDocument.onload = loadHandler;
		domDocument.load("search.xml");

		function loadHandler(){
			var root=domDocument.documentElement;
			if(root==null){
				document.getElementById("resultOut").innerHTML="XMLの読み込みに失敗しました";
				return;
			}
			else if(root.tagName=='parsererror' && root.namespaceURI=='http://www.mozilla.org/newlayout/xml/parsererror.xml'){
				document.getElementById("resultOut").innerHTML=root.firstChild.nodeValue;
				return;
			}
			else{
				searchXml(domDocument);
			}
		}
	}

}


/* ---------------------------検索処理--------------------------- */
function searchXml(source){
	var objUserAgent=navigator.userAgent;

	var root=source.documentElement;

	var strSearchWordTemp=document.form1.input1.value;
	var strSearchWord=strSearchWordTemp.split(" ");

	var objRootNodeList=root.getElementsByTagName("html");

	var strReg=new Array(strSearchWord.length);
	var result=new Array(strSearchWord.length);
	var strSearchText="";
	var strSearchResult="";

	var intHitCounter=null;

	for(var i=0;i<objRootNodeList.length;i++){
		var intRootCounter=null;
		var objRootNode=objRootNodeList.item(i);
		var objNodeList=objRootNode.getElementsByTagName("body");

		for(var j=0;j<objNodeList.length;j++){
			var resultCounter=null;
			strSearchText=getText(objNodeList.item(j));
			for(var m=0;m<strSearchWord.length;m++){
				strReg[m]=new RegExp(strSearchWord[m],"i");
				result[m]=strSearchText.match(strReg[m]);
				if(result[m]!=null){
					resultCounter++;
				}
			}

			if(resultCounter==strSearchWord.length){
				if(intRootCounter==null){
					strSearchResult+="<hr size='1'>";
					var strTitle="";
					var strTitle=objRootNode.getElementsByTagName("title").item(0).firstChild.nodeValue;
					var objMeta=objRootNode.getElementsByTagName("meta");
					for(var l=0;l<objMeta.length;l++){
						if(objMeta[l].getAttribute("name")=="filename"){
							var strFileName=objMeta[l].getAttribute("content");
						}
					}
					strSearchResult+="<a href='" + strFileName + "' target='main'>" + strTitle + "</a><br>";
					intHitCounter++;
				}
				intRootCounter++;

				for(var m=0;m<strSearchWord.length;m++){
					var strReplaceText="<span style='color:#0066FF;'>" + result[m] + "</span>";
					var strReplaceResult;
					if(m==0){
						strReplaceResult=strSearchText.replace(result[m],strReplaceText);
					}
					else{
						strReplaceResult=strReplaceResult.replace(result[m],strReplaceText);
					}
				}
				strReplaceResult+="<br>";

				var strSplitText=strReplaceResult.split("<span style='color:#0066FF;'>");
				for(var k=0;k<strSplitText.length;k++){
					var intSplitTextLength=strSplitText[k].length;
					if(intSplitTextLength>80){
						var strFormer=strSplitText[k].substring(0,40);
						var strLatter=strSplitText[k].substring(intSplitTextLength-20,intSplitTextLength);
						if(k==0){
							var test1="..." + strLatter;
							strReplaceResult=strReplaceResult.replace(strSplitText[k],test1);
						}
						else if(k==strSplitText.length-1){
							var test2=strFormer + "...";
							strReplaceResult=strReplaceResult.replace(strSplitText[k],test2);
						}
						else{
							var test3=strFormer + "......" + strLatter;
							strReplaceResult=strReplaceResult.replace(strSplitText[k],test3);
						}
					}
				}
				strSearchResult+=strReplaceResult;
			}
		}
	}

	if(strSearchResult!=""){
		document.all?document.all('resultOut').style.display="block":document.getElementById('resultOut').style.display="block";
		with(document){
			getElementById("resultOut").innerHTML="";
			getElementById("resultOut").innerHTML="<div class='close'>［<a href='javascript:searchResultOnOff(\"bodyOut\")'>検索結果を閉じる</a>］</div>";
			getElementById("resultOut").innerHTML+="<div class='result_title'>検索結果</div><br>"
			getElementById("resultOut").innerHTML+=intHitCounter + "件あります。";
			getElementById("resultOut").innerHTML+=strSearchResult;
		}
		searchResultOnOff('resultOut');
	}
	else if(strSearchResult==""){
		document.all?document.all('resultOut').style.display="block":document.getElementById('resultOut').style.display="block";
		with(document){
			getElementById("resultOut").innerHTML="<div class='close'>［<a href='javascript:searchResultOnOff(\"bodyOut\")'>検索結果を閉じる</a>］</div>";
			getElementById("resultOut").innerHTML+="<div class='result_title'>検索結果</div><br>";
			getElementById("resultOut").innerHTML+="検索語に一致するページはありません。";
		}
		searchResultOnOff('resultOut');
	}

	function getText(objNode){
		var strSearch="";
		getNode(objNode);

		function getNode(node){
			if(node.nodeType==3){
				if(node.nodeValue.match(/[\n|\t]+/)){
					strSearch+=node.nodeValue.replace(/[\n|\t]+/,"");
				}
				else{
					strSearch+=node.nodeValue;
				}
			}
			for(var objChildnode=node.firstChild;objChildnode!=null;objChildnode=objChildnode.nextSibling){
				getNode(objChildnode);
			}
		}

	return strSearch;

	}

}

/* -------------------------検索結果を表示したら、目次や索引を消す処理------------------------- */
function searchResultOnOff(id){
	if(id=="resultOut"){
		document.all?document.all('resultOut').style.display="block":document.getElementById('resultOut').style.display="block";
		document.all?document.all('bodyOut').style.display="none":document.getElementById('bodyOut').style.display="none";
	}
	else{
		document.all?document.all('resultOut').style.display="none":document.getElementById('resultOut').style.display="none";
		document.all?document.all('bodyOut').style.display="block":document.getElementById('bodyOut').style.display="block";
		if(document.getElementById('tocpage')){
			location.reload();
		}
	}
}
