/*
* @version : 4.4
* @update : 2010-03-29
*/
function insertOverlay() {
	$("div.qtip").hide();
	if($("div#bg-overlay").length == 0) $('<div id="bg-overlay"></div>').appendTo('body');
}

function destroyOverlay() {
	//console.log( $("div#bg-overlay").length );
	$("div#bg-overlay").remove();
}

function closeLoginTop() {
	if ( $("div#navTop div.Shortcuts").css("display") == "none" || parseInt($("div#cTopLogin").css("top")) == 0) {
		$("div#navTop div.Shortcuts").fadeIn(10);
		$("div#cTopLogin").animate(
			{ top: -350 }, {
			queue: false, duration: 300,
			complete:function(callback){
				destroyOverlay();
				$(document).unbind("mousemove");
			}
		});
	}
}

function hideProfileTop() {
	if ( $("div#navTop div.Shortcuts").css("display") == "none" || parseInt($("div#cTopProfile").css("top")) == 0) {
		$("div#navTop div.Shortcuts").fadeIn(10);
		$("div#cTopProfile").animate( { top: -500 }, { queue: false, duration: 300, complete:function() { destroyOverlay(); $(document).unbind("mousemove");} } );
	}
}

function closeProfileTop() {
	/*if ( $("div#navTop div.Shortcuts").css("display") == "none" || parseInt($("div#cTopProfile").css("top")) == 0) {
		$("div#navTop div.Shortcuts").fadeIn(10);
		$("div#cTopProfile").animate( { top: -450 }, { queue: false, duration: 300, complete:function(){ destroyOverlay(); $(document).unbind("mousemove");} } );
	}*/

	if ( $("div#navTop div.Shortcuts").css("display") == "none" || parseInt($("div#cTopProfile").css("top")) == 0) {
		$("div#navTop div.Shortcuts").fadeIn(10);
		$("div#cTopProfile").animate(
			{ top: -450 }, {
			queue: false, duration: 300,
			complete: function(){
				//destroyOverlay();
				$(document).unbind("mousemove");
			}
		});
	}
}

function closeProfileView() {
	$("div#cProfileView").fadeOut("fast", function() {
		$('html, body').animate({scrollTop:0}, 'slow');
		$("div#cProfileView").remove();
		destroyOverlay();
	});
}

function closeProfileEdit() {
	$("div#cProfileEdit").fadeOut("fast", function() {
		$('html, body').animate({scrollTop:0}, 'slow');
		destroyOverlay();
		$("div#cProfileEdit").remove();
	});
}

function closeDeliveryAddress() {
	$("div#cDeliveryAddress").fadeOut("fast", function() {
		$('html, body').animate({scrollTop:0}, 'slow');
		$("div#cDeliveryAddress").remove();
		destroyOverlay();
	});
}

function closeTaxAddress() {
	$("div#cTaxAddress").fadeOut("fast", function() {
		$('html, body').animate({scrollTop:0}, 'slow');
		$("div#cTaxAddress").remove();
		destroyOverlay();
	});
}

function saveProfile() {
	$("div#cProfileEdit").fadeOut("fast", function() {
		$('html, body').animate({scrollTop:0}, 'slow');
		$("div#cProfileEdit").remove();
		destroyOverlay();
		bindDisplayMessage("Your profile has been update", "");
	});
}

function saveDeliveryAdddress() {
	$("div#cDeliveryAddress").fadeOut("fast", function() {
		$('html, body').animate({scrollTop:0}, 'slow');
		$("div#cDeliveryAddress").remove();
		destroyOverlay();
		bindDisplayMessage("Your address has been update", "",  function() { document.location.reload(); } );
	});
}

function saveTaxAdddress() {
	$("div#cTaxAddress").fadeOut("fast", function() {
		$('html, body').animate({scrollTop:0}, 'slow');
		$("div#cTaxAddress").remove();
		destroyOverlay();
		bindDisplayMessage("Your address has been update", "", function() { document.location.reload(); } );
	});
}

function bindLoginTop() {

	if ( $("div#cTopLogin").length == 0 ) {
		if ($("div#navTop div.Shortcuts a#mnLogout").length == 0) {
			$.get("api/popup.php", { action: "login_top" }, function(html){
				$("div.Wrapper").prepend( html );

				bindLoginTop();
			});
		}

	} else {
		$("div#cTopLogin div#swfLoginPromotion").flash({ src: "flash/top-login_promotion.swf", width: 472, height: 200, name: "", wmode: "opaque", menu: "false" });

		$("div#navTop div.Shortcuts a#mnLogin").mouseover(function(){
			insertOverlay();

			$("div#navTop div.Shortcuts").fadeOut(10);
			$("div#cTopLogin").animate( { top: 0 },
				{
				queue: false, duration: 300,
				complete: function() {
					$("div.qtip").hide();
					$(document).bind("mousemove", function(e) {

						if (e.pageY > parseInt($("div#cTopLogin").css("height")) ) {
							closeLoginTop();
							//$("div#navTop div.Shortcuts").fadeIn(10);
							//$("div#cTopLogin").animate( { top: -350 }, { queue: false, duration: 300, complete:function(){ destroyOverlay();$(document).unbind("mousemove");} } );
						}
					});

				}
			});



			$("div#cTopLogin a.btPanelClose").each(function() {
				$(this).click(function(){
					closeLoginTop();
					//$("div#navTop div.Shortcuts").fadeIn(10);
					//$("div#cTopLogin").animate( { top: -350 }, { queue: false, duration: 300, complete: function() { destroyOverlay();} } );
					return false;
				});

				$(this).focus( function() { $(this).blur(); });
			});

		});

		$("div#navTop div.Shortcuts a#mnLogin").click(function(){ return false;} );

		/*$("div#navTop div.Shortcuts a#mnLogout").click(function(){
			insertOverlay();
			return false;
		});*/

		if($("form#frmTopLogin").length > 0) {
			$("form#frmTopLogin").each( function(){
				$(this).submit(function(e){
					e.preventDefault();
					var validationError = false;
					$("input[type=text], input[type=password]", this).each( function() {
						if ($(this).attr("class")) {
							if (!validateField(this)) validationError = true;
						}
					});

					if ( validationError==true && $("form#frmTopLogin input#topUsername").hasClass("focus") ) {
						alert("Please enter your username.");
						$("form#frmTopLogin input#topUsername").focus();
					} else if ( validationError==true && $("form#frmTopLogin input#topPassword").hasClass("focus") ) {
						alert("Please enter your password.");
						$("form#frmTopLogin input#topPassword").focus();
					}
					if ( !validationError ) {
						$.ajax({
							url: "api/login.php",
							data: $("form#frmTopLogin").serialize(),
							type: "post",
							cache: false,
							dataType: "html",
							complete: function (result) {
								switch( result.responseText ) {
									case "1":
										/*document.location.reload();*/
										$("div#cTopLogin").animate( { top: -350 }, { queue: false, duration: 300, complete: function() { destroyOverlay(); $(document).unbind("mousemove"); bindLocation(); } } );
										break;
									case "100": alert("Please try again."); break;
									case "101":
									case "102": alert("Invalid username or password. Please try again."); break;
									case "103": alert("Your registration not complete. Please activate your account."); break;
								}
								$("form#frmTopLogin input#topUsername").focus();

							},
							error: function(result) {
								//alert(result.statusText);
							}
						});

					}
					return false;
					//return !validationError;
				});

			});

		}

	}
}

function bindEmotion() {

	$("div.Emotions a").each(function() {
		$(this).click(function(e) {
			e.preventDefault();
			var emo =0;
			if ( $(this).hasClass("btEmotion1") ) {
				emo = 1;
			} else if ( $(this).hasClass("btEmotion2") ) {
				emo = 2;
			} else if ( $(this).hasClass("btEmotion3") ) {
				emo = 3;
			} else if ( $(this).hasClass("btEmotion4") ) {
				emo = 4;
			}
			$("div.Emotions a").removeClass("btEmotion1_active").removeClass("btEmotion2_active").removeClass("btEmotion3_active").removeClass("btEmotion4_active");

			$.get("api/member.php", { action: "set_emotion", id: emo }, function(html){
				emo = parseInt(html);
				$("div.Emotions a.btEmotion"+emo).addClass("btEmotion"+emo+"_active");
			});
		});
		$(this).focus(function() { $(this).blur(); });
	});
}

function bindProfileTop() {

	if ( $("div#cTopProfile").length == 0 ) {

		$.get("api/popup.php", { action: "profile_top" }, function(html){
			$("body").prepend( html );
			bindProfileTop();
		});

	} else {

		$.getScript("js/page-member_top.js");

		//console.log('mouseover');
		$("div#navTop div.Shortcuts a#mnProfile").mouseover(function(){
			insertOverlay();

			$("div#navTop div.Shortcuts").fadeOut(10);
			$("div#cTopProfile").animate( { top: 0 },
				{
				queue: false, duration: 400,
				complete: function() {
					$("div.qtip").hide();
					$(document).bind("mousemove", function(e) {

						if (e.pageY > parseInt($("div#cTopProfile").css("height")) ) {
							hideProfileTop();
						}
					});

				}
			});
		});

	}
}

