// 画像リサイズ
function img_resize(imgid){
	var size_w = document.images[imgid].width;
	var size_h = document.images[imgid].height;
	var size_max = 100;
	if(size_w < 30 || size_h < 30){
		document.images[imgid].width = 1;
		document.images[imgid].height = 1;
	}else if(size_w >= size_h){
		if(size_w > size_max){ document.images[imgid].width = size_max; }
	}else{
		if(size_h > size_max){ document.images[imgid].height = size_max; }
	}
}

// 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 td{margin:0;padding:3px;border-bottom:1px #999999 solid;}\n"
	code += "div.g_title{margin:0 0 5px 0;padding:0;font-weight:bold;}\n"
	code += "div.g_price{margin:0;padding:0 0 10px 0;}\n"
	code += "span.g_price1{margin:0;padding:2px;color:#CC0000;font-size:13px;font-weight:bold;}\n"
	code += "span.g_price2{margin:0;padding:2px;font-size:11px;color:#666666;}\n"
	code += "div.g_capt{margin:0;padding:0;}\n"
	code += "div.g_shop{margin:0;padding:0;text-align: right;}\n"
	code += "span.g_capt2{display:none;}\n"
	code += "//--></style>\n"
	code += "<script language=\"javascript\"><!--\n"
	code += "window.onload = function(){\n"
	code += "for(i=1;i<=50;i++){\n"
	code += "var imgid = 'i'+i;\n"
	code += "var size_w = document.images[imgid].width;\n"
	code += "var size_h = document.images[imgid].height;\n"
	code += "var size_max = 100;\n"
	code += "if(size_w < 30 || size_h < 30){\n"
	code += "document.images[imgid].width = 1;\n"
	code += "document.images[imgid].height = 1;\n"
	code += "}else if(size_w >= size_h){\n"
	code += "if(size_w > size_max){ document.images[imgid].width = size_max; }\n"
	code += "}else{\n"
	code += "if(size_h > size_max){ document.images[imgid].height = size_max; }\n"
	code += "}\n"
	code += "}\n"
	code += "};\n"
	code += "//--></script>\n"
	if(document.all){
		code += document.all.goods.innerHTML;
	}else{
		code += document.getElementById('goods').innerHTML;
	}
	code += '<a href="http://lsstore.atools.jp/" style="color:#666666;">通販＆リンク生成 Atools</a>';
	// タブ削除
	code = code.replace(/\t/ig,"");
	// マーチャントリンクをマーチャント名だけに変更
	code = code.replace(/<a href=\"\/\d+\/\".+?>(.+?)<\/a>/ig,"$1");
	// 主カテゴリリンクをカテゴリ名だけに変更
	code = code.replace(/<a href=\"\/.+?\">(.+?)<\/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  = "lstoken";
	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();
};
