﻿// 初期設定
var file="partners.xml";		// XMLファイルの場所
var page_url="default.aspx";	// このスクリプトを読み込むページ名
var view_item=5;				// 1ページに表示するアイテム数
var color_top_bg="5588CC";		// パートナー情報 タイトルの背景色
var color_top_font="FFFFFF";	// パートナー情報 タイトルの文字色
var color_sub_bg="EEF3F3";		// パートナー情報 サブタイトルの背景色
var color_sub_font="333355";	// パートナー情報 サブタイトルの文字色
var table_width="700px";	// パートナー情報 全体の横幅
var table_width_txt="65%";	// パートナー情報 説明文の横幅
var table_width_img="35%";	// パートナー情報 画像の横幅

// 以下、設定不要です
var max_item=0;
var item_start=0;
var item_end=0;
var item_rand="";
var item_rand2="";
var random_table=new Array();
var result_table=new Array();
var img_table=new Array();
var html_partners="";
var html_table="";
var word="";

// アルファベットリスト
alphabet1=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
alphabet2=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];


// XMLデータの確認
function display_content() {
	document.write('<div style="margin:0px 0px 15px 0px; padding:5px 3px 7px 10px; border:solid 1px #CCCCCC; width:710px; background-color:#'+color_sub_bg+'; color:#'+color_sub_font+';"><span style="padding:0px; font-weight:bold;">パートナー検索<form style="padding:0px 0px 0px 10px; margin:0px; display:inline;" onsubmit="search_word(); return(false);"><input type="text" id="input_word" size="60" /><span style="margin:0px 0px 0px 15px;"><input type="button" onclick="search_word();" value="検索する" /></span><span style="margin:0px 0px 0px 10px;"><input type="reset" value="クリア" /></span></form></div><div id="partners_main"></div>');
	http_request = false;
	if(window.XMLHttpRequest){
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType){http_request.overrideMimeType('text/xml');}
	}else if (window.ActiveXObject) {
		try {http_request = new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){
			try {http_request = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e){}
		}
	}
	if (!http_request) {return false;}
	http_request.onreadystatechange = build_page;
	http_request.open('GET', file+'?'+Math.floor(Math.random() * 10000), true);
	http_request.send(null);
}
function build_page(){if (http_request.readyState == 4 && http_request.status == 200) {openxml();}}

// XMLデータの読み込み
function openxml(){
	xmldoc=http_request.responseXML;
	if(xmldoc.getElementsByTagName('item').length>0){max_item=xmldoc.getElementsByTagName('item').length; display_partners();}
}