function bindProfileView() {
	if ( $("div#cProfileView").length > 0 ) $("div#cProfileView").remove();

	$.get("api/popup.php", { action: "profile_preview" }, function(html){
		$("body").prepend( html );

		//insertOverlay();
		$.getScript("js/page-member_view.js");

	});
}

function bindProfileEdit() {
	if ( $("div#cProfileEdit").length > 0 ) $("div#cProfileEdit").remove();

	$.get("api/popup.php", { action: "profile_edit" }, function(html){
		$("body").prepend( html );

		$.getScript("js/page-member_edit.js");
	});
}

function bindDeliveryAddress() {
	if ( $("div#cDeliveryAddress").length > 0 ) $("div#cDeliveryAddress").remove();

	insertOverlay();
	$.get("api/popup.php", { action: "address_delivery" }, function(html){
		$("body").prepend( html );

		$.getScript("js/page-address_delivery.js");
	});
}

function bindTaxAddress() {
	if ( $("div#cTaxAddress").length > 0 ) $("div#cTaxAddress").remove();

	$.get("api/popup.php", { action: "address_tax" }, function(html){
		$("body").prepend( html );
		insertOverlay();
		$.getScript("js/page-address_tax.js");
	});
}

function bindEventVirtualMoney() {
	if ( $("div.cProfile div.VirtualMoney").length > 0 ) {
		$("div.cProfile div.VirtualMoney a").filter(".enable").click(function(){

			bindVirtualMoney();
		});
	}/**/
}

function bindVirtualMoney() {

	if ( $("div#cVirtualMoney").length > 0 ) $("div#cVirtualMoney").remove();

	$(document).unbind("mousemove");

	$.ajax({
		url: 'api/popup.php',
		data: "action=virtual_money",
		type: 'get',
		cache: false,
		dataType: 'html',
		complete: function (result) {
			if ( result.responseText != "0" ) {
				$("body").append( result.responseText );

				$.getScript("js/page-virtual_money.js");
			}
		},
		success: function(html){
			//$("div#VirtualMoney").empty().append(html);
			//$.getScript("js/page-member_voucher.js");
		}

	});
}

function delivery_address ( callback) {
	$("div#cDeliveryAddress").modal({
		close: false,
		position: ["0%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();

		}
	});
}

function Voucher_close ( callback) {

	$("div#Choose").modal({
		close: false,
		position: ["20%",],
		persist: true,
		overlayId: 'Profile',
		containerId: 'Choose',
		onShow: function (dialog) {

			$(".qtip").hide();$("div#Choose input:checkbox").checkbox( {empty: 'images/spacer.gif'} );

				dialog.data.find('a.modalClose').click(function () {
					$("div#Choose").remove();


				$.modal.close();
			});

		}
	});
}

function displayAvatar( eleName ) {
	$.get("api/member.php", { action: "get_avatar" }, function(html) {
		//alert(html)
		//imgSrc = 'uploads/member/' + $.trim(response) ;
		$(eleName+" div.Avatar div.image img").css("background-image", "url('"+html+"')");
	});
}

function openProfileView() {
	$("div#navTop div.Shortcuts").fadeIn(10);
	$("div#cTopProfile").animate({ top: -450 },
		{ queue: false, duration: 300,
		complete:function() { bindProfileView(); $(document).unbind("mousemove");}
	});
}

function openProfileEdit() {
	$("div#cProfileView").fadeOut("fast", function(){
		$("div#cProfileView").remove()
		bindProfileEdit();
		//destroyOverlay();
	});
}

/*function bindLoginLeft() {

	if($("form#frmLeftLogin").length > 0) {
		$("form#frmLeftLogin").each( function(){
			$(this).submit(function(e){
				e.preventDefault();
				var validationError = false;
				$("input[type=text], input[type=password]", this).each( function() {
					if ($(this).attr("class")) {
						if (!validateField(this)) validationError = true;
					}
				});

				if ( validationError==true && $("form#frmLeftLogin input#topUsername").hasClass("focus") ) {
					alert("Please enter your username.");
					$("form#frmLeftLogin input#topUsername").focus();
				} else if ( validationError==true && $("form#frmLeftLogin input#topPassword").hasClass("focus") ) {
					alert("Please enter your password.");
					$("form#frmLeftLogin input#topPassword").focus();
				}
				if ( !validationError ) {
						$.ajax({
							url: "api/login.php",
							data: $("form#frmLeftLogin").serialize(),
							type: "post",
							cache: false,
							dataType: "html",
							complete: function (result) {
								//console.log(result.responseText)
								if (  result.responseText  == "1" )  {
									bindLocation();
								} else {
									switch( result.responseText ) {
										//case "1": document.location.reload(); break;
										case "100": alert("Please try again."); break;
										case "101": alert("Your registration not complete. Please activate your account."); break;
										case "102":
										case "103": alert("Invalid username or password. Please try again."); break;
									}
									$("form#frmLeftLogin input#leftUsername").focus();
								}

							},
							error: function(result) {
								//alert(result.statusText);
							}
						});

				} else {
					alert("Please enter your username and password.");
				}
				//return !validationError;
				return false;
			});

		});

	//	/*$("form#frmLeftLogin input.btLogin").click(function (e) {
	//		e.preventDefault();
	//		// Validate
	//		var validationError = false;
	//		$("input[type=text], input[type=password]", this).each( function() {
	//			console.log(  $(this).attr("id") );
	//			if ($(this).attr("class")) {
	//				if (!validateField(this)) validationError = true;
	//			}
	//		});
	//	});
	}
}*/

function bindLocation(reload) {
	if ( $("div#Pizza_modelConfirmLocation").length == 0 ) {
		$.get("api/popup.php", { action: "select_location" }, function(html){
			$("body").append( html );
			bindLocation(reload);
		});

	} else {
		//confirm_location();
		confirm_location( function() {

			//alert('form#frmConfirmLocation.serialize='+$("form#frmConfirmLocation")+'|'+$("form#frmConfirmLocation").serialize());
			$.ajax({
				url: "api/set_location.php",
				data: $("form#frmConfirmLocation").serialize(),
				type: "post",
				cache: false,
				dataType: "html",
				complete: function (result) {

					//alert('bindLocation:'+result.responseText);
					if (result.responseText=='1')
					{
						check_service_area();
					}
				},
				error: function (result) {
					document.location.reload();
				}
			});


		});
	}
}

function check_service_area(){
		$.ajax({
				url: "api/popup.php",
				data: "action=check_service_area",
				type: "get",
				cache: false,
				dataType: "html",
				complete: function (result) {
					//alert('check_service_area:'+result.responseText+':');
					if (result.responseText=='1') { // available
						if (is_reload()) {
							document.location.reload();
						} else {

							document.location.href = "promotion.php";
						}
					} else if (result.responseText=='-1') {
						bindErrorMessage("ที่อยู่จัดส่งของท่านอยู่นอกพื้นที่ให้บริการกรุณาเปลี่ยน/เพิ่มที่อยู่จัดส่ง<br>ขออภัยในความไม่สะดวก<br><br>","<b>Your address is not in Pizza Hut service area.<br> Please change/ add different address.<br>Sorry for inconvenience. </b>", function(html){
							document.location.href = "profile.php";

							/*if (is_reload()) {
								//document.location.href = "promotion.php";
								document.location.reload();
							} else {

								document.location.href = "promotion.php";
							}*/
						});
						//document.location.reload();
						//bindLocation(reload);
					}



				},
				error: function (result) {
					document.location.reload();
				}
			});
}

function bindForgotPassword() {
	if ( $("div#modelForgotPassword").length == 0 ) {
		$.get("api/popup.php", { action: "forgot_password" }, function(html){
			$("body").append( html );

			bindForgotPassword();
		});

	} else {
		closeLoginTop();

		forgot_password();
	}
}

function bindErrorMessage( title, message, callback ) {
	if ( $("div#modelMessage").length == 0 ) {
		$.get("api/popup.php", { action: "error_message" }, function(html){
			$("body").append( html );
			//alert(html)
			bindErrorMessage( title, message, callback );
		});

	} else {
		/*displayMessage( title, message, function() {
			$.get("api/logout.php", function(html){
				document.location.reload();
			});
		});*/
		displayMessage( title, message, callback );
	}
}

