$(function(){
setSelected();
countdownTask();
countOrderTotal();
setInterval(countdownTask, 1000);
});
function changeImg(filePath) {
$("#heroImg").attr("src", filePath);
}
function prodDetail(filename) {
window.open(encodeURI(filename));
}
var numOperation = {
// a:输入框对象;b:prodId
enter: function(a, b) {
var val = $(a).val().replace(/[^\d\.]/g,'');
if ($.trim(val) == "") {
val = 0;
}
var max = $("[id=max_" + b + "]").val();
if (val - max > 0) {
alert("不能超過限購個數!");
val = max;
}
$(a).val(val);
},
// a:prodId
minus: function(a) {
var id = "num_" + a;
var val = $("[id=" + id + "]").val();
if (isNaN(val)) {
val = 0;
} else {
if (val > 0) {
val--;
}
}
$("[id=" + id + "]").val(val);
},
// a:prodId
plus: function(a) {
var id = "num_" + a;
var val = $("[id=" + id + "]").val();
if (isNaN(val)) {
val = 1;
} else {
var max = $("[id=max_" + a + "]").val();
if (val - max < 0) {
val++;
}
}
$("[id=" + id + "]").val(val);
}
};
function setSelected(){
var selected = $("#selected").val().split(',');
for (var item in selected) {
var info = selected[item].split('-');
if (info.length > 0) {
if (info[2] > 0) {
if (info[1] == "hgc") {
$("#price_" + info[0] + "_hgc").attr("checked", "checked");
}
$("#num_" + info[0]).val(info[2]);
}
}
}
}
function countOrderTotal() {
$("#divSelectedProd").html("");
// 以{$:100,¥:200}的格式存储不同货币的总计
var total = "";
$("[id^=num_]").each(function(){
if ($(this).val() == 0) return true;
var prodId = this.id.replace(/num_/g, '');
var ccy = $("#ccy_" + prodId).val();
var unitPriceType = $("[name=price_" + prodId + "]:checked").val();
var unitPrice = $("#price_" + unitPriceType + "_" + prodId).val();
var unitTotal = unitPrice * 1000000 * $(this).val() / 1000000;
unitTotal = unitTotal.toFixed(1);
writeToForm(prodId, $(this).val(), unitPriceType);
if (total != "") {
var isCcy = false;
var ccyList = total.split(',');
for (var i in ccyList) {
if (ccyList[i].indexOf(ccy) == 0) {
var existVal = ccyList[i].split(':')[1];
var newVal = (parseFloat(existVal) * 1000000
+ parseFloat(unitTotal) * 1000000)
/ 1000000;
newVal = newVal.toFixed(1);
total = total.replace(ccyList[i], ccy + ":" + newVal);
isCcy = true;
break;
}
}
if (!isCcy) {
total += "," + ccy + ":" + unitTotal;
}
} else {
total = ccy + ":" + unitTotal;
}
});
if (total == "") {
total = "0.0";
} else {
// 把{$:100,¥:200}转成页面显示格式
total = total.replace(':', ' ').replace(',', " | ");
}
$("[class=orderTotal]").html(total);
}
function countdownTask() {
$("[id^=remain_]").each(function(){
var prodId = this.id.replace(/remain_/g, '');
if (this.value == -1) {
// 剩余秒数为0
buyForbidden(prodId, "已截止");
} else if (this.value == -2) {
buyForbidden(prodId, "即將開始");
} else {
// 剩余秒数转为页面显示格式
setCountdown(prodId, this.value);
}
if (this.value >= 0) {
this.value--;
}
});
}
function setCountdown(prodId, remain) {
if (remain < 0) remain = 0;
var hours = parseInt(remain / 3600);
var minutes = parseInt(remain % 3600 / 60);
var seconds = remain % 60;
$("#h_" + prodId).html(pad(hours, 2));
$("#m_" + prodId).html(pad(minutes, 2));
$("#s_" + prodId).html(pad(seconds, 2));
}
function buyForbidden(prodId, msg) {
if ($("#buy_" + prodId).css("text-align") != "right") {
$("#buy_" + prodId).html("" + msg + "");
$("#buy_" + prodId).css("text-align", "right");
}
}
// 补零
function pad(num, n) {
var len = num.toString().length;
while(len < n) {
num = "0" + num;
len++;
}
return num;
}
/** 记录选购商品及数量 */
function writeToForm(prodId, num, priceType) {
var content = $("#divSelectedProd").html();
if (num != "" && num != 0) {
content += '';
content += '';
content += '';
$("#divSelectedProd").html(content);
}
}
function validate() {
if ($("#divSelectedProd").html() == "") {
alert("還沒選擇商品呢!");
return false;
}
}