/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
// 親ウィンドウの存在確認.
function fnIsopener() {
    var ua = navigator.userAgent;
    if( !!window.opener ) {
        if( ua.indexOf('MSIE 4')!=-1 && ua.indexOf('Win')!=-1 ) {
            return !window.opener.closed;
        } else {
        	return typeof window.opener.document == 'object';
        }
	} else {
		return false;
	}
}

// 郵便番号入力呼び出し.
function fnCallAddress(php_url, tagname1, tagname2, input1, input2) {
	var idx = tagname1.indexOf(",");
	var send_no = "";
	if (idx >= 0) {
		tmp = tagname1;
		tagname1 = tmp.substr(0, idx);
		send_no = tmp.substr(idx+1);
	}

	if (send_no == "") {
		zip1 = document.form1[tagname1].value;
		zip2 = document.form1[tagname2].value;
	} else {
		zip1 = document.getElementsByName(tagname1)[send_no].value;
		zip2 = document.getElementsByName(tagname2)[send_no].value;
	}

	if(zip1.length == 3 && zip2.length == 4) {
		url = php_url + "?zip1=" + zip1 + "&zip2=" + zip2 + "&input1=" + input1 + "&input2=" + input2 + "&send_no=" + send_no;
		window.open(url,"nomenu","width=500,height=350,scrollbars=yes,resizable=yes,toolbar=no,location=no,directories=no,status=no");
	} else {
		alert("郵便番号を正しく入力して下さい。");
	}
}

// 郵便番号から検索した住所を渡す.
function fnPutAddress(input1, input2) {
	// 親ウィンドウの存在確認。.
	if(fnIsopener()) {
		if(document.form1['state'].value != "") {
			// 項目に値を入力する.
			state_id = document.form1['state'].value;
			town = document.form1['city'].value + document.form1['town'].value;

			var arrTmp = input1.split(",");
			input1 = arrTmp[0];
			var send_no = arrTmp[1];
			if (send_no == "") {
				window.opener.document.form1[input1].selectedIndex = state_id;
				window.opener.document.form1[input2].value = town;
			} else {
				window.opener.document.getElementsByName(input1)[send_no].selectedIndex = state_id;
				window.opener.document.getElementsByName(input2)[send_no].value = town;
			}
		}
	} else {
		window.close();
	}
}

function fnOpenNoMenu(URL) {
	window.open(URL,"nomenu","scrollbars=yes,resizable=yes,toolbar=no,location=no,directories=no,status=no");
}

function fnOpenWindow(URL,name,width,height) {
	window.open(URL,name,"width="+width+",height="+height+",scrollbars=yes,resizable=no,toolbar=no,location=no,directories=no,status=no");
}

function fnSetFocus(name) {
	if(document.form1[name]) {
		document.form1[name].focus();
	}
}

// セレクトボックスに項目を割り当てる.
function fnSetSelect(name1, name2, val) {
	sele1 = document.form1[name1];
	sele2 = document.form1[name2];

	if(sele1 && sele2) {
		index=sele1.selectedIndex;

		// セレクトボックスのクリア
		count=sele2.options.length
		for(i = count; i >= 0; i--) {
			sele2.options[i]=null;
		}

		// セレクトボックスに値を割り当てる。
		len = lists[index].length
		for(i = 0; i < len; i++) {
			sele2.options[i]=new Option(lists[index][i], vals[index][i]);
			if(val != "" && vals[index][i] == val) {
				sele2.options[i].selected = true;
			}
		}
	}
}

// Enterキー入力をキャンセルする。(IEに対応)
function fnCancelEnter()
{
	if (gCssUA.indexOf("WIN") != -1 && gCssUA.indexOf("MSIE") != -1) {
		if (window.event.keyCode == 13)
		{
			return false;
		}
	}
	return true;
}

// モードとキーを指定してSUBMITを行う。
function fnModeSubmit(mode, keyname, keyid) {
	switch(mode) {
	case 'delete_category':
		if(!window.confirm('選択したカテゴリとカテゴリ内のすべてのカテゴリを削除します')){
			return;
		}
		break;
	case 'delete':
		if(!window.confirm('一度削除したデータは、元に戻せません。\n削除しても宜しいですか？')){
			return;
		}
		break;
	case 'confirm':
		/*
		if(!window.confirm('登録しても宜しいですか')){
			return;
		}
		*/
		break;
	case 'delete_all':
		if(!window.confirm('検索結果をすべて削除しても宜しいですか')){
			return;
		}
		break;
	case 'delete_favorite':
		if(!window.confirm('ウィッシュリストから商品を削除しても宜しいですか？')){
			return;
		}
		break;
	default:
		break;
	}
	document.form1['mode'].value = mode;
	if(keyname != "" && keyid != "") {
		document.form1[keyname].value = keyid;
	}
	document.form1.submit();
}

function fnFormModeSubmit(form, mode, keyname, keyid) {
	switch(mode) {
	case 'delete':
		if(!window.confirm('一度削除したデータは、元に戻せません。\n削除しても宜しいですか？')){
			return;
		}
		break;
	case 'confirm':
		if(!window.confirm('登録しても宜しいですか')){
			return;
		}
		break;
	case 'regist':
		if(!window.confirm('登録しても宜しいですか')){
			return;
		}
		break;
	default:
		break;
	}
	document.forms[form]['mode'].value = mode;
	if(keyname != "" && keyid != "") {
		document.forms[form][keyname].value = keyid;
	}
	document.forms[form].submit();
}

function fnSetFormSubmit(form, key, val) {
	document.forms[form][key].value = val;
	document.forms[form].submit();
	return false;
}

function fnSetFormVal(form, key, val) {
	document.forms[form][key].value = val;
}

function fnChangeAction(url) {
	document.form1.action = url;
}

// ページナビで使用する。
function fnNaviPage(pageno) {
	document.form1.pageno.value = pageno;
	document.form1.submit();
}

function fnSearchPageNavi(pageno) {
	document.form1['pageno'].value = pageno;
	document.form1['mode'].value = 'search';
	document.form1.submit();
	}

	function fnSubmit(){
	document.form1.submit();
}

