﻿// JavaScript Document



//var transferfee=20;

//var currency=1.18;

var transferfee = 500;

var hkpostrate = 1200;

var hkpostrate2 = 20;//per kg

var hkpostrate_abs2 = 18;

var hkpostrate_abs3 = 600;

var hkpostrate_abs = 650;

var hkpostrate4 = 600;

var hkpostrate_abs4 = 500;

var hkpostrate5 = 1200;

var hkpostrate_abs5 = 650;

var convfact=5000;

var goodshkd = [];

var r = 1;

var rr = 1;

var txt = "不是有效的數值";

var hkpostrate = 15;



function getHKPostFee2(weight)

{

	var hkpostfee2;

	

	if (weight < 0.5)

		hkpostfee2 = hkpostrate_abs2;

	else

		hkpostfee2 = (weight - 0.5) * hkpostrate2 + hkpostrate_abs2;

		

	return hkpostfee2;

}



function getHKPostFee(weight)

{

	var hkpostfee;

	hkpostfee = parseFloat(weight) * hkpostrate;

	return hkpostfee;

}



function getCurrency ()

{

	document.write(currency);

}

/*

function getTransferfee ()

{

	document.write(transferfee);

}

*/



function calculate ()

{

	var price = document.myform.price.value;

	var postfee = document.myform.postfee.value;

	//var weight = document.myform.weight.value;



	var serviceCharge = 50;

	var total = 0;

	var deposit = 0;



	// invalid input

	if (price == "" || isNaN(price) || price < 0)

	{

		alert("請輸入預計金額");

		return;

	}

	

	// tw post fee

	if (postfee == "" ||isNaN(postfee ) || postfee <= 0 ) 

	{

		postfee = 0;	

		 alert("請Email聯絡本公司: bidinone@gmail.com");

		return;

	}

	

	

	





	document.getElementById("displayservicecharge").innerHTML = (parseFloat(serviceCharge));

	document.getElementById("displayservicecharge2").innerHTML = (parseFloat(serviceCharge));

	document.getElementById("displayprice").innerHTML = (parseFloat(price));

	document.getElementById("displayprice2").innerHTML = (parseFloat(price));

	document.getElementById("displaypostfee").innerHTML = (parseFloat(postfee));



	//document.getElementById("displaytransferfee").innerHTML = (parseFloat(transferfee));

	

	document.getElementById("displaycurrency").innerHTML = (parseFloat(currency));



	total=(parseFloat(price)+parseFloat(postfee))*parseFloat(currency)+parseFloat(serviceCharge);

	total=Math.round(total);

	document.getElementById("displaytotal").innerHTML = (parseFloat(total));

	

	deposit=Math.round(parseFloat(price)*(parseFloat(currency)));

	document.getElementById("displaydeposit").innerHTML = (parseFloat(deposit));

}



function clearValues()

