window.onload = function(){
	menu_view('ra:', '', '');
	cset_view();
};

function menu_view(fs, fc, fm)
{
	fs.match(/^(\w\w):(.*)$/);
	fs1 = RegExp.$1;
	fs2 = RegExp.$2;
	s = new Array("ra:,楽天市場（楽天アフィリエイト）", "ra:vc,楽天市場（バリューコマース）", "ra:tg,楽天市場（トラフィックゲート）", "ra:a8,楽天市場（A8.net）", 
	"ys:vc,YAHOO ショッピング（バリューコマース）", "ld:,livedoor デパート（livedoorアフィリエイト）", "ld:vc,livedoor デパート（バリューコマース）", 
	"am:,amazon（amazonアソシエイト）", "bi:,ビッダーズ（ビッダーズアフィリエイト）", "bi:vc,ビッダーズ（バリューコマース）");
	for (i = 0; i < s.length; i++)
	{
		s[i].match(/^(\w\w:\w{0,2}),(.+)$/);
		value = RegExp.$1;
		name = RegExp.$2;
		document.form.s.options[i] = new Option(name, value);
		if (RegExp.$1 == fs1 + ":" + fs2) {
			document.form.s.selectedIndex = i;
		}
	}
	c = new Array("ra,,全て", "ra,100371,ファッション・アパレル・靴", "ra,216131,バッグ・小物・ブランド雑貨", "ra,216129,ジュエリー・腕時計", 
	"ra,100939,美容・コスメ・香水", "ra,100026,パソコン・周辺機器", "ra,211742,家電・AV・カメラ", "ra,100227,食品・スイーツ", "ra,100316,ドリンク・お酒", 
	"ra,100804,インテリア・寝具", "ra,215783,キッチン・日用品雑貨・文具", "ra,101164,おもちゃ・ホビー・ゲーム", "ra,100533,キッズ・ベビー・マタニティ", 
	"ra,101070,スポーツ・アウトドア", "ra,100938,ダイエット・健康・介護", "ra,101114,車・バイク", "ra,101381,旅行・出張・チケット", "ra,100005,花・園芸・エクステリア", 
	"ra,101213,ペット・ペットグッズ", "ra,101240,CD・DVD・楽器", "ra,200162,本・雑誌・コミック", "ra,402853,デジタルコンテンツ", "ra,200163,不動産・住まい", 
	"ra,101438,サービス", "ys,,全て", "ys,2075503022,コンピュータ", "ys,2075400293,家電 AV機器", "ys,2075732312,音楽 CD", 
	"ys,2076557169,DVD", "ys,2077328286,本、コミック", "ys,2075397243,フード ドリンク", "ys,2075397713,ファッション アクセサリー", 
	"ys,2075706374,美容 健康", "ys,2075397714,おもちゃ ホビー", "ys,2075588116,レジャー アウトドア", "ys,2075773955,生活 インテリア", 
	"ys,2075516154,ベビー キッズ マタニティ", "ys,2075427490,ビジネス ステーショナリー", "ld,,全て", "ld,1,CD DVD ゲーム", "ld,4929,本", 
	"ld,411,キッズ ベビー マタニティ", "ld,535,グルメ（ドリンク）", "ld,873,グルメ（フード）", "ld,1497,スポーツ アウトドア", "ld,1939,パソコン 家電 AV", 
	"ld,2422,ファッション", "ld,2970,フラワー ガーデン", "ld,3069,ヘルス ビューティ", "ld,3361,ホビー ペット コレクション", "ld,3616,車 バイク", 
	"ld,4199,生活 インテリア", "am,,全て", "am,Books,書籍 和書", "am,ForeignBooks,書籍 洋書", "am,Electronics,エレクトロニクス", 
	"am,Kitchen,ホーム＆キッチン", "am,Music,音楽CD", "am,Classical,クラシック", "am,DVD,DVD", "am,VHS,ビデオ", "am,Software,PCソフト", 
	"am,VideoGames,テレビゲーム", "am,Toys,おもちゃ＆ホビー", "am,SportingGoods,スポーツ", "am,HealthPersonalCare,ヘルス&ビューティー", 
	"bi,7,コンピューター", "bi,8,家電 AV カメラ", "bi,4,ブランド", "bi,3,ファッション", "bi,931,ペット いきもの", "bi,909,グルメ ドリンク", 
	"bi,211,チケット 金券", "bi,10,キッズ ベビー", "bi,921,ヘルス ビューティー", "bi,27,アクセサリー 時計", "bi,6,アンティーク コレクション", "bi,28,ホビー カルチャー", 
	"bi,5,レジャー スポーツ", "bi,508,自動車 バイク", "bi,9,住まい インテリア 花", "bi,2,音楽 映画 DVD", "bi,1019,おもちゃ ゲーム", "bi,1,タレント キャラクタ-グッズ", 
	"bi,212,本 雑誌 コミック", "bi,11,ビジネス 事務 店舗用品 文房具");
	for (i = 0; i < c.length; i++) {
		if (document.form.c.options[0]) {
			document.form.c.options[0] = null;
		}
	}
	vc = - 1;
	var flagC = 0;
	for (i = 0; i < c.length; i++)
	{
		c[i].match(/^(\w\w),([^,]*),([^,]+)$/);
		if (RegExp.$1 == fs1)
		{
			value = RegExp.$2;
			name = RegExp.$3;
			vc++;
			document.form.c.options[vc] = new Option(name, value);
		}
		if (fs1 == RegExp.$1 && fc == RegExp.$2) {
			document.form.c.selectedIndex = vc;
			flagC = 1;
		}
	}
	if (!flagC) {
		document.form.c.selectedIndex = 0;
	}
	m = new Array("ra,,未設定", "ra,6,感想件数順", "ra,2,安い順", "ra,1,高い順", "ra,5,新着順", "ys,0,スコア順", "ys,2,安い順", 
	"ys,3,高い順", "ld,,おすすめ順", "ld,2,安い順", "ld,3,高い順", "ld,7,新着順", "am,salesrank,売れている順", "am,price,安い順", 
	"am,-price,高い順", "am,titlerank,タイトル名 昇順", "am,-titlerank,タイトル名 降順", "am,-release-date,リリース 新順", "am,release-date,リリース 古順", 
	"bi,,", "de,,");
	for (i = 0; i < m.length; i++) {
		if (document.form.m.options[0]) {
			document.form.m.options[0] = null;
		}
	}
	vc = - 1;
	var flagM = 0;
	for (i = 0; i < m.length; i++)
	{
		m[i].match(/^(\w\w),([^,]*),([^,]*)$/);
		if (RegExp.$1 == fs1)
		{
			name = RegExp.$3;
			value = RegExp.$2;
			vc++;
			document.form.m.options[vc] = new Option(name, value);
		}
		if (fs1 == RegExp.$1 && fm == RegExp.$2) {
			document.form.m.selectedIndex = vc;
			flagM = 1;
		}
	}
	if (!flagM) {
		document.form.m.selectedIndex = 0;
	}
	main_read();
}
function code_on(mode)
{
	RSs = document.form.s.value;
	RSc = document.form.c.value;
	RSm = document.form.m.value;
	RSsite = RSs + "," + RSc + "," + RSm;
	RSk = document.form.k.value;
	RSk = RSk.replace(/"/g, '\\\"');
	RSword = RSk;
	RSi1 = document.form.i1.value;
	RSi2 = document.form.i2.value;
	RSafid = RSi1 + "," + RSi2;
	RSs1 = document.form.s1.value;
	RSs2 = document.form.s2.value;
	RSs3 = document.form.s3.value;
	RSsize = RSs1 + "-" + RSs2 + "-" + RSs3;
	RStarg = document.form.t.value;
	if (!RStarg) {
		RStarg = "-";
	}
	RScb1 = document.form.cb1.value;
	RScb2 = document.form.cb2.value;
	RScl = document.form.cl.value;
	RSct = document.form.ct.value;
	RScp = document.form.cp.value;
	RScset = RScb1 + "," + RScb2 + "," + RScl + "," + RSct + "," + RScp;
	if (!RSs) {
		alert("対象サイトを選択してください");
		return false;
	}
	else if (!RSk) {
		alert("キーワードを入力してください");
		return false;
	}
	else if (!RSk.match(/^.{2,100}$/i)) {
		alert("キーワードは2〜100文字以内で入力してください");
		return false;
	}
	else if (!RSi1) {
		alert("アフィリエイトIDを入力してください");
		return false;
	}
	else if (RSi1.match(/[^\d\w\.\+-_]/i)) {
		alert("アフィリエイトIDは半角英数字と「.+-_」で入力してください");
		return false;
	}
	else if (!RSs1) {
		alert("サイズ（列）を選択してください");
		return false;
	}
	else if (!RSs2) {
		alert("サイズ（行）を選択してください");
		return false;
	}
	else if (!RSs3) {
		alert("サイズ（幅）を入力してください");
		return false;
	}
	else if (RSs1.match(/[^\d]/i)) {
		alert("サイズ（列）は半角数字で入力してください");
		return false;
	}
	else if (RSs2.match(/[^\d]/i)) {
		alert("サイズ（行）は半角数字で入力してください");
		return false;
	}
	else if (RSs3.match(/[^\d]/i)) {
		alert("サイズ（幅）は半角数字で入力してください");
		return false;
	}
	else if (RSs3 > 999) {
		alert("サイズ（幅）は999以下で入力してください。");
		return false;
	}
	else if (RSs1 == "1" && RSs3 < 160) {
		alert("サイズ（横）で「１列」を選択した場合、横幅は160以上で入力してください。");
		return false;
	}
	else if (RSs1 == "2" && RSs3 < 320) {
		alert("サイズ（横）で「２列」を選択した場合、横幅は320以上で入力してください。");
		return false;
	}
	else if (!RStarg.match(/^[\\d\w-_]{0,10}$/i)) {
		alert("リンク表示先は半角英数字の10文字以下で入力してください。");
		return false;
	}
	else if (!RScb1 | !RScb2 | !RScl | !RSct | !RScp) {
		alert("カラーを全て設定してください。");
		return false;
	}
	main_save();
	if (mode == "1")
	{
		var code = "";
		code += "<script type=\"text/javascript\"><!--\n";
		code += "var RSsite = \"" + RSsite + "\";\n";
		code += "var RSword = \"" + RSword + "\";\n";
		code += "var RSafid = \"" + RSafid + "\";\n";
		code += "var RStarg = \"" + RStarg + "\";\n";
		code += "var RScset = \"" + RScset + "\";\n";
		if (document.form.op1.checked) {
			code += "RSopti = \"noimg\";\n";
		}
		code += "\/\/--><\/script>\n";
		code += "<script type=\"text/javascript\"\n src=\"http:/" + "/rs.atools.jp/" + RSsize + ".js\"></script>\n";
		code += "<div><a href=\"http:/" + "/www.atools.jp/\" target=\"_blank\">アフィリエイトツール Atools</a></div>\n";
		document.form.code.value = code;
	}
	else if (mode == "2")
	{
		var code = "";
		code += "<script type=\"text/javascript\">";
		code += "var RSsite = \"" + RSsite + "\";";
		code += "var RSword = \"" + RSword + "\";";
		code += "var RSafid = \"" + RSafid + "\";";
		code += "var RStarg = \"" + RStarg + "\";";
		code += "var RScset = \"" + RScset + "\";";
		if (document.form.op1.checked) {
			code += "RSopti = \"noimg\";";
		}
		code += "<\/script>";
		code += "<script type=\"text/javascript\" src=\"http:/" + "/rs.atools.jp/" + RSsize + ".js\"></script>";
		code += "<div><a href=\"http:/" + "/www.atools.jp/\" target=\"_blank\">アフィリエイトツール Atools</a></div>";
		document.form.code.value = code;
	}
	else {
		document.form.mode.value = "実際の表示を確認";
		document.form.submit();
	}
}
function cset_view()
{
	cset_read();
	vc = 0;
	lines = theData.match(/[^,]+/g);
	if (lines)
	{
		for (i = 0; i < lines.length; i++)
		{
			if (lines[i].match(/^(.+):(.+):(.+):(.+):(.+):(.+)$/))
			{
				name = RegExp.$1;
				value = RegExp.$2 + "," + RegExp.$3 + "," + RegExp.$4 + "," + RegExp.$5 + "," + RegExp.$6;
				document.form.cset.options[vc] = new Option(name, value);
				vc++;
			}
		}
	}
	mc = new Array("白黒:000000:cccccc:000000:333333:666666", "水色１:00cccc:ccffff:00cccc:009999:003333", 
	"水色２:99ffff:ffffff:99ffff:00cccc:006666", "青:000066:99ccff:000066:3300cc:000066", "紫:cc00cc:ffccff:cc00cc:cc00cc:330033", 
	"茶:993333:ffcc99:993333:663333:cc3333", "赤:cc0000:ffcccc:cc0000:660000:330000", "ピンク:ff3399:ffccff:ff3399:cc66cc:6666cc", 
	"オレンジ:ff6600:ffcc99:ff6600:cc6600:663300", "黄:cccc00:ffffcc:cccc00:666600:333300", "緑:009900:ccffcc:009900:006600:003300");
	for (i = 0; i < mc.length; i++)
	{
		mc[i].match(/^(.+):(.+):(.+):(.+):(.+):(.+)$/);
		name = RegExp.$1;
		value = RegExp.$2 + "," + RegExp.$3 + "," + RegExp.$4 + "," + RegExp.$5 + "," + RegExp.$6;
		document.form.cset.options[vc] = new Option(name, value);
		vc++;
	}
}
function cset_save()
{
	cset_read();
	var error;
	if (!document.form.cb1.value) {
		alert("外枠の色を設定してください");
		return false;
	}
	else if (!document.form.cb2.value) {
		alert("背景色の色を設定してください");
		return false;
	}
	else if (!document.form.cl.value) {
		alert("仕切色の色を設定してください");
		return false;
	}
	else if (!document.form.ct.value) {
		alert("品名色の色を設定してください");
		return false;
	}
	else if (!document.form.cp.value) {
		alert("価格色の色を設定してください");
		return false;
	}
	else if (!document.form.cn.value) {
		alert("色セット名を設定してください");
		return false;
	}
	else if (!document.form.cb1.value.match(/^[0-9a-f]{6}$/i)) {
		alert("外枠の色は半角英数字（0〜9とA〜F）の6文字の組合せで設定してください。");
		return false;
	}
	else if (!document.form.cb2.value.match(/^[0-9a-f]{6}$/i)) {
		alert("外枠の色は半角英数字（0〜9とA〜F）の6文字の組合せで設定してください。");
		return false;
	}
	else if (!document.form.cl.value.match(/^[0-9a-f]{6}$/i)) {
		alert("外枠の色は半角英数字（0〜9とA〜F）の6文字の組合せで設定してください。");
		return false;
	}
	else if (!document.form.ct.value.match(/^[0-9a-f]{6}$/i)) {
		alert("外枠の色は半角英数字（0〜9とA〜F）の6文字の組合せで設定してください。");
		return false;
	}
	else if (!document.form.cp.value.match(/^[0-9a-f]{6}$/i)) {
		alert("外枠の色は半角英数字（0〜9とA〜F）の6文字の組合せで設定してください。");
		return false;
	}
	else if (!document.form.cn.value.match(/^[^,:]{1,10}$/i)) {
		alert("色セット名は「,:」以外の組み合わせで10文字以下にしてください。");
		return false;
	}
	theData = document.form.cn.value + ":" + document.form.cb1.value + ":" + document.form.cb2.value + ":" + document.form.cl.value + ":" + document.form.ct.value + ":" + document.form.cp.value + "," + theData;
	cset_renew();
	cset_view();
}
function cset_delete()
{
	cset_read();
	delno = document.form.cset.selectedIndex;
	lines = theData.match(/[^,]+/g);
	theData = "";
	for (i = 0; i < lines.length; i++) {
		if (i !== delno) {
			theData += lines[i] + ",";
		}
		document.form.cset.options[i] = null;
	}
	cset_renew();
	cset_view();
}
function cset_read()
{
	theData = "";
	theName = "AtoolsCS=";
	theCookie = document.cookie + ";";
	start = theCookie.indexOf(theName);
	if (start != - 1)
	{
		end = theCookie.indexOf(";", start);
		theData = unescape(theCookie.substring(start + theName.length, end));
	}
}
function cset_renew()
{
	check_cookie();
	theName = "AtoolsCS";
	setDay = new Date();
	setDay.setTime(setDay.getTime() + (365 * 1000 * 60 * 60 * 24));
	expDay = setDay.toGMTString();
	document.cookie = theName + "=" + escape(theData) + ";expires=" + expDay;
}
function cset_form(cset)
{
	var data;
	data = cset.match(/^(\w{6}),(\w{6}),(\w{6}),(\w{6}),(\w{6})$/i);
	if (!data) {
		alert("error");
	}
	document.form.cb1.value = RegExp.$1;
	document.form.cb2.value = RegExp.$2;
	document.form.cl.value = RegExp.$3;
	document.form.ct.value = RegExp.$4;
	document.form.cp.value = RegExp.$5;
	cset_sample();
}
function cset_sample()
{
	if (document.form.cb1.value)
	{
		document.getElementById("Scb1").style.backgroundColor = '#' + document.form.cb1.value;
	}
	if (document.form.cb2.value)
	{
		document.getElementById("Scb2").style.backgroundColor = '#' + document.form.cb2.value;
	}
	if (document.form.cl.value) {
		document.getElementById("Scl").style.backgroundColor = '#' + document.form.cl.value;
	}
	if (document.form.ct.value) {
		document.getElementById("Sct1").style.color = '#' + document.form.ct.value;
	}
	if (document.form.ct.value) {
		document.getElementById("Sct2").style.color = '#' + document.form.ct.value;
	}
	if (document.form.cp.value) {
		document.getElementById("Scp1").style.color = '#' + document.form.cp.value;
	}
	if (document.form.cp.value) {
		document.getElementById("Scp2").style.color = '#' + document.form.cp.value;
	}
	if (document.form.cb1.value)
	{
		var cc1 = parseInt(document.form.cb1.value.substring(0, 2), 16);
		var cc2 = parseInt(document.form.cb1.value.substring(2, 4), 16);
		var cc3 = parseInt(document.form.cb1.value.substring(4, 6), 16);
		if ((0.3 * cc1 + 0.59 * cc2 + 0.11 * cc3) <= 128) {
			document.getElementById("Scr").style.color = "#FFFFFF";
		}
		else {
			document.getElementById("Scr").style.color = "#000000";
		}
	}
}
function main_save()
{
	check_cookie();
	theName = "AtoolsRS-" + RSs;
	theData = RSi1 + "," + RSi2 + "," + RSs3 + "," + RStarg;
	setDay = new Date();
	setDay.setTime(setDay.getTime() + (365 * 1000 * 60 * 60 * 24));
	expDay = setDay.toGMTString();
	document.cookie = theName + "=" + escape(theData) + ";expires=" + expDay;
}
function main_read()
{
	theData = "";
	Fs = document.form.s.value;
	theName = "AtoolsRS-" + Fs + "=";
	theCookie = document.cookie + ";";
	start = theCookie.indexOf(theName);
	if (start != - 1)
	{
		end = theCookie.indexOf(";", start);
		theData = unescape(theCookie.substring(start + theName.length, end));
	}
	if (theData.match(/^([^,]*),([^,]*),([^,]*),(.*)$/))
	{
		document.form.i1.value = RegExp.$1;
		document.form.i2.value = RegExp.$2;
		document.form.s3.value = RegExp.$3;
		document.form.t.value = RegExp.$4;
		document.form.cookie.checked = 1;
	}
	else
	{
		document.form.i1.value = "";
		document.form.i2.value = "";
		document.form.s3.value = "";
		document.form.t.value = "";
		document.form.cookie.checked = "";
	}
}
function main_delete()
{
	check_cookie();
	res = confirm("全ての登録内容を削除しますがよろしいですか？");
	if (res == true)
	{
		for (i = 0; i < 20; i++)
		{
			if (document.form.s.options[i].value)
			{
				theName = "AtoolsRS-" + document.form.s.options[i].value + "=";
				document.cookie = theName + "=;expires=Thu,01-Jan-70 00:00:01 GMT";
			}
		}
		alert("削除が完了しました。");
	}
	else {
		alert("削除を取り消しました。");
	}
}
function check_cookie()
{
	if (!navigator.cookieEnabled) {
		alert("エラー：クッキーの設定を「受け入れる」又は「有効」にしてからもう一度行ってください。");
		return false;
	}
}