function display_partners(){
	headmenu=""; footmenu=""; pagemenu=""; get_word=""; url_word="";
	if(document.URL.match(/partnerid/g)){
		partnerid=document.URL.split("partnerid="); build_table(Number(partnerid[1]),Number(partnerid[1]),get_word,1);
	}else{
		if(document.URL.match(/word/g)){
			word_temp1=document.URL.split("word=");
			if(word_temp1[1].match(/&/g)){word_temp2=word_temp1[1].split("&"); get_word=word_temp2[0]}else{get_word=word_temp1[1];}
			if(get_word.match(/%u3000/i)){word_length=get_word.split("%u3000").length; for(n=0; n<word_length; n++){get_word=get_word.replace("%u3000", "%20");}}
			if(get_word.match(/%3C/i)){word_length=get_word.split("%3C").length; for(n=0; n<word_length; n++){get_word=get_word.replace("%3C", "%26lt%3B");}}
			if(get_word.match(/%3E/i)){word_length=get_word.split("%3E").length; for(n=0; n<word_length; n++){get_word=get_word.replace("%3E", "%26gt%3B");}}
			document.getElementById("input_word").value=unescape(get_word);
			url_word='word='+get_word+'&amp;';
		}
		if(document.URL.match(/pageid/g)){
			pageid=document.URL.split("pageid=");
			if(pageid[1].match(/_/g)){
				randomid=pageid[1].split("_"); item_rand=randomid[0];  item_start=Number(randomid[1])-1;
				if(item_start>=max_item || item_start<0){item_start=0;}
				item_end=item_start+view_item;
				if(item_end>max_item){item_end=max_item;}
				build_table(item_start,item_end,get_word,0);
			}else{
				item_rand=pageid[1];  item_start=0; item_end=view_item;
				if(item_end>max_item){item_end=max_item;}
				build_table(item_start,item_end,get_word,0);
			}
		}else{
			item_start=0; item_end=view_item;
			for(n=0; n<10; n++){item_rand+=String(Math.floor(Math.random() * 10));}
			item_rand2="";
			if(item_end>max_item){item_end=max_item;}
			build_table(item_start,item_end,get_word,0);
		}
		pagemenu+='<div style="margin:10px 0px 5px 0px;" id="page_menu">'; item_back=""; item_next="";
		if(item_start>0){
			if(item_start<view_item){item_back="";}else{item_back="_"+(item_start-view_item+1);}
			pagemenu+='<span style="padding:0px 5px 0px 5px;"><a href="?'+url_word+'pageid='+item_rand+item_back+'" style="color:#0000CC;">前へ</a></span>';
		}else{pagemenu+='<span style="padding:0px 5px 0px 5px; color:#666666;">前へ</span>';}
		for(n=0; n<Math.ceil(max_item/view_item); n++){
			if(item_start==(n*view_item)){
				pagemenu+='<span style="padding:0px 5px 0px 5px; font-weight:bold; border:solid 1px #CCCCCC; background-color:#'+color_sub_bg+'">'+(n+1)+'</span>';
			}else{
				pagemenu+='<span style="padding:0px 5px 0px 5px;"><a href="?'+url_word+'pageid='+item_rand+'_'+((n*view_item)+1)+'" style="color:#0000CC;">'+(n+1)+'</a></span>';
			}
		}
		if(item_end<max_item){
			item_next="_"+(item_end+1); pagemenu+='<span style="padding:0px 5px 0px 5px;"><a href="?'+url_word+'pageid='+item_rand+item_next+'" style="color:#0000CC;">次へ</a></span>';
		}else{pagemenu+='<span style="padding:0px 5px 0px 5px; color:#666666;">次へ</span>';}
		pagemenu+='</div>';
	}
	if(max_item==0){headmenu=""; pagemenu="";}
	html_partners+=headmenu+pagemenu+html_table+pagemenu+footmenu;
	document.getElementById("partners_main").innerHTML=html_partners;
}

