/*
 *  見積もり算出
 *  Copyright Seatrustaim
 *
 */

function checkInit(){
	var max = 73;
	
	for ( var i = 1; i < max; i++ ){
		
		$("#ch" + to00(i)).attr('checked', false);
		$("#num" + to00(i)).val( "1" );
	}
	
	$("#ch600").attr('checked', false);
	$("#ch601").attr('checked', false);
	
	
}

function checkHp( i ){
	
	checkInit();
	
	if ( i == "01"){
		$('#ch01').attr('checked', true);
		$('#ch41').attr('checked', true);
		$('#ch50').attr('checked', true);
		$('#ch51').attr('checked', true);
	}
	
	if ( i == "02"){
		$('#ch01').attr('checked', true);
		$('#ch02').attr('checked', true);
		$('#ch03').attr('checked', true);
		$('#num03').val( "4" );
		
		$('#ch06').attr('checked', true);
		$('#ch08').attr('checked', true);
		$('#num08').val( "10" );
		$('#ch09').attr('checked', true);
		$('#num09').val( "4" );
		$('#ch10').attr('checked', true);
		$('#num10').val( "1" );
		$('#ch12').attr('checked', true);
		$('#num12').val( "3" );
		
		$('#ch15').attr('checked', true);
		$('#ch26').attr('checked', true);
		$('#ch29').attr('checked', true);
		
		$('#ch41').attr('checked', true);
		$('#ch50').attr('checked', true);
		$('#ch51').attr('checked', true);
		$('#ch57').attr('checked', true);
		
		$('#ch63').attr('checked', true);
		$('#ch69').attr('checked', true);
		$('#ch70').attr('checked', true);
		$('#ch71').attr('checked', true);
		
	}
	
	if ( i == "03"){
		$('#ch01').attr('checked', true);
		$('#ch02').attr('checked', true);
		$('#ch03').attr('checked', true);
		$('#num03').val( "9" );
		
		$('#ch06').attr('checked', true);
		$('#ch08').attr('checked', true);
		$('#num08').val( "20" );
		$('#ch09').attr('checked', true);
		$('#num09').val( "8" );
		$('#ch10').attr('checked', true);
		$('#num10').val( "2" );
		$('#ch12').attr('checked', true);
		$('#num12').val( "10" );
		
		$('#ch15').attr('checked', true);
		$('#ch26').attr('checked', true);
		$('#ch30').attr('checked', true);
		
		$('#ch31').attr('checked', true);
		$('#ch32').attr('checked', true);
		$('#ch33').attr('checked', true);
		$('#ch601').attr('checked', true);
		
		$('#ch41').attr('checked', true);
		$('#ch50').attr('checked', true);
		$('#ch51').attr('checked', true);
		$('#ch53').attr('checked', true);
		$('#ch57').attr('checked', true);
		
		$('#ch63').attr('checked', true);
		$('#ch69').attr('checked', true);
		$('#ch70').attr('checked', true);
		$('#ch71').attr('checked', true);
		
	}
	
	
	if ( i == "04"){
		$('#ch01').attr('checked', true);
		$('#ch02').attr('checked', true);
		$('#ch03').attr('checked', true);
		$('#num03').val( "9" );
		
		$('#ch06').attr('checked', true);
		$('#ch08').attr('checked', true);
		$('#num08').val( "30" );
		$('#ch09').attr('checked', true);
		$('#num09').val( "10" );
		$('#ch10').attr('checked', true);
		$('#num10').val( "3" );
		$('#ch11').attr('checked', true);
		$('#ch12').attr('checked', true);
		$('#num12').val( "10" );
		$('#ch13').attr('checked', true);
		
		$('#ch15').attr('checked', true);
		$('#ch26').attr('checked', true);
		$('#ch30').attr('checked', true);
		
		$('#ch31').attr('checked', true);
		$('#ch32').attr('checked', true);
		$('#ch33').attr('checked', true);
		$('#ch601').attr('checked', true);
		
		$('#ch41').attr('checked', true);
		$('#ch50').attr('checked', true);
		$('#ch51').attr('checked', true);
		$('#ch53').attr('checked', true);
		$('#ch57').attr('checked', true);
		$('#ch59').attr('checked', true);
		
		$('#ch63').attr('checked', true);
		$('#ch67').attr('checked', true);
		$('#num67').val( "10" );
		$('#ch69').attr('checked', true);
		$('#ch70').attr('checked', true);
		$('#ch71').attr('checked', true);
		
	}
	
	if ( i == "05"){
		$('#ch01').attr('checked', true);
		$('#ch02').attr('checked', true);
		$('#ch03').attr('checked', true);
		$('#num03').val( "4" );
		
		$('#ch06').attr('checked', true);
		$('#ch08').attr('checked', true);
		$('#num08').val( "20" );
		$('#ch09').attr('checked', true);
		$('#num09').val( "7" );
		$('#ch10').attr('checked', true);
		$('#num10').val( "3" );
		$('#ch11').attr('checked', true);
		$('#ch12').attr('checked', true);
		$('#num12').val( "10" );
		//$('#ch13').attr('checked', true);
		
		//$('#ch15').attr('checked', true);
		$('#ch26').attr('checked', true);
		$('#ch30').attr('checked', true);
		
		//$('#ch31').attr('checked', true);
		//$('#ch32').attr('checked', true);
		//$('#ch33').attr('checked', true);
		//$('#ch601').attr('checked', true);
		
		$('#ch34').attr('checked', true);
		$('#ch35').attr('checked', true);
		$('#ch36').attr('checked', true);
		$('#ch37').attr('checked', true);
		$('#num37').val( "100" );
		$('#ch38').attr('checked', true);
		$('#num38').val( "100" );
		//$('#ch38').attr('checked', true);
		
		$('#ch41').attr('checked', true);
		$('#ch50').attr('checked', true);
		$('#ch51').attr('checked', true);
		$('#ch53').attr('checked', true);
		$('#ch57').attr('checked', true);
		$('#ch59').attr('checked', true);
		
		$('#ch63').attr('checked', true);
		$('#ch67').attr('checked', true);
		$('#num67').val( "10" );
		$('#ch69').attr('checked', true);
		$('#ch70').attr('checked', true);
		$('#ch71').attr('checked', true);
		
	}
	calHp( "00" );
	return;
	
}
function calHp( id ){
	
	//alert( "[" + to00("1") + "]");
	//return ;
	
	//alert( $("#ch" + to00(01)).attr('checked') );
	
	var max = 73;
	var sumHu = 0;
	var sumPri = 0;
	var sumMon = 0;
	
	for ( var i = 1; i < max; i++ ){
		// ch ・・　チェック
		// hu　・・ 開発期間
		// pri ・・ 価格
		// num ・・ 個数
		if ( $("#ch" + to00(i)).attr('checked') ){
			
			
			
			if (i==51 || i==53 || i==55 || i==57 || i==59 || i==61 ){
				if ( !isNaN( $("#pri" + to00(i)).text() ) && !isNaN( $("#num" + to00(i)).val() )){
					sumMon += ( $("#pri" + to00(i)).text() * $("#num" + to00(i)).val() );
					continue;
				}
			}
			
			if ( !isNaN( $("#pri" + to00(i)).text() ) && !isNaN( $("#num" + to00(i)).val() )){
				sumPri += ( $("#pri" + to00(i)).text() * $("#num" + to00(i)).val() );
			}
			if ( !isNaN( $("#hu" + to00(i)).text() ) && !isNaN( $("#num" + to00(i)).val() ) ){
				sumHu  += ( $("#hu" + to00(i)).text() * $("#num" + to00(i)).val() );
			}
			
				 
		}
	}
	
	var mo = Math.ceil( sumHu ) / 20
	$("#sumHu").html( Math.ceil( sumHu ) + " <span class=\"font10\">(約" + mo + "ヶ月)</span>" );
	$("#sumTru").text( ( addFigure( Math.ceil( sumPri / 1.05 ) ) ));
	$("#sumTax").text( addFigure( Math.ceil( sumPri - sumPri / 1.05 ) ) );
	$("#sumPri").text( addFigure( sumPri ));
	$("#sumMon").text( addFigure( sumMon ));
	
	$("#sumHuF").html(  $("#sumHu").html()  );
	$("#sumTruF").text( $("#sumTru").text() );
	$("#sumTaxF").text( $("#sumTax").text() );
	$("#sumPriF").text( $("#sumPri").text() );
	$("#sumMonF").text( $("#sumMon").text() );
	
}

