/*
 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 */
// 親ウィンドウの存在確認.
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) {
	zip1 = document.form1[tagname1].value;
	zip2 = document.form1[tagname2].value;
	
	if(zip1.length == 3 && zip2.length == 4) {
		url = php_url + "?zip1=" + zip1 + "&zip2=" + zip2 + "&input1=" + input1 + "&input2=" + input2;
		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;
			window.opener.document.form1[input1].selectedIndex = state_id;
			window.opener.document.form1[input2].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 'del_cart_all':
		if(!window.confirm('一度削除したデータは、元に戻せません。\n削除しても宜しいですか？')){
			return;
		}
		break;
	case 'confirm':
		if(!window.confirm('登録しても宜しいですか')){
			return;
		}
		break;
	case 'delete_all':
		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() {
	if(document.form1['point_check']) {
		list = new Array(
						'use_point'
						);
	
		if(!document.form1['point_check'][0].checked) {
			color = "#dddddd";
			flag = true;
		} else {
			color = "";
			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 == "") {
				// 有効にする。
				document.form1[list[i]].disabled = false;
				document.form1[list[i]].style.backgroundColor = g_savecolor[list[i]];
			} else {
				// 無効にする。
				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('メールアドレス/パスワードを入力して下さい。');
		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;
	}
}

// カート振り分け(個数)
function confirmNum(order, num, tmp, tmp_select){
	numTmp = tmp.value;
	if(numTmp == ""){
		numTmp = 0;
	}
	numVal = num.value;
	numVal = Fulltohalf(numVal);
	orderVal = order.options[order.selectedIndex].value;
	if(orderVal == "" || orderVal == null){
		return ;
	}
	
	if(numVal.match("[^0-9]+")){
		alert("半角数字で入力してください。");
		num.focus();
	 	num.value = tmp.value;
	}else if(numVal.length == 0){
	 	orderLimit = parseInt(document.getElementById("order_" + orderVal).innerHTML);
	 	document.getElementById("order_" + orderVal).innerHTML = orderLimit + parseInt(numTmp);
	 	tmp.value = "";
	}else{
		if(numVal < 1){
			alert("1以上の個数を入力してください。");
			num.focus();
			return ;
		}
	 	orderLimit = parseInt(document.getElementById("order_" + orderVal).firstChild.nodeValue);
	 	stockRest = orderLimit - (parseInt(numVal) - parseInt(numTmp));
	 	if(stockRest < 0){
	 		alert("カートに入ってる個数の範囲内で入力してください。");
	 		num.value = tmp.value;
	 		num.focus();
	 		return ;
	 	}
	 	document.getElementById("order_" + orderVal).innerHTML = stockRest;
		tmp.value = numVal;
		tmp_select.value = order.options[order.selectedIndex].value;
	}
	
}

// カート振り分け(商品選択)
function confirmOrder(order, num, tmp){
	// セレクトボックスの選択を変える前の値
	tmpVal = tmp.value;
	/*
	if(numTmp == ""){
		numTmp = 0;
	}
	*/
	// 入力個数
	numVal = num.value;
	// セレクトボックスの選択を変えた後の値
	orderVal = order.options[order.selectedIndex].value;
	
	// 個数の入力がなかったら何もしない
	if(numVal == "" || numVal == null){
		
		return ;
	}
	
	// selectedIndex = getSelectedIndex(order, orderVal);
	// alert(tmpVal + "\n" + numVal + "\n" + orderVal);
	// 選択している商品が変わったら
	if(orderVal != tmpVal){
		// 変更前に選択していた項目番号を取得
		tmpIndex = getSelectedIndex(order, tmpVal);
		
		// 変更後に選択した項目番号を取得
		selectedIndex = getSelectedIndex(order, orderVal);
		
		// 変更前が未選択ではなかったら
		if(tmpVal != "" && tmpVal != null){
			orderLimit = document.getElementById("order_" + order.options[tmpIndex].value).firstChild.nodeValue;
			orderLimit = Fulltohalf(orderLimit);
			orderLimit = parseInt(orderLimit);
			
			if(orderLimit == "" || orderLimit == null){
				orderLimit = 0;
			}
			// alert(orderLimit + "\n" + numVal + "\n" + orderVal);
			
			document.getElementById("order_" + order.options[tmpIndex].value).innerHTML = orderLimit + parseInt(Fulltohalf(numVal));
		}

		if(orderVal != "" && orderVal != null){
			orderLimit = document.getElementById("order_" + order.options[selectedIndex].value).firstChild.nodeValue;
			orderLimit = Fulltohalf(orderLimit);
			orderLimit = parseInt(orderLimit);
			resultVal = orderLimit - parseInt(Fulltohalf(numVal));
			// alert(orderLimit + "\n" + numVal + "\n" + resultVal + "\n" + orderVal + "\n:tmpIndex" + tmpIndex);
			// 選択を変えた後の商品がマイナスだったら選択前の商品に戻す
			if(resultVal < 0){
				if(tmpIndex > 0){
					orderLimit = document.getElementById("order_" + order.options[tmpIndex].value).firstChild.nodeValue;
					orderLimit = Fulltohalf(orderLimit);
					orderLimit = parseInt(orderLimit);
					document.getElementById("order_" + order.options[tmpIndex].value).innerHTML = orderLimit - parseInt(Fulltohalf(numVal));
				}
				alert("カートに入ってる個数の範囲内で入力してください。");
				order.options[tmpIndex].selected = true;
				return ;
			}
			document.getElementById("order_" + order.options[selectedIndex].value).innerHTML = resultVal;
		}
		
		tmp.value = orderVal;
	}
}

function getSelectedIndex(order, val){
	for(i = 0; i < order.options.length; i++){
		if(order.options[i].value == val){
			return i;
		} 
	}
	return -1;
}

function fnBackAction(url) {
	document.form1.mode.value = "back";
	document.form1.action = url;
	document.form1.submit();
}


function fnEdit(customer_id){
	document.form1.action = './deliv_multi.php';
	document.form1.mode.value = "from_confirm_back";
	// document.form1['edit_customer_id'].value = customer_id;
	document.form1.submit();
	return false;
}

function fnNonMemberEdit(){
	document.form1.action = '/shopping/index.php?deliv_mode=1';
	document.form1.mode.value = "from_confirm_back";
	// document.form1['edit_customer_id'].value = customer_id;
	document.form1.submit();
	return false;
}

function fnCart(classcategory1){
	
}

function setMultiDelivSendForm(parentFormNum, selectNum){
	formCol = "send" + parentFormNum;
	name = formCol + "_name1";
	kana = formCol + "_kana1";
	zip1 = formCol + "_zip1";
	zip2 = formCol + "_zip2";
	pref = formCol + "_pref";
	addr = formCol + "_address1";
	tel1 = formCol + "_tel1";
	// tel2 = formCol + "_tel2";
	// tel3 = formCol + "_tel3";
	fax = formCol + "_fax";
	window.opener.document.form1[name].value = document.form1.name01[selectNum].value;
	window.opener.document.form1[kana].value = document.form1.kana01[selectNum].value;
	window.opener.document.form1[zip1].value = document.form1.zip01[selectNum].value;
	window.opener.document.form1[zip2].value = document.form1.zip02[selectNum].value;
	window.opener.document.form1[pref].value = document.form1.pref[selectNum].value;
	window.opener.document.form1[addr].value = document.form1.addr01[selectNum].value;
	window.opener.document.form1[tel1].value = document.form1.tel01[selectNum].value;
	// window.opener.document.form1[tel2].value = document.form1.tel02[selectNum].value;
	// window.opener.document.form1[tel3].value = document.form1.tel03[selectNum].value;
	window.opener.document.form1[fax].value = document.form1.fax[selectNum].value;
	window.close();
}

function setHomeSendForm(parentFormNum){
	formCol = "send" + parentFormNum;
	name = formCol + "_name1";
	kana = formCol + "_kana1";
	zip1 = formCol + "_zip1";
	zip2 = formCol + "_zip2";
	pref = formCol + "_pref";
	addr = formCol + "_address1";
	tel1 = formCol + "_tel1";
	fax = formCol + "_fax";
	window.opener.document.form1[name].value = document.form1.name01.value;
	window.opener.document.form1[kana].value = document.form1.kana01.value;
	window.opener.document.form1[zip1].value = document.form1.zip01.value;
	window.opener.document.form1[zip2].value = document.form1.zip02.value;
	window.opener.document.form1[pref].value = document.form1.pref.value;
	window.opener.document.form1[addr].value = document.form1.addr01.value;
	window.opener.document.form1[tel1].value = document.form1.tel01.value;
	window.opener.document.form1[fax].value = document.form1.fax.value;
	window.close();
}

function clearForm(send_num, order_num){
	if(!window.confirm('お届け先' + send_num + 'の内容を削除してもよろしいですか？')){
		return;
	}
	formCol = "send" + send_num;
	name = formCol + "_name1";
	kana = formCol + "_kana1";
	zip1 = formCol + "_zip1";
	zip2 = formCol + "_zip2";
	pref = formCol + "_pref";
	addr = formCol + "_address1";
	tel1 = formCol + "_tel1";
	fax = formCol + "_fax";
	deliv_date = formCol + "_deliv_date";
	deliv_time_id = formCol + "_deliv_time_id";
	noshi = formCol + "_noshi";
	comment = formCol + "_comment";
	
	document.form1[name].value = "";
	document.form1[kana].value = "";
	document.form1[zip1].value = "";
	document.form1[zip2].value = "";
	document.form1[pref].value = "";
	document.form1[addr].value = "";
	document.form1[tel1].value = "";
	document.form1[fax].value = "";
	document.form1[deliv_date].selectedIndex = 0;
	document.form1[deliv_time_id].selectedIndex = 0;
	document.form1[noshi].value = "";
	document.form1[comment].value = "";
	
	// 商品選択の解除
	len = parseInt(order_num);
	for(i = 0; i < len; i++){
		order_select = formCol + "_order" + (i + 1) + "_select";
		order_num = formCol + "_order" + (i + 1) + "_num";
		order_select_tmp = formCol + "_order" + (i + 1) + "_select_tmp";
		order_num_tmp = formCol + "_order" + (i + 1) + "_num_tmp";
		inputNum = document.form1[order_num].value;
		inputNum = Fulltohalf(inputNum);
		
		if(document.form1[order_select].selectedIndex > 0 && inputNum != ""	&& !inputNum.match("[^0-9]+")){
			selectedIndex = document.form1[order_select].selectedIndex;
			restNum = document.getElementById("order_" + document.form1[order_select].options[selectedIndex].value).innerHTML;
			restNum = Fulltohalf(restNum);
			restNum = parseInt(restNum);
			inputNum = parseInt(inputNum);
			document.getElementById("order_" + document.form1[order_select].options[selectedIndex].value).innerHTML = restNum + inputNum;
		}
		document.form1[order_select].selectedIndex = 0;
		document.form1[order_num].value = "";
		document.form1[order_select_tmp].value = "";
		document.form1[order_num_tmp].value = "";
	}
	return false;
}

function Fulltohalf(data){
	var char1 = new Array("１","２","３","４","５","６","７","８","９","０");
	var char2 = new Array(1,2,3,4,5,6,7,8,9,0);
	while(data.match(/[０-９]/)){     	//入力データに全角数字がある場合
		for(count = 0; count < char1.length; count++){
				//入力データを全角数字から半角数字に置換する
			data = data.replace(char1[count], char2[count]);
		}
	}
	return data;
}

// 商品詳細ページ画像拡大スクリプト
function openImageSizedWindow(product_id, category){
    // 画像をロードしてサイズを取得
    src = "/img/log/goods/" + product_id + "/" + product_id + "_" + category + "_large.jpg";
    var i = new Image();
    i.src = src;
    // 画像のサイズに合った空ウィンドウを開く
    var pop_win = window.open(
                      "",
                      "_blank",
                      "width="+i.width+",height="+i.height+",scrollbars=no,resizable=no"
                  );
    // 空ウィンドウに画像を出力するためのHTML
    
    pop_win.resizeTo(550, 600);
    
    pop_win.window.document.open();
    pop_win.window.document.write(
     '<html>'
    +'<head><title>商品詳細</title></head>'
    +'<body>'
    +'<center><img src="'+i.src+'" alt="" /></center>'
    +'</body>'
    +'</html>'
    );
    pop_win.window.document.close();
}

// 商品詳細ページ画像拡大スクリプト
function openListImageSizedWindow(product_id, category){
    // 画像をロードしてサイズを取得
    src = "/img/log/goods/" + product_id + "/" + product_id + "_" + category + "_large.jpg";
    var i = new Image();
    i.src = src;
    // 画像のサイズに合った空ウィンドウを開く
    var pop_win = window.open(
                      "",
                      "_blank",
                      "width="+i.width+",height="+i.height+",scrollbars=no,resizable=no"
                  );
    // 空ウィンドウに画像を出力するためのHTML
    
    pop_win.resizeTo(550, 600);
    
    pop_win.window.document.open();
    pop_win.window.document.write(
     '<html>'
    +'<head><title>商品詳細</title></head>'
    +'<body>'
    +'<center><img src="'+i.src+'" alt="" /></center>'
    +'</body>'
    +'</html>'
    );
    pop_win.window.document.close();
}

// 商品詳細ページ画像拡大スクリプト
function imgWindow(img, width, height){
    // 画像をロードしてサイズを取得
    var i = new Image();
    i.src = img;
    // 画像のサイズに合った空ウィンドウを開く
    pop_win = window.open(
                      "",
                      "_blank",
                      "width="+width+",height="+height+",scrollbars=no,resizable=no"
                  );
    // 空ウィンドウに画像を出力するためのHTML
    
    // pop_win.resizeTo(width, height);
    
    pop_win.window.document.open();
    pop_win.window.document.write(
     '<html>'
    +'<head><title></title></head>'
    +'<body style="margin:0;padding:0;border:0;">'
    +'<img src="'+i.src+'" alt="" />'
    +'</body>'
    +'</html>'
    );
    pop_win.window.document.close();
}

function alertDelivFee(){
	for(i = 0; i < document.form1.payment_id.length; i++){
		if(document.form1.payment_id[i].checked && (document.form1.payment_id[i].value == "4" || document.form1.payment_id[i].value == "6" || document.form1.payment_id[i].value == "7" || document.form1.payment_id[i].value == "8")){
			okFlg = confirm("お届け先にご自宅を含まないお客様は、お届け先で代金を頂くことになります。\nよろしければ「OK」を押してお進みください。");
			if(okFlg){
				document.form1.submit();
			}else{
				return false;
			}
		}
	}
	document.form1.submit();
}

// モードとキーを指定してSUBMITを行う。
function fnKindListSubmit(mode, keyname, keyid, catid) {
	document.form1['mode'].value = mode;
	if(keyname != "" && keyid != "") {
		document.form1[keyname].value = keyid;
	}
	document.form1.classcat1.value = catid;
	document.form1.submit();
}