/* JavaScript for Kimiya  (kimiya.net)  */
/* (C)2007 Yosuke Okayama (webapte.com) */

// フレーム対策
if (self != top) {
	top.location = self.location;
}

// 画像を表示
function show_image(image_name, width, height, mode) {
	wx = width  + 20;
	wy = height + 40;
	x = (screen.width  - wx) / 2;
	y = (screen.height - wy) / 2;

	URL = "/pds/view.cgi?image=" + image_name + ";width=" + width + ";height=" + height;
	if (mode) {
		URL += ";mode=" + mode;
	}
	imagewindow = image_name.replace(/\/|\.|\-/g, "");	// 複数開けるようにする
	option  = "left=" + x + ",top=" + y + ",width=" + wx + ",height=" + wy;
	option += ",scrollbars=yes,location=no,status=no,resizable=yes";
	window.open(URL, imagewindow, option);
}

// URL が正しいかチェック
function check_url(URL, num) {
	if (!URL || (URL == "http://")) {
		if (num) {
			document.myform.url.value = "http://";
			document.myform.url.focus();
		}
		alert("URL を入力してください。");
	}
	else if (URL.length < 12) {
		if (num) {
			document.myform.url.value = URL;
			document.myform.url.focus();
		}
		alert("URL を正しく入力してください。");
	}
	else {
		window.open(URL, "urlwindow");
	}
}

// すべて選択
function square_check() {
	if (document.myform.square.checked == true) {
		checkall(1);
	}
	else {
		checkall(0);
	}

}
function checkall(check, square) {
	var count;
	for (count = 0; count < document.myform.code.length; count++) {
		document.myform.code[count].checked = check;
	}
	if (square) {
		if (check) {
			document.myform.square.checked = true;
		}
		else {
			document.myform.square.checked = false;
		}
	}

}
function selectall(check) {
	max = document.myform.select_count.value;
	for (count = 1; count <= max; count++) {
		document.myform.elements["order_" + count].options[check].selected = true;
	}
}

// 郵便番号→住所検索
function zipsearch(company, e_key, address_check) {
	if (e_key) {
		zip1 = document.myform.e_zip1.value;
		zip2 = document.myform.e_zip2.value;
	}
	else {
		zip1 = document.myform.zip1.value;
		zip2 = document.myform.zip2.value;
	}
	zip = zip1 + "-" + zip2;

	// 全角から半角に変換
	han = "0123456789";
	zen = "０１２３４５６７８９";
	str = "";
	for (i=0; i<zip.length; i++) {
		c = zip.charAt(i);
		n = zen.indexOf(c,0);
		if (n >= 0) c = han.charAt(n);
		str += c;
	}
	zip = str;

	if ((zip1 == "") && (zip2 == "")) {
		zip_focus(1, e_key);
		alert("郵便番号を入力してください。");
		return;
	}
	else if (!zip.match(/^\d{3}-\d{4}$/)) {
		if (!zip1.match(/^\d{3}$/)) {
			zip_focus(1, e_key);
		}
		else if (!zip2.match(/^\d{4}$/)) {
			zip_focus(2, e_key);
		}
		alert("郵便番号が正しくありません。3桁-4桁で入力してください。");
		return;
	}
	url = "/pds/zipsearch.cgi?zip=" + zip;

	if (company) {
		url = url + ";mode=company";
	}
	if (e_key) {
		url = url + ";e_key=e_";
	}
	if (address_check) {
		url = url + ";address_check=1";
	}

	wx = 500;
	wy = 300;
	x = (screen.width  - wx) / 2;
	y = (screen.height - wy) / 2;

	window.open(url, "zipwindow", "left=" + x + ",top=" + y + ",width=500,height=300,status=yes,resizable=yes");
}
function zip_focus(zip_type, e_key) {
	if (zip_type == 1) {
		if (e_key) {
			document.myform.e_zip1.focus();
		}
		else {
			document.myform.zip1.focus();
		}
	}
	else {
		if (e_key) {
			document.myform.e_zip2.focus();
		}
		else {
			document.myform.zip2.focus();
		}
	}
}
function check_address(e_key) {
	if (e_key) {
		address = document.myform.e_address.value;
	}
	else {
		address = document.myform.address.value;
	}
	address_check = document.myform.address_check.value;
	company_check = document.myform.company_check.value;

	if (!address) {
		if (e_key) {
			document.myform.e_address.focus();
		}
		else {
			document.myform.address.focus();
		}
		alert("住所を入力してください。");
		return false;
	}
	if (address_check && !company_check && (address_check == address)) {
		flag = confirm("番地等が入力されていませんが、このまま次のページに進んでよろしいですか？");
		if (flag) {
			return true;
		}
		else {
			if (e_key) {
				document.myform.e_address.focus();
			}
			else {
				document.myform.address.focus();
			}
			return false;
		}
	}
	return true;
}