// ポイント入力制限。
function fnCheckInputPoint(errColor) {
	if(document.form1['point_check']) {
		list = new Array(
						'use_point'
						);

		if(!document.form1['point_check'][0].checked) {
			color = "#dddddd";
			flag = true;
		} else {
			color = "";
			if (errColor != "") {
				i = errColor.indexOf(":");
				if (i >= 0)
					color = errColor.substring(i+1);
			}
			flag = false;
		}

		len = list.length
		for(i = 0; i < len; i++) {
			if(document.form1[list[i]]) {
				document.form1[list[i]].disabled = flag;
				document.form1[list[i]].style.backgroundColor = color;
			}
		}
	}
}

// 別のお届け先入力制限。
function fnCheckInputDeliv() {
	if(!document.form1) {
		return;
	}
	if(document.form1['deliv_check']) {
		list = new Array(
						'deliv_name01',
						'deliv_name02',
						'deliv_kana01',
						'deliv_kana02',
						'deliv_pref',
						'deliv_zip01',
						'deliv_zip02',
						'deliv_addr01',
						'deliv_addr02',
						'deliv_tel01',
						'deliv_tel02',
						'deliv_tel03'
						);

		if(!document.form1['deliv_check'].checked) {
			fnChangeDisabled(list, '#dddddd');
		} else {
			fnChangeDisabled(list, '');
		}
	}
}


// 購入時会員登録入力制限。
function fnCheckInputMember() {
	if(document.form1['member_check']) {
		list = new Array(
						'password',
						'password_confirm',
						'reminder',
						'reminder_answer'
						);

		if(!document.form1['member_check'].checked) {
			fnChangeDisabled(list, '#dddddd');
		} else {
			fnChangeDisabled(list, '');
		}
	}
}

// 最初に設定されていた色を保存しておく。
var g_savecolor = new Array();

function fnChangeDisabled(list, color) {
	len = list.length;

	for(i = 0; i < len; i++) {
		if(document.form1[list[i]]) {
			if(color == "") {
				// 有効にする。
				if (!document.form1[list[i]].disabled)
					continue;

				document.form1[list[i]].disabled = false;
				document.form1[list[i]].style.backgroundColor = g_savecolor[list[i]];
			} else {
				// 無効にする。
				if (document.form1[list[i]].disabled)
					continue;

				document.form1[list[i]].disabled = true;
				g_savecolor[list[i]] = document.form1[list[i]].style.backgroundColor;
				document.form1[list[i]].style.backgroundColor = color;//"#f0f0f0";
			}
		}
	}
}


// ログイン時の入力チェック
function fnCheckLogin(formname) {
	var lstitem = new Array();

	if(formname == 'login_mypage'){
	lstitem[0] = 'mypage_login_email';
	lstitem[1] = 'mypage_login_pass';
	}else{
	lstitem[0] = 'login_email';
	lstitem[1] = 'login_pass';
	}
	var max = lstitem.length;
	var errflg = false;
	var cnt = 0;

	//　必須項目のチェック
	for(cnt = 0; cnt < max; cnt++) {
		if(document.forms[formname][lstitem[cnt]].value == "") {
			errflg = true;
			break;
		}
	}

	// 必須項目が入力されていない場合
	if(errflg == true) {
		alert('メールアドレス（または会員No.）/パスワードを入力して下さい。');
		return false;
	}
}

// 時間の計測.
function fnPassTime(){
	end_time = new Date();
	time = end_time.getTime() - start_time.getTime();
	alert((time/1000));
}
start_time = new Date();

//親ウィンドウのページを変更する.
function fnUpdateParent(url) {
	// 親ウィンドウの存在確認
	if(fnIsopener()) {
		window.opener.location.href = url;
	} else {
		window.close();
	}
}

//特定のキーをSUBMITする.
function fnKeySubmit(keyname, keyid) {
	if(keyname != "" && keyid != "") {
		document.form1[keyname].value = keyid;
	}
	document.form1.submit();
}

//文字数をカウントする。
//引数1：フォーム名称
//引数2：文字数カウント対象
//引数3：カウント結果格納対象
function fnCharCount(form,sch,cnt) {
	document.forms[form][cnt].value= document.forms[form][sch].value.length;
}


// テキストエリアのサイズを変更する.
function ChangeSize(button, TextArea, Max, Min, row_tmp){

	if(TextArea.rows <= Min){
		TextArea.rows=Max; button.value="小さくする"; row_tmp.value=Max;
	}else{
		TextArea.rows =Min; button.value="大きくする"; row_tmp.value=Min;
	}
}

// メイン画像切替
var flashvars={};
var params = {};
var attributes = {};
var chip_flag=0;
function SwitchProductImage(image_file,color_name,product_id,stock,chip_id,product_code,stock_unlimited,default_flg){
	if (default_flg == 1) {
		if (document.form1.color_click_flg.value == 1)
			return;
	}
	document.form1.color_click_flg.value = 1;

	chip_flag=chip_id;
	/*
	$("flashCode").innerHTML="<script type='text/javascript'>var flashvars = {image: '/online/upload/save_image/" + image_file + "'};var params = {};var attributes = {};swfobject.embedSWF('product.swf', 'flashContent', '583', '389', '9', '', flashvars, params, attributes);</script>";
	// alert($("flashCode").innerHTML);
	*/
	if(swfobject){
		if(image_file != ""){
			flashvars = {
				image: "/online/upload/save_image/" + image_file   // 商品画像
			};
			swfobject.embedSWF("product.swf", "flashContent", "583", "389", "9", "", flashvars, params, attributes);
		}
	}else{
		alert("FLASHを読み込んでいないため、商品詳細写真を表示できませんでした。\nFLASHプラグインの設定をご確認下さい。");
	}
	/*
	*/
	$("color_name").innerHTML=color_name;
	$("color_name2").innerHTML=color_name;
	$("product_code").innerHTML=product_code;
	var elements = document.getElementsByTagName("a");
	var ar = $A(elements);
	var oid;
	ar.each(function(obj){
		oid=obj.id;
		if(oid.search(/chip_/i) != -1){
			$(oid).style.backgroundPosition="0 0";
		}
	});
	$(chip_id).style.backgroundPosition="0 -37px";
	document.form1.product_id.value=product_id;
	//stock=999;
	if(stock_unlimited != 1 && stock == 0){
		$("quantity").disabled=true;
		$("btn_addcart").disabled=true;
		$("nostock").style.display="inline";
	}else{
		$("quantity").disabled=false;
		$("btn_addcart").disabled=false;
		$("nostock").style.display="none";
	}
}

