$j(document).ready(function() {
  // 避免在非首頁的地方執行下面的程式
  if ($j('#ezbuy_calculator').length === 0) return;
  
  // 每個 type 為 text 的 input 欄位為空白時, 將該欄位填 0
  $j('input').each(function() {
    if ($j(this).attr('type') != 'text') return;
    $j(this).change(function() {
      // 文字處理
      var text = $j.trim($j(this).val());
      if (text == '') text = 0;
      $j(this).val(text);
    });
  });
  
  // 處理轉帳手續費的計算 (目前只有日本)
  calculate_ebank('jp');
  $j('input[name="jp_input_price"]').change(function() { calculate_ebank('jp'); });
  // 計算海外運費 (目前只有日本)
  calculate_trans_fee('jp');
  $j('select[name="jp_select_trans_fee"]').change(function() { calculate_trans_fee('jp'); });
  
  // 各國別事件處理
  var nations = ["jp", "us", "kr"];
  for (var i = 0; i < 3; ++i)
  {
    // 計算國際運費
    calculate_xx_shipping_fee(nations[i], true);
    $j('select[name="'+ nations[i] +'_select_weight"]').change(function() {
      var nation = $j(this).attr('name').replace('_select_weight', '');
      calculate_xx_shipping_fee(nation, true);
    });
    $j('input[name="'+ nations[i] +'_input_weight"]').change(function() {
      var nation = $j(this).attr('name').replace('_input_weight', '');
      calculate_xx_shipping_fee(nation, false);
    });
    
    // 計算台灣運費
    calculate_tw_shipping_fee(nations[i]);
    $j('#'+ nations[i] +'_input_deliver_1').click(function() {    // 宅配按鈕
      var nation = $j(this).attr('name').replace('_input_deliver', '');
      calculate_tw_shipping_fee(nation);
    });
    $j('#'+ nations[i] +'_input_deliver_0').click(function() {    // 面交按鈕
      var nation = $j(this).attr('name').replace('_input_deliver', '');
      calculate_tw_shipping_fee(nation);
    });
    
    // 設定「我要試算」按鈕 onclick 的動作
    $j('input[name="'+ nations[i] +'_submit_calculate"]').click(function() {
      var nation = $j(this).attr('name').replace('_submit_calculate', '');
      calculate_initialization(nation);
      if (calculate_field_validation(nation)) calculate_cost(nation);
    });
  }
});

// 計算總金額
function calculate_cost(nation)
{
  // 先取得匯率
  var rate = parseFloat($j('#'+ nation + '_calculator_rate').text());
  
  // 計算海外商品費用 (商品售價 + 轉帳手續費 + 海外運費) * 匯率
  var price = $j('input[name="'+ nation +'_input_price"]').val();
  var ebank_fee = $j('input[name="'+ nation +'_input_ebank"]').val();
  var trans_fee = $j('input[name="'+ nation +'_input_trans_fee"]').val();
  var calculator_ntd1 = parseFloat(price) + parseFloat(ebank_fee) + parseFloat(trans_fee);
  calculator_ntd1 = Math.round(calculator_ntd1 * rate);
  $j('#'+ nation +'_calculator_ntd1').text(calculator_ntd1);    // 顯示海外商品費用
  
  // 計算國際及內運費
  var xx_shipping_fee = $j('input[name="'+ nation +'_input_xx_shipping_fee"]').val();
  var tw_shipping_fee = $j('input[name="'+ nation +'_input_tw_shipping_fee"]').val();
  var calculator_ntd2 = parseFloat(xx_shipping_fee) + parseFloat(tw_shipping_fee);
  calculator_ntd2 = Math.round(calculator_ntd2);
  $j('#'+ nation +'_calculator_ntd2').text(calculator_ntd2);    // 顯示國際及內運費
  
  // 取得服務費
  var calculator_ntd3 = $j('#'+ nation +'_calculator_ntd3').text();
  calculator_ntd3 = parseInt(calculator_ntd3);
  
  // 通通加起來~
  var calculator_ntd4 = calculator_ntd1 + calculator_ntd2 + calculator_ntd3;
  
  $j('#'+ nation +'_calculator_ntd4').text(calculator_ntd4);    // 顯示試算結果
}