function bindDisplayMessage( title, message, callback ) {
	if ( $("div#modelMessage").length == 0 ) {
		$.get("api/popup.php", { action: "error_message" }, function(html){
			$("body").append( html );

			bindDisplayMessage( title, message, callback );
		});

	} else {
		displayMessage( title, message, callback );
	}
}

function bindDeliveryFree( callback ) {
	if ( $("div#modelFreeDelivery").length == 0 ) {
		$.get("api/popup.php", { action: "delivery_free" }, function(html){
			$("body").append( html );

			bindDeliveryFree( callback );
		});

	} else {
		displayDeliveryFree( callback );
	}
}

function bindLogout() {
	if ( $("div#modelConfirmLogout").length == 0 ) {

		$.get("api/popup.php", { action: "logout_confirm" }, function(html){
			$("body").append( html );
			//alert(html)
			bindLogout();
		});

	} else {
		confirm_logout( function() {
			$.get("api/logout.php", function(html){
				//document.location.reload();
				document.location.href="home.php";
			});
		});
	}
}

function bindMenuOptions( id ) {

	if ( $("div#modelMenuOption").length > 0 ) $("div#modelMenuOption").remove();

	$.ajax({
		url: "api/popup.php",
		data: "action=add_cart_option&id="+id,
		type: 'get',
		cache: false,
		dataType: "html",
		complete: function (result) {
			$("body").append( result.responseText );

			bindFormCart();

			menu_option( function() {});
		}
	});
}

function bindMenuCheckout( ) {
	if ( $("div#MenuAdditional").length > 0 ) $("div#MenuAdditional").remove();

	$.ajax({
		url: "api/popup.php",
		data: "action=add_cart_checkout",
		type: "get",
		cache: false,
		dataType: 'html',
		complete: function (result) {
			$("body").append( result.responseText );

			menu_checkout( function() {
			});
		}
	});
}

function bindFormCart() {
	$("form#frmAddCart div.swf_title").each(function() {
		var meta = $.metadata.get(this);
		$( this ).flash({
			src: "flash/text-menu_price_4.swf",
			width: 220, height: 35,
			wmode: "transparent", menu: "false",
			flashvars: { title: meta.title, price: meta.price, lang: meta.lang }
		});
	});
	$("form#frmAddCart input:radio").checkbox( {empty: 'images/spacer.gif'} );
	$("form#frmAddCart input:checkbox").checkbox( {empty: 'images/spacer.gif'} );
	//$("form#frmAddCart input:checkbox").trigger( ($(this).checked );

	$("form#frmAddCart").each( function() {
		$(this).submit(function(){
			var validationError = false;
			$("input[type=text], input[type=password]", this).each( function() {
				if ($(this).attr("class")) {
					if (!validateField(this)) validationError = true;
				}
			});

			if ( validationError==true && $("form#frmAddCart input#Username").hasClass("focus") ) {
				alert("Please enter your username.");
				$("form#frmAddCart input#Username").focus();
			} else if ( validationError==true && $("form#frmAddCart input#Password").hasClass("focus") ) {
				alert("Please enter your password.");
				$("form#frmAddCart input#Password").focus();
			}
			return !validationError;
		});

	});

	$("form#frmAddCart input#product_num").each(function() {
		$(this).keypress(function(e) {
			var KeyCode = (e.keyCode) ? e.keyCode : e.which;
			var CharCode = (e.charCode) ? e.charCode : 0;
			CharCode = ($.browser.msie ? -1 : CharCode );
			return ((KeyCode == 8) // backspace
				|| (KeyCode == 9) // tab
				|| (KeyCode == 37) // left arrow
				|| (KeyCode == 39) // right arrow
				|| ((KeyCode == 46) && (CharCode == 0)) // delete
				|| (CharCode == 0)
				|| ((KeyCode > 47) && (KeyCode < 58)) // 0 - 9
			);
		});

		$(this).keyup(function(e) {
			if ( parseInt($(this).val()) == 0 || isNaN(parseInt($(this).val())) ) {
				$(this).val( 1 );
			}
			updateProductChoice();
			updatePrice();
		});
	});

	$("form#frmAddCart input.choice").each(function() {
		$(this).keypress(function(e) {
			var KeyCode = (e.keyCode) ? e.keyCode : e.which;
			var CharCode = (e.charCode) ? e.charCode : 0;
			CharCode = ($.browser.msie ? -1 : CharCode );
			return ((KeyCode == 8) // backspace
				|| (KeyCode == 9) // tab
				|| (KeyCode == 37) // left arrow
				|| (KeyCode == 39) // right arrow
				|| ((KeyCode == 46) && (CharCode == 0)) // delete
				|| (CharCode == 0)
				|| ((KeyCode > 47) && (KeyCode < 58)) // 0 - 9
			);
		});

		$(this).keyup(function(e) {
			if ( parseInt($(this).val()) > (parseInt($("form#frmAddCart input#chicken_amt").val()) * parseInt($("form#frmAddCart input#product_num").val())) ) {
				$(this).val( parseInt($("form#frmAddCart input#chicken_amt").val()) * parseInt($("form#frmAddCart input#product_num").val()) );
			}
			updateChoice( $(this).attr("id") );
		});

		$(this).focus(function() { $(this).select(); } );
	});

	$("form#frmAddCart input.product_select").bind("check", function() {
		updateProductSelect();
	});

	$("form#frmAddCart input.tradeup").bind("check", function() {
		updatePrice();
	});
	$("form#frmAddCart input.tradeup").bind("uncheck", function() {
		updatePrice();
	});
}

function updateProductSelect() {

	var meta = $("form#frmAddCart input.product_select:checked").metadata();
	$("form#frmAddCart input#product_code").val( meta.code );
	$("form#frmAddCart input#product_price").val( meta.price );

	if ( parseInt($("form#frmAddCart input#product_num").val()) == 0 || isNaN(parseInt($("form#frmAddCart input#product_num").val())) ) {
		$("form#frmAddCart input#product_num").val(1);
	}

	var val = meta.code;

	if ( $("form#frmAddCart input#chicken_amt").length > 0 ) {
		$("form#frmAddCart input#chicken_amt").val( val.replace(/x/g,'') );

		if ( $("form#frmAddCart input.choice").length > 0 ) {
			i=0;
			$("form#frmAddCart input.choice").each(function() {
				i += 1;
				val = 0;
				if (i==1) { val = $("form#frmAddCart input#chicken_amt").val(); }
				$(this).val( val );
			});
		}

	}
	updateProductChoice();
	updateChoice('none');
	updatePrice();
}

function updateProductChoice() {
	var i=0;
	var chicken_amt = parseInt($("form#frmAddCart input#chicken_amt").val()) * parseInt($("form#frmAddCart input#product_num").val());

	$("form#frmAddCart input#product_chicken_amt").val( chicken_amt );

	$("form#frmAddCart input.choice").each(function() {
		i++;
		if (i == 1) {
			$(this).val( $("form#frmAddCart input#product_chicken_amt").val() );
		} else {
			$(this).val( 0 );
		}
	});
}

function updateChoice( curr_id ) {
	var total = parseInt($("form#frmAddCart input#chicken_amt").val()) * parseInt($("form#frmAddCart input#product_num").val());
	var curr = 0;
	if ( $("form#frmAddCart input#"+curr_id).length > 0) curr = parseInt($("form#frmAddCart input#"+curr_id).val());
	var avalible = parseInt(total - curr);
	//console.log('total: '+total)
	//console.log('curr: '+curr)
	//console.log('avalible: '+avalible)
	$("form#frmAddCart input.choice").each(function() {
		if ( $(this).attr("id") != curr_id ) {
			new_value = 0;
			curr_value = parseInt($(this).val());

			if ( curr_value > avalible ) {
				new_value = avalible;
			} else {
				new_value = avalible;
			}
			if (isNaN(new_value)) new_value = 0;

			avalible = avalible - new_value;
			//console.log('new_value: '+new_value)
			$(this).val( new_value );
		}
	});
}

function updatePrice() {
	var price_product = parseInt($("form#frmAddCart input#product_price").val()) * parseInt($("form#frmAddCart input#product_num").val());
	var price_tradeup = 0;

	$("form#frmAddCart div.sum_total label").html( price_product );
	$("form#frmAddCart div.sum_total span").empty();

	$("form#frmAddCart input.tradeup").each(function() {
		if ( $(this).attr("checked") ) {
			var meta = $.metadata.get(this);
			if ( meta.price == undefined ) meta.price = 0;
			price_tradeup += parseInt(meta.price) * parseInt($("form#frmAddCart input#product_num").val());
		}
	})
	if ( price_tradeup > 0 ) $("form#frmAddCart div.sum_total span").html( "&nbsp;+" + price_tradeup );
}