// 商品一覧画像切替
function ChangeProductImage(target_product,target_image,target_type){
	var target_id="image_" + target_product;
	if(target_type == "large"){
		var file=target_image.split('.');
		$(target_id).src="/online/upload/save_image/" + file[0] + "_thumb." + file[1];
	}else{
		// window.status=target_image;
		$(target_id).src="/online/upload/save_image/" + target_image;
	}
}

// 刺繍イニシャルの入力状態変更
function fnChangeInitialDisabled() {
	if(document.form1['font_id'].length) {
		list = new Array(
						'groom_initial',
						'bride_initial'
						);

		var len = document.form1['font_id'].length;
		var noInitialFlg = false;
		if ( document.form1['font_id'][len-1].checked) {
			noInitialFlg = true;
		}
		if(noInitialFlg) {
			fnChangeDisabled(list, '#dddddd');
		} else {
			fnChangeDisabled(list, '');
		}
	}
}

//メッセージカード情報の入力状態変更
function fnChangeMsgCardDisabled() {
	if(document.form1['msg_card_id'].length) {
		list = new Array('freetext');

		list2 = new Array(
						'to_name',
						'from_name'
						);

		var len = document.form1['msg_card_id'].length;
		var noMsgFlg = false;
		var freeFlg = false;
		if ( document.form1['msg_card_id'][len-1].checked) {
			noMsgFlg = true;
		}

		if ( document.form1['msg_card_id'][4].checked) {
			freeFlg = true;
		}

		if (!freeFlg) {
			fnChangeDisabled(list, '#dddddd');
		} else {
			fnChangeDisabled(list, '');
		}

		if(noMsgFlg) {
			fnChangeDisabled(list2, '#dddddd');
		} else {
			fnChangeDisabled(list2, '');
		}
	}
}

//のしお名前欄の入力状態変更
function fnChangeBrdNoshiDisabled() {
	if(document.form1['bridal_wrapping_id'].length) {
		list = new Array(
						'bridal_noshi_name'
						);

		var len = document.form1['bridal_wrapping_id'].length;
		var noNoshiFlg = false;
		var i;
		var wrapping_id = 0;
		for (i = 0; i < len; i++) {
			if ( document.form1['bridal_wrapping_id'][i].checked) {
				wrapping_id = document.form1['bridal_wrapping_id'][i].value;
				break;
			}
		}
		if (wrapping_id == 1 || wrapping_id == 2) {
			noNoshiFlg = true;
		}

		if(noNoshiFlg) {
			fnChangeDisabled(list, '#dddddd');
		} else {
			fnChangeDisabled(list, '');
		}
	}
}

//ネーム刺繍枚数制御
function fnSetEmbroideryQnt() {
	var num = "";

	if(document.form1['target_product_id'].length) {
		var index = document.form1['target_product_id'].selectedIndex;
		var product_text = document.form1['target_product_id'].options[index].text;
		var index2 = product_text.indexOf("：");

		if (index2 > 0) {
			num = product_text.substr(index2+1, 1);

			if (isNaN(num))
				num = "";
		}

//		// 刺繍対象商品がセット商品の場合、メッセーを表示する
//		cmb_val = document.form1.target_product_id.options[index].value;
//		arr_cmb_val = cmb_val.split(",");
//
//		// 未選択(データエラーも含む)
//		if (arr_cmb_val.length ==3) {
//			// セット商品
//			if (arr_cmb_val[1] >= 1) {
//				document.getElementById("hidd_not_setprodcts").value = "block";		// submit用フラグ
//				document.getElementById("not_setprodcts").style.display = "block";	// デフォルトメッセージ
//				document.getElementById("setprodcts").style.display = "none";		// セット商品メッセージ
////				document.getElementById("hidd_not_setprodcts").value = "none";		// submit用フラグ
////				document.getElementById("not_setprodcts").style.display = "none";	// デフォルトメッセージ
////				document.getElementById("setprodcts").style.display = "block";		// セット商品用メッセージ
//				num = arr_cmb_val[2];
//			// 単品商品
//			}else {
//				document.getElementById("hidd_not_setprodcts").value = "block";		// submit用フラグ
//				document.getElementById("not_setprodcts").style.display = "block";	// デフォルトメッセージ
//				document.getElementById("setprodcts").style.display = "none";		// セット商品メッセージ
//			}
//		}else {
//			document.getElementById("hidd_not_setprodcts").value = "block";			// submit用フラグ
//			document.getElementById("not_setprodcts").style.display = "block";		// デフォルトメセージ
//			document.getElementById("setprodcts").style.display = "none";			// セット商品メッセージ
//		}
	}

	document.form1['embroidery_target_num'].value = num;

	fnSetEmbroideryTxtCnt();
}