// ヘルプを表示
function pdshelp(num) {
	wx = screen.width  - 200;
	wy = screen.height - 200;
	x = (screen.width  - wx) / 2;
	y = (screen.height - wy) / 2;

	URL = "/pds/help/?code=" + num;
	option  = "left=" + x + ",top=" + y + ",width=" + wx + ",height=" + wy;
	option += ",scrollbars=yes,location=no,status=yes,resizable=yes";
	window.open(URL, "helpwindow", option);
}

function open_help(URL) {
	wx = screen.width  - 200;
	wy = screen.height - 200;
	x = (screen.width  - wx) / 2;
	y = (screen.height - wy) / 2;

	option  = "left=" + x + ",top=" + y + ",width=" + wx + ",height=" + wy;
	option += ",scrollbars=yes,location=no,status=yes,resizable=yes";
	window.open(URL, "helpwindow", option);
}

// ヘルプ用リンク
function pdslink(URL) {
	if (window.opener) {
		if (!confirm("このウィンドウを閉じて該当するページに移動しますか？")) {
			return false;
		}
		window.opener.location.href = URL;
		window.close();
	}
	else {
		location.href = URL;
	}
}
// ヘルプ用リンク (前のページに戻る)
function help_back() {
	if (history.back()) {
		history.back();
	}
	else if (document.referrer) {
		location.href = document.referrer;
	}
	else {
		window.close();
	}
}

// 2度ボタンを押せないようにする
function submit_check() {
	document.myform.button.disabled = true;
}

// **************** 以下、特定のページでのみ使用 ****************

// トップページ
prImg = new Array();
for (i = 1; i <= 4; i++) {
	prImg[i] = new Image();
	prImg[i].src = "/pds/common/market" + i + "o.jpg";
}

function changeimage(name, image) {
	document.images["market" + name].src = "/pds/common/market" + image + ".jpg";
}

// sell, buy, new
/*
function type_change(selOBJ, query) {
	type = selOBJ.options[selOBJ.selectedIndex].value;
	location.href = "/pds/" + type + "/" + query;
}

function category_change(selOBJ, type, query) {
	category = selOBJ.options[selOBJ.selectedIndex].value;

	if (category && query) {
		url = "/pds/" + type + "/?category=" + category + ";" + query;
	}
	else if (category) {
		url = "/pds/" + type + "/?category=" + category;
	}
	else if (query) {
		url = "/pds/" + type + "/?" + query;
	}
	else {
		url = "/pds/" + type + "/";
	}
	location.href = url;
}
*/


// 画像切り替え
function show_catalog(num, max) {
	for (i = 1; i <= max; i++) {
		if (i == num) {
			document.getElementById("catalog_" + i).style.display = "block";
		}
		else {
			document.getElementById("catalog_" + i).style.display = "none";
		}
	}
}

// 資料請求ボタン
function catalog() {
	document.myform.contact_type.value = 2;
	document.myform.submit();
}