/*function swfAddCart( id ) {
	if ( $("div#navTop div.Shortcuts a#mnLogout").length == 1 ) {
		$.ajax({
			url: "api/popup.php",
			data: "action=chk_add_cart&id="+id,
			type: "get",
			cache: false,
			dataType: "html",
			complete: function (result) {
				if ( parseInt(result.responseText) == 1 ) {
					bindMenuOptions(id);
				} else {
					// action=add_cart_option&id=117
				}
			},
			error: function(result) {
				//alert(result.statusText);
			}
		});

	} else {
		//bindErrorMessage("PLEASE LOGIN FIRST ?", "");
		bindErrorMessage("กรุณาเช้าสู่ระบบก่อนสั่งซื้ออาหารค่ะ", "");
	}
}*/

// id:0, code:'P000061', type:'productcode', page:'promotion'
function swfAddCart( code, type ) {

	if ( $("a#btHomeLogout, a#Pizza_mnLogout, a#mnLogout").length > 0 ) {

		if ( $("div#modelSelectPizza").length > 0 ) {$("div#modelSelectPizza").remove();}

		if (type==undefined) type = 'productcode';
		var page = 'home';
		if (type=="combo") page = "promotion";

		$.ajax({
			url: "api/popup.php",
			data: "action=chk_add_cart&code="+code+"&type="+type,
			type: "get",
			cache: false,
			dataType: "html",
			complete: function (result) {
				//document.write ();
				//alert(result.responseText);
				if ( parseInt(result.responseText) == 1 ) {

					if (type == 'pizza') {
						bindPizzaCrustNormal(id);
					} else if (type=='combo') {
						//alert('COMBO');

						bindCombo(code, page);

					} else {
						//alert('test');
						addCartMenu( code);
					}

				} else if ( parseInt(result.responseText) == 2 ) {
					bindDisplayMessage("ขออภัยขณะนี้เป็นเวลาปิดให้บริการของร้านค่ะ", "");
					// action=add_cart_option&id=117
				} else if ( parseInt(result.responseText) == 3 ) {
					bindDisplayMessage("ขออภัยที่อยู่จัดส่งของท่านอยู่นอกพื้นที่ให้บริการ", "กรุณา เปลี่ยน/เพิ่ม ที่อยู่จัดส่งค่ะ");
					// action=add_cart_option&id=117
				}

			},
			error: function(result) {
				//alert(result.statusText);
			}
		});
	} else {
		bindLogin();
	}
}

function displayMessage ( title, message, callback) {

	$("div#modelMessage").modal({
		close: false,
		position: ["20%",],
		overlayId: 'bg-overlay',
		//containerId: 'modelMessage',
		onShow: function (dialog) {
			$(".qtip").hide();

			dialog.data.find('.title').html(title);
			dialog.data.find('.message').html(message);

			dialog.data.find(".btClose").click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
				return false;
			});

		}
	});
}

function displayDeliveryFree (callback) {

	$("div#modelFreeDelivery").modal({
		close: false,
		position: ["20%",],
		overlayId: 'bg-overlay',
		//containerId: 'modelMessage',
		onShow: function (dialog) {

			dialog.data.find(".modalClose").click(function () {
				// close the dialog
				$.modal.close();
				return false;
			});

			dialog.data.find(".btNext").click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
				return false;
			});

		}
	});
}

function confirm_logout ( callback) {
	$("div#modelConfirmLogout").modal({
		close: false,
		position: ["20%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();
			//dialog.data.find('.message').append(message);

			// if the user clicks "yes"
			dialog.data.find('a.btConfirm').click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
				return false;
			});

			dialog.data.find('a.btCancel').click(function () {
				// close the dialog
				$("div#modelConfirmLogout").remove();
				$.modal.close();
				return false;
			});
		}
	});
}

function confirm_location ( callback) {
	$("div#Pizza_modelConfirmLocation").modal({
		close: false,
		position: ["10%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();

			// if the user clicks "yes"
			dialog.data.find("input.btProcess").click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// Save Location
				//save_location();
				// close the dialog
				$.modal.close();
			});

			// close the dialog
			dialog.data.find('a.modalClose').click(function () {
				$.modal.close();
			});
		}
	});
}

function save_location(){

			//alert('form#frmConfirmLocation.serialize='+$("form#frmConfirmLocation")+'|'+$("form#frmConfirmLocation").serialize());
			$.ajax({
				url: "api/set_location.php",
				data: $("form#frmConfirmLocation").serialize(),
				type: "post",
				cache: false,
				dataType: "html",
				complete: function (result) {

					//alert('bindLocation:'+result.responseText);
					if (result.responseText=='1')
					{
						check_service_area();
					}
				},
				error: function (result) {
					document.location.reload();
				}
			});
}

function forgot_password () {
	$("div#modelForgotPassword").modal({
		close: false,
		position: ["20%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();

			$("form#frmForgotPassword").each( function() {
				// handle submissions without filling any field
				$(this).submit(function () {

					var validationError = false;

					// for each field validate value
					$("form#frmForgotPassword input[type=text]").each( function() {
						if ($(this).attr("class")) {
							if (!validateField(this)) {
								validationError = true;
							}
						}
					});
					if (validationError) {
						$("form#frmForgotPassword input[type=text]:first").focus();
					}
					return !validationError;
				});

			});

			// if the user clicks "yes"
			/*dialog.data.find("input.btRequestPassword").click(function () {
				// close the dialog
				$.modal.close();
				$("div#modelForgotPassword").remove();
			});*/
			dialog.data.find("input.btRequestPassword").click(function () {
				$("form#frmForgotPassword").submit();
			});

			dialog.data.find("input.btCancel").click(function (e) {
				e.preventDefault();
				// close the dialog
				$.modal.close();
				$("div#modelForgotPassword").remove();
			});

			dialog.data.find(".imgBtCancel").click(function () {
				// close the dialog
				$.modal.close();
				$("div#modelForgotPassword").remove();
			});

		}
	});
}

function menu_option ( callback ) {
	$("div#modelMenuOption").modal({
		close: false,
		position: ["0%",],
		persist: true,
		overlayId: 'bg-overlay',
		//containerId: 'modelMenuOption',
		onShow: function (dialog) {
			$(".qtip").hide();

			// if the user clicks "yes"
			dialog.data.find("input.btProcess").click(function () {
				// close the dialog
				$.modal.close();

				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
			});

			// close the dialog
			dialog.data.find('input.btCancel').click(function () {
				//$("div#MenuOptions").remove();
				//$("div#modelMenuOption").remove();

				$.modal.close();
			});
			dialog.data.find('a.modalClose').click(function () {
				//$("div#MenuOptions").remove();
				//$("div#modelMenuOption").remove();

				$.modal.close();
			});
		}
	});
}

function menu_checkout ( callback ) {
	$("div#modelMenuAdditional").modal({
		close: false,
		position: ["20%",],
		persist: true,
		overlayId: 'bg-overlay',
		containerId: 'modelMenuAdditional',
		onShow: function (dialog) {
			$(".qtip").hide();
			$("div#modelMenuAdditional input:checkbox").checkbox( {empty: 'images/spacer.gif'} );
			$("div#modelMenuAdditional div.item_list").jScrollHorizontalPane( {showArrows:true, arrowSize: 23, dragMinWidth:76, dragMaxWidth:76, scrollbarHeight:23, scrollbarMargin:2} );

			$("div#modelMenuAdditional div.swf_price").each(function() {
				var meta = $.metadata.get(this);

				$( this ).flash({
					src: meta.swf,
					width: 115, height: 45,
					//name: $(this).attr("id"),
					wmode: "transparent", menu: "false",
					flashvars: { title: meta.title, price: meta.price, lang: meta.lang }
				});
			});
			// if the user clicks "yes"
			dialog.data.find("input.btProcess").click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
			});

			dialog.data.find('a.modalClose').click(function () {
				$("div#modelMenuAdditional").remove();

				$.modal.close();
			});
		}
	});
}

/*function profile_edit ( callback) {
	$("div#Profile").modal({
		close: false,
		position: ["0%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();

			// if the user clicks "yes"
			dialog.data.find("input.btProcess").click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
			});

			// close the dialog
			dialog.data.find('a.modalClose').click(function () {
				$.modal.close();
			});
		}
	});
}*/

function delivery_address ( callback ) {
	$("div#cDeliveryAddress").modal({
		close: false,
		position: ["0%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();
		}
	});
}