//ネーム刺繍文字数設定
function fnSetEmbroideryTxtCnt() {
	var tmp_str = document.form1['name_for_emb'].value.trim();
	var name_for_emb = tmp_str.replace(/[　\s\.．]+/g, "");
	var str_len = name_for_emb.length;
	var err_msg = "";

	var str_kanji_len = 0;
	var str_kana_len = 0;
	var wk_star_kana = "";

	var num = document.form1['embroidery_target_num'].value;
	document.form1['embroidery_target_num_wk_kana'].value = num;
	document.form1['embroidery_target_num_wk_kanji'].value = num;

	// 漢字文字を削除し、漢字以外の文字だけにする
	wk_star_kana = name_for_emb.replace(/[一-龠|々]+/g, "");
	// 漢字以外の文字数
	str_kana_len = wk_star_kana.length;
	// 漢字の文字数
	str_kanji_len = str_len - str_kana_len;


// 2011-04-27 kmaeda カタカナ・漢字対応のため、コメント
/*
	if(str_len > 0 && document.form1['name_embroidery_font_id'].length) {
		var index = document.form1['name_embroidery_font_id'].selectedIndex;
		var font = document.form1['name_embroidery_font_id'].options[index].text;

		if (index <= 0) {
			err_msg = "※「書体」を選択して下さい。";
		} else if (font.indexOf("ひらがな") >= 0) {
			if (!fnEmbHiraganaCheck(name_for_emb))
				//document.form1['name_for_emb'].value = "入れたい文字は、「ひらがな」で入力して下さい。";
				err_msg = "※「入れたい文字」は、ひらがなで入力して下さい。";
		} else {
			if (!fnEmbAlphabetCheck(name_for_emb))
				err_msg = "※「入れたい文字」は、半角アルファベットで入力して下さい。";
		}

		document.getElementById("name_for_emb_err").innerHTML = err_msg;
	}
*/


	if (err_msg != "")
		str_len = "";

	// 刺繍を入れる枚数が数値か確認
	var wk_embroidery_target_num = 0;
	if (!document.form1['embroidery_target_num'].value.match(/[^0-9]/g)) {
		wk_embroidery_target_num = document.form1['embroidery_target_num'].value;
	}

	// 刺繍文字数をセット
	document.form1['name_for_emb_cnt'].value = str_len;
	document.form1['name_for_emb_kana_cnt'].value = str_kana_len;
	document.form1['name_for_emb_kanji_cnt'].value = str_kanji_len;

	// 料金を計算(文字数×単価×枚数)
	document.form1['name_for_emb_kana_yen'].value = str_kana_len * wk_embroidery_target_num * (document.form1['embroidery_kana_price'].value - 0);
	document.form1['name_for_emb_kanji_yen'].value = str_kanji_len * wk_embroidery_target_num * (document.form1['embroidery_kanji_price'].value - 0);
	document.form1['name_for_emb_yen'].value = (document.form1['name_for_emb_kana_yen'].value - 0) + (document.form1['name_for_emb_kanji_yen'].value- 0);
}

//ネーム刺繍入力文字のIMEモード設定
function fnSetEmbImeMode() {
	if(document.form1['name_embroidery_font_id'].length) {
		var index = document.form1['name_embroidery_font_id'].selectedIndex;
		var font = document.form1['name_embroidery_font_id'].options[index].text;
		var emb_ime_mode = "disabled";

		if (index <= 0 || font.indexOf("ひらがな") >= 0) {
			emb_ime_mode = "auto";
		}

		document.form1['name_for_emb'].style.imeMode = emb_ime_mode;
	}
}

/* ネーム刺繍用ひらがなチェック */
function fnEmbHiraganaCheck(str) {
   if( str.match( /[^ぁ-ん　\s!?&.！？＆．]+/ ) ) {
      return false;
   }
   return true;
}

/* ネーム刺繍用アルファベットチェック */
function fnEmbAlphabetCheck(str) {
   if( str.match( /[^A-Za-z0-9\s!?&.]+/ ) ) {
      return false;
   }
   return true;
}

//StringクラスにTrimメソッドの追加
String.prototype.trim = function() {
    return this.replace(/^[ ]+|[ ]+$/g, '');
}

//サマーギフト商品用備考
function fnSetBikouTxt() {
	document.form1['message'].value = "１．ラッピング方法\n\r（ラッピングのみ・リボン・内のし・外のし）：\n\r２．のしのご用途：\n\r３．のしへの名入れ：";

}

//巻紙シール備考
function fnMakigamiBikouTxt(flg) {
	if (flg == 1) {
		document.form1['message'].value = "巻紙のシール：こころばかりシール\n\r枚数：　枚";
	}else if (flg == 2) {
		document.form1['message'].value = "巻紙のシール：さくらシール\n\r枚数：　枚";
	}
}

//カート内商品の削除時チェック処理
function fnChkCartDelete(mode, keyname, keyid, quantity, check_num, t_type, emb_target_num, product_code) {
	var del_flg = true;
	var emb_flg = false;
	var up_down_flg = false;

	switch(mode) {
	case 'down':
	case 'up':
		up_down_flg = true;
		if (emb_target_num != "") {
			emb_flg = true;
		}
	}

	//刺繍
	if (emb_flg) {
		alert("刺繍の文字数は変更できません。");
		return;
	}

	if (up_down_flg) {
		if (product_code == "wrapping" || product_code == "wrapping2") {
			alert("ラッピングの点数は変更できません。");
			return;
		}
	}

	if (check_num > 0) {
		switch(mode) {
		case 'delete':
		case 'delete_product':
			del_flg = false;
			break;
		case 'down':
			if (quantity <= check_num)
				del_flg = false;
			break;
		}
	}

	if (!del_flg) {
		var target = "";
		var arr_type = t_type.split(",");
		var msg_deliv = "";
		var msg_emb = "";
		var i;

		for (i = 0; i < arr_type.length; i++) {
			if (arr_type[i] == "deliv")
				msg_deliv = "この商品は配送先が指定されています。\n\r配送先指定を解除後、削除してください。";
			else if (arr_type[i] == "embroidery")
				msg_emb = "この商品はオリジナルネーム刺繍の対象となっています。\n\rオリジナルネーム刺繍を削除後、削除してください。";
		}
		var msg = msg_deliv;
		if (msg_emb != "" && msg_deliv != "") msg = msg + "\n\r\n\r";
		msg = msg + msg_emb;

		alert(msg);
		return;
	}

	fnModeSubmit(mode, keyname, keyid);
}

