var priceSheet = function() {
	
}

priceSheet.prototype = {
	price: 0,
	vaccine: 0,
	passbook: 0,
	plan: 0,
	size: 0,
	refund: 0,
	shipping: 0,
	shipping_sel: "",
	tax: 0,
	tax_per: 0.05,
	total: 0,
	lovelove_pack: 0,
	bcourse: 0,
	microtip_price: 0,
	rabies_control_price: 0,
	
	recast: function() {
	
		var sum_price = this.price_add_tax;
		//sum_price += this.vaccine;
		//sum_price += this.passbook;
		sum_price += this.lovelove_pack;
		sum_price += this.bcourse;
		sum_price += this.microtip_price;
		sum_price += this.rabies_control_price;
		//sum_price += this.plan;
		sum_price += this.size;
		sum_price += this.refund;
		sum_price += this.shipping;
		//window.alert(this.bcourse);
		//this.tax = Math.ceil(sum_price * this.tax_per);
		this.total = sum_price;
		this.tax = Math.ceil(sum_price - (sum_price / 1.05));
	},
	
	refreshSheet: function() {
		$("#total").html(this.numberFormat(this.total));
		$("#tax").html(this.numberFormat(this.tax));
		
		var shipping_str = this.numberFormat(this.shipping) + "円";
		if(this.shipping_sel == "4") {
			shipping_str = "別途見積";
		}
		$("#shipping").html(shipping_str);
	},
	
	numberFormat: function(num) {
		var str = String(num);
		if(str.length < 4) {
			return str;
		}
		
		var ret = "";
		
		for(var i=0; i<str.length; i++) {
			var from = str.length - 1 - i;
			var to = from + 1;
			var c = str.substring(from, to);
			
			if(i > 0 && i % 3 == 0) {
				ret = "," + ret;
			}
			ret = c + ret;
		}
		
		return ret;
	}
	
};

var sheet;
function init_priceSheet() {
	
	sheet = new priceSheet();
	sheet.price = parseInt($("#pet_price").val(), 10);
	sheet.price_add_tax = parseInt($("#pet_price_tax").val(), 10);
	
	sheet.lovelove_pack = parseInt($("#lovelove_pack").val(), 10);
	sheet.bcourse = parseInt($("#bcourse_price").val(), 10);
	sheet.microtip_price = parseInt($("#microtip_price").val(), 10);
	sheet.rabies_control_price = parseInt($("#rabies_control_price").val(), 10);
	//sheet.vaccine = parseInt($("#vaccine_price").val(), 10);
	//sheet.passbook = parseInt($("#passbook_price").val(), 10);
	//sheet.plan = parseInt($("#plan_price").val(), 10);
	sheet.size = parseInt($("#size_price").val(), 10);
	sheet.refund = parseInt($("#refund_price").val(), 10);
	sheet.shipping = parseInt($("#shipping_price").val(), 10);
	sheet.recast();
}

function chg_refund(val) {
	
	var add_price = 0;
	
	if(val == "1") {
		add_price = parseInt($("#refund_cont").val(), 10);
	}
	
	if(sheet == null) {
		init_priceSheet();
	}
	
	add_price = add_price * 1.05;
	
	sheet.refund = add_price;
	sheet.recast();
	
	sheet.refreshSheet();
}

function chg_bcourse(val) {
	var add_price = 0;
	
	if(val == "1") {
		add_price = parseInt($("#bcourse").val(), 10);
	}
	
	if(sheet == null) {
		init_priceSheet();
	}
	
	sheet.bcourse = add_price;
	sheet.recast();
	
	sheet.refreshSheet();
}


/*function chg_plan(val) {
	var pet_price = $("#pet_price").val();
	var pet_price_tax = parseInt(pet_price,10) * 0.05;
	pet_price = parseInt(pet_price, 10) + pet_price_tax;
	var tmp = 0;
	var add_price = 0;
	
	if(pet_price > 100000) {
		if(val == "a") {
			tmp = $("#a_plan").val();
		} else {
			if(val == "b") {
				tmp = $("#b_plan").val();
			} else {
				tmp = 0;
			}
		}
		tmp = parseInt(tmp, 10);
	
		add_price = parseInt(pet_price, 10) * (tmp / 100);
		add_price = Math.ceil(add_price);
	} else {
		if(val == "a") {
			add_price = 15000;
		} else {
			if(val == "b") {
				add_price = 25000;
			}
		}
	}
	//window.alert(add_price);
	if(sheet == null) {
		init_priceSheet();
	}
	
	sheet.plan = add_price;
	sheet.recast();

	sheet.refreshSheet();
}*/

function chg_shipping(val) {

	var add_price = 0;
	
	if(val == "2") { // 陸送
		add_price = $("#shipping01").val();
	}
	
	if(val == "3") { // 空輸
		add_price = $("#shipping02").val();
	}
	
	add_price = parseInt(add_price, 10);
	
	//window.alert(add_price);
	
	if(sheet == null) {
		init_priceSheet();
	}
	
	var pet_price = $("#pet_price").val();
	if(pet_price < 100000) {
		add_price = add_price * 2;	
	}
	add_price = add_price * 1.05;
	
	sheet.shipping = add_price;
	sheet.shipping_sel = val;
	sheet.recast();

	sheet.refreshSheet();
	
}

function form_smt() {
	document.f1.mode.value = "submit";
	document.f1.submit();
}
function form_reset() {
	document.f1.mode.value = "reset";
	document.f1.submit();
}

