var lang = $('#requestHandle').attr('lang');
$.msg = {
	1 : {
		card_error       : 'Please select a e-gift card',
		quantity_error   : 'Please input quantity',
		email_error      : 'Invalid E-mail format',
		msg_error        : 'Words of message over the limit',
		date_error       : 'Invalid date format'
	},
	2 : {
		card_error       : '請選擇一款禮物卡',
		quantity_error   : '請輸入一個數量',
		email_error      : '欠缺電郵或電郵格式錯誤',
		msg_error        : '贈言字數超過限制',
		date_error       : '日期必須填寫或日期格式不對'
	},
	3 : {
		card_error       : '请选择一款礼物卡',
		quantity_error   : '请输入一个数量',
		email_error      : '欠缺电邮或电邮格式错误',
		msg_error        : '赠言字数超过限制',
		date_error       : '日期必须填写或日期格式不对'
	}
};

function addCart(img,product_id){
	var position = $(img).offset().top;
	var position_left = $(img).offset().left;
	
	var h_seid = $('#h_seid').val();
	var quantity = $('#quantity'+product_id).val();
	if(!isInteger(quantity) || quantity <= 0 || typeof(quantity) == 'undefined') {
		quantity = 1;
	}
	
	var color_id = $('#color').val();
	if(typeof(color_id) == 'undefined') {
		color_id = 0;
	}
	 
	$.ajax({
		type: 'POST',
		url: 'requestHandle.php',
		data: 'act=addToCart&product_id='+product_id+'&color_id='+color_id+'&quantity='+quantity+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
		cache: 'false',
		success: function(json){
			var json = eval('(' + json + ')');
			var msg = decodeURI(json[0].msg);
			if(json[0].type == 1) {
				$('#cart_count').html(json[0].cart_count);
				$('#addCartDiv').offset();
				$('#addCartDiv').css('top',position);
				$('#addCartDiv').css('left',position_left-142);
				$('#addCartDiv').show();
			}
			else if(json[0].type == 2) {
				$('#product_id').val(product_id);
				$('#emailNotice').html(msg);
				$('#emailDiv').offset();
				$('#emailDiv').css('top',position);
				$('#emailDiv').css('left',position_left-142);
				$('#emailDiv').show();
			}
			else if(json[0].type == 3) {
				alert(msg);
				window.location.href = 'https://www.aster.com.hk/memberLogin.php?lang='+lang;
			}
			else {
				alert(msg);
			}
		}
	});
}

function updateCart(){
	var h_seid = $('#h_seid').val();
	var quantityArr = new Array();
	$('select[name="quantity"]').each(
		function(){
			if(typeof($(this).val()) == 'undefined') {
				quantityArr[quantityArr.length] = 0;
			}
			else {
				quantityArr[quantityArr.length] = $(this).val();
			}
		}
	);
	
	var colorArr = new Array();
	$('select[name="color"]').each(
		function(){
			if(typeof($(this).val()) == 'undefined') {
				colorArr[colorArr.length] = 0;
			}
			else {
				colorArr[colorArr.length] = $(this).val();
			}
		}
	);
	
	$.ajax({
	   type: 'POST',
	   url: 'requestHandle.php',
	   data: 'act=updateCart&quantityArr='+quantityArr+'&colorArr='+colorArr+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
	   cache: 'false',
	   success: function(json){
		   var json = eval('(' + json + ')');
		   var msg = decodeURI(json[0].msg);
		   if(json[0].type == 1) {
			   window.location.reload();
		   }
		   else if(json[0].type == 3) {
				alert(msg);
				window.location.href = 'https://www.aster.com.hk/memberLogin.php?lang='+lang;
			}
		   else {
			   alert(msg);
		   }
	   }
	});
}

function deleteCart(key){
	var h_seid = $('#h_seid').val();
	$.ajax({
	   type: 'POST',
	   url: 'requestHandle.php',
	   data: 'act=deleteCart&key='+key+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
	   cache: 'false',
	   success: function(json){
		   var json = eval('(' + json + ')');
		   var msg = decodeURI(json[0].msg);
		   if(json[0].type == 1) {
			   window.location.reload();
		   }
		   else {
			   alert(msg);
		   }
	   }
	});
}