//配送先指定画面のギフトラッピング（お箱詰め包装）・のしのご指定制御
function fnChangeWrappingDisabled2(send_no, target_names) {
	var purpose = 0;
	var wrappingFlg = false;
	var color = "#dddddd";
	var i;
	var input_name = "purpose" + send_no;

	if (document.form1[input_name]) {
		for (i = 0; i < document.getElementsByName(input_name).length; i++) {
			if (document.getElementsByName(input_name)[i].checked) {
				purpose = document.getElementsByName(input_name)[i].value;
				break;
			}
		}
	}

	if (purpose != 0) {
		wrappingFlg = true;
		color = "";
		// 2011-04-11 kmaeda
		// デフォルト値をMAX設定
		if (document.getElementsByName('not_gift_cnt[]')[send_no].selectedIndex == 0 && document.getElementsByName('not_gift_cnt[]')[send_no].disabled) {
			document.getElementsByName('not_gift_cnt[]')[send_no].selectedIndex = document.getElementsByName('not_gift_cnt[]')[send_no].length - 1;
		}
	}

	if (purpose == 0) {
		document.getElementsByName('not_gift_cnt[]')[send_no].selectedIndex = 0;
	}

	var before_disabled = document.getElementsByName('wrap_type[]')[send_no].disabled;

	document.getElementsByName('wrap_type[]')[send_no].disabled = !wrappingFlg;
	if (before_disabled == wrappingFlg) {
		document.getElementsByName('wrap_type[]')[send_no].style.backgroundColor = color;
		document.getElementsByName('not_gift_cnt[]')[send_no].disabled = !wrappingFlg;
		document.getElementsByName('not_gift_cnt[]')[send_no].style.backgroundColor = color;
	}

	//ラッピングの入力状態変更
	fnChangeWrappingDisabled(send_no, target_names);
}

//ラッピングの入力状態変更
function fnChangeWrappingDisabled(send_no, target_names) {
	var noshiFlg = false;
	var wrapping = "";
	var color = "#dddddd";

	if (document.form1['wrap_type[]']) {
		if ( window.document.form1['wrap_type[]'][send_no].type == "hidden") {
			wrapping = document.form1['wrap_type[]'][send_no].value;
		}
		else {
			if (document.getElementsByName("wrap_type[]")[send_no].length) {
				if (document.getElementsByName('wrap_type[]')[send_no].disabled == false) {
					var index = document.getElementsByName('wrap_type[]')[send_no].selectedIndex;
					wrapping = document.getElementsByName('wrap_type[]')[send_no].options[index].text;
				}
			}
		}
	}

	if (wrapping != "" && wrapping.indexOf("のし") >= 0) {
		noshiFlg = true;
		color = "";
	}

	if (noshiFlg == document.getElementsByName('wrap_noshi[]')[send_no].disabled) {
		document.getElementsByName('wrap_noshi[]')[send_no].disabled = !noshiFlg;
		document.getElementsByName('wrap_noshi[]')[send_no].style.backgroundColor = color;
		document.getElementsByName('other_wrap_noshi[]')[send_no].disabled = !noshiFlg;
		document.getElementsByName('other_wrap_noshi[]')[send_no].style.backgroundColor = color;
		document.getElementsByName('wrap_name[]')[send_no].disabled = !noshiFlg;
		document.getElementsByName('wrap_name[]')[send_no].style.backgroundColor = color;
	}
	//「のし表書（その他）」欄の入力状態変更
	fnChangeOtherNoshiDisabled(send_no);

	if (target_names == "")
		//お届けする商品のコンボボックス名を取得
		var target_names = fnGetTargetProductName(false);

	// 2011-04-04 kmaeda
	//箱代を設定
	fnSetBoxPrice(send_no, target_names);

	//送料を設定
	fnSetDelivFee(send_no, false, target_names);
}

//「のし表書（その他）」欄の入力状態変更
function fnChangeOtherNoshiDisabled(send_no) {
	var other_noshiFlg = false;
	var noshi = "";
	var color = "#dddddd";

	if (document.form1['wrap_noshi[]']) {
		if ( window.document.form1['wrap_noshi[]'][send_no].type == "hidden") {
			noshi = document.form1['wrap_noshi[]'][send_no].value;
		}
		else {
			if (document.getElementsByName('wrap_noshi[]')[send_no].length && !document.getElementsByName('wrap_noshi[]')[send_no].disabled) {
				var index = document.getElementsByName('wrap_noshi[]')[send_no].selectedIndex;
				noshi = document.getElementsByName('wrap_noshi[]')[send_no].options[index].text;
			}
		}
	}

	if (noshi != "" && (noshi.indexOf("蝶結びその他") >= 0 || noshi.indexOf("結びきりその他") >= 0)) {
		other_noshiFlg = true;
		color = "";
	}

	var now_other_wrap_noshi = false;

	if (document.form1['other_wrap_noshi[]'][send_no] && document.form1['other_wrap_noshi[]'].length > 0)
		now_other_wrap_noshi = document.form1['other_wrap_noshi[]'][send_no].disabled;
	else
		now_other_wrap_noshi = document.form1['other_wrap_noshi[]'].disabled;

	if (other_noshiFlg == now_other_wrap_noshi) {
		if (document.form1['other_wrap_noshi[]'][send_no]) {
			document.form1['other_wrap_noshi[]'][send_no].disabled = !other_noshiFlg;
			document.form1['other_wrap_noshi[]'][send_no].style.backgroundColor = color;
		} else {
			document.form1['other_wrap_noshi[]'].disabled = !other_noshiFlg;
			document.form1['other_wrap_noshi[]'].style.backgroundColor = color;
		}
	}
}

//配送先指定画面のお届けする商品のコンボボックス名を取得する
function fnGetTargetProductName(parent_flg) {
	var len = 0;
	var i, j;

	var target_names = new Array();
	if (parent_flg)
		len = window.opener.document.form1.elements.length;
	else
		len = document.form1.elements.length;

	for (i = 0; i < len; i++) {
		var hidden_flg = false;
		var element_name = "";

		if (parent_flg) {
			hidden_flg = window.opener.document.form1.elements[i].type;
		} else {
			hidden_flg = document.form1.elements[i].type;
		}

		if ( hidden_flg == "hidden")
			continue;

		if (parent_flg) {
			element_name = window.opener.document.form1.elements[i].name;
		} else {
			element_name = document.form1.elements[i].name;
		}
		if ( element_name.indexOf("t_product_") != 0)
			continue;

		var exist_flg = false;
		for (j = 0; j < target_names.length; j++) {
			if ( target_names[j] == element_name) {
				exist_flg = true;
				break;
			}
		}
		if (!exist_flg) target_names.push(element_name);
	}

	return target_names;
}