{

	document.myform.price.value="";

	document.myform.postfee.value="";

	//document.myform.weight.value="";

	document.getElementById("displayserviceCharge").innerHTML = "-.--";

	document.getElementById("displayserviceCharge2").innerHTML = "";

	document.getElementById("displayprice").innerHTML = "";

	document.getElementById("displayprice2").innerHTML = "";

	document.getElementById("displaypostfee").innerHTML = "";

	

	//document.getElementById("displaytransferfee").innerHTML = "";

	document.getElementById("displaycurrency").innerHTML = "";

	document.getElementById("displaytotal").innerHTML = "";

	document.getElementById("displaydeposit").innerHTML = "";

	

	}

	

	function calgoods(currency, tax, postfee, weight, lenght, width, height, weight2)

	{

		var tab = TabbedPanels1.getCurrentTabIndex();

		if (tab == "1")

		{

			var goodstotalhkd = 0;

			var goodstotaljpy = 0;

			var totalqty = 0;

			price = document.form.elements["price[]"];

			for (i=0;i<=rr - 1;i++)

			{

				if (rr == 1)

				{

					goodshkd[i] = Math.round(document.form.elements["price[]"].value * document.form.elements["qty[]"].value * currency*Math.pow(10,1))/Math.pow(10,1);

					goodstotalhkd = goodstotalhkd + goodshkd[i];

					goodstotaljpy = goodstotaljpy + (document.form.elements["price[]"].value * document.form.elements["qty[]"].value);

					totalqty = parseFloat(document.form.elements["qty[]"].value);

					document.getElementById("displaygoodshkd" + i).innerHTML = goodshkd[i];

				}

				else if (rr > 1)

				{

					goodshkd[i] = Math.round(document.form.elements["price[]"][i].value * document.form.elements["qty[]"][i].value * currency*Math.pow(10,1))/Math.pow(10,1);

					goodstotalhkd = goodstotalhkd + goodshkd[i];

					goodstotaljpy = goodstotaljpy + (document.form.elements["price[]"][i].value * document.form.elements["qty[]"][i].value);

					totalqty = parseFloat(totalqty) + parseFloat(document.form.elements["qty[]"][i].value);

					document.getElementById("displaygoodshkd" + i).innerHTML = goodshkd[i];

				}

				//var price = document.form.elements["price[" + i + "]"].value;

				//var qty = document.form.elements["qty[" + i + "]"].value;

				//a = document.form.elements["price[]"];

				//alert(a.value);

				//document.getElementById("displaygoodshkd[" + i + "]").innerHTML = goodshkd[i];

			}

			//goodshkd = Math.round(price * qty * currency*Math.pow(10,1))/Math.pow(10,1);

			taxjpd = Math.round(goodstotaljpy * tax*Math.pow(10,1))/Math.pow(10,1);

			taxhkd = Math.round(goodstotaljpy * tax * currency*Math.pow(10,1))/Math.pow(10,1);

			postfeehkd = Math.round(postfee * currency * Math.pow(10,1))/Math.pow(10,1);

			//hkpostfeejpy = getHKPostFee(weight);

			//hkpostfeehkd = Math.round(getHKPostFee(weight) * currency * Math.pow(10,1))/Math.pow(10,1);

			if (length != 0 || width !=0 || height != 0)

		{

			vol = lenght * width * height;

  			volw =  vol / 5000;

  			b = volw - parseInt( volw );

  			if( b <= .5 && 0 < b && convfact == 5000 )

  			{

    			c=.5;

  			}

  			else // if( (.5 < b) && (b < 1 ) )

  			{

    			c=1;

  			}

  			if( b==0 ) c=0;

  			fweight = volw - b + c;

  			if( vol != 0 && ( isNaN( fweight ) || fweight < 1 ) ) fweight = 1;

			document.getElementById("dw").value = parseFloat(fweight);

			hkpostfeejpy = getHKPostFee2(parseFloat(fweight));

		}

		else

		{

			hkpostfeejpy = getHKPostFee2(parseFloat(weight2));

		}

		hkpostfeehkd = Math.round(hkpostfeejpy * currency * Math.pow(10,1))/Math.pow(10,1);

			//transferfeehkd = Math.round(transferfee * currency*Math.pow(10,1))/Math.pow(10,1);

			servicefee = Math.ceil(goodstotaljpy/50) * 50;

			if (servicefee < 100)

			{

				servicefee = 100;

			}

			if (servicefee > 1000)

			{

				servicefee = 1000;

			}

			totalhkd = Math.round((goodstotalhkd + taxhkd + postfeehkd + hkpostfeehkd + servicefee)*Math.pow(10,1))/Math.pow(10,1);

			deposite = Math.round((goodstotalhkd + postfeehkd + hkpostfeehkd)*Math.pow(10,1))/Math.pow(10,1);

			a = Math.round((goodstotalhkd + postfeehkd + servicefee + taxhkd)*Math.pow(10,1))/Math.pow(10,1);

			b = Math.round((goodstotalhkd + postfeehkd + taxhkd)*Math.pow(10,1))/Math.pow(10,1);

			//document.getElementById("displaygoodshkd").innerHTML = goodshkd;

			document.getElementById("displaytaxjpy").innerHTML = taxjpd

			document.getElementById("displaytaxhkd").innerHTML = taxhkd;

			document.getElementById("displaypostfeehkd").innerHTML = postfeehkd;

			document.getElementById("displayhkpostfeejpy").innerHTML = hkpostfeejpy;

			document.getElementById("hkpostfeejpy").value = hkpostfeejpy;

			document.getElementById("displayhkpostfeehkd").innerHTML = hkpostfeehkd;

			document.getElementById("estimatedtotal").value = totalhkd;

			document.getElementById("displaytotalhkd").innerHTML = totalhkd;

			document.getElementById("displayservicefee").innerHTML = servicefee;

			document.getElementById("servicefee").value = servicefee;

			document.getElementById("displaydeposite").innerHTML = deposite;

			document.getElementById("estimateddeposite").value = deposite;

			document.getElementById("a").value = a;

			document.getElementById("b").value = b;

		}

		if (tab == "0")

		{

			var goodstotalhkd = 0;

			var goodstotaljpy = 0;

			var totalqty = 0;

			price = document.form.elements["price2[]"];

			lastround = 0;

			for (i=0;i<=r - 1;i++)

			{

				if (r == 1)

				{

					goodshkd[i] = Math.round(document.form.elements["price2[]"].value * document.form.elements["qty2[]"].value * currency*Math.pow(10,1))/Math.pow(10,1);

					goodstotalhkd = goodstotalhkd + goodshkd[i];

					goodstotaljpy = goodstotaljpy + (document.form.elements["price2[]"].value * document.form.elements["qty2[]"].value);

					totalqty = parseFloat(document.form.elements["qty2[]"].value);

					document.getElementById("displaygoodshkd2" + i).innerHTML = goodshkd[i];

					thisround = document.form.elements["price2[]"].value;

				}

				else if (r > 1)

				{

					goodshkd[i] = Math.round(document.form.elements["price2[]"][i].value * document.form.elements["qty2[]"][i].value * currency*Math.pow(10,1))/Math.pow(10,1);

					goodstotalhkd = goodstotalhkd + goodshkd[i];

					goodstotaljpy = goodstotaljpy + (document.form.elements["price2[]"][i].value * document.form.elements["qty2[]"][i].value);

					totalqty = parseFloat(totalqty) + parseFloat(document.form.elements["qty2[]"][i].value);

					document.getElementById("displaygoodshkd2" + i).innerHTML = goodshkd[i];

					thisround = document.form.elements["price2[]"][i].value;

				}

				//var price = document.form.elements["price[" + i + "]"].value;

				//var qty = document.form.elements["qty[" + i + "]"].value;

				//a = document.form.elements["price[]"];

				//alert(a.value);

				//document.getElementById("displaygoodshkd[" + i + "]").innerHTML = goodshkd[i];

				if (thisround > lastround)

				{

					lastround = thisround;

				}

			}

			if (lastround > 100)

			{

				first = 100;

			}

			else

			{

				first = 50;

			}

			//goodshkd = Math.round(price * qty * currency*Math.pow(10,1))/Math.pow(10,1);

			//taxjpd = Math.round(goodstotaljpy * tax*Math.pow(10,1))/Math.pow(10,1);

			//taxhkd = Math.round(goodstotaljpy * tax * currency*Math.pow(10,1))/Math.pow(10,1);

			postfeehkd = Math.round(postfee * currency * Math.pow(10,1))/Math.pow(10,1);

			//hkpostfeejpy = getHKPostFee(weight);

			//hkpostfeehkd = Math.round(getHKPostFee(weight) * currency * Math.pow(10,1))/Math.pow(10,1);

			//transferfeehkd = Math.round(transferfee * currency*Math.pow(10,1))/Math.pow(10,1);

			servicefee = (totalqty - 1) * 20 + first;

			if (servicefee > 1000)

			{

				servicefee = 1000;

			}

			totalhkd = Math.round((goodstotalhkd + postfeehkd + servicefee)*Math.pow(10,1))/Math.pow(10,1);

			deposite = Math.round((goodstotalhkd + postfeehkd)*Math.pow(10,1))/Math.pow(10,1);

			a = Math.round((goodstotalhkd + postfeehkd + servicefee)*Math.pow(10,1))/Math.pow(10,1);

			b = Math.round((goodstotalhkd + postfeehkd)*Math.pow(10,1))/Math.pow(10,1);

			//document.getElementById("displaygoodshkd2").innerHTML = goodshkd;

			//document.getElementById("displaytaxjpy").innerHTML = taxjpd

			//document.getElementById("displaytaxhkd").innerHTML = taxhkd;

			//document.getElementById("displayhkpostfeehkd2").innerHTML = postfeehkd;

			//document.getElementById("displayhkpostfeejpy2").innerHTML = hkpostfeejpy;

			//document.getElementById("hkpostfeejpy2").value = hkpostfeejpy;

			document.getElementById("displayhkpostfeehkd2").innerHTML = postfeehkd;

			document.getElementById("estimatedtotal2").value = totalhkd;

			document.getElementById("displaytotalhkd2").innerHTML = totalhkd;

			document.getElementById("displayservicefee2").innerHTML = servicefee;

			document.getElementById("servicefee2").value = servicefee;

			document.getElementById("displaydeposite2").innerHTML = deposite;

			document.getElementById("estimateddeposite2").value = deposite;

			document.getElementById("a2").value = a;

			document.getElementById("b2").value = b;

		}

	}