// PDS会員出品時
function check_exhibit(type) {
	explain = document.myform.explain.value;
	if (!explain) {
		if (type == "sell") {
			flag = confirm("「詳細説明」が入力されていませんが、このまま次のページに進んでよろしいですか？");
		}
		else if (type == "buy") {
			flag = confirm("「詳細説明」が入力されていません。希望する商品の仕様など、詳しく記載することをおすすめします。");
		}
		else {	// new
			alert("「詳細説明」を入力してください。");
			document.myform.explain.focus();
			return false;
		}

		if (!flag) {
			document.myform.explain.focus();
			return false;
		}
	}

	// 「買います」ならスルー
	price = document.myform.price.value;
	if (type == "buy") {
		if(!price){
			flag = confirm("業者渡価格が入力されていませんが、このまま次のページに進んでよろしいですか？");
			if (flag) {
				return true;
			}
			else {
				document.myform.price.focus();
				return false;
			}
		}
	}

	price = document.myform.price.value;
	// 価格が入力されているなら OK
	if (price && (price != 0)) {
		return true;
	}
	// 「新品」
	if (type == "new") {
		alert("希望価格が入力されていません。");
		document.myform.price.focus();
		return false;
	}
	// 「売ります」
	else {
		alert("業者渡価格を入力してください。");
		document.myform.price.focus();
		return false;
	}
}

// 出品時
function upload(upload_script, order) {
	if (order) {
		document.myform.order.value = order;
	}
	document.myform.action = upload_script;
	document.myform.submit();
}

// 画像の切り抜き
point = 1;
function check(mx, my) {
	if (point == 1) {
		document.myform["point"][1].checked = true;
		point = 2;
		document.myform.pointX1.value = mx;
		document.myform.pointY1.value = my;

		// IE5.5
		if (window.createPopup) {
			document.getElementById("myImage1").style.visibility = "visible";
			X =  event.x + document.body.scrollLeft - 25;
			Y =  event.y + document.body.scrollTop  - 25;
			document.getElementById("myImage1").style.left = X + "px";
			document.getElementById("myImage1").style.top  = Y + "px";
		}
	}
	else {
		document.myform["point"][0].checked = true;
		point = 1;
		document.myform.pointX2.value = mx;
		document.myform.pointY2.value = my;

		// IE5.5
		if (window.createPopup) {
			document.getElementById("myImage2").style.visibility = "visible";
			X =  event.x + document.body.scrollLeft - 25;
			Y =  event.y + document.body.scrollTop  - 25;
			document.getElementById("myImage2").style.left = X + "px";
			document.getElementById("myImage2").style.top  = Y + "px";
		}
	}
}
function check_status(mx,my) {
	document.myform.statusX.value = mx;
	document.myform.statusY.value = my;
}

// 仮登録農家用
function mailset(num) {
	if (num == 1) {
		if ((document.myform.subject.value == "") && (document.myform.message.value == "")) {
			if (confirm("「件名」と「本文」をリセット (元に戻す) します。\nよろしいですか？")) {
				document.myform.reset();
				mail_reset();
			}
		}
	}
	else {
		document.myform.save_text.checked = false;
		if ((document.myform.subject.value != "") && (document.myform.message.value != "")) {
			if (confirm("「件名」と「本文」をクリア (消去) します。\nよろしいですか？")) {
				mail_clear();
			}
		}
	}
}
function mail_reset() {
	document.myform.type[0].checked = true
	document.myform.save_text.checked = true;
}
function mail_clear() {
	document.myform.subject.value = "";
	document.myform.message.value = "";
}

// 署名の編集
function insert_email(email) {
	textarea = document.myform.user_signature;
	str = textarea.value;
	selection = new Selection(textarea);
	s = selection.create();
	ss = str.substring(0, s.start);
	se = str.substring(s.end, str.length);
	textarea.value = ss + email + se;

	// IE5.5
	if (window.createPopup) {
		point = (ss + email).length;
		range = textarea.createTextRange();
		range.move("character", point);
		range.select();
	}
}

// 農家の検索と管理
function clear_form() {
	document.search_form.address.value = "";
	document.search_form.name.value = "";
	document.search_form.tel.value = "";
	document.search_form.email.value = "";
	document.search_form.year.options[0].selected = true;
	document.search_form.mon.options[0].selected = true;
	document.search_form.day.options[0].selected = true;
}