//配送先指定画面のお届けする商品数コンボックス制御
function fnChangeTargetProductsDisabled(send_no, target_names) {
	if (target_names == "")
		//お届けする商品のコンボボックス名を取得
		var target_names = fnGetTargetProductName(false);

	//箱代を設定
	fnSetBoxPrice(send_no, target_names);

	//送料を設定
	fnSetDelivFee(send_no, false, target_names);
}

//配送先設定画面の箱代設定
function fnSetBoxPrice(send_no, target_names) {
	var i;
	var num = 0;
	var box_flag = -1;

	for (i = 0; i < target_names.length; i++) {
		var val_str = document.getElementsByName(target_names[i])[send_no].value;
		var arr_val = val_str.split(",");
		if (arr_val.length < 3) continue;

		if (!document.getElementsByName(target_names[i])[send_no].disabled) {
			if (arr_val[0] > 0) {
				num = arr_val[0];
				if (box_flag < 0)
					box_flag = arr_val[1];
				else if (arr_val[1] < box_flag)
					box_flag = arr_val[1];
			}
		}
	}

	//箱代を設定
	var box_price = 0;
	var wrap_type = 0;
	var obj_box_cnt;

	// 2011-04-04 kmaeda
	//if (!document.getElementsByName('wrap_type[]')[send_no].disabled)
	//	wrap_type = document.getElementsByName('wrap_type[]')[send_no].selectedIndex;

	//if (wrap_type !=0 && box_flag == 0 && num > 0) {
	//	box_price = document.form1['box_price'].value;
	//}
	//document.getElementsByName('wrapping[]')[send_no].value = box_price;

	//if (box_flag == 0) {
	//	box_price = document.form1['box_price'].value;
	//}
	box_price = document.form1['box_price'].value;
	obj_box_cnt = document.getElementsByName('not_gift_cnt[]')[send_no];

	document.getElementsByName('wrapping[]')[send_no].value = box_price * obj_box_cnt.value;
}

//配送先設定画面の送料設定
function fnSetDelivFee(send_no, parent_flg, target_names) {
	var total_pretax_price = 0;		//税込み合計金額
	var tax = 0;
	var tax_rule = 0;
	var free_rule = 0;
	var box_price = 0;

	if (parent_flg) {
		tax = window.opener.document.form1['tax'].value;
		tax_rule = window.opener.document.form1['tax_rule'].value;
		free_rule = window.opener.document.form1['free_rule'].value;
	}
	else {
		tax = document.form1['tax'].value;
		tax_rule = document.form1['tax_rule'].value;
		free_rule = document.form1['free_rule'].value;
	}

	if (target_names == '')
		//お届けする商品のコンボボックス名を取得
		var target_names = fnGetTargetProductName(parent_flg);

		var str_hakuho_code = "1627";				// 白鵬写真集コード
		var wk_hakuho_photo_cnt = 0;				// 白鵬写真集個数
		var wk_hakuho_photo_othercnt = 0;			// 白鵬写真集以外の個数

		var str_fukubukuro_code = "2548";			// 5000円福袋コード
		var wk_fukubukuro_cnt = 0;					// 5000円福袋個数
		var wk_fukubukuro_othercnt = 0;				// 5000円福袋以外の個数

	for (i = 0; i < target_names.length; i++) {
		var val_str = "";
		if (parent_flg) {
			if (window.opener.document.form1[target_names[i]][send_no] && window.opener.document.form1[target_names[i]][send_no].length > 0)
				val_str = window.opener.document.form1[target_names[i]][send_no].value;
			else
				val_str = window.opener.document.form1[target_names[i]].value;
		} else {
			val_str = document.getElementsByName(target_names[i])[send_no].value;
		}

		var arr_val = val_str.split(",");
		if (arr_val.length < 3) continue;

		// 白鵬写真集の個数とその他の個数を分けて集計
		if (target_names[i].indexOf(str_hakuho_code) != -1) {
			if (isNaN(arr_val[0])) {
				wk_hakuho_photo_cnt += 0;
			}else {
				wk_hakuho_photo_cnt += Number(arr_val[0]);
			}
		}else {
			if (isNaN(arr_val[0])) {
				wk_hakuho_photo_othercnt += 0;
			}else {
				wk_hakuho_photo_othercnt += Number(arr_val[0]);
			}
		}

		// 5000円福袋の個数とその他の個数を分けて集計
		if (target_names[i].indexOf(str_fukubukuro_code) != -1) {
			if (isNaN(arr_val[0])) {
				wk_fukubukuro_cnt += 0;
			}else {
				wk_fukubukuro_cnt += Number(arr_val[0]);
			}
		}else {
			if (isNaN(arr_val[0])) {
				wk_fukubukuro_othercnt += 0;
			}else {
				wk_fukubukuro_othercnt += Number(arr_val[0]);
			}
		}

		var disabled_flg = false;
		if (parent_flg) {
			if (window.opener.document.form1[target_names[i]][send_no] && window.opener.document.form1[target_names[i]][send_no].length > 0)
				disabled_flg = window.opener.document.form1[target_names[i]][send_no].disabled;
			else
				disabled_flg = window.opener.document.form1[target_names[i]].disabled;
		} else {
			disabled_flg =document.getElementsByName(target_names[i])[send_no].disabled;
		}

		if (disabled_flg)
			continue;

		if (arr_val[0] > 0) {
			total_pretax_price += arr_val[0] * fnPreTax(arr_val[2], tax, tax_rule);
		}
	}

	var deliv_fee = 0;
	var deliv_fee2 = 0;

	if (total_pretax_price > 0) {
		var pref = "";
		if (parent_flg) {
			if (window.opener.document.form1['pref[]'][send_no] && window.opener.document.form1['pref[]'][send_no].length > 0) {
				pref = window.opener.document.form1['pref[]'][send_no].value;
			} else {
				pref = window.opener.document.form1['pref[]'].value;
			}
			box_price = window.opener.document.getElementsByName('wrapping[]')[send_no].value;

		} else {
			pref = document.getElementsByName('pref[]')[send_no].value;
			box_price = document.getElementsByName('wrapping[]')[send_no].value;
		}

		total_pretax_price += parseInt(box_price);

		if (pref != "" && total_pretax_price < free_rule) {
			deliv_fee = pref == 47 ? "1,155" : "525";
			deliv_fee2 = pref == 47 ? "1155" : "525";
		}

		// 白鵬写真集が1個のみ購入の場合、メール便で発送するため、送料を315円にする
		if (wk_hakuho_photo_cnt == 1 && wk_hakuho_photo_othercnt == 0) {
			deliv_fee = 315;
			deliv_fee2 = 315;
		}

		// 5000円福袋が1個のみ購入の場合、送料を0円にする
		if (wk_fukubukuro_cnt == 1 && wk_fukubukuro_othercnt == 0) {
			deliv_fee = 0;
			deliv_fee2 = 0;
		}
	}

	if (parent_flg) {
		if (window.opener.document.form1['deliv_fee[]'][send_no]) {
			window.opener.document.form1['deliv_fee[]'][send_no].value = deliv_fee;
			window.opener.document.form1['deliv_fee2[]'][send_no].value = deliv_fee2;
		} else {
			window.opener.document.form1['deliv_fee[]'].value = deliv_fee;
			window.opener.document.form1['deliv_fee2[]'].value = deliv_fee2;
		}
	}
	else {
		document.getElementsByName('deliv_fee[]')[send_no].value = deliv_fee;
		document.getElementsByName('deliv_fee2[]')[send_no].value = deliv_fee2;
	}
}

