﻿	function Calculate(Weights, Post, Zone) {
    // 单价列表，包括基本价格
    var price_post = [
						 					 [28.9,  25.9,  25.9, 23.9,  25.9, 25.9, 29.9],
						 					 [ 4.0,   3.5,   4.0,  3.0,   3.5,  3.5,  4.0],
						 					 [ 4.0,   3.5,   4.0,  3.0,   3.5,  3.5,  4.0],
						 					 [ 4.5,   4.2,   4.5,  3.6,   4.2,  4.2,  4.5],
						 					 ];
		price_other = [
						 					[46.9,  31.7, 49.9,  25.7, 52.7, 40.7, 58.9],
						 					[12.2,   7.5,  12.2,  8.0,  11.2,  9.1, 13.4],
						 					[10.4,   8.0,  10.4,  8.0,  11.7,  9.6,  13.9],
						 					[10.1,   7.8,  10.1,  7.5,  11.4,  9.2,  13.7],
						 					[10.1,   7.8,  10.1,  7.5,  11.4,  9.2,  13.7],
						 					];

    // Zone 必须在 1 到 6 之间
    	
    var price = 0.0;
	  var defaults = 0.0;
		var weight ;
		var loop = 0.0;
		var tmp = 0.0;
		
    if ((Zone < 1) || (Zone > 7)) 
    	throw new Error("Zone must be 1 - 7");
    Zone = Zone -1;
    Weights = parseFloat(Weights);
    
			if (Post == 1)
			{				
				if (Weights <= 0.5)
					price = price_post[0][Zone];
				else if (Weights <= 1.0)  {
						weight = (Weights + 0.0999 - 0.5)/0.1;
						price = price_post[0][Zone] + price_post[1][Zone] * Math.floor(weight);
				}
				else if (Weights <= 1.5)  {						
						weight = (Weights + 0.099999999999 - 1.0)/0.1; 											
						price = price_post[0][Zone] + price_post[1][Zone] * 5 + price_post[2][Zone] * Math.floor(weight);
				}
				else if (Weights <= 2.0)  {
						weight = (Weights + 0.0999 - 1.5)/0.1;
						price = price_post[0][Zone] + price_post[1][Zone] * 5 + price_post[2][Zone] * 5 + price_post[3][Zone] * Math.floor(weight);
				}
				else {
						price = 0;
				}
			}			
			else 
			{   
					
					if (Weights <= 0.5)
						price = price_other[0][Zone];
					else if (Weights <= 10)  {
							weight = (Weights + 0.4999 - 0.5)/0.5;
							price = price_other[0][Zone] + price_other[1][Zone] * Math.floor(weight);
					}
					else if (Weights <= 20)  {
							weight = (Weights + 0.4999 - 10)/0.5;
							price = price_other[0][Zone] + price_other[1][Zone] * 19 + price_other[2][Zone] * Math.floor(weight);					
					}
					else if (Weights <= 30)  {
							weight = (Weights + 0.4999 - 20)/0.5;
							price = price_other[0][Zone] + price_other[1][Zone] * 19 + price_other[2][Zone] * 20 + price_other[3][Zone] * Math.floor(weight);					
					}
					else if (Weights > 30)  {
							weight = (Weights + 0.4999 - 30)/0.5;
							price = price_other[0][Zone] + price_other[1][Zone] * 19 + price_other[2][Zone] * 20 + price_other[3][Zone] * 20 + price_other[4][Zone] * Math.floor(weight);					
					}
			}
 		 			
     return price.toFixed(2);
}

function CalIt() {
    var oWeight;
    var oZone;
    var oRate;
    var oPost;
    oWeight = document.getElementById("Weight");
    oZone = document.getElementById("Zone");
    oPost = document.getElementById("Post");
    oRate = document.getElementById("Rate");
    if (isNaN(oWeight.value)) {
        oRate.value = "Please input a number";
    }
    else {
        oRate.value = Calculate(oWeight.value, oPost.value, oZone.value);
    }   
   
}