function add(currency)

{  

	var tab = TabbedPanels1.getCurrentTabIndex();

	if (tab == "1")

	{

		var tbl = document.getElementById('goods2');

		var lastRow = tbl.rows.length;

		// if there's no header row in the table, then iteration = lastRow + 1

		var iteration = lastRow - 13;

		if (iteration <= 10)

		{

			var row = tbl.insertRow(lastRow - 11);

			rr = rr + 1;

			// left cell

			var cell1 = row.insertCell(0);

			var textNode = document.createTextNode("貨品(" + iteration + ")");

			cell1.setAttribute("class", "sub_text");

			cell1.appendChild(textNode);

  

			// right cell

			var cell2 = row.insertCell(1);

			cell2.innerHTML = "<input type='text' name='price[]' id='price[]' size='5' onchange='javascript:if (isFloatNumber(this.value)){calgoods(this.form.currency.value,this.form.tax.value,this.form.postfee.value, null, this.form.lenght.value, this.form.width.value, this.form.height.value, this.form.dw.value);}else{alert(txt);this.value=null}' onKeyUp='javascript:if (isFloatNumber(this.value)){calgoods(this.form.currency.value,this.form.tax.value,this.form.postfee.value, null, this.form.lenght.value, this.form.width.value, this.form.height.value, this.form.dw.value);}else{alert(txt);this.value=null}'>";

			//var el2 = document.createElement("input");

			//el2.type = "text";

			//el2.name = "price[]";

			//el2.id = "price[]";

			//el2.size = 6;

			//el2.onchange = "javascript:calgoods(this.form.currency.value,this.form.tax.value,this.form.postfee.value, this.form.weight.value, this.form.lenght.value, this.form.width.value, this.form.height.value, this.form.dw.value);";

			var textNode2 = document.createTextNode(" x");

  

			cell2.setAttribute("class", "sub_text");

			cell2.setAttribute("align", "right");

			//cell2.appendChild(el2);

			cell2.appendChild(textNode2);

  

			// select cell

			var cell3 = row.insertCell(2);

			cell3.innerHTML = "<input type='text' name='qty[]' id='qty[]' size='3' value='1' onchange='javascript:if (isNumber(this.value) && this.value.length > 0){calgoods(this.form.currency.value,this.form.tax.value,this.form.postfee.value, null, this.form.lenght.value, this.form.width.value, this.form.height.value, this.form.dw.value);}else{alert(txt);this.value=1;}' onchange='javascript:if (isNumber(this.value) && this.value.length > 0){calgoods(this.form.currency.value,this.form.tax.value,this.form.postfee.value, null, this.form.lenght.value, this.form.width.value, this.form.height.value, this.form.dw.value);}else{alert(txt);this.value=1;}'>";

			//var el3 = document.createElement("input");

			//el3.type = "text";

			//el3.name = "qty[]";

			//el3.id = "qty[]";

			//el3.value = "1";

			//el3.size = 3;

			//el3.onchange = "javascript:calgoods(this.form.currency.value,this.form.tax.value,this.form.postfee.value, this.form.weight.value, this.form.lenght.value, this.form.width.value, this.form.height.value, this.form.dw.value);";

			var textNode3 = document.createTextNode(" x");

  

			cell3.setAttribute("class", "sub_text");

			cell3.setAttribute("align", "right");

			//cell3.appendChild(el3);

			cell3.appendChild(textNode3);

  

			//

			var cell4 = row.insertCell(3);

			var textNode4 = document.createTextNode(currency + " =");

			cell4.setAttribute("class", "sub_text");

			cell4.setAttribute("align", "right");

			cell4.appendChild(textNode4);

  

			//

			var cell5 = row.insertCell(4);

			var el5 = document.createElement("span");

			//el5.type = "text";

			//el5.name = "qty"; //+ iteration;

			//el5.id = "displaygoodshkd" + iteration;

			//el5.size = 3;

			//var textNode3 = document.createTextNode("$");

  

			cell5.setAttribute("class", "sub_text");

			display = iteration - 1;

			cell5.innerHTML = "$ <span id='displaygoodshkd" + display + "'>0</span>";

			//cell5.appendChild(textNode3);

			//cell5.appendChild(el5);

			//var cell3 = row.insertCell(2);

			//var sel = document.createElement('select');

			//sel.name = 'selRow' + iteration;

			//sel.options[0] = new Option('text zero', 'value0');

			//sel.options[1] = new Option('text one', 'value1');

			//cell3.appendChild(sel);

		}

	}

	if (tab == "0")

	{

		var tbl = document.getElementById('goods3');

		var lastRow = tbl.rows.length;

		// if there's no header row in the table, then iteration = lastRow + 1

		var iteration = lastRow - 10;

		if (iteration <= 10)

		{

			var row = tbl.insertRow(lastRow - 8);

			r = r + 1;

			// left cell

			var cell1 = row.insertCell(0);

			var textNode = document.createTextNode("貨品(" + iteration + ")");

			cell1.setAttribute("class", "sub_text");

			cell1.appendChild(textNode);

  

			// right cell

			var cell2 = row.insertCell(1);

			cell2.innerHTML = "<input type='text' name='price2[]' id='price2[]' size='5' onchange='javascript:if (isFloatNumber(this.value)){calgoods(this.form.currency.value, null,this.form.hkpostfee2.value, null, null, null, null, null);}else{alert(txt);this.value=null}' onkeyup='javascript:if (isFloatNumber(this.value)){calgoods(this.form.currency.value, null,this.form.hkpostfee2.value, null, null, null, null, null);}else{alert(txt);this.value=null}'>";

			//var el2 = document.createElement("input");

			//el2.type = "text";

			//el2.name = "price[]";

			//el2.id = "price[]";

			//el2.size = 6;

			//el2.onchange = "javascript:calgoods(this.form.currency.value,this.form.tax.value,this.form.postfee.value, this.form.weight.value, this.form.lenght.value, this.form.width.value, this.form.height.value, this.form.dw.value);";

			var textNode2 = document.createTextNode(" x");

 

			cell2.setAttribute("class", "sub_text");

			cell2.setAttribute("align", "right");

			//cell2.appendChild(el2);

			cell2.appendChild(textNode2);

 

			// select cell

			var cell3 = row.insertCell(2);

			cell3.innerHTML = "<input type='text' name='qty2[]' id='qty2[]' size='3' value='1' onchange='javascript:if (isNumber(this.value)){calgoods(this.form.currency.value, null,this.form.hkpostfee2.value, null, null, null, null, null);}else{alert(txt);this.value=1}' onkeyup='javascript:if (isNumber(this.value)){calgoods(this.form.currency.value, null,this.form.hkpostfee2.value, null, null, null, null, null);}else{alert(txt);this.value=1}'>";

			//var el3 = document.createElement("input");

			//el3.type = "text";

			//el3.name = "qty[]";

			//el3.id = "qty[]";

			//el3.value = "1";

			//el3.size = 3;

			//el3.onchange = "javascript:calgoods(this.form.currency.value,this.form.tax.value,this.form.postfee.value, this.form.weight.value, this.form.lenght.value, this.form.width.value, this.form.height.value, this.form.dw.value);";

			var textNode3 = document.createTextNode(" x");

 

			cell3.setAttribute("class", "sub_text");

			cell3.setAttribute("align", "right");

			//cell3.appendChild(el3);

			cell3.appendChild(textNode3);

 

			//

			var cell4 = row.insertCell(3);

			var textNode4 = document.createTextNode(currency + " =");

			cell4.setAttribute("class", "sub_text");

			cell4.setAttribute("align", "right");

			cell4.appendChild(textNode4);

 

			//

			var cell5 = row.insertCell(4);

			var el5 = document.createElement("span");

			//el5.type = "text";

			//el5.name = "qty"; //+ iteration;

			//el5.id = "displaygoodshkd" + iteration;

			//el5.size = 3;

			//var textNode3 = document.createTextNode("$");

 

			cell5.setAttribute("class", "sub_text");

			display = iteration - 1;

			cell5.innerHTML = "$ <span id='displaygoodshkd2" + display + "'>0</span>";

			//cell5.appendChild(textNode3);

			//cell5.appendChild(el5);

			//var cell3 = row.insertCell(2);

			//var sel = document.createElement('select');

			//sel.name = 'selRow' + iteration;

			//sel.options[0] = new Option('text zero', 'value0');

			//sel.options[1] = new Option('text one', 'value1');

			//cell3.appendChild(sel);

		}

	}

}