// 計算總金額之前, 先初始化結果欄位
function calculate_initialization(nation)
{
  $j('#'+ nation +'_calculator_ntd1').text(0);    // 初始化海外商品費用
  $j('#'+ nation +'_calculator_ntd2').text(0);    // 初始化國際及內運費
  $j('#'+ nation +'_calculator_ntd4').text('---');    // 初始化試算結果
}

// 檢查欄位是否正確
function calculate_field_validation(nation)
{
  // 檢查商品價格 (商品價格要大於零)
  var price = $j('input[name="'+ nation +'_input_price"]').val();
  if (isNaN(price) || parseFloat(price) <= 0)
  {
    $j('input[name="'+ nation +'_input_price"]').val(0);
    alert('請輸入正確的商品價格');
    $j('input[name="'+ nation +'_input_price"]').focus();
    return false;
  }
  
  // 檢查轉帳手續費
  var ebank_fee = $j('input[name="'+ nation +'_input_ebank"]').val();
  if (isNaN(ebank_fee))
  {
    calculate_ebank(nation);
    alert('請輸入正確的轉帳手續費');
    $j('input[name="'+ nation +'_input_ebank"]').focus();
    return false;
  }
  
  // 檢查海外運費
  var trans_fee = $j('input[name="'+ nation +'_input_trans_fee"]').val();
  if (isNaN(trans_fee))
  {
    calculate_trans_fee(nation);
    alert('請輸入正確的海外運費');
    $j('input[name="'+ nation +'_input_trans_fee"]').focus();
    return false;
  }
  
  // 檢查商品重量
  var weight = $j('input[name="'+ nation +'_input_weight"]').val();
  if (isNaN(weight))
  {
    $j('input[name="'+ nation +'_input_weight"]').val(0);
    alert('請輸入正確的商品重量');
    $j('input[name="'+ nation +'_input_weight"]').focus();
    return false;
  }
  
  // 檢查國際運費
  var xx_shipping_fee = $j('input[name="'+ nation +'_input_xx_shipping_fee"]').val();
  if (isNaN(xx_shipping_fee))
  {
    $j('input[name="'+ nation +'_input_xx_shipping_fee"]').val(0);
    alert('請輸入正確的國際運費');
    $j('input[name="'+ nation +'_input_xx_shipping_fee"]').focus();
    return false;
  }
  
  // 檢查國內運費
  var tw_shipping_fee = $j('input[name="'+ nation +'_input_tw_shipping_fee"]').val();
  if (isNaN(tw_shipping_fee))
  {
    $j('input[name="'+ nation +'_input_tw_shipping_fee"]').val(0);
    alert('請輸入正確的國內運費');
    $j('input[name="'+ nation +'_input_tw_shipping_fee"]').focus();
    return false;
  }
  
  return true;
}

// 計算轉帳手續費
function calculate_ebank(nation)
{
  // 取得商品售價
  var fee = $j('input[name="'+ nation +'_input_price"]').val();
  if (isNaN(fee))
  {
    $j('input[name="'+ nation +'_input_price"]').val(0);
    fee = 0;
    alert('請輸入正確的商品售價');
    $j('input[name="'+ nation +'_input_price"]').focus();
  }
  
  // 計算轉帳手續費
  fee = parseFloat(fee);
  if (fee <= 0) return;
  
  var ebank_fee = 0;
  switch (nation)
  {
    case 'jp':
    {
      if (fee < 9399) ebank_fee = 315;
    	else if (fee >= 9400  && fee  < 29399) ebank_fee = 420;
    	else if (fee >= 29400 && fee  < 99399) ebank_fee = 630;
    	else if (fee >= 99400 && fee < 199399) ebank_fee = 1050;
    	else ebank_fee = 2100;
      break;
    }
    case 'us':
    {
      ebank_fee = 3;
      break;
    }
    case 'kr':
    {
      ebank_fee = 1200;
      break;
    }
  }
  
  // 顯示轉帳手續費
  $j('input[name="'+ nation +'_input_ebank"]').val(ebank_fee);
}

