
// token設定
function set_token(){
	var id1 = document.form_aff.id1.value;
	if(!id1){
		alert('tokenを入力してください');
		return;
	}
	// 入力内容記録
	if(id1){ set_id1(id1); }
}

// リンクコード生成
function create_code(){
	var target = document.form_aff.target.value;
	var save = document.form_aff.save.checked;
	if(target.length > 30){
		alert('リンク表示先の文字数を30文字以内にしてください');
		return;
	}
	// 入力内容記録
	if(save){
		if(target){ set_target(target); }
	}
	// コード生成
	var code = '';
	code += "<style><!--\n"
	code += "table#goods img{border:none;}\n"
	code += "table#goods td{font-size:85%;}\n"
	code += "table#goods td{padding: 10px 0;border-bottom: 1px #ccc solid;vertical-align:top;}\n"
	code += "table#goods td.l{width: 80px;text-align: center;}\n"
	code += "table#goods td.c{width: 120px;text-align: center;}\n"
	code += "table#goods td.r{width: 460px;}\n"
	code += "table#goods div.title{margin: 0 0 5px 0;font-size: 120%;;}\n"
	code += "table#goods div.price span{color: #cc0000;font-size: 130%;;}\n"
	code += "table#goods div.tax,table#goods div.card{color: #666;font-size: 85%;}\n"
	code += "//--></style>\n"
	if(document.all){
		code += '<table id="goods">' + document.all.goods.innerHTML + '</table>';
	}else{
		code += '<table id="goods">' + document.getElementById('goods').innerHTML + '</table>';
	}
	code += '<a href="http://vcstore.atools.jp/" style="color:#666666;"><strong>通販価格比較＆リンク生成</strong></a>';
	// タブ削除
	code = code.replace(/\t/ig,"");
	// ecリンクをマーチャント名だけに変更
	code = code.replace(/<a href=\"\/ec\/[^>]+?>(.+?)<\/a>/ig,"$1");
	// target設定
	if(target){
		// これでは win ie が変換不可
	//	code = code.replace(/ target=".*?"/ig," target=\"" + target + "\"");
		// こちらで対応
		code = code.replace(/ target="?[^" >]*"?/ig," target=\"" + target + "\"");
	}
	// コード表示
	document.form_aff.code.value = code;
}

// cookie記録
function set_id1(id1){
	theName  = "vctoken";
	setDay = new Date();
	setDay.setTime(setDay.getTime()+(1000*60*60*24*365));
	expDay = setDay.toGMTString();
	document.cookie = theName + "="+escape(id1)+";path=/;expires="+expDay;
	alert('設定が完了しました。');
}
function set_target(target){
	theName  = "target";
	setDay = new Date();
	setDay.setTime(setDay.getTime()+(1000*60*60*24*365));
	expDay = setDay.toGMTString();
	document.cookie = theName + "="+escape(target)+";path=/;expires="+expDay;
}

// cookie呼び出し
function get_id1(){
	theName   = "lstoken=";
	theCookie = document.cookie+";";
	start = theCookie.indexOf(theName);
	if(start != -1){
		end = theCookie.indexOf(";",start);
		id1 = unescape(theCookie.substring(start+theName.length,end));
		document.form_aff.id1.value = id1;
	}
}
function get_target(){
	theName   = "target=";
	theCookie = document.cookie+";";
	start = theCookie.indexOf(theName);
	if(start != -1){
		end = theCookie.indexOf(";",start);
		target = unescape(theCookie.substring(start+theName.length,end));
		document.form_aff.target.value = target;
	}
}

window.onload = function(){
	get_id1();
	get_target();
};