/******** PIZZA ********/
function bindLogin() {
	if ( $("div#modelMenuLogin").length > 0 ) {$("div#modelMenuLogin").remove();}
	//alert('test 3 : div#modelMenuLogin');
	//alert('test 4 : div#modelMenuLogin');
	$.ajax({
		url: "api/popup.php",
		data: "action=login_home",
		type: 'get',
		cache: false,
		dataType: "html",
		complete: function (result) {
			//alert('test 4');
			$("body").append( result.responseText );


			/***** Login Form *****/
			//if($("form#frmTopLogin").length > 0) {
			//$("form#frmTopLogin").each( function(){
			//alert("11");
			$("form#frmTopLogin").submit(function(e){
				e.preventDefault();
				var validationError = false;
				$("input[type=text], input[type=password]", this).each( function() {
					if ($(this).attr("class")) {
						if (!validateField(this)) validationError = true;
					}
				});

				if ( validationError==true && $("div#modelMenuLogin input#topUsername").hasClass("focus") ) {
					alert("Please enter your username.");
					$("div#modelMenuLogin input#topUsername").focus();
				} else if ( validationError==true && $("div#modelMenuLogin input#topPassword").hasClass("focus") ) {
					alert("Please enter your password.");
					$("div#modelMenuLogin input#topPassword").focus();
				}
				if ( !validationError ) {
					$.ajax({
						url: "api/login.php",
						data: $("form#frmTopLogin").serialize(),
						type: "post",
						cache: false,
						dataType: "html",
						complete: function (result) {
							//alert(result.responseText );
							switch( result.responseText ) {
								case "1":
									$.modal.close();
									//destroyOverlay();
									//document.location.reload();
									bindLocation();
									//$("div#cTopLogin").animate( { top: -350 }, { queue: false, duration: 300, complete: function() { destroyOverlay(); $(document).unbind("mousemove"); bindPostLogin(); } } );
									break;
								case "100": alert("Please try again."); break;
								case "101":
								case "102": alert("Invalid username or password. Please try again."); break;
								case "103": alert("Your registration not complete. Please activate your account."); break;
							}
							$("form#frmTopLogin input#topUsername").focus();

						},
						error: function(result) {
							//alert('test 4');
							alert(result.statusText);
						}
					});

				}
				return false;
				//return !validationError;
			});

			/*** Popup Register ***/
			$('a#popupRegister').click(function(e){
				document.location.href = "register.php";
			});

			$("a#btHomeForgot").click(function (e) {
				e.preventDefault();
				$.modal.close();
				bindForgotPassword();
			});


			login_form();
			//bindFormCart();

			//menu_option( function() {});
		},
		error: function() {
			//alert('test 5');
			//alert(result.statusText);
		}
	});

}

function bindPostLogin(){
	document.location.href='pizza.php';
}

function login_form(callback){
	//alert($("div#modelMenuLogin").html());
	$("div#modelMenuLogin").modal({
		close: false,
		position: ["5%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();

			dialog.data.find('a#btnClose').click(function (e) {
				e.preventDefault();
				//$("div#MenuOptions").remove();
				//$("div#modelMenuOption").remove();

				$.modal.close();
			});

		}
	});
}

function location_form(callback){
	$("div#Pizza_modelConfirmLocation").modal({
		close: false,
		position: ["0%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();

			dialog.data.find('a.modalClose').click(function () {
				//$("div#MenuOptions").remove();
				//$("div#modelMenuOption").remove();

				$.modal.close();
			});

		}
	});
}

// Add to cart except Pizza and Drinks
function addCartMenu( code, type) {
	//alert('addCartMenu');
	if (type==undefined) { type = ''; }

	$.ajax({
		url: "api/cart.php",
		data: "action=add_cart&product_choice["+code+"]=1",
		type: "post",
		cache: false,
		dataType: "html",
		complete: function (result) {
			switch(result.responseText) {
				case "1":
					checkIsPizza(code);
					break;
				case "2":
				case "3":
					var is_pizza = false;
					if ( code != '') { is_pizza = isPizza(code); }
					var btClose = ( is_pizza ? 2 : -1 );
					//alert("btClose : " + btClose);
					showProductBuyGet( btClose );
					break;
				default:
					//bindDisplayMessage("กรุณาตรวจสอบการสั่งรายการอาหารค่ะ", "");
			}
										//alert(code+','+result.responseText);
										/*if ( parseInt(result.responseText) == 1 ) {

											//if (checkHave1150()) {
											//	alert('have 1150');
											//} else {
											//	alert('no 1150');
											//checkHave1150();
											checkIsPizza(code);
												/*if (type=='') {

													if (is_reload()) {
														document.location.href = "promotion.php";
													} else {
														document.location.reload();
													}

												} else if (type=='P') {
													//unbindPopup("div#modelSelectPizza");
													//bind11Menu50();
													//bindPizzaCrust( code );
													document.location.reload();
												}* /
											//}



										} else {
											//alert(result.responseText);
											//document.write (result.responseText);
											bindDisplayMessage("กรุณาตรวจสอบการสั่งรายการอาหารค่ะ", "");
										}*/
		}
	});


	//}
}

function addCartMenuDrink( params ) {

	$.ajax({
		url: "api/cart.php",
		data: params,
		type: "post",
		cache: false,
		dataType: "html",
		complete: function (result) {
			//alert(code+','+result.responseText);
			if ( parseInt(result.responseText) == 1 ) {
				var pathname = window.location.pathname;
				var isHome = pathname.toLowerCase().indexOf('/home.php');

				if (is_reload()) {
					//document.location.href = "promotion.php";
					document.location.reload();
				} else {
					document.location.href = "promotion.php";
					//document.location.reload();
				}
			} else {
				//alert(result.responseText);
				//document.write (result.responseText);
				bindDisplayMessage("กรุณาตรวจสอบการสั่งรายการอาหารค่ะ", "");
			}
		}
	});
	//}
}

function bindPizzaTopping ( id ) {

	if ( $("div#modelPizzaTopping").length > 0 ) $("div#modelPizzaTopping").remove();

	$("div.qtip").hide();
	insertOverlay();

	$.ajax({
		url: "api/popup.php",
		data: "action=select_pizza_topping"+( id!=undefined ? "&id="+id: ""),
		type: "get",
		cache: false,
		dataType: "html",
		complete: function (result) {
			//alert(result.responseText);
			$("body").append( result.responseText );

			$("div#modelPizzaTopping a.btClose").click(function(e){
				e.preventDefault();
				$("div#modelPizzaTopping").remove();
				destroyOverlay();
			});

			$("html, body").animate({scrollTop:0}, "slow");

			$.getScript("js/page-pizza_topping.js", function(){
				$("div#modelPizzaTopping").css("top", "50px");
				$("div#modelPizzaTopping").show();

				//$("div#modelPizzaTopping").animate( { top: 10 }, { queue: false, duration: 400 } );
			});
		}
	});
}

function bindPizzaCrust( id ) {

	if ( $("div#modelPizzaCrust").length > 0 ) $("div#modelPizzaCrust").remove();
	//alert('select_pizza_crust&id='+id);
	$.ajax({
		url: "api/popup.php",
		data: "action=select_pizza_crust"+( id!=undefined ? "&id="+id: ""),
		type: "get",
		cache: false,
		dataType: "html",
		complete: function (result) {

			//alert(result.responseText);

			$("body").prepend( result.responseText );
			insertOverlay();

			//alert('div#modelPizzaCrust');


			$("div#modelPizzaCrust a.btClose").click(function(e){
				e.preventDefault();
				$("div#modelPizzaCrust").remove();
				destroyOverlay();
			});


			$("html, body").animate({scrollTop:0}, "slow");

			$.getScript("js/page-pizza_crust.js", function(){
				$("div#modelPizzaCrust").css("top", "50px");
				$("div#modelPizzaCrust").show();

				//$("div#modelPizzaTopping").animate( { top: 10 }, { queue: false, duration: 400 } );
			});

		}
	});
}

function bindPizzaCrustNormal( id ) {
	//alert('bindPizzaCrustNormal')

	if ( $("div#modelSelectPizza").length > 0 ) $("div#modelSelectPizza").remove();

	$.ajax({
		url: "api/popup.php",
		data: "action=select_crust&id="+id,
		type: 'get',
		cache: false,
		dataType: "html",
		complete: function (result) {
			//alert(result.responseText);
			$("body").prepend( result.responseText );
			insertOverlay();

			$.getScript("js/page-select_crust.js");
			////////
				//$("div#modelSelectPizza").animate( { top: 0 },
				position = $(window).width()/2- $("div#modelSelectPizza").width()/2;
				$("div#modelSelectPizza").animate( {top: $(window).scrollTop()+20+"px", left:position},
					{

						queue: false, duration: 0,
						margin:'0px auto',
						complete: function() {
						$("div.qtip").hide();
					}

				});


				$("div#modelSelectPizza a#btnClose").each(function() {
					$(this).click(function(e){
						e.preventDefault();

						unbindPopup("div#modelSelectPizza");
						return false;
					});

					$(this).focus( function() { $(this).blur(); });
				});

			//select_pizza( function() {});
		}
	});
}

