// For BB reference
// define the exchange rate, modify it when necessary
var EXCHANGE_RATE;
var FROM_DATE = "05年 05 月 25 日";
var TO_DATE = "05年 06 月 25 日";

// End for BB reference

// CONSTANTS
  var separator = ",";  // use comma as 000's separator
  var decpoint = ".";  // use period as decimal point
  var percent = "%";
  var currency = "$";  // use dollar sign for currency

function showrate(){
	document.writeln(FROM_DATE + " 至 " + TO_DATE + " 本公司之匯率為：&nbsp; " + EXCHANGE_RATE );
}

  function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
    if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

    if (number - 0 != number) return null;  // if number is NaN return null
    var useSeparator = format.indexOf(separator) != -1;  // use separators in number
    var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
    var useCurrency = format.indexOf(currency) != -1;  // use currency format
    var isNegative = (number < 0);
    number = Math.abs (number);
    if (usePercent) number *= 100;
    format = strip(format, separator + percent + currency);  // remove key characters
    number = "" + number;  // convert number input to string

     // split input value into LHS and RHS using decpoint as divider
    var dec = number.indexOf(decpoint) != -1;
    var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
    var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

     // split format string into LHS and RHS using decpoint as divider
    dec = format.indexOf(decpoint) != -1;
    var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
    var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

     // adjust decimal places by cropping or adding zeros to LHS of number
    if (srightEnd.length < nrightEnd.length) {
      var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
      nrightEnd = nrightEnd.substring(0, srightEnd.length);
      if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

 // patch provided by Patti Marcoux 1999/08/06
      while (srightEnd.length > nrightEnd.length) {
        nrightEnd = "0" + nrightEnd;
      }

      if (srightEnd.length < nrightEnd.length) {
        nrightEnd = nrightEnd.substring(1);
        nleftEnd = (nleftEnd - 0) + 1;
      }
    } else {
      for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
        if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
        else break;
      }
    }

     // adjust leading zeros
    sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
    while (sleftEnd.length > nleftEnd.length) {
      nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
    }

    if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
    var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
    output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
    if (isNegative) {
      // patch suggested by Tom Denn 25/4/2001
      output = (useCurrency) ? "(" + output + ")" : "-" + output;
    }
    return output;
  }

  function strip(input, chars) {  // strip all characters in 'chars' from input
    var output = "";  // initialise output string
    for (var i=0; i < input.length; i++)
      if (chars.indexOf(input.charAt(i)) == -1)
        output += input.charAt(i);
    return output;
  }

  function separate(input, separator) {  // format input using 'separator' to mark 000's
    input = "" + input;
    var output = "";  // initialise output string
    for (var i=0; i < input.length; i++) {
      if (i != 0 && (input.length - i) % 3 == 0) output += separator;
      output += input.charAt(i);
    }
    return output;
  }

function calculate(){
	
	// Get the input from the HTML page
	var price = document.form1.price.value * 1;
	var weight = document.form1.weight.value * 1;
	var rate = document.form1.rate.value * 1;

	// error detection for invalid input
	if (price >=1 && price <= 10000000){
	}
	else{
		alert("Price is invalid");
		return;
	}

	if (weight >=0 && weight <=20){
	}
	else{
		alert("Weight is invalid");
		return;
	}

	if (rate >= 60 && rate <=90){
		EXCHANGE_RATE = rate / 1000;
	}	
	else{
		alert("Exchange rate must between 60 and 90");
		return;
	}

	// determine the local ship cost based on the weight
	var local_ship_cost;
	if (weight <=2)
		local_ship_cost = 800;
	if (weight >2 && weight <=4)
		local_ship_cost = 950;
	if (weight >4 && weight <=6)
		local_ship_cost = 1100;
	if (weight >6 && weight <=8)
		local_ship_cost = 1200;
	if (weight >8 && weight <=11)
		local_ship_cost = 1300;
	if (weight >11 && weight <=14)
		local_ship_cost = 1400;
	if (weight >14 && weight <=17)
		local_ship_cost = 1500;
	if (weight >17)
		local_ship_cost = 1600;

	// determine the EMS cost based on the weight
	var ems_cost;
	if (weight <= 0.3)
		ems_cost = 900;
	if (weight >0.3 && weight <=0.5)
		ems_cost = 1100;
	if (weight >0.5 && weight <=0.6)
		ems_cost = 1240;
	if (weight >0.6 && weight <=0.7)
		ems_cost = 1380;
	if (weight >0.7 && weight <=0.8)
		ems_cost = 1520;
	if (weight >0.8 && weight <=0.9)
		ems_cost = 1660;
	if (weight >0.9 && weight <=1)
		ems_cost = 1800;	
	if (weight >1 && weight <=1.25)
		ems_cost = 2100;	
	if (weight >1.25 && weight <=1.5)
		ems_cost = 2400;	
	if (weight >1.5 && weight <=1.75)
		ems_cost = 2700;	
	if (weight >1.75 && weight <=2)
		ems_cost = 3000;	
	if (weight >2 && weight <=2.5)
		ems_cost = 3500;	
	if (weight >2.5 && weight <=3)
		ems_cost = 4000;	
	if (weight >3 && weight <=3.5)
		ems_cost = 4500;	
	if (weight >3.5 && weight <=4)
		ems_cost = 5000;	
	if (weight >4 && weight <=4.5)
		ems_cost = 5500;	
	if (weight >4.5 && weight <=5)
		ems_cost = 6000;	
	if (weight >5 && weight <=5.5)
		ems_cost = 6500;	
	if (weight >5.5 && weight <=6)
		ems_cost = 7000;	
	if (weight >6 && weight <=7)
		ems_cost = 7800;	
	if (weight >7 && weight <=8)
		ems_cost = 8600;	
	if (weight >8 && weight <=9)
		ems_cost = 9400;	
	if (weight >9 && weight <=10)
		ems_cost = 10200;	
	if (weight >10 && weight <=11)
		ems_cost = 11000;	
	if (weight >11 && weight <=12)
		ems_cost = 11800;	
	if (weight >12 && weight <=13)
		ems_cost = 12600;	
	if (weight >13 && weight <=14)
		ems_cost = 13400;	
	if (weight >14 && weight <=15)
		ems_cost = 14200;	
	if (weight >15 && weight <=16)
		ems_cost = 15000;	
	if (weight >16 && weight <=17)
		ems_cost = 15800;	
	if (weight >17 && weight <=18)
		ems_cost = 16600;	
	if (weight >18 && weight <=19)
		ems_cost = 17400;	
	if (weight >19 && weight <=20)
		ems_cost = 18200;	
	if (weight >20)
		ems_cost = 18200;	

	// check if the bit price is smaller than 5000 or not
	var bonus;

	if ((price + 500 + local_ship_cost + ems_cost) <= 5000)
		bonus = 50;
	else
		bonus = (price + 500 + local_ship_cost + ems_cost) * 0.01;

	// there will be 2 outputs, depending on whether the user click the 5% checkbox		
	var total;
	
	if (document.form1.tax.checked)
		total = (1.05 * price + 500 + local_ship_cost + ems_cost) * EXCHANGE_RATE + bonus;
	else
		total = (price + 500 + local_ship_cost + ems_cost) * EXCHANGE_RATE + bonus;
	
	document.form1.total.value = formatNumber(total, "##0.#");

}

