//***************************************//
// P3110_INC
//	P3110_INC.phpのＪＳ
// create  2009/5/15
//***************************************//
var P3110_INC = {
	member : {
		popup_now:"",
		hint_flg:"",
		cart_num:Array(),
		bLoaded:false
	},
	init : function(){
		this.member.hint_flg = "";
		this.member.bLoaded = true;
	},
	// member登録
	insertMember : function(cd,cart_num) {
		this.member.cart_num[cd] = cart_num;
	},
	// 数量変更(元の数量1以上)
	cartChange : function(cd) {
	    var cartMsg = $("cart_msg_" + cd);
	    var cartNum = $("cart_numB_" + cd);
	    $("cart_numA_" + cd).value = cartNum.value;
	    if(cartNum.value==""){
	    	return false;
	    }
		if(!cartNum.value.match(/^[0-9]+$/)){
			alert("数値を入力してください。");
			cartNum.focus();
		}
		if(this.member.cart_num[cd] != cartNum.value){
			cartMsg.style.display="";
			cartMsg.className = "henkoutyui";
			cartMsg.innerHTML = "※変更ボタンを押してください";	
			//changeBoxSize();
			$("cart_buttonA_" + cd).style.display="none";
			$("cart_buttonB_" + cd).style.display="";
			$("cart_buttonC_" + cd).style.display="none";
		}
	},
	// 数量変更(元の数量0)
	cartChange2 : function(cd) { 
	    var cartMsg = $("cart_msg_" + cd);
	    var cartNum = $("cart_numA_" + cd);
	    $("cart_numB_" + cd).value = cartNum.value;
	    if(cartNum.value=="" || cartNum.value==0){
	    	return false;
	    }
		if(!cartNum.value.match(/^[0-9]+$/)){
			alert("数値を入力してください。");
			cartNum.focus();
		}
		cartMsg.style.display="";
		cartMsg.className = "henkoutyui";
		cartMsg.innerHTML = "※カートへボタンを押してください";	
		//changeBoxSize();
		$("cart_buttonA_" + cd).style.display="none";
		$("cart_buttonB_" + cd).style.display="none";
		$("cart_buttonC_" + cd).style.display="";
	},
	//カートに入れる
	insertCart : function(kikaku,cd) { 
	    var cartMsg = $("cart_msg_" + cd);
		cartMsg.style.display="";
		cartMsg.className = "henkou_ok";
		cartMsg.innerHTML = "カートへ追加しました";
		$("cart_buttonA_" + cd).style.display="none";
		$("cart_buttonB_" + cd).style.display="";
		$("cart_buttonC_" + cd).style.display="none";
		$("cart_kazuA_" + cd).style.display="none";
		$("cart_kazuB_" + cd).style.display="";
	    $("cart_numA_" + cd).value = 1;
	    $("cart_numB_" + cd).value = 1;
		$("popup").style.left = "-1000px";
		$("popup").style.top = "-1000px";
		//変更前のカート数量メンバーを更新
		this.member.cart_num[cd] = $("cart_numA_" + cd).value;
		//"/common/js/heightLine.js"を呼び出し
		//changeBoxSize();	
		//Ajaxでカートに入れる
		var parameter = "api=changeCartCnt&cnt=1&kikaku=" + kikaku + "&cd=" + cd;
		common_lib.ajaxPostData(parameter, this.completeCart_num.bindAsEventListener(this), this.failure, this.exeption,true);
	},

	//変更ボタン(元の数量1以上)
	cartEdit : function(kikaku,cd,lmt_kbn) { 
	    var cartMsg = $("cart_msg_" + cd); 
		var cartNum = $("cart_numB_" + cd);
	    $("cart_numA_" + cd).value = cartNum.value;
		if(!cartNum.value.match(/^[0-9]+$/)){
			alert("数値を入力してください。");
			cartNum.focus();
			return false;
		}
		if(cartNum.value == 0){
			if(!confirm("カートから削除してもよろしいですか？")){
				cartNum.value = this.member.cart_num[cd];
				cartMsg.style.display="none";
				return false;
			}
		}
		if(this.member.cart_num[cd] == cartNum.value){
			cartMsg.style.display="";
			cartMsg.className = "henkoutyui";
			cartMsg.innerHTML = "※先に数量を変更してください。";	
//			alert("先に数量を変更してください。");
			cartNum.focus();
			return false;
		} 
		//lmt_kbn（一人当たり注文数制限)が0（無制限）もしくは、注文数が制限を超えていなければカートに入れる
		if(eval(lmt_kbn) != 0 && eval(cartNum.value) > eval(lmt_kbn)){
			alert('注文数の制限を超えています。');
		} else {
			//カートメッセージ領域を表示
			cartMsg.style.display="";
			//変更時メッセージを表示
			cartMsg.className = "henkou_ok";
			//カートに入れた数量が0ならボタンを変更
			if(cartNum.value == 0){
				$("cart_buttonA_" + cd).style.display="";
				$("cart_buttonB_" + cd).style.display="none";
				$("cart_kazuA_" + cd).style.display="";
				$("cart_kazuB_" + cd).style.display="none";
				cartMsg.innerHTML = "カートから削除しました";
			}else{
				cartMsg.innerHTML = "数量を変更しました";
			}
			//変更前のカート数量メンバーを更新
			this.member.cart_num[cd] = cartNum.value;
			//"/common/js/heightLine.js"を呼び出し
			//changeBoxSize();
			//Ajaxでカートに入れる
			var parameter = "api=changeCartCnt&cnt=" + cartNum.value + "&kikaku=" + kikaku + "&cd=" + cd;
			common_lib.ajaxPostData(parameter, this.completeCart_num.bindAsEventListener(this), this.failure, this.exeption,true);
		}
	},
	//カートボタン(元の数量0)
	cartEdit2 : function(kikaku,cd,lmt_kbn) { 
	    var cartMsg = $("cart_msg_" + cd); 
		var cartNum = $("cart_numA_" + cd);
		if(!cartNum.value.match(/^[0-9]+$/)){
			alert("数値を入力してください。");
			cartNum.focus();
			return false;
		}
		//lmt_kbn（一人当たり注文数制限)が0（無制限）もしくは、注文数が制限を超えていなければカートに入れる
		if(eval(lmt_kbn) != 0 && eval(cartNum.value) > eval(lmt_kbn)){
			alert('注文数の制限を超えています。');
		} else {
			//カートメッセージ領域を表示
			cartMsg.style.display="";
			//ボタンを変更
			$("cart_buttonA_" + cd).style.display="none";
			$("cart_buttonB_" + cd).style.display="";
			$("cart_buttonC_" + cd).style.display="none";
			$("cart_kazuA_" + cd).style.display="none";
			$("cart_kazuB_" + cd).style.display="";
			//変更時メッセージを表示
			cartMsg.className = "henkou_ok";
			cartMsg.innerHTML = "カートに追加しました";
			//変更前のカート数量メンバーを更新
			this.member.cart_num[cd] = cartNum.value;
			//"/common/js/heightLine.js"を呼び出し
			//changeBoxSize();
			//Ajaxでカートに入れる
			var parameter = "api=changeCartCnt&cnt=" + cartNum.value + "&kikaku=" + kikaku + "&cd=" + cd;
			common_lib.ajaxPostData(parameter, this.completeCart_num.bindAsEventListener(this), this.failure, this.exeption,true);
		}
	},

	//カート数量変更
	completeCart_num : function(request){
		var res = request.responseText;
		if(res!=""){
			//カートに入れた数量を、画面に反映
//			var a = res.split(",");
//			var cd = a[0];
//			var num = a[1];	
//		    var cartNum = $("cart_num_" + cd);
//			$("cart_num_" + cd).value = num;
		}
		common_lib.ajaxPostData("api=update_order_summary",this.completeOrderSummaryUpdate.bindAsEventListener(this),this.failure, this.exeption,true);
	},
	
	//**************************//
	// ポップアップ
	//**************************//	
	
	//ポップアップ表示
	openPopup : function(id) {
		if(this.member.hint_flg != 1){
			//ポップアップフィールドの現在地を記録
			this.member.popup_now = id;
			//エレメントの座標を取得
			oPos = common_lib.getDocumentOffset($('button2_' + id))
			//ポップアップフィールドの座標を変更
			$("popup").style.left = oPos.x +30 + "px";
			$("popup").style.top = oPos.y +20 + "px";
		}
	},
	//ポップアップ非表示
	closePopup : function(){
//		if ($("hint_flg").checked == true){
//			var parameter = "api=closehint&id=" + $("hint_flg").value;
//			common_lib.ajaxPostData(parameter, this.completePopup.bindAsEventListener(this), this.failure, this.exeption,true);
//		}
			var parameter = "api=closehint&id=1";
			common_lib.ajaxPostData(parameter, this.completePopup.bindAsEventListener(this), this.failure, this.exeption,true);
			$("popup").style.left = "-1000px";
			$("popup").style.top = "-1000px";
	},
	completePopup : function(request){
		if(request.responseText=="0"){
			location.href="P4010.php";
			return;
		}
		if(request.responseText=="2"){
			location.href="P4000.php";
			return;
		}
		if(request.responseText=="1"){
			this.member.hint_flg = 1;
			return;
		}
	},
/*	
	//お気に入りボタン
	favoriteEdit : function(cd,id) { 
	    var buttonA = $("favorite_buttonA_" + cd); 
	    var buttonB = $("favorite_buttonB_" + cd); 
		if(id == 1){
			buttonA.style.display="";
			buttonB.style.display="none";
		}else{
			buttonA.style.display="none";
			buttonB.style.display="";
		}
		var parameter = "api=favorite&id=" + id + "&cd=" + cd;
		common_lib.ajaxPostData(parameter, this.completeFavorite.bindAsEventListener(this), this.failure, this.exeption,true);
	},
*/
	//お気に入りボタン
	favoriteEdit : function(scd,cmd) { 
	    var buttonA = $("favorite_buttonA_" + scd); 
	    var buttonB = $("favorite_buttonB_" + scd); 
	    var cmd;
		if(cmd == 1){
			buttonA.style.display="";
			buttonB.style.display="none";
		}else{
			buttonA.style.display="none";
			buttonB.style.display="";
		}
		var parameter = "api=favorite&cmd=" + cmd + "&scd=" + scd;
		common_lib.displayWaitOverlay("block");
		common_lib.ajaxPostData(parameter, this.completeFavorite.bindAsEventListener(this), this.failure, this.exeption,true);
		return false;
	},
	completeFavorite : function(request){
		common_lib.displayWaitOverlay("none");
		if(request.responseText=="1"){
//			location.reload();
			return;
		}
		alert("処理失敗");
		//location.href = "P4010.php";
		return;		
	},
	openLightWindow : function(url){
		myLightWindow.activateWindow({
			href : url,
			title: "",
			width:600,
			height:280
		});
	},
	closeLightWindow: function(){
		myLightWindow.deactivate();
	},

	//右ナビゲーションの概要更新 2009/6/1 by kikuchi
	completeOrderSummaryUpdate: function(request){
		if(request.responseText=="0"){
			return;
		}
		res = request.responseText;
		a = res.split("|");
		$("cart_summary_item_count").innerHTML = common_lib.numberFormat(a[0]);
		$("cart_summary_total_count").innerHTML = common_lib.numberFormat(a[1]);
		$("cart_summary_total_price").innerHTML = common_lib.numberFormat(a[2]);
		$("order_summary_item_count").innerHTML = common_lib.numberFormat(a[3]);
		$("order_summary_total_count").innerHTML = common_lib.numberFormat(a[4]);
		$("order_summary_total_price").innerHTML = common_lib.numberFormat(a[5]);		
	},
	failure : function(){},
	exeption : function(){}
}
Event.observe(document, 'dom:loaded', function() {//dom:loaded or contentloaded or load
	P3110_INC.init();
	common_lib.initWaitOverlay();
}, false);
Event.observe(window, 'resize', function() {
	if(P3110_INC.member.popup_now !="" && $("cart_buttonA_" + P3110_INC.member.popup_now).style.display==""){
		P3110_INC.openPopup(P3110_INC.member.popup_now);
	}
}, false);