function getCode(product_id,color_id){	
	var h_seid = $('#h_seid').val();
	$.ajax({
	   type: 'POST',
	   url: 'requestHandle.php',
	   data: 'act=getCode&product_id='+product_id+'&color_id='+color_id+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
	   cache: 'false',
	   success: function(json){
		   var json = eval('(' + json + ')');
		   var msg = decodeURI(json[0].msg);
		   if(json[0].type == 1) {
			   $('#productCode').html(msg);
		   }
		   else {
			   window.location.href = 'https://www.aster.com.hk/memberLogin.php?lang='+lang;
		   }
	   }
	});
}

function hide_addCartBox(){
	$('#addCartDiv').hide();
}

function hide_emailBox(){
	$('#emailDiv').hide();
}

function emailNotice(product_id){
	var h_seid = $('#h_seid').val();
	if(typeof(product_id) == 'undefined') {
		product_id = $('#product_id').val();
	}
	
	if(product_id > 0) {
		$.ajax({
		   type: 'POST',
		   url: 'requestHandle.php',
		   data: 'act=emailNotice&product_id='+product_id+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
		   cache: 'false',
		   success: function(json){
			   var json = eval('(' + json + ')');
			   var msg = decodeURI(json[0].msg);
			   $('#emailNotice').html(msg);
			   if(json[0].type == 1) {
				   hide_emailBox();
			   }
			   else if(json[0].type == 2) {
				   window.location.href = 'https://www.aster.com.hk/memberLogin.php?lang='+lang;
			   }
			   else {
				   $('#emailNotice').html(msg);
			   }			   
		   }
		});
	}
}

function addToWishList(product_id){
	var h_seid = $('#h_seid').val();
	var productCode = $('#productCode').html();
		
	$.ajax({
	   type: 'POST',
	   url: 'requestHandle.php',
	   data: 'act=addToWishList&product_id='+product_id+'&productCode='+productCode+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
	   cache: 'false',
	   success: function(json){
		   var json = eval('(' + json + ')');
		   var msg = decodeURI(json[0].msg);
		   alert(msg);
	   }
	});
}
function deleteWishList(list_id){		
	var h_seid = $('#h_seid').val();
	
	$.ajax({
	   type: 'POST',
	   url: 'requestHandle.php',
	   data: 'act=deleteWishList&list_id='+list_id+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
	   cache: 'false',
	   success: function(json){
		   var json = eval('(' + json + ')');
		   var msg = decodeURI(json[0].msg);
		   if(json[0].type == 1) {
			   window.location.reload();
		   }
		   else {
			   alert(msg);
		   }
	   }
	});
}

function setRegion(preUrl){
	var region = $('input[name=region]:checked').val();
		
	$.ajax({
	   type: 'POST',
	   url: 'requestHandle.php',
	   data: 'act=setRegion&region='+region+'&lang'+lang+'&authnum='+Math.random(),
	   cache: 'false',
	   success: function(json){
		   var json = eval('(' + json + ')');
		   var msg = decodeURI(json[0].msg);
		   if(json[0].type == 1 && preUrl != '') {
			   window.location.href = preUrl;
		   }
		   else if(json[0].type == 1) {
			   window.history.go(-1);
		   }
		   else {
			   alert(msg);
		   }
	   }
	});
}

function getShippingMethod(destination_id){	
	var h_seid = $('#h_seid').val();
	var h_amount = $('#h_amount').val();
	$.ajax({
	   type: 'POST',
	   url: 'requestHandle.php',
	   data: 'act=getShippingMethod&destination_id='+destination_id+'&h_amount='+h_amount+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
	   cache: 'false',
	   success: function(json){
		   var json = eval('(' + json + ')');
		   var msg = decodeURI(json[0].msg);
		   var shippingFee = decodeURI(json[0].shippingFee);
		   if(json[0].type == 1) {
			   /*$('#shippingMethod_id').html(msg);
			   $('#shippingFee').html(shippingFee);
			   assignPrice(json[0].total);*/
			   window.location.reload();
		   }
		   else {
			   $('#shippingMethod_id').html('');
			   alert(msg);
		   }
	   }
	});
}

function getShippingFee(shippingMethod_id){	
	var h_seid = $('#h_seid').val();
	var h_amount = $('#h_amount').val();
	$.ajax({
		type: 'POST',
		url: 'requestHandle.php',
		data: 'act=getShippingFee&shippingMethod_id='+shippingMethod_id+'&h_amount='+h_amount+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
		cache: 'false',
		success: function(json){
			var json = eval('(' + json + ')');
			var msg = decodeURI(json[0].msg);
			if(json[0].type == 1) {
				$('#shippingFee').html(msg);
				assignPrice(json[0].total);
			}
		}
	});
}