function build_table(n_from, n_to, word, flag){
	table_flag=1;
	if(flag==1){
		table_flag=0;
		for(n=0; n<xmldoc.getElementsByTagName('item').length; n++){if(xmldoc.getElementsByTagName('item')[n].getElementsByTagName('id')[0].firstChild.nodeValue==n_from){table_flag=1; n_from=n; n_to=n+1; break;}}
		if(table_flag==0){html_table+='<div style="margin:10px; color:#CC3333;">指定されたパートナー ID の情報が見つかりませんでした。</div>';}else{html_table+='<div style="margin:5px 0px 0px 0px; font-weight:normal;">パートナー ID '+(n_from+1)+' の情報を表示しています。</div>';}
		footmenu+='<div style="margin:5px 0px 0px 5px; font-weight:normal;"><a href="'+page_url+'">一覧へ戻る</a></div>';
	}
	if(table_flag==1){
		if(word){
			if(word.match(/\//g)){
				word_length=word.split("\/").length;
				for(n=0; n<word_length; n++){word=word.replace("\/", " ");}
				for(n=0; n<word_length; n++){word=word.replace(" ", "%20\/%20");}
			}
			if(word.match(/%20/g)){word_list=word.split("%20");}else{word_list=new Array(word);}
			for(n=0; n<xmldoc.getElementsByTagName('item').length; n++){
				result_check=0;
				for(n1=0; n1<word_list.length; n1++){
					if(word_list[n1].match(/%uFF1/i)){
						word_split="%uFF1"; word_temp=word_list[n1].split(word_split); word_list[n1]="";
						for(n_eb1=0; n_eb1<word_temp.length; n_eb1++){if(!isNaN(word_temp[n_eb1]) || n_eb1==0){word_list[n1]+=word_temp[n_eb1];}else{word_list[n1]+=word_split+word_temp[n_eb1];}}
					}
					if(word_list[n1].match(/%uFF/i)){
						eb1=word_list[n1].split('%uFF'); word_list[n1]="";
						for(n_eb1=0; n_eb1<eb1.length; n_eb1++){
							sp2_alp=""; sp2_alp1=eb1[n_eb1].charCodeAt(0); sp2_alp2=eb1[n_eb1].charCodeAt(1);
							if((sp2_alp1==52 || sp2_alp1==53)&&(5248<(sp2_alp1*100+sp2_alp2) && (sp2_alp1*100+sp2_alp2)<5366)){
								sp2_alp=(sp2_alp1-52)*15+(sp2_alp1-52);
								if(sp2_alp2>64){sp2_alp+=sp2_alp2-56;}else{sp2_alp+=sp2_alp2-49;}
								sp2_alp=alphabet2[sp2_alp];
								word_list[n1]+=sp2_alp+eb1[n_eb1].substr(2,eb1[n_eb1].length);
							}else if((sp2_alp1==50 || sp2_alp1==51)&&(5048<(sp2_alp1*100+sp2_alp2) && (sp2_alp1*100+sp2_alp2)<5166)){
								sp2_alp=(sp2_alp1-50)*15+(sp2_alp1-50);
								if(sp2_alp2>64){sp2_alp+=sp2_alp2-56;}else{sp2_alp+=sp2_alp2-49;}
								sp2_alp=alphabet1[sp2_alp];
								word_list[n1]+=sp2_alp+eb1[n_eb1].substr(2,eb1[n_eb1].length);
							}else{
								if(n_eb1>0){word_list[n1]+="%uFF"+eb1[n_eb1];}
							}
						}
					}
					word_m=eval('/'+unescape(word_list[n1])+'/ig');
					if(xmldoc.getElementsByTagName('item')[n].getElementsByTagName('title')[0].firstChild.nodeValue.match(word_m) || xmldoc.getElementsByTagName('item')[n].getElementsByTagName('subtitle')[0].firstChild.nodeValue.match(word_m) || xmldoc.getElementsByTagName('item')[n].getElementsByTagName('description')[0].firstChild.nodeValue.match(word_m)){result_check++;}
				}
				if(result_check==word_list.length){result_table.push(n);}
			}
			if(n_to>result_table.length){n_to=result_table.length;}
			headmenu+='<div id="search_head" style="margin:15px 0px 0px 5px;">検索キーワード<span style="margin:0px 5px 0px 5px; color:#3333CC; font-weight:bold;">'+unescape(get_word)+'</span>を含むパートナー情報を表示しています。('+result_table.length+'件がヒットしました)</div>';
			footmenu+='<div style="margin:15px 0px 0px 5px; font-weight:normal;"><a href="'+page_url+'">一覧へ戻る</a></div>';
			max_item=result_table.length;
		}
		random_sort(item_rand);
		if(max_item>0){
			for(n=n_from; n<n_to; n++){
				html_table+='<div style="padding:10px 0px 10px 0px;"><table border="0" cellspacing="0" cellpadding="0" class="tbl" width="'+table_width+'">';
				if(word){xmlitem_n=result_table[random_table[n]];}else if(flag==0){xmlitem_n=random_table[n];}else{xmlitem_n=n;}
				xmlitem=xmldoc.getElementsByTagName('item')[xmlitem_n];
				html_table+='<tr><td style="padding:0px; font-size:100%; background-color:#'+color_top_bg+';" colspan="2"><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td style="font-size:120%; color:#'+color_top_font+'; font-weight:bold; border:0;">'+xmlitem.getElementsByTagName('title')[0].firstChild.nodeValue;
				if(xmlitem.getElementsByTagName('salestime')[0].firstChild){html_table+='<td style="padding:0px 15px 0px 0px; color:#'+color_top_font+'; border:0;" align="right">'+xmlitem.getElementsByTagName('salestime')[0].firstChild.nodeValue+'</td>';}
				get_image='<img src="'+xmlitem.getElementsByTagName('image')[0].firstChild.nodeValue+'" alt="'+xmlitem.getElementsByTagName('title')[0].firstChild.nodeValue+'" id="img_'+xmlitem.getElementsByTagName('id')[0].firstChild.nodeValue+'" border="0" />';
				if(xmlitem.getElementsByTagName('largeimage')[0] && xmlitem.getElementsByTagName('largeimage')[0].firstChild){get_image='<a href="'+xmlitem.getElementsByTagName('largeimage')[0].firstChild.nodeValue+'" target="_blank">'+get_image+'</a>';}
				html_table+='</td></tr></table></td></tr><tr valign="top"><td style="padding:0px 0px 5px 0px; width:'+table_width_txt+'; line-height:150%;"><div style="padding:5px; font-weight:bold; color:#'+color_sub_font+'; background-color:#'+color_sub_bg+'; border-bottom:solid 1px #CCCCCC;">'+xmlitem.getElementsByTagName('subtitle')[0].firstChild.nodeValue+'</div><div style="padding:5px;">'+xmlitem.getElementsByTagName('description')[0].firstChild.nodeValue+'</div></td><td width="'+table_width_img+'" align="center" valign="middle">'+get_image+'</td></tr>';
				
				table_contactname=''; table_tel=''; table_url=''; table_mail='';
				if(xmlitem.getElementsByTagName('contactname')[0].firstChild){table_contactname='<span style="margin:0px 10px 0px 0px; font-weight:bold; font-size:120%;">'+xmlitem.getElementsByTagName('contactname')[0].firstChild.nodeValue+'</span>';}
				if(xmlitem.getElementsByTagName('tel')[0].firstChild){table_tel='<td style="border:0;" nowrap="true"><span style="padding:2px 4px 3px 4px; background-color:#EEEEFF; border:solid 1px #CCCCCC;"><span style="margin:0px 8px 0px 0px;"><img src="images/img_tel.gif" alt="Tel" border="0" /></span>'+xmlitem.getElementsByTagName('tel')[0].firstChild.nodeValue+'</span></td>';}
				if(xmlitem.getElementsByTagName('url')[0].firstChild){table_url='<td style="border:0;" nowrap="true"><a href="'+xmlitem.getElementsByTagName('url')[0].firstChild.nodeValue+'" target="_blank" class="info"><img src="images/img_web.gif" alt="Web" border="0" /></a></td>';}
				if(xmlitem.getElementsByTagName('mail')[0].firstChild){table_mail='<td style="border:0;" nowrap="true"><a href="mailto:'+xmlitem.getElementsByTagName('mail')[0].firstChild.nodeValue+'" class="info"><img src="images/img_mail.gif" alt="Email" border="0" /></a></td>';}
				table_original='<td style="border:0;" nowrap="true"><a href="'+page_url+'?partnerid='+xmlitem.getElementsByTagName('id')[0].firstChild.nodeValue+'" class="info" style="padding:3px 6px 6px 6px;"><img src="images/img_original.gif" alt="単体ページの表示" border="0" /></a></td>';
				
				html_table+='<tr><td colspan="2"><div style="margin:2px 0px 5px 2px;"><img src="/japan/msdn/vstudio/partners/images/img_info.gif" alt="お問い合わせ先" /></div><table border="0" cellspacing="0" cellpadding="2"><tr><td style="border:0;">'+table_contactname+'</td>'+table_tel+table_url+table_mail+table_original+'</tr></table></td></tr></table></div>';
			}
		}else{
			html_table+='<div style="margin:15px 0px 0px 5px;">検索キーワード<span style="margin:0px 5px 0px 5px; color:#3333CC; font-weight:bold;">'+unescape(get_word)+'</span>に該当するパートナー情報は見つかりませんでした。';
		}
	}
}
function random_sort(rand_num){
	for(n=0; n<max_item; n++){
		random_key=(n*Number(rand_num.charAt(n%rand_num.length)))%max_item;
		if(!random_table[n]){random_table[n]=String(n);}
		random_temp=random_table[n];
		if(random_table[random_key]){random_table[n]=random_table[random_key];}else{random_table[n]=random_key;}
		random_table[random_key]=String(random_temp);
	}
}

function search_word(){
	get_word=document.getElementById("input_word").value;
	if(get_word){location.href=page_url+'?word='+escape(get_word);}else{location.href=page_url;}
}