function farmer_edit(no) {
	document.myform.mycode.value = no;
	document.myform.mode.value = "farm_detail";
	document.myform.submit();
}

// 出品中の商品の一覧
function set_view_exhibit(type) {
	if (type == "new") {
		if ((document.myform["mode"][1].checked != true) && (document.myform["mode"][2].checked != true)) {
			document.myform["mode"][0].checked = true;
		}
		document.getElementById("ex_status").style.display = "none";
	}
	else {
		document.getElementById("ex_status").style.display = "block";
	}
}

// 取引先の指定
function id_check() {
	if ((document.myform2.oudan.value == 1) && !document.myform2.sold_price.value) {
		alert("商談成立価格を入力してください。");
		document.myform2.sold_price.focus();
		return false;
	}

	if (document.myform2.id.value != "") {
		return true;
	}

	if (document.myform2.trade.value != 0) {
		alert("取引先を指定してください。");
		document.myform1.company.focus();
		return false;
	}

	flag = confirm("取引先が入力されていませんが、このまま商談成立にしてよろしいですか？\nこのサイト以外で商談が成立した場合は削除してください。\n詳しくはヘルプをご覧ください。");
	if (flag) {
		pdshelp(131);
		return true;
	}
	else {
		return false;
	}
}

// direct 出品時
function maker_change_num(page) {
	document.myform.page.value = page;
	document.myform.change_mode.value = 1;
	document.myform.action = "./#detail";
	document.myform.submit();
}
function maker_edit(no, page, myform) {
	document.forms[myform].no.value = no;
	document.forms[myform].page.value = page;
	document.forms[myform].submit();
}

function maker_clear() {
	document.search_form.no.value = "";
	document.search_form.category.options[0].selected = true;
	document.search_form.maker.options[0].selected = true;
	document.search_form.article.value = "";
	document.search_form.pickup.options[0].selected = true;
}

function excel_mode(upload_script) {
	document.myform.page.value = "excel";
	document.myform.action = upload_script;
	document.myform.submit();
}

function maker_upload(upload_script, page, order) {
	wx = screen.width  - 200;
	wy = screen.height - 200;
	x = (screen.width  - wx) / 2;
	y = (screen.height - wy) / 2;

	URL = upload_script + "?mode=maker;page=" + page;
	if (order) {
		URL += ";order=" + order;
	}
	if (page == "exhibit") {
		URL += ";no=" + document.myform.no.value;
	}
	else if (page == "sale") {
		URL += ";no=" + document.myform.no.value;
	}
	option  = "left=" + x + ",top=" + y + ",width=" + wx + ",height=" + wy;
	option += ",scrollbars=yes,location=no,status=yes,resizable=yes";
	window.open(URL, "uploadwindow", option);
}

// 販売価格自動入力
function maker_auto(maxlength) {
	find = 0;
	for (i = 1; i <= maxlength; i++) {
		if (document.myform.elements["regular_price" + i].value) {
			find = 1;
			break;
		}
	}
	if (!find) {
		document.myform.regular_price1.focus();
		alert("「小売価格」を入力してから実行してください。");
		return;
	}

	wx = 400;
	wy = 200;
	if (document.all) {
		x = document.myform.x.value;
		y = document.myform.y.value - 150;
		check_x = screen.width - x;
		if (check_x <= 400) {
			x = screen.width - 500;
		}
	}
	else {
		x = (screen.width  - wx) / 2;
		y = (screen.height - wy) / 2;
	}

	URL = "/pds/?mode=maker;page=exhibit_auto;maxlength=" + maxlength;
	option  = "left=" + x + ",top=" + y + ",width=" + wx + ",height=" + wy;
	option += ",scrollbars=yes,location=no,status=yes,resizable=yes";
	window.open(URL, "autowindow", option);
}