function packagePrice(package_id){
	var h_seid = $('#h_seid').val();
	var packaging = $('input[name=packaging]:checked').val();
	if($('#packagingType'+package_id).attr('checked') && packaging == 1) {		
		$.ajax({
			type: 'POST',
			url: 'requestHandle.php',
			data: 'act=setPackage&packaging='+packaging+'&package_id='+package_id+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
			cache: 'false',
			success: function(json){
				var json = eval('(' + json + ')');
				assignPrice(json[0].total);
			}
		});
	}
}

function isPackaging(){
	var h_seid = $('#h_seid').val();
	var packaging = $('input[name=packaging]:checked').val();
	var package_id = 0;
	if(packaging == 1) {
		$('input[name=packagingType]').each(function () {
			if($(this).attr('checked')) {
				package_id = $(this).val();
				return false; //跳出each
			}
		});
	}
	
	$.ajax({
		type: 'POST',
		url: 'requestHandle.php',
		data: 'act=setPackage&packaging='+packaging+'&package_id='+package_id+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
		cache: 'false',
		success: function(json){
			var json = eval('(' + json + ')');
			assignPrice(json[0].total);
		}
	});
}

function packageInfo() {
	var h_seid = $('#h_seid').val();
	var gift_sender = $('#gift_sender').val();
	var gift_recipient = $('#gift_recipient').val();
	var gift_msg = $('#gift_msg').val();
	
	$.post('requestHandle.php', { act: 'setPackageInfo', gift_sender: gift_sender, gift_recipient: gift_recipient, gift_msg: gift_msg, h_seid: h_seid, lang: lang, authnum: Math.random() });
}

function checkEcoupon(){
	var h_seid = $('#h_seid').val();
	var h_amount = $('#h_amount').val();
	var vipDiscount = $('#vipDiscount').html();
	var eCoupon = $('#eCoupon').val();
	if(eCoupon.length == 8) {
		$.ajax({
			type: 'POST',
			url: 'requestHandle.php',
			data: 'act=checkEcoupon&confirm_use=0&eCoupon='+eCoupon+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
			cache: 'false',
			success: function(json){
				var json = eval('(' + json + ')');
				var msg = decodeURI(json[0].msg);
				if(json[0].type == 1) {
					$('#couponList').html(msg);
					assignPrice(json[0].total);
				}
				else if(json[0].type == 2) {
					if(confirm(msg)) {
						$.ajax({
							type: 'POST',
							url: 'requestHandle.php',
							data: 'act=checkEcoupon&confirm_use=1&eCoupon='+eCoupon+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
							cache: 'false',
							success: function(json){
								var json = eval('(' + json + ')');
								var msg = decodeURI(json[0].msg);
								if(json[0].type == 1) {									
									$('#couponList').html(msg);
									assignPrice(json[0].total);
								}
								else {
									alert(msg);
								}
							}
						});
					}
				}
				else {
					alert(msg);
				}
			}
		});
	}
}

function deleteEcoupon(eCoupon){
	var h_seid = $('#h_seid').val();
	var h_amount = $('#h_amount').val();
	var vipDiscount = $('#vipDiscount').html();
	if(eCoupon.length == 8) {
		$.ajax({
			type: 'POST',
			url: 'requestHandle.php',
			data: 'act=deleteEcoupon&vipDiscount='+vipDiscount+'&eCoupon='+eCoupon+'&h_amount='+h_amount+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
			cache: 'false',
			success: function(json){
				var json = eval('(' + json + ')');
				var msg = decodeURI(json[0].msg);
				if(json[0].type == 1) {
					$('#couponList').html(msg);
					assignPrice(json[0].total);
				}
				else {
					alert(msg);
				}
			}
		});
	}
}

function checkGiftCard(){
	var h_seid = $('#h_seid').val();
	var vipDiscount = $('#vipDiscount').html();
	var eGiftCard = $('#eGiftCard').val();
	if(eGiftCard.length == 14) {
		$.ajax({
			type: 'POST',
			url: 'requestHandle.php',
			data: 'act=checkGiftCard&vipDiscount='+vipDiscount+'&eGiftCard='+eGiftCard+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
			cache: 'false',
			success: function(json){
				var json = eval('(' + json + ')');
				var msg = decodeURI(json[0].msg);
				if(json[0].type == 1) {
					$('#giftCardList').html(msg);
					assignPrice(json[0].total);
				}
				else {
					alert(msg);
				}
			}
		});
	}
}