function calculate_buy(){
	
	// Get the input from the HTML page
	var price = document.form1.price.value * 1;
	var ship_cost = document.form1.ship_cost.value * 1;
	var weight = document.form1.weight.value * 1;
	var rate = document.form1.rate.value * 1;	
	// error detection for invalid input
	if (price >=1 && price <= 10000000){
	}
	else{
		alert("Price is invalid");
		return;
	}

	if (ship_cost >=0 && ship_cost <=10000000){
	}
	else{
		alert("Ship Cost is invalid");
		return;
	}

	if (weight >=0 && weight <=20){
	}
	else{
		alert("Weight is invalid");
		return;
	}

	if (rate >= 60 && rate <=90){
		EXCHANGE_RATE = rate / 1000;
	}	
	else{
		alert("Exchange rate must between 60 and 90");
		return;
	}

	// determine the EMS cost based on the weight
	var ems_cost;
	if (weight <= 0.3)
		ems_cost = 900;
	if (weight >0.3 && weight <=0.5)
		ems_cost = 1100;
	if (weight >0.5 && weight <=0.6)
		ems_cost = 1240;
	if (weight >0.6 && weight <=0.7)
		ems_cost = 1380;
	if (weight >0.7 && weight <=0.8)
		ems_cost = 1520;
	if (weight >0.8 && weight <=0.9)
		ems_cost = 1660;
	if (weight >0.9 && weight <=1)
		ems_cost = 1800;	
	if (weight >1 && weight <=1.25)
		ems_cost = 2100;	
	if (weight >1.25 && weight <=1.5)
		ems_cost = 2400;	
	if (weight >1.5 && weight <=1.75)
		ems_cost = 2700;	
	if (weight >1.75 && weight <=2)
		ems_cost = 3000;	
	if (weight >2 && weight <=2.5)
		ems_cost = 3500;	
	if (weight >2.5 && weight <=3)
		ems_cost = 4000;	
	if (weight >3 && weight <=3.5)
		ems_cost = 4500;	
	if (weight >3.5 && weight <=4)
		ems_cost = 5000;	
	if (weight >4 && weight <=4.5)
		ems_cost = 5500;	
	if (weight >4.5 && weight <=5)
		ems_cost = 6000;	
	if (weight >5 && weight <=5.5)
		ems_cost = 6500;	
	if (weight >5.5 && weight <=6)
		ems_cost = 7000;	
	if (weight >6 && weight <=7)
		ems_cost = 7800;	
	if (weight >7 && weight <=8)
		ems_cost = 8600;	
	if (weight >8 && weight <=9)
		ems_cost = 9400;	
	if (weight >9 && weight <=10)
		ems_cost = 10200;	
	if (weight >10 && weight <=11)
		ems_cost = 11000;	
	if (weight >11 && weight <=12)
		ems_cost = 11800;	
	if (weight >12 && weight <=13)
		ems_cost = 12600;	
	if (weight >13 && weight <=14)
		ems_cost = 13400;	
	if (weight >14 && weight <=15)
		ems_cost = 14200;	
	if (weight >15 && weight <=16)
		ems_cost = 15000;	
	if (weight >16 && weight <=17)
		ems_cost = 15800;	
	if (weight >17 && weight <=18)
		ems_cost = 16600;	
	if (weight >18 && weight <=19)
		ems_cost = 17400;	
	if (weight >19 && weight <=20)
		ems_cost = 18200;	
	if (weight >20)
		ems_cost = 18200;	

	// check if the bit price is smaller than 5000 or not
	var bonus;

	if ((price + ship_cost + ems_cost) <= 5000)
		bonus = 50;
	else
		bonus = (price + ship_cost + ems_cost) * 0.01;

	// there will be 2 outputs, depending on whether the user click the 5% checkbox		
	var total;
	
	if (document.form1.tax.checked)
		total = (1.05 * price + ship_cost + ems_cost) * EXCHANGE_RATE + bonus;
	else
		total = (price + ship_cost + ems_cost) * EXCHANGE_RATE + bonus;
	
	document.form1.total.value = formatNumber(total, "##0.#");

}