/* 税金計算 */
function fnTax(price, tax, tax_rule) {
    var real_tax = tax / 100;
    var ret = price * real_tax;

    switch (tax_rule) {
    // 四捨五入
    case 1:
        ret = Math.round(ret);
        break;
    // 切り捨て
    case 2:
        ret = Math.floor(ret);
        break;
    // 切り上げ
    case 3:
        ret = Math.ceil(ret);
        break;
    // デフォルト:切り上げ
    default:
        ret = Math.ceil(ret);
        break;
    }

    return ret;
}

/* 税金付与 */
function fnPreTax(price, tax, tax_rule) {
    return parseInt(price) + parseInt(fnTax(price, tax, tax_rule));
}

// 一つ前のラッピング情報を設定
function fnSetWrappingInfo(send_no) {
	if (send_no <= 0)
		return;

	//購入目的
	var prev_name = "purpose" + (send_no-1);
	var set_name = "purpose" + send_no;
	document.form1[set_name][0].checked = document.form1[prev_name][0].checked;
	document.form1[set_name][1].checked = document.form1[prev_name][1].checked;
	//のし・リボン
	document.form1['wrap_type[]'][send_no].selectedIndex = document.form1['wrap_type[]'][send_no-1].selectedIndex;
	//のし表書
	document.form1['wrap_noshi[]'][send_no].selectedIndex = document.form1['wrap_noshi[]'][send_no-1].selectedIndex;
	//のし表書（その他）
	document.form1['other_wrap_noshi[]'][send_no].value = document.form1['other_wrap_noshi[]'][send_no-1].value;
	//のし 名入れ
	document.form1['wrap_name[]'][send_no].value = document.form1['wrap_name[]'][send_no-1].value;

	//ラッピングの入力状態変更
	fnChangeWrappingDisabled2(send_no, "");

	//ギフトボックス数
	document.form1['not_gift_cnt[]'][send_no].selectedIndex = document.form1['not_gift_cnt[]'][send_no-1].selectedIndex;

	//箱代を設定
	fnSetBoxPrice(send_no, "");
}

//一つ前のお届けする商品情報を設定
function fnSetProductsInfo(send_no) {
	if (send_no <= 0)
		return;

	//お届けする商品のコンボボックス名を取得
	var target_names = fnGetTargetProductName(false);

	for (i = 0; i < target_names.length; i++) {
		document.form1[target_names[i]][send_no].selectedIndex = document.form1[target_names[i]][send_no-1].selectedIndex;
	}

	//箱代を設定
	fnSetBoxPrice(send_no, target_names);

	//送料を設定
	fnSetDelivFee(send_no, false, target_names);
}

//配送先指定画面入力情報をクリア
function fnClearSendInfo(send_no) {
	var i;

	//テキストボックス名
	var input_name = new Array("name01[]", "name02[]", "kana01[]", "kana02[]", "zip01[]", "zip02[]", "addr01[]",
							"addr02[]", "tel01[]", "tel02[]", "tel03[]", "other_wrap_noshi[]", "wrap_name[]", "box_demand[]", "card_message[]");

	for (i = 0; i < input_name.length; i++) {
		document.getElementsByName(input_name[i])[send_no].value = "";
	}

	//コンボボックス名
	var select_name = new Array("pref[]", "wrap_type[]", "wrap_noshi[]", "not_gift_cnt[]");

	for (i = 0; i < select_name.length; i++) {
		document.getElementsByName(select_name[i])[send_no].selectedIndex = 0;
	}

	//お届けする商品のコンボボックス名を取得
	var target_names = fnGetTargetProductName(false);

	for (i = 0; i < target_names.length; i++) {
		document.getElementsByName(target_names[i])[send_no].selectedIndex = 0;
	}

	var check_name = "purpose" + send_no;
	document.getElementsByName(check_name)[1].checked = true;

	//ラッピングの入力状態変更
	fnChangeWrappingDisabled2(send_no, target_names);

	//お届けする商品コンボボックス入力状態制御
	fnChangeTargetProductsDisabled(send_no, target_names);
}