function bindCombo( code, page ) {
	//alert('bindCombo')
	if ( $("div#modelSelectPizzaCombo").length > 0 ) $("div#modelSelectPizzaCombo").remove();

	$.ajax({
		url: "api/popup_combo.php",
		data: "action=select_combo&code="+code+"&page="+page,
		type: 'get',
		cache: false,
		dataType: "html",
		complete: function (result) {
			//alert(result.responseText);
			//document.write(result.responseText);
			$("body").prepend( result.responseText );
			insertOverlay();

			$.getScript("js/page-select_combo.js");
			////////
				//$("div#modelSelectPizza").animate( { top: 0 },
				position = $(window).width()/2- $("div#modelSelectPizzaCombo").width()/2;
				$("div#modelSelectPizzaCombo").animate( {top: $(window).scrollTop()+20+"px", left:position}, {
					queue: false, duration: 0,
					margin:'0px auto',
					complete: function() {
						$("div.qtip").hide();
					}
				});


				$("div#modelSelectPizzaCombo a#btnClose").each(function() {
					$(this).click(function(e){
						e.preventDefault();

						unbindPopup("div#modelSelectPizzaCombo");
						return false;
					});

					$(this).focus( function() { $(this).blur(); });
				});

			//select_pizza( function() {});
		}
	});
}

// Check for popup 1150
function checkHave1150(){
	//alert('Check Have1150');
	var result = 0;
	$.ajax({
		url: "api/cart.php",
		data: "action=check_1150_quota",
		type: 'get',
		cache: false,
		dataType: "html",
		complete: function (result) {
			//alert(result.responseText);
			//result = result.responseText
			if ( result.responseText == "1" ) {
				if (is_reload()) {
					//document.location.href = "promotion.php";
					document.location.reload();
				} else {
					document.location.href = "promotion.php";
				}
			} else if ( result.responseText == "2" ) {
				bind11Menu50();
				//break;
			}
		}
	});
}

// Check for popup 1150
function checkIsPizza(code){
	//alert('checkIsPizza :'+code);
	var result = 0;
	$.ajax({
		url: "api/popup.php",
		data: "action=check_pizza&code="+code,
		type: "post",
		cache: false,
		dataType: "html",
		complete: function (result) {
			//alert(result.responseText);
			//result = result.responseText
			if ( result.responseText == "0" ) {
				if (is_reload()) {
					//
					document.location.reload();
				} else {
					document.location.href = "promotion.php";

				}
			} else if ( result.responseText == "1" ) {
				bind11Menu50();
				//showProductBuyGet();
			} else if ( result.responseText == "2" ) {
				//bind11Menu50();
				//break;
				showProductBuyGet();
			} else {
				//alert(result.responseText);
			}

		}
	});
}

function isPizza(code, only_pizza) {
	$.get("api/popup.php", { action: "check_pizza", code: code, chk_pizza: (only_pizza || "0") }, function(data){
		//alert("Data Loaded: " + data);
		return (data== "1" ? true : false );
	});
	/*$.ajax({
		url: "api/popup.php",
		data: "action=check_pizza&code="+code+"&chk_pizza="+(only_pizza || "0"),
		type: "get",
		cache: false,
		dataType: "html",
		complete: function (result) {
			alert(result.responseText )
			return (result.responseText == "1" ? true : false );
		}
	});*/
}

function crustShowBuyXGetY(code) {
	$.get("api/popup.php", { action: "check_pizza", code: code, chk_pizza: "1" }, function(data){
		//alert("Data Loaded: " + data);
		btClose = ( data!="1" ? -1 : 2 );
			showProductBuyGet( btClose );
		//return (data== "1" ? true : false );
	});
}

function showproduct_a( btClose ) {
	//alert('showproduct_a: '+btClose);
	showProductBuyGet(btClose);
}

function showProductBuyGet( btClose ){
	//alert('showProductBuyGet : ' +btClose);

	if ( $("div#modelSelectPizza").length > 0 ) $("div#modelSelectPizza").remove();
	if ( $("div#model11Menu50").length > 0 ) $("div#model11Menu50").remove();

	destroyOverlay();

	$.get("api/popup.php", { action: "chk_buy_get" }, function(data){
		//alert("Data Loaded: " + data);

		if ( data=="1" ) {
			$.ajax({
				url: "api/popup.php",
				data: "action=select_buy_get"+( btClose!=undefined ? "&add_method="+ ( btClose == "combo" ? "combo" : "banner") : ""),//select_product_a",//select_buy_get,
				type: "get",
				cache: false,
				dataType: "html",
				complete: function (result) {

					//alert(result.responseText);//return false;

					if(result.responseText != "1" && result.responseText.length > 0){

						$("body").prepend( result.responseText );
						insertOverlay();

						$.getScript("js/page-select_crust.js");
						//
						////////

						position = $(window).width()/2- $("div#modelSelectPizza").width()/2;
						$("div#modelSelectPizza").animate( {top: $(window).scrollTop()+20+"px", left:position}, {
							queue: false, duration: 0,
							margin:'0px auto',
							complete: function() {
								$("div.qtip").hide();
							}
						});


						$("div#modelSelectPizza a#btnClose").each(function() {
							$(this).click(function(e){
								//alert(btClose);
								e.preventDefault();

								unbindPopup("div#modelSelectPizza");
								if ( btClose == 0 ) {
									//location.reload(true);
								} else if ( btClose == 1 ) {
									//location.reload(true);
									if (is_reload()) {
										document.location.reload();
									} else {
										document.location.href = "promotion.php";
									}
								} else if ( btClose == -1 ) {
									location.reload(true);

								} else if ( btClose == "combo" ) {
									//location.reload(true);
									if (is_reload()) {
										document.location.reload();
									} else {
										document.location.href = "promotion.php";
									}
								} else {
									bind11Menu50();
								}
								/*if (is_reload()) {
									//document.location.href = "promotion.php";
									document.location.reload();
									alert(1);
								} else {
									//document.location.reload();
									document.location.href = "promotion.php";
								}*/

								//return false;
							});

							$(this).focus( function() { $(this).blur(); });
						});



						/*$("div#model11Menu50 a#btnClose").each(function() {
							$(this).click(function(e){
								e.preventDefault();

								unbindPopup("div#model11Menu50");
								return false;
							});

							$(this).focus( function() { $(this).blur(); });
						});*/

					}else{
						document.location.href = "promotion.php";
						/*if (is_reload()) {
							document.location.reload();
						} else {
							//document.location.reload();
							document.location.href = "promotion.php";
						}*/
					}
				}
			});
		} else {
			unbindPopup("div#modelSelectPizza");
			if ( btClose == 0 ) {
				//location.reload(true);
			} else if ( btClose == 1 ) {
				//location.reload(true);
				if (is_reload()) {
					document.location.reload();
				} else {
					document.location.href = "promotion.php";
				}
			} else if ( btClose == -1 ) {
				location.reload(true);
			} else {
				bind11Menu50();
			}
		}
		//return (data== "1" ? true : false );
	});

	/*$.ajax({
		url: "api/popup.php",
		data: "action=select_buy_get"+( btClose!=undefined ? "&add_method=banner" : ""),//select_product_a",//select_buy_get,
		type: "get",
		cache: false,
		dataType: "html",
		complete: function (result) {

			//alert(result.responseText);//return false;

			if(result.responseText != "1" && result.responseText.length > 0){

				$("body").prepend( result.responseText );
				insertOverlay();

				$.getScript("js/page-select_crust.js");
				//
				////////

				position = $(window).width()/2- $("div#modelSelectPizza").width()/2;
				$("div#modelSelectPizza").animate( {top: $(window).scrollTop()+20+"px", left:position}, {
					queue: false, duration: 0,
					margin:'0px auto',
					complete: function() {
						$("div.qtip").hide();
					}
				});


				$("div#modelSelectPizza a#btnClose").each(function() {
					$(this).click(function(e){
						//alert(btClose);
						e.preventDefault();

						unbindPopup("div#modelSelectPizza");
						if ( btClose == 0 ) {
							//location.reload(true);
						} else if ( btClose == 1 ) {
							//location.reload(true);
							if (is_reload()) {
								document.location.reload();
							} else {
								document.location.href = "promotion.php";
							}
						} else if ( btClose == -1 ) {
							location.reload(true);
						} else {
							bind11Menu50();
						}
						/*if (is_reload()) {
							//document.location.href = "promotion.php";
							document.location.reload();
							alert(1);
						} else {
							//document.location.reload();
							document.location.href = "promotion.php";
						}* /

						//return false;
					});

					$(this).focus( function() { $(this).blur(); });
				});



				/*$("div#model11Menu50 a#btnClose").each(function() {
					$(this).click(function(e){
						e.preventDefault();

						unbindPopup("div#model11Menu50");
						return false;
					});

					$(this).focus( function() { $(this).blur(); });
				});* /

			}else{
				document.location.href = "promotion.php";
				/*if (is_reload()) {
					document.location.reload();
				} else {
					//document.location.reload();
					document.location.href = "promotion.php";
				}* /
			}
		}
	});*/
}