function del()

{

	var tab = TabbedPanels1.getCurrentTabIndex();

	if (tab == "1")

	{

  		var tbl = document.getElementById('goods2');

		var lastRow = tbl.rows.length;

		var row = lastRow - 12;

		if (rr > 1)

		{

			tbl.deleteRow(row);

			rr = rr - 1;

		}

	}

	if (tab == "0")

	{

  		var tbl = document.getElementById('goods3');

		var lastRow = tbl.rows.length;

		var row = lastRow - 9;

		if (r > 1)

		{

			tbl.deleteRow(row);

			r = r - 1;

		}

	}

}



function calgoods2(hkpostfeejpy, currency, a, b)

{

	hkpostfeehkd = Math.round(hkpostfeejpy * currency * Math.pow(10,1))/Math.pow(10,1);

	totalhkd = Math.round((parseFloat(a) + hkpostfeehkd)*Math.pow(10,1))/Math.pow(10,1);

	deposite = Math.round((parseFloat(b) + hkpostfeehkd)*Math.pow(10,1))/Math.pow(10,1);

	document.getElementById("displayhkPostFeehkd").innerHTML = hkpostfeehkd;

	document.getElementById("displaytotalhkd").innerHTML = totalhkd;

	document.getElementById("displaydeposite").innerHTML = deposite;

}