function referHp(){
	
	
	var max = 73;
	var sumHu = 0;
	var sumPri = 0;
	var sumMon = 0;
	var flag = false;
	var msg = "\n\n\n\n\n-----------------------------------------\n＜ホームページ見積内容＞\n";
	var title = "ホームページ見積の件";
	
	for ( var i = 1; i < max; i++ ){
		
		if (  $("#ch" + to00(i)).attr('checked') ){
			flag = true;
			msg += $("#nm" + to00(i)).text() + "," + $("#pri" + to00(i)).text() + "," + $("#num" + to00(i)).val() + "," + $("#hu" + to00(i)).text() + "\n";
			
			if (i==51 || i==53 || i==55 || i==57 || i==59 || i==61 ){
				if ( !isNaN( $("#pri" + to00(i)).text() ) && !isNaN( $("#num" + to00(i)).val() )){
					sumMon += ( $("#pri" + to00(i)).text() * $("#num" + to00(i)).val() );
					continue;
				}
			}
			
			if ( !isNaN( $("#pri" + to00(i)).text() ) && !isNaN( $("#num" + to00(i)).val() )){
				sumPri += ( $("#pri" + to00(i)).text() * $("#num" + to00(i)).val() );
			}
			if ( !isNaN( $("#hu" + to00(i)).text() ) && !isNaN( $("#num" + to00(i)).val() ) ){
				sumHu  += ( $("#hu" + to00(i)).text() * $("#num" + to00(i)).val() );
			}
			
			
		}
	}
	
	msg += "\n-----------------------------------------\n"
	msg += "開発期間：" + Math.ceil( sumHu ) / 20 + "\n";
	msg += "税抜金額：" + addFigure( Math.ceil( sumPri / 1.05 ) ) + "\n";
	msg += "消費税：" + addFigure( Math.ceil( sumPri - sumPri / 1.05 ) ) + "\n";
	msg += "合計金額：" + addFigure( sumPri ) + "\n";
	msg += "月額：" + addFigure( sumMon ) + "\n";
	
	//$("#ref").val( msg );
	
	if ( flag == true ){
		obj=document.referForm;
		obj.ref.value = msg;
		obj.refTitle.value = title;
		obj.method ="post";
		obj.action="./refer";
		obj.submit();
	} else {
		location.href = "./refer.jsp";
	}
	
	
}

function to00( id ){
	
	id += "";
	if ( id.length == 1 ){
		return "0" + id; 
	}
	return id;
	
}

//カンマ挿入関数
function addFigure(str) {
	var num = new String(str).replace(/,/g, "");
	while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	return num;
}