function bind11Menu50() {
	if ( $("div#modelSelectPizza").length > 0 ) $("div#modelSelectPizza").remove();
	if ( $("div#model11Menu50").length > 0 ) $("div#model11Menu50").remove();

	destroyOverlay();
	$.ajax({
		url: "api/popup.php",
		data: "action=select_11m50",
		type: 'get',
		cache: false,
		dataType: "html",
		complete: function (result) {

			//alert(result.responseText);return false;

			$("body").prepend( result.responseText );
			insertOverlay();

			$.getScript("js/page-popup_1150.js");
			//
			////////

			position = $(window).width()/2- $("div#modelSelectPizza").width()/2;
			$("div#modelSelectPizza").animate( {top: $(window).scrollTop()+20+"px", left:position}, {
				queue: false, duration: 0,
				margin:'0px auto',
				complete: function() {
					$("div.qtip").hide();
				}
			});


			$("div#modelSelectPizza a#btnClose").each(function() {
				$(this).click(function(e){
					e.preventDefault();

					unbindPopup("div#modelSelectPizza");
					if (is_reload()) {
						//document.location.href = "promotion.php";
						document.location.reload();
					} else {
						//document.location.reload();
						document.location.href = "promotion.php";
					}
					//showProductBuyGet();
					//return false;
				});

				$(this).focus( function() { $(this).blur(); });
			});

			/*$("div#model11Menu50 a#btnClose").each(function() {
				$(this).click(function(e){
					e.preventDefault();

					unbindPopup("div#model11Menu50");
					return false;
				});

				$(this).focus( function() { $(this).blur(); });
			});*/

		}
	});
}

function unbindPopup(popup) {
	$('html, body').animate({queue: false, duration: 0});
	destroyOverlay();
	$(popup).remove();
	$('html, body').find("select").each(function(){
		$(this).show();
	});
}

function select_pizza(callback){

	//insertOverlay()
	$("div#modelSelectPizza").modal({

		close: false,
		position: ["0%",],
		overlayId: 'bg-overlay',

		onShow: function (dialog) {
			//$(".qtip").hide();

			dialog.data.find('a.modalClose').click(function () {
				//$("div#MenuOptions").remove();
				//$("div#modelMenuOption").remove();

				$.modal.close();
			});

		}
	});
}

//*** Combo ***//
function bindPizzaCombo( id, rule_no ) {
	//alert(rule_no);
	if ( $("div#modelPizzaCombo").length > 0 ) $("div#modelPizzaCombo").remove();
	//alert("action=select_combo&id="+id+"&rule_no="+rule_no);
	$.ajax({
		url: "api/popup.php",
		data: "action=select_combo&id="+id+"&rule_no="+rule_no,
		type: 'get',
		cache: false,
		dataType: "html",
		complete: function (result) {
			//alert(result.responseText);
			$("body").append( result.responseText );
			//$.getScript("js/page-combo.php?id="+id+"&rule_no="+rule_no);
			//$.getScript("js/page-combo.js");
			select_combo( function() {});
		},
		error: function(result) {
			//alert("ERROR: " + result.statusText);
		}
	});
}

function select_combo(callback){

	//insertOverlay()
	$("div#modelPizzaCombo").modal({

		close: false,
		position: ["0%",],
		overlayId: 'bg-overlay',

		onShow: function (dialog) {
			//$(".qtip").hide();

			dialog.data.find('a.modalClose').click(function () {
				//$("div#MenuOptions").remove();
				//$("div#modelMenuOption").remove();

				$.modal.close();
			});

		}
	});
}

/*------------------------------------Tradeup----------------*/

//*** Tradeup  ***//
function bindTradeup() {

	if ( $("div#pizza_trade_container").length > 0 ) $("div#pizza_trade_container").remove();
	$.ajax({
		url: "api/popup.php",
		data: "action=check_tradeup",
		type: 'get',
		cache: false,
		dataType: "html",
		complete: function (result) {
			$("body").append( result.responseText );
			insertOverlay();

			$.getScript("js/jquery.min.js");
			$.getScript("js/initial.js");

			$.getScript("js/page-tradeup.js");

			position = $(window).width()/2- $("div#modelSelectPizza").width()/2;
				$("div#modelSelectPizza").animate( {top: $(window).scrollTop()+20+"px", left:position},
					{

						queue: false, duration: 0,
						margin:'0px auto',
						complete: function() {
						$("div.qtip").hide();
					}


				});

				$("div#modelSelectPizza a#btnClose").each(function() {
					$(this).click(function(e){
						e.preventDefault();

						unbindPopup("div#modelSelectPizza");
						bindPayment();
						return false;
					});

					$(this).focus( function() { $(this).blur(); });
				});





			//select_Tradeup( function() {});
		},
		error: function(result) {
			//alert("ERROR: " + result.statusText);
		}
	});
}

function bindPayment(){
	document.location.href='ordering_step1.php?step=1';
}

function is_reload(){
	var pathname = window.location.pathname;
	var arrPage = ["pizza.php", "pasta.php", "baked_rice.php", "side_order.php", "dessert_drink.php", "best_sellers.php", "1150.php"];

	for(var i = 0, l = arrPage.length; i < l; i++) {
		//alert(pathname.toLowerCase()+' : ' +arrPage[i]+' = '+pathname.toLowerCase().indexOf('/'+arrPage[i]+'') );
		if (pathname.toLowerCase().indexOf('/'+arrPage[i]+'') >= 0) {

			return true;
		}
	}

	return false;
}

function bindLoginkfc() {
	if ($("div.flash_title").length > 0 ) {
		$("div.flash_title").each(function() {
			wmode = ( $(this).metadata().flashvars.wmode != undefined ? $(this).metadata().flashvars.wmode :  "opaque" ); // opaque, transparent;
			$(this).flash({
					src: $(this).metadata().flash,
					width: $(this).metadata().width,
					height: $(this).metadata().height,
					wmode: wmode, menu: "false",
					flashvars: { id: $(this).attr("id"), title_th: $(this).metadata().flashvars.title_th, title_en: $(this).metadata().flashvars.title_en }
			});
		});
	}
	if ( $("div#Pizza_modelConfirmLogin").length == 0 ) {
		$.get("api/popup.php", { action: "login_kfc" }, function(html){
			$("body").append( html );
			bindLoginkfc();
		});

	} else {
		confirm_login();

	}
}

function bindErrorMessage_KFC(id) {
	$("div#Pizza_modelConfirmLogin").remove();
	if ( $("div#Pizza_modelConfirmLogin").length == 0 ) {
		$.ajax({
			url: "api/popup.php",
			data: "action=mail_kfc_show1&id="+id,
			type: 'get',
			cache: false,
			dataType: "html",
			complete: function (result) {
				//alert(result.responseText);
				$("body").append( result.responseText );
				$("div#Pizza_modelConfirmLogin").modal({
					close: false,
					position: ["10%",],
					overlayId: 'bg-overlay',
					onShow: function (dialog) {
						// close the dialog
						dialog.data.find('a.modalClose').click(function (e) {
							e.preventDefault();
							$.modal.close();
						});

						/*dialog.data.find("input.btBack").click(function (e) {
							e.preventDefault();
							$.modal.close();
						});
						dialog.data.find('input.btLoginm').click(function (e) {
							e.preventDefault();
							$.modal.close();
						});
						dialog.data.find('input.btForget').click(function (e) {
							e.preventDefault();
							bindForgotPassword();
							$.modal.close();
						});*/
					}
				});
			}
		});
	}
}

function bindMessageOrder() {
	$("div#Pizza_modelConfirmLogin").remove();

	if ( $("div#Pizza_modelConfirmLogin").length == 0 ) {

		$.get("api/popup.php", { action: "order_complete" }, function(html){
			$("body").append( html );
			$("div#Pizza_modelConfirmLogin").modal({
				close: false,
				position: ["10%",],
				overlayId: 'bg-overlay',
				onShow: function (dialog) {
					// close the dialog
					dialog.data.find('a.modalClose').click(function (e) {
						e.preventDefault();
						$.modal.close();
					});
				}
			});
		});
	}
}

function bindMassageAddress() {

	if ( $("div#Pizza_modelConfirmLogin").length == 0 ) {

		$.get("api/popup.php", { action: "address_complete" }, function(html){
			$("body").append( html );
			$("div#Pizza_modelConfirmLogin").modal({
				close: false,
				position: ["10%",],
				overlayId: 'bg-overlay',
				onShow: function (dialog) {
					// close the dialog
					dialog.data.find('a.modalClose').click(function (e) {
						e.preventDefault();
						$.modal.close();
					});
					dialog.data.find('input.btLogin').click(function (e) {
						e.preventDefault();
						$.modal.close();
					});
				}
			});
		});
	}
}

