
var error_msg = "找不到您要的資料 !!";
var sel_error_msg = "請選擇要購買的商品 !!";
var noitems_msg = "您的購物車目前尚無任何產品，歡迎至「 <a href='../products/products_list.php' >產品型錄</a> 」中查閱產品資料。"

function addInquiry(){
	$.ajax({
		url: "../estimate/cart_add.php",
		type: "POST",
		data: {
			pid: $("#pid").val()
		},
		dataType: "json",
		beforeSend: function () {
			showFadeUp("LoadingLink");
			$("#LoadingContainer").html(loading_bar);
			setPosition("LoadingContent");
		},
		success: function(data) {
			ShowMsg('msg',data.msg,'', 'LoadingContainer');
			setPosition("LoadingContent");
		}
	});
}

function bidPrice(){
	$.ajax({
		url: "../auction/ajax.php",
		type: "POST",
		data: {
			mode: 'bid',
			id: $("#id").val(),
			pid: $("#pid").val(),
			price: $("#price").val()
		},
		dataType: "json",
		beforeSend: function () {
			return checkBid();
		},
		success: function(data) {
			ShowMsg('msg',data.msg,'', 'LoadingContainer');
			setPosition("LoadingContent");
			BuildBidList();
		}
	});
}

function checkBid(){

	var now_price = parseInt($('#max_price').val());
	var range = parseInt($('#range').val());
	var price = parseInt($('#price').val());
	var litprice = now_price + range;
	
	if(isNaN(price) || trim($('#price').val()) == ""){
		alert('出價必需為數字!')
		$('#price').val('');
		$('#price').focus();
		return false;
	}
	
	price = Math.round(price);
	
	if(price < litprice){
		alert("出價不能小於" + litprice);
		$('#price').focus();
		return false;
	}
	
	if(confirm("確定要出價「" + price + "」元嗎？")){
		showFadeUp("LoadingLink");
		$("#LoadingContainer").html(loading_bar);
		setPosition("LoadingContent");
		return true;
	}else{
		return false;
	}
	
}

function BuildBidList(){
	$.ajax({
		url: "../auction/ajax.php",
		type: "POST",
		data: {
			mode: 'bid_list',
			id: $("#id").val()
		},
		success: function(data) {
			$("#bidContent").html(data);
		}
	});
}

function PostFaq(){
	$.ajax({
		url: "../products/ajax.php",
		type: "POST",
		data: {
			mode: 'post_faq',
			pid: $("#pid").val(),
			name: $("#name").val(),
			email: $("#email").val(),
			content: $("#content").val()
		},
		dataType: "json",
		beforeSend: function () {
			return checkFaq();
		},
		success: function(data) {
			ShowMsg('msg',data.msg,'', 'LoadingContainer');
			setPosition("LoadingContent");
			FaqInit();
			BuildFaqList();
			item = "faq";
			$(".pro_box").each(function(){
				$(this).attr("item") == item ? $("#"+$(this).attr("item")).show() :	$("#"+$(this).attr("item")).hide();
			});
			$(".pro_box").each(function(){
				var $item = $(this).attr("item");
				var $src = $(this).children('img').attr('src');
				if($src.match('_off')=='_off' && $item == item){
					var newsrc = $src.replace(/_off/,'_on');
					$(this).children('img').attr('src',newsrc);
				}else{
					if($src.match('_on')=='_on' && $item != item){
						var newsrc = $src.replace(/_on/,'_off');
						$(this).children('img').attr('src',newsrc);
					}
				}
			});
		}
	});
}

function FaqInit(){
	$('#name').val('');
	$('#email').val('');
	$('#content').val('');
	$('#authinput').val('');
}

function checkFaq(){

	if(trim($('#name').val()) == ""){
		alert('請輸入【姓名】!')
		$('#name').focus();
		return false;
	}
	
	if ($('#email').val().indexOf("@") == -1 || $('#email').val().indexOf(".") == -1){
		alert('【E-Mail】格式有誤，請更正!')
		$('#email').focus();
		return false;
	}
	
	if(trim($('#content').val()) == ""){
		alert('請輸入【內容】!')
		$('#content').focus();
		return false;
	}

	if (trim($('#authinput').val())==""){
		alert("請輸入【認證碼】!!");
		$('#authinput').focus();    
		return false;
	}

	if ($('#authinput').val() != $('#ck_code_src').val() ){
		alert("【認證碼】錯誤，請重新輸入 !!");
		$('#authinput').focus();    
		return false;;
	}

	showFadeUp("LoadingLink");
	$("#LoadingContainer").html(loading_bar);
	setPosition("LoadingContent");
	return true;

}

function BuildFaqList(){
	$.ajax({
		url: "../products/ajax.php",
		type: "POST",
		data: {
			mode: 'faq_list',
			pid: $("#pid").val()
		},
		success: function(data) {
			$("#faq").html(data);
		}
	});
}

function addShopCart(o){

	if($("input['"+t+"_cart[]']:checked").size() <= 0){
		alert("Inquiry Error...");
		return false;
	}

	var t = o.id.split("-")[0];
	var cart = 0;
	var count = 0;
	var chk = 0;

	/*$("input['"+t+"_cart[]']:checked").each(function(){
		if($("#"+t+"_psid"+this.value).val() == ''){
			chk++;
		}
	});

	if(chk > 0){
		alert("請選擇商品規格 !!");
		return false;
	}*/

	showFadeUp("LoadingLink");
	$("#LoadingContainer").html('<img src="../images/_eng/loadingAnimation.gif">');
	setPosition("LoadingContent");
	
	$("input['"+t+"_cart[]']:checked").each(function(e){
		count++;
		$.ajax({
			url: "../inquiry/cart_add2.php",
			type: "POST",
			data: {
				mode: "shop",
				pid:$("#"+t+"_psid"+this.value).val(),
				orderQty : 1
			},
			dataType: "json",
			success: function(data) {
				if(data.result == 'true') cart++;
				if(count == $("input['"+t+"_cart[]']:checked").size()){
					ShowMsg('url','',data.link_url, 'LoadingContainer');
				}
			}
		});
	});
}
