//  自動見積りスクリプト
//    各種Webプログラムの作成はuniaso.comへご相談ください。

	var pA = new Product(840,620,500,460,380,280);
	var pB = new Product(940,720,580,540,440,340);
	var pC = new Product(1200,900,740,680,600,520);

function reCalc(form) {
	qty = form.qty.value;
	product = form.product.value;
	colorlanyard = form.colorlanyard.value;
	basicFee = 7000;
	deliFee	= 3000;
	if(!isNumber(qty)) { alert('半角の数字で数量を入力してください'); form.qty.focus(); return false; }
	if(!isNumber(colorlanyard)) { alert('色数は半角の数字で入力してください'); form.colorlanyard.focus(); return false; }
	if(qty<30) { alert('最低注文数は30本です'); form.qty.value=qty=30; }
	//  if(qty<50) { form.addreel.checked=false; form.addreel.disabled=true; } else form.addreel.disabled=false;
	if(qty>3000) { alert('3000本以上のご注文についてはお問合わせください'); form.qty.value=qty=3000; }
	if(colorlanyard>1) {
		if(qty<50) { alert('50本以下ではストラップ色は１色のみとなります'); form.colorlanyard.value=colorlanyard=1; }
		else if(qty>=50 && colorlanyard>Math.floor(qty/50)) { alert('ご注文の総本数で選べるストラップ色の数を超えています。\n（複数の色のご選択は１色あたり50本以上でご注文ください）'); form.colorlanyard.value=colorlanyard=Math.floor(qty/50); }
	}

	switch(product) {
		case 'pA': unitPrice = pA.unitPrice(); break;
		case 'pB': unitPrice = pB.unitPrice(); break;
		case 'pC': unitPrice = pC.unitPrice(); break;
	}

	if(form.addmobile.checked==true) unitPrice += (qty<500 ? 50 : 40);
	//  if(form.blackcolor.checked==true) unitPrice += 20;
	if(form.addreel.checked==true) unitPrice += reelPrice(qty);
	if(form.addsafety.checked==true) unitPrice += safetyPrice(product,qty);
	//  if(form.addmo.checked==true) unitPrice += (qty<500 ? 50 : 40);
	//  else if(form.blackco.checked==true) unitPrice += 20;
	if(form.exorder.checked==true) { form.exdeli.checked=true; unitPrice *= 1.3; }
	if(form.discount.checked==true) basicFee /= 2;
	if(colorlanyard > 1) basicFee += ((colorlanyard-1)*1500);
	if(form.makedata.checked==true) basicFee += 3000;
	if(form.colorscreen.checked==true) basicFee += 3000;
	if(form.exdeli.checked==true) deliFee *= 2;

	TAX		= 5;
	sum		= unitPrice * qty;
	subtotal= sum + basicFee + deliFee;
	tax		= subtotal * TAX/100;
	total	= subtotal + tax;

	form.unitprice.value= unitPrice;
	form.sum.value		= sum;
	form.basicfee.value	= basicFee;
	form.delifee.value	= deliFee;
	form.subtotal.value	= subtotal;
	form.tax.value		= tax;
	form.total.value	= total;
}

function Product(p1,p2,p3,p4,p5,p6) {
	this.p1 = p1;
	this.p2 = p2;
	this.p3 = p3;
	this.p4 = p4;
	this.p5 = p5;
	this.p6 = p6;
}

Product.prototype.unitPrice = function() {
	switch(true) {
		case (qty>=30 && qty<50): return this.p1; break;
		case (qty>=50 && qty<100): return this.p2; break;
		case (qty>=100 && qty<300): return this.p3; break;
		case (qty>=300 && qty<500): return this.p4; break;
		case (qty>=500 && qty<1000): return this.p5; break;
		case (qty>=1000): return this.p6; break;
	}
}

function reelPrice(qty) {
	switch(true) {
		//  case (qty>=30 && qty<50): return 0; break;
		case (qty>=30 && qty<50): return 700; break;
		case (qty>=50 && qty<100): return 600; break;
		case (qty>=100 && qty<300): return 500; break;
		case (qty>=300 && qty<1000): return 400; break;
		case (qty>=1000): return 360; break;
	}
}

function safetyPrice(product,qty) {
	switch(true) {
		case (product=="pA" && qty>=30 && qty<300): return 60; break;
		case (product=="pA" && qty>=300 && qty<1000): return 50; break;
		case (product=="pA" && qty>=1000): return 40; break;
		case (product=="pB" && qty>=30 && qty<300): return 120; break;
		case (product=="pB" && qty>=300 && qty<1000): return 100; break;
		case (product=="pB" && qty>=1000): return 80; break;
	}
}

function isNumber(strInput) {
	var digit = "0123456789";
	var temp;
	for (var i=0; i<strInput.length; i++) {
		temp = strInput.substring(i,i+1);
		if (digit.indexOf(temp) == -1) return false;
	}
	return true;
}