// 送料自動入力
function postage(pref) {
	wx = 400;
	wy = 100;
	if (document.all) {
		x = document.myform.x.value;
		y = document.myform.y.value - 75;
		check_x = screen.width - x;
		if (check_x <= 400) {
			x = screen.width - 500;
		}
	}
	else {
		x = (screen.width  - wx) / 2;
		y = (screen.height - wy) / 2;
	}
	pref = pref.replace(/,/g, "%2C");
	URL = "/pds/?mode=maker;page=postage_auto;pref=" + pref;
	option  = "left=" + x + ",top=" + y + ",width=" + wx + ",height=" + wy;
	option += ",scrollbars=yes,location=no,status=yes,resizable=yes";
	window.open(URL, "postagewindow", option);
}

// 売り上げレポート
function proceeds(no, mode) {
	if (mode == "delete") {
		if (!confirm("選択された商品をリストから削除してよろしいですか？")) {
			return;
		}
	}
	document.myform.page.value = "proceeds_" + mode;
	document.myform.no.value = no;
	document.myform.submit();
}

// 確認ウィンドウ
function confirm_window(id, mode, message) {
	if (!confirm(message)) {
		return;
	}
	document.myform.page.value = mode;
	document.myform.no.value = id;
	document.myform.submit();
}

// 送料の設定を確認
function postage_check() {
	type = document.myform.postage_setting.selectedIndex + 1;
	URL = "/pds/?mode=maker;page=postage;check=1;type=" + type;
	window.open(URL, "postage_check_window");
}

function sale_delete() {
	if (confirm("選択されたセール情報を削除してよろしいですか？")) {
		document.myform2.page.value = "sale_delete";
		document.myform2.submit();
	}
}

function sale_search() {
	document.myform.page.value = "sale_search";
	document.myform.submit();
}

function simulate() {
	category = new Array("sell", "buy");
	type  = document.simform.type.selectedIndex;
	price = document.simform.price.value;

	// 全角から半角に変換
	han = "0123456789";
	zen = "０１２３４５６７８９";
	str = "";
	for (i=0; i<price.length; i++) {
		c = price.charAt(i);
		n = zen.indexOf(c,0);
		if (n >= 0) c = han.charAt(n);
		str += c;
	}
	str = str.replace(/,|\./g, "");
	price = str;

	wx = 420;
	wy = 300;
	x = (screen.width  - wx) / 2;
	y = (screen.height - wy) / 2;
	URL = "/pds/?mode=simulate;type=" + category[type] + ";price=" + price;
	option  = "left=" + x + ",top=" + y + ",width=" + wx + ",height=" + wy;
	option += ",scrollbars=yes,location=no,status=yes,resizable=yes";
	window.open(URL, "simulatewindow", option);
}


// open 出品時
function open_upload(upload_script, order) {
	wx = screen.width  - 200;
	wy = screen.height - 200;
	x = (screen.width  - wx) / 2;
	y = (screen.height - wy) / 2;

	URL = upload_script + "?mode=open_main;order=" + order + ";no=" + document.myform.no.value;
	option  = "left=" + x + ",top=" + y + ",width=" + wx + ",height=" + wy;
	option += ",scrollbars=yes,location=no,status=yes,resizable=yes";
	window.open(URL, "uploadwindow", option);
}

function open_edit(mode, no) {
	document.myform.mode.value = mode;
	document.myform.no.value   = no;
	document.myform.submit();
}

function open_flag(mode) {
	document.myform.mode.value = mode;
}

function hourmeter_title(type) {
	// IE5.5
	if (window.createPopup) {
		if (type == "h") {
			hourmeter.innerText = "アワーメーター";
		}
		else {
			hourmeter.innerText = "走行距離";
		}
	}
}

// メニューを非表示にする
function close_menu() {
		document.getElementById("list").style.display = "none";
}

function OpenMenu(str, id){
	linkObj = document.getElementsByTagName("div");
	for (i=0; i<linkObj.length; i++){
		tmp = linkObj[i].id;
		regobj = new RegExp(str, "i");
		result = tmp.match(regobj);
		if(result){
			if(tmp == id){
				linkObj[i].style.display = "block";
			}else{
				linkObj[i].style.display = "none";
			}
		}
	}
}