function deleteGiftCard(eGiftCard){
	var h_seid = $('#h_seid').val();
	var vipDiscount = $('#vipDiscount').html();
	if(eGiftCard.length == 14) {
		$.ajax({
			type: 'POST',
			url: 'requestHandle.php',
			data: 'act=deleteGiftCard&vipDiscount='+vipDiscount+'&eGiftCard='+eGiftCard+'&h_seid='+h_seid+'&lang'+lang+'&authnum='+Math.random(),
			cache: 'false',
			success: function(json){
				var json = eval('(' + json + ')');
				var msg = decodeURI(json[0].msg);
				if(json[0].type == 1) {
					$('#giftCardList').html(msg);					
					assignPrice(json[0].total);
				}
				else {
					alert(msg);
				}
			}
		});
	}
}

function assignPrice(totalArr) {	
	$('#total').html(totalArr['total']);
	$('#shippingFee').html(totalArr['shippingFee']);
	$('#vipDiscount').html(totalArr['vipDiscount']);
	$('#wholesaleDiscount').html(totalArr['wholesaleDiscount']);
	$('#couponDiscount').html(totalArr['couponDiscount']);
	$('#giftCardDiscount').html(totalArr['giftCardDiscount']);
	$('#totalPrice').html(totalArr['totalPrice']);
	$('#points').html(totalArr['points']);
}

function countPrice(rate) {
	var price = $('#price').val();
	price = changeTwoDecimal(price/rate);
	if ($('#listInfo').css('display') == 'none') {
		var quantity = $('#quantity').val();
	}
	else {
		var emailList = $('#emailList').val().split(',');
		var quantity = emailList.length;
	}
	
	if(!isInteger(quantity) || quantity <= 0 || typeof(quantity) == 'undefined') {
		return false;
	}
	
	var totalPrice = eval(price) * eval(quantity);
	$('#eGiftCard_totalAmount').html(changeTwoDecimal(totalPrice));
}

function sendTomore() {
	if ($('#listInfo').css('display') == 'none') {
		$('#listInfo').show();
		$('#txt_comma').show();
		$('#email').hide();
		$('#emailList').attr('disabled',false);
		$('#email').attr('disabled',true);
		$('#quantity').attr('disabled',true);
	}
	else {
		$('#listInfo').hide();
		$('#txt_comma').hide();
		$('#email').show();
		$('#emailList').attr('disabled',true);
		$('#email').attr('disabled',false);
		$('#quantity').attr('disabled',false);
	}
}

function submitGiftCard() {	
	var check = false;
	$('input[name="cardType"]').each(function(){
		if ( $(this).attr('checked') ) {
			check = true;
		}
	});
	if(check == false) {
		$('input[name="cardType"]').focus();
		alert($.msg[lang].card_error);
		return false;
	}
	
	if ($('#listInfo').css('display') == 'none') {
		var quantity = $('#quantity').val();
	}
	else {
		var emailList = $('#emailList').val().split(',');
		var quantity = emailList.length;
	}
	if(!isInteger(quantity) || quantity <= 0 || typeof(quantity) == 'undefined') {
		$('#quantity').focus();
		alert($.msg[lang].quantity_error);
		return false;
	}
	
	if ($('#listInfo').css('display') == 'none') {
		var email = $('#email').val();
		if (!isValidEmail(email)) {
			$('#email').focus();
			alert($.msg[lang].email_error);
			return false;
		}
	}
	else {
		var emailList = $('#emailList').val();
		var emailArray = emailList.split(',');
		if(emailArray.length <= 0) {
			$('#emailList').focus();
			alert($.msg[lang].email_error);
			return false;
		}
		
		for(var i = 0; i < emailArray.length; i++) {
			if (!isValidEmail(emailArray[i])) {
				$('#emailList').focus();
				alert($.msg[lang].email_error);
				return false;
			}
		}
	}
	
	var msg = $('#msg').val();
	if(msg.length > 0 && msg.length > 300) {
		$('#msg').focus();
		alert($.msg[lang].msg_error);
		return false;
	}
	
	
	var date = trim($('#datepicker').val());
	if(date == '' || !checkDate(date)) {
		$('#datepicker').focus();
		alert($.msg[lang].date_error);
		return false;
	}
	
	return true;
}

function checkDate(DateStr)
{
	DateStr = new Date(DateStr);
	
	var a = new Date();
	var b = a.valueOf() + 90 * 24 * 60 * 60 * 1000;//90天
	b = new Date(b);
	
	if(DateStr > b || DateStr < a) {
		return false;
	}
	else {
		return true;
	}
}
