var searchResults = new Array();var locationPathname = location.protocol + '//' + location.host + location.pathname;var searchUrl = '';var gistLength = 100;var recordsOfPage = 10;if(locationPathname.match(/^(.+\/)[^\/]+$/)){searchUrl = RegExp.$1;}if (window.ActiveXObject && !window.XMLHttpRequest){window.XMLHttpRequest = function(){try{return (new ActiveXObject('Msxml2.XMLHTTP'));}catch (e) {}try{return (new ActiveXObject('Microsoft.XMLHTTP'));}catch (e) {}return (null);}}function siteSearch(keyword, element, from){if(!keyword || !element) return false;searchResults.length = 0;keyword = keyword.replace('　', ' ');keyword = keyword.replace(/\s+/, ' ');var keywords = keyword.split(' ');siteSearch2(searchUrl, keywords, element);var tempArray = new Array();for(var i = 0; i < searchResults.length; i ++){if(searchResults[i]['titleMatch'] == 0 && searchResults[i]['bodyMatch'] == 0) continue;tempArray.push(searchResults[i]);}if(from == undefined) from = 1;var to = from + recordsOfPage - 1;if(to > tempArray.length) to = tempArray.length;var resultHtml = '<h2>サイト内検索結果';if(tempArray.length > 0) resultHtml += '(' + tempArray.length + '件中&nbsp;' + from + '～' + to + '件目)';resultHtml += "</h2>\n";if(tempArray.length > 0){resultHtml += '<ol start="' + from + '">\n';tempArray.sort(arraySort);for(var i = from - 1; i < to; i ++){if(tempArray[i]['titleMatch'] == 0 && tempArray[i]['bodyMatch'] == 0) break;resultHtml += "<li>";resultHtml += '<a href="javascript:void(0)" onclick="window.opener.location.href=' + "'" + tempArray[i]['url'] + "';window.opener.focus()" + '">' + tempArray[i]['title'] + "</a><br>\n";resultHtml += tempArray[i]['gist'];resultHtml += "</li>\n";}resultHtml += "</ol>\n<p>";if(from > 1){var before = from - recordsOfPage;resultHtml += '<a href="javascript:void(0)" onclick="siteSearch(';resultHtml += "'" + keyword + "', '" + element + "', " + before + ')">';resultHtml += '[前の' + recordsOfPage + '件]</a>&nbsp;';}if(to < tempArray.length){to = from + recordsOfPage;var nextRecords = recordsOfPage;if(to + recordsOfPage - 1 > tempArray.length) nextRecords = tempArray.length - to + 1;resultHtml += '<a href="javascript:void(0)" onclick="siteSearch(';resultHtml += "'" + keyword + "', '" + element + "', " + to + ')">';resultHtml += '[次の' + nextRecords + '件]</a>&nbsp;';}}else{resultHtml += "<p>該当するページがありませんでした。";}resultHtml += '<a href="javascript:void(0)" onclick="window.close()">[閉じる]</a>' + "</p>\n";resultHtml += '<hr><p style="text-align: center; font-size: 80%">(C)2007&nbsp;<a href="http://www.saga-n.net/" target="_blank">SAGA-N.Net</a>' + "</p>\n";document.getElementById('searchresult').innerHTML = resultHtml;scrollTo(0, 0);window.focus();return false;}function siteSearch2(url, keywords, element){for(var i = 0; i < searchResults.length; i ++){if(searchResults[i]['url'] == url) return;}xmlhttp = new XMLHttpRequest();xmlhttp.open('GET', url, false);xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");xmlhttp.send(null);if (xmlhttp.status == 200){RegExp.multiline = true;if(!xmlhttp.responseText.match(/<meta(?:(?!>).)*charset\s*=\s*utf-8/i)) return;searchResult = new Array();searchResult['url'] = url;searchResult['title'] = '';searchResult['titleMatch'] = 0;searchResult['bodyMatch'] = 0;searchResult['gist'] = '';if(xmlhttp.responseText.match(/<title>([^<\/]+)<\/title>/i)){searchResult['title'] = RegExp.$1;for(var i = 0; i < keywords.length; i++){var keyword = new RegExp(quoteMeta(keywords[i]), 'ig');if(keyword.test(searchResult['title'])){searchResult['titleMatch'] += searchResult['title'].match(keyword).length;}var keyword = new RegExp(quoteMeta(keywords[i]), 'i');var ptr = 0;var matchPtr = 0;var replace = '';while(true){matchPtr = searchResult['title'].substr(ptr).search(keyword);if(matchPtr == -1) break;ptr += matchPtr;replace = '<span style="font-weight: bold">' + searchResult['title'].substr(ptr, keywords[i].length) + '</span>';searchResult['title'] = searchResult['title'].substr(0, ptr) + replace + searchResult['title'].substr(ptr + keywords[i].length),ptr += replace.length;}}}var body = '';var re = new RegExp('<(?:(?!>).)*id\s*=[\s|"|\']*' + element, 'i');if(xmlhttp.responseText.toLowerCase().search(re)){body = xmlhttp.responseText.substr(xmlhttp.responseText.toLowerCase().search(re));}if(body.toLowerCase().indexOf('</body')){body = body.substr(0, body.toLowerCase().indexOf('</body'));}body = body.replace(/<[^>]+>/g, '');for(var i = 0; i < keywords.length; i++){var keyword = new RegExp(keywords[i], 'ig');if(keyword.test(body)){searchResult['bodyMatch'] += body.match(keyword).length;}}body = body.replace(/[\s]/g, '');body = body.replace(/^(?:&nbsp;)*/, '');searchResult['gist'] = body.substr(0, gistLength) + '...';for(var i = 0; i < keywords.length; i++){var keyword = new RegExp(quoteMeta(keywords[i]), 'i');var ptr = 0;var matchPtr = 0;var replace = '';while(true){matchPtr = searchResult['gist'].substr(ptr).search(keyword);if(matchPtr == -1) break;ptr += matchPtr;replace = '<span style="font-weight: bold">' + searchResult['gist'].substr(ptr, keywords[i].length) + '</span>';searchResult['gist'] = searchResult['gist'].substr(0, ptr) + replace + searchResult['gist'].substr(ptr + keywords[i].length),ptr += replace.length;}}searchResults.push(searchResult);var aTags = new Array();if(xmlhttp.responseText.match(/<a\s[^>]+>/i)){aTags = xmlhttp.responseText.match(/<a\s[^>]+>/ig);var locationPath = location.protocol + '//' + location.host + location.pathname;if(locationPath.match(/^(.+\/)[^\/]+$/)){locationPath = RegExp.$1;}var href = '';for(var i = 0; i < aTags.length; i ++){if(aTags[i].match(/(?:href\s*=\s*"([^"]+))|(?:href\s*=\s*'([^']+))'/)){href = RegExp.$1;if(href.match(/^\.\.\//)) continue;if(href.match(/^\.\//)) href = href.replace(/^\.\//, locationPath);if(!href.match(/http:\/\//)) href = locationPath + href;if(href.indexOf(searchUrl) == 0){siteSearch2(href, keywords, element);}}}}}}function arraySort(a, b){if(b['titleMatch'] == a['titleMatch']){return(b['bodyMatch'] - a['bodyMatch']);}else{return(b['titleMatch'] - a['titleMatch']);}}function quoteMeta(a){a = a.replace('/', '\\/');a = a.replace('[', '\\[');a = a.replace(']', '\\]');a = a.replace('^', '\\^');a = a.replace('(', '\\(');a = a.replace(')', '\\)');a = a.replace('|', '\\|');a = a.replace('$', '\\$');a = a.replace('*', '\\*');a = a.replace('+', '\\+');a = a.replace('?', '\\?');a = a.replace('{', '\\{');a = a.replace('}', '\\}');return a;}