//登録住所選択
function fnSelectAddress(php_url, send_no) {
	var url = php_url + "?send_no=" + send_no;
	window.open(url,"nomenu","width=810,height=550,scrollbars=yes,resizable=yes,toolbar=no,location=no,directories=no,status=no");
}

//選択した登録住所を渡す.
function fnPutSendAddress(send_no, name01, name02, kana01, kana02, zip01, zip02, pref, addr01, addr02, tel01, tel02, tel03) {
	// 親ウィンドウの存在確認。.
	if(fnIsopener()) {
		if (window.opener.document.form1['name01[]'][send_no]) {
			// 項目に値を入力する.
			window.opener.document.form1['name01[]'][send_no].value = name01;
			window.opener.document.form1['name02[]'][send_no].value = name02;
			window.opener.document.form1['kana01[]'][send_no].value = kana01;
			window.opener.document.form1['kana02[]'][send_no].value = kana02;
			window.opener.document.form1['zip01[]'][send_no].value = zip01;
			window.opener.document.form1['zip02[]'][send_no].value = zip02;
			window.opener.document.form1['pref[]'][send_no].selectedIndex = pref;
			window.opener.document.form1['addr01[]'][send_no].value = addr01;
			window.opener.document.form1['addr02[]'][send_no].value = addr02;
			window.opener.document.form1['tel01[]'][send_no].value = tel01;
			window.opener.document.form1['tel02[]'][send_no].value = tel02;
			window.opener.document.form1['tel03[]'][send_no].value = tel03;
		} else {
			window.opener.document.form1['name01[]'].value = name01;
			window.opener.document.form1['name02[]'].value = name02;
			window.opener.document.form1['kana01[]'].value = kana01;
			window.opener.document.form1['kana02[]'].value = kana02;
			window.opener.document.form1['zip01[]'].value = zip01;
			window.opener.document.form1['zip02[]'].value = zip02;
			window.opener.document.form1['pref[]'].selectedIndex = pref;
			window.opener.document.form1['addr01[]'].value = addr01;
			window.opener.document.form1['addr02[]'].value = addr02;
			window.opener.document.form1['tel01[]'].value = tel01;
			window.opener.document.form1['tel02[]'].value = tel02;
			window.opener.document.form1['tel03[]'].value = tel03;
		}

		//お届けする商品のコンボボックス名を取得
		var target_names = fnGetTargetProductName(true);

		//送料を設定
		fnSetDelivFee(send_no, true, target_names);
	} else {
		window.close();
	}
}

//複数配送画面初期表示
function fnDelivViewDefault() {
	var i;
	var unit_cnt = 0;
	var check_flg = false;

	if (document.form1['pref[]'][0]) {
		if ( window.document.form1['pref[]'][0].type != "hidden")
			check_flg = true;
	}
	if (check_flg == false) {
		// scripterr 確認画面(deliv.php)では不要　本関数から抜ける 2010/10/31
		if (!document.getElementById('wait_msg')) {
			return;
		}
		document.getElementById('wait_msg').style.display = 'none';
		return;
	}

	if (document.form1['unit_cnt'])
		unit_cnt = document.form1['unit_cnt'].value;

	//お届けする商品のコンボボックス名を取得
	var target_names = fnGetTargetProductName(false);

	// 最後尾のお届け先が以下の条件を全て満たす場合
	// 最後尾のお届けする商品個数を全てMAX選択設定にする
	// 
	// １．最後尾のお届け先のテキストボックスが全て空
	// ２．最後尾のお届け先で選択されている商品個数が全て0個の場合
	// ３．最後尾のお届け先ののしのご指定がいいえ
	if (fnCheckDefoSet(target_names, unit_cnt)) {
		// 購入商品コンボボックス分ループ
		for (i = 0; i < target_names.length; i++) {
			document.getElementsByName(target_names[i])[unit_cnt-1].selectedIndex = document.getElementsByName(target_names[i])[unit_cnt-1].length -1;
		}

		//箱代を設定
//		fnSetBoxPrice(i, target_names);
	}

	for ( i = 0; i < unit_cnt; i++) {
		//ラッピングの入力状態変更
		fnChangeWrappingDisabled2(i, target_names);

		//送料を設定
		fnSetDelivFee(i, false, target_names);
	}

	document.getElementById('wait_msg').style.display = 'none';
}


// 複数配送入力画面で商品個数をMAX設定するかチェックする
function fnCheckDefoSet(target_names, unit_cnt) {
	var i;										// 添字
	var defo_flg = false;						// 商品個数MAX設定判定フラグ（false:しない true:する）
	var str_unit_cnt = "" + unit_cnt - 1;		// ラジオボタン用にunit_cntを文字列化

	// unit_cntが数値でない場合、falseを返す
	if (isNaN(unit_cnt)) {
		return defo_flg;
	}else {
		if (unit_cnt < 1) {
			return defo_flg;
		}
	}

	// テキストボックスに文字が入っているか確認する
	var input_name = new Array("name01[]", "name02[]", "kana01[]", "kana02[]", "zip01[]", "zip02[]", "addr01[]",
							"addr02[]", "tel01[]", "tel02[]", "tel03[]", "other_wrap_noshi[]", "wrap_name[]");

	for (i = 0; i < input_name.length; i++) {
		if (document.getElementsByName(input_name[i])[unit_cnt-1].value != "") {
			return defo_flg;
		}
	}

	// 都道府県コンボボックスで都道府県が選択されているか確認する
	if(document.getElementsByName("pref[]")[unit_cnt-1].selectedIndex >0) {
		return defo_flg;
	}

	// 商品コンボボックスで個数が選択されているか確認する
	for (i = 0; i < target_names.length; i++) {
		if(document.getElementsByName(target_names[i])[unit_cnt-1].selectedIndex >0) {
			return defo_flg;
		}
	}

	// ラッピングラジオボタンで「はい」が選択されていないか確認する
	if (document.form1["purpose"+str_unit_cnt][0].checked) {
		return defo_flg;
	}

	defo_flg = true;
	return defo_flg;
}