function confirm_login ( callback ) {
	$("div#Pizza_modelConfirmLogin").modal({
		close: false,
		position: ["10%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();

			// if the user clicks "yes"
			dialog.data.find("input.btProcess").click(function (e) {
				e.preventDefault();
				// call the callback
				/*if ($.isFunction(callback)) {
					callback.apply();
				}*/

				// close the dialog
				bindLocation();
				$.modal.close();

			});

			// close the dialog
			dialog.data.find('a.modalClose').click(function (e) {
				e.preventDefault();
				$.modal.close();
			});
		}
	});
}

function confirm_massage_email ( callback ) {
	$("div#Pizza_modelConfirmLogin").modal({
		close: false,
		position: ["10%",],
		overlayId: 'bg-overlay',
		onShow: function (dialog) {
			$(".qtip").hide();

			// if the user clicks "yes"
			/*dialog.data.find("input.btProcess").click(function () {
				// call the callback
				/*if ($.isFunction(callback)) {
					callback.apply();
				}
			});*/

			// close the dialog
			dialog.data.find('a.modalClose').click(function (e) {
				e.preventDefault();
				$.modal.close();
			});
		}
	});
}

/*** Pizza ***/
$(document).ready(function () {

		/*$("a.btOrder").bind("click", function (e) {
			e.preventDefault();
			var meta = $.metadata.get(this);

			if (meta.id != undefined ) {
				$.ajax({
					url: "api/popup.php",
					data: "action=chk_add_cart&id="+meta.id,
					type: "get",
					cache: false,
					dataType: "html",
					complete: function (result) {


						//alert(result.responseText);
						if ( parseInt(result.responseText) == 1 ) {
							bindMenuOptions(meta.id);
						} else if ( parseInt(result.responseText) == 2 ) {
							//alert(2);
							bindDisplayMessage("ขออภัยขณะนี้เป็นเวลาปิดให้บริการของร้านค่ะ", "");
							// action=add_cart_option&id=117
						}

					},
					error: function(result) {
						//alert(result.statusText);
					}
				});
			}

		});*/

		/***** Topping ******/
		$("a.btTopping, a.btAddCart, a.btOrderNow, a.btOrderMenu, a.btOrderMenuDrink, a#pizza_highlight").bind("click", function (e) {

			//alert($("a#Pizza_mnLogout").length);
			if ( $("a#btHomeLogout, a#Pizza_mnLogout, a#mnLogout").length >= 1 ) {
				if ( $("div#modelSelectPizza").length > 0 ) {$("div#modelSelectPizza").remove();}
				e.preventDefault();

				var meta = $.metadata.get(this);

				//alert('code='+meta.code);
				//alert(meta.type);
				if (meta.type != undefined){ type = meta.type; } else { type = ''; }
				if (meta.code != undefined){ code = meta.code; } else { code = ''; }
				//if (meta.option_code != undefined){ option_code = meta.option_code; } else { option_code = ''; }
				id = meta.id;
				//alert(option_code);
				//if (meta.class_no != undefined){ class_no = meta.class_no; } else { class_no = ''; }

				//if (class_no == '001') {
				//	type = 'pizza';
				//}
				//if (id != undefined ) {
					$.ajax({
						url: "api/popup.php",
						data: "action=chk_add_cart&id="+id+"&code="+code+"&type="+type,
						type: "get",
						cache: false,
						dataType: "html",
						complete: function (result) {
							//document.write ();
							//alert(result.responseText);
							if ( parseInt(result.responseText) == 1 ) {
								//alert(type);
								if (type == 'pizza') {
									bindPizzaCrustNormal(id);
								} else if (type=='combo') {
									//alert('COMBO');
									if (meta.page != undefined){ page = meta.page; } else { page = ''; }
									//alert(page);
									bindCombo(code, page);
								} else if (type=='drink') {

									formname = meta.formname;
									//alert('DRINK : '+ $("form#"+formname).serialize());
									addCartMenuDrink($("form#"+formname).serialize());
								} else {
									addCartMenu( code);
								}

							} else if ( parseInt(result.responseText) == 2 ) {
								bindDisplayMessage("ขออภัยขณะนี้เป็นเวลาปิดให้บริการของร้านค่ะ", "");
								// action=add_cart_option&id=117
							} else if ( parseInt(result.responseText) == 3 ) {
								bindDisplayMessage("ขออภัยที่อยู่จัดส่งของท่านอยู่นอกพื้นที่ให้บริการ", "กรุณา เปลี่ยน/เพิ่ม ที่อยู่จัดส่งค่ะ");
								// action=add_cart_option&id=117
							}

						},
						error: function(result) {
							//alert(result.statusText);
						}
					});
				//}

			} else {
				bindLogin();
			}

		});


		/***** Combo ******/
		$("a.btCombo").bind("click", function (e) {
			//alert($("a#btHomeLogout").length);
			if ( $("a#btHomeLogout").length == 1 ) {
				if ( $("div#modelSelectPizza").length > 0 ) {$("div#modelSelectPizza").remove();}
				e.preventDefault();
				var meta = $.metadata.get(this);
				//alert(meta.id);
				if (meta.id != undefined ) {
					$.ajax({
						url: "api/popup.php",
						data: "action=chk_add_cart&id="+meta.id+"&rule_no="+meta.rule_no+"&type="+meta.type,
						type: "get",
						cache: false,
						dataType: "html",
						complete: function (result) {
							//alert(result.responseText);
							if ( parseInt(result.responseText) == 1 ) {
								bindPizzaCombo(meta.id, meta.rule_no);
							} else if ( parseInt(result.responseText) == 2 ) {
								bindDisplayMessage("ขออภัยขณะนี้เป็นเวลาปิดให้บริการของร้านค่ะ", "");
								// action=add_cart_option&id=117
							}

						},
						error: function(result) {
							//alert(result.statusText);
						}
					});
				}

			} else {
				bindLogin();
			}

		});

		/******** Home Login ********/
		$("a#btHomeLogin, a#Pizza_mnLogin, a#mnLogin").click(function (e) {
			bindLogin();
			e.preventDefault();
		});

		/**** Home Logout ***/
		$("a#btHomeLogout, a#Pizza_mnLogout, a#mnLogout").click(function (e) {
			e.preventDefault();
			bindLogout();
		});

		$("a#btPanelLogout").click(function (e) {
			e.preventDefault();
			bindLogout();
		});

		/*** Forgot Password ***/
		$("a#btForgot").click(function (e) {
			e.preventDefault();
			bindForgotPassword();
		});

		/*$("a.btHomeLogin").bind("click", function (e) {

			e.preventDefault();
			//var meta = $.metadata.get(this);

			//if (meta.id != undefined ) {
				$.ajax({
					url: "api/popup.php",
					//data: "action=chk_add_cart&id="+meta.id,
					data: "action=login_home",
					type: "get",
					cache: false,
					dataType: "html",
					complete: function (result) {

						if ( parseInt(result.responseText) == 1 ) {
							bindMenuOptions(0);
						} else {
							// action=add_cart_option&id=117
						}
					},
					error: function(result) {
						//alert(result.statusText);
					}
				});
			//}

		});*/

});
/*************/

/*** KFC ***/
/*$(document).ready(function () {

	if ( $("div#navTop div.Shortcuts a#mnLogout").length == 1 ) {
		$("a.btLogout").click(function (e) {
			e.preventDefault();
			bindLogout();
		});

		$("a.btOrder").bind("click", function (e) {
			e.preventDefault();
			var meta = $.metadata.get(this);
			if (meta.id != undefined ) {
				$.ajax({
					url: "api/popup.php",
					data: "action=chk_add_cart&id="+meta.id,
					type: "get",
					cache: false,
					dataType: "html",
					complete: function (result) {
						if ( parseInt(result.responseText) == 1 ) {
							bindMenuOptions(meta.id);
						} else {
							// action=add_cart_option&id=117
						}
					},
					error: function(result) {
						//alert(result.statusText);
					}
				});
			}

		});

		bindProfileTop();


		$("a.btAddDeliveryAddress").bind("click", function (e) {
			//alert('Click');
			bindDeliveryAddress();
			e.preventDefault();
		});

		$("a.btEditTaxAddress").bind("click", function (e) {
			//alert('Click');
			bindTaxAddress();
			e.preventDefault();
		});



	} else {

		// Top Login Panel
		bindLoginTop();

		// Left Login Panel
		bindLoginLeft();


		$("a.btForgot").click(function (e) {
			bindForgotPassword();
			e.preventDefault();
		});

		$("a.btOrder").click( function (e) {
			//alert('sfsdf');
			bindErrorMessage("กรุณาเช้าสู่ระบบก่อนสั่งซื้ออาหารค่ะ", "");
			e.preventDefault();
		});
	}

});*/