// 計算海外運費
function calculate_trans_fee(nation)
{
  var fee = 0;
  
  switch (nation)
  {
    case 'jp':
    {
      fee = $j('select[name="'+ nation +'_select_trans_fee"]').val();
      break;
    }
    case 'us':
    {
      fee = 8;
      break;
    }
    case 'kr':
    {
      fee = 3000;
      break;
    }
  }
  
  // 顯示海外運費
  $j('input[name="'+ nation +'_input_trans_fee"]').val(fee);
}

// 計算國際運費
function calculate_xx_shipping_fee(nation, from_select)
{
  var weight = $j('input[name="'+ nation +'_input_weight"]').val();
  if (from_select == true)
  {
    weight = $j('select[name="'+ nation +'_select_weight"]').val();
    // 顯示重量
    $j('input[name="'+ nation +'_input_weight"]').val(weight);
  }
  if (isNaN(weight))
  {
    weight = 0;
    $j('input[name="'+ nation +'_input_weight"]').val(weight);
    alert('請輸入正確的重量');
    $j('input[name="'+ nation +'_input_weight"]').focus();
  }
  weight = parseInt(weight);
  
  var fee = 0;
  
  // 計算國際運費 (套用重量公式)
  switch (nation)
  {
    case 'jp':
    {
      fee = 300;
    	if (weight % 50 > 0) weight = (Math.ceil(weight / 50)) * 50;    // 重量級距
    	if (weight > 10000) fee = 250;
    	fee = Math.round(weight / 1000 * fee);
      break;
    }
    case 'us':
    {
      fee = 470;
    	if (weight % 50 > 0) weight = (Math.ceil(weight / 50)) * 50;    // 重量級距
    	if (weight > 3000  && weight <= 5000)  fee = 450;
    	else if (weight > 5000  && weight <= 10000) fee = 430;
    	else if (weight > 10000 && weight <= 20000) fee = 400;
    	else if (weight > 20000) fee = 370;
      fee = Math.round(weight / 1000 * fee);
      break;
    }
    case 'kr':
    {
      fee = 380;
    	if (weight % 50 > 0) weight = (Math.ceil(weight / 50)) * 50;    // 重量級距
    	if (weight > 10000) fee = 320;
    	fee = Math.round(weight / 1000 * fee);
      break;
    }
    case 'fr':
    {
      fee = 560;
    	if (weight % 500 > 0) weight = (Math.ceil(weight / 500)) * 500; // 重量級距
    	if (weight > 500  && weight <= 3000)        fee = 560;
    	else if (weight > 3000 && weight <= 5000)   fee = 540;
    	else if (weight > 5000 && weight <= 10000)  fee = 520;
    	else if (weight > 10000 && weight <= 20000) fee = 490;
    	else if (weight > 20000)                    fee = 450;
      fee = Math.round(weight / 1000 * fee);
      break;
    }
  }
  
  // 顯示國際運費
  $j('input[name="'+ nation +'_input_xx_shipping_fee"]').val(fee);
}

// 計算台灣運費
function calculate_tw_shipping_fee(nation)
{
  var type_1 = $j('#'+ nation +'_input_deliver_1').attr('checked');
  var type_0 = $j('#'+ nation +'_input_deliver_0').attr('checked');
  
  var fee = 110;
  if (type_1) fee = 110;
  else fee = 0;
  
  // 顯示台灣運費
  $j('input[name="'+ nation +'_input_tw_shipping_fee"]').val(fee);
}