var aes_items = new Object;

function num_format(num){
	var tmp;
	num = num.toString(10);
	tmp = num.match(/./g).reverse().join('');
	tmp = tmp.replace(/(\d{3})/g,"$1,");
	tmp = tmp.match(/./g).reverse().join('').replace(/^,/,'');
	return '￥'+tmp;
}

function print_sheet(calc_items){
	var price = 0;
	var estimation_sheet = '';
	estimation_sheet += '<table id="EstimationSheet">';
	estimation_sheet += '<tr><th class="no">No.</th><th class="title">項目</th><th class="unitprice" style="text-align:center;">単価</th><th class="amount">数量</th><th class="price" style="text-align:center;">金額</th></tr>';
	for(var i=1;i<calc_items.length;i++){
		estimation_sheet += '<tr><td class="no">'+i+'</td><td class="title">'+calc_items[i].title+'</td><td class="unitprice">'+num_format(calc_items[i].unit_price)+'</td><td class="amount">'+calc_items[i].amount+'</td><td class="price">'+num_format(calc_items[i].price)+'</td></tr>';
		price += calc_items[i].price;
	}
	estimation_sheet += '<tr><td colspan="3" rowspan="3">&nbsp;</td><td>小計</td><td class="price">'+num_format(price)+'</td></tr>';
	estimation_sheet += '<tr><td>消費税</td><td class="price">'+num_format(parseInt(price*0.05))+'</td></tr>';
	estimation_sheet += '<tr><td colspan="2" id="totalprice">合計 '+num_format(parseInt(price*1.05))+'</td></tr>';
	estimation_sheet += '</table>';
	document.getElementById('ef_sheet').innerHTML = estimation_sheet;
}

function addEstimationItem(name, title, type){
	aes_items[name]			= new Object;
	aes_items[name].name		= name;
	aes_items[name].type		= type;
	aes_items[name].title	= title;
	switch(type){
		case 'select':
			aes_items[name].options		= arguments[3];
			break;
		case 'text':
			aes_items[name].size			= arguments[3];
			aes_items[name].defaultvalue	= arguments[4];
			aes_items[name].texttype		= arguments[5];
			break;
	}
}
function addform(obj){
	document.write('<tr><th>'+obj.title+' ：</th><td>');
	switch(obj.type){
		case 'select':
			document.write('<select name="ef_inputs['+obj.name+']" id="ef_inputs['+obj.name+']">');
			for(var i=0;i<obj.options.length;i++) document.write('<option value="'+i+'">'+obj.options[i]+'</option>');
			document.write('</select></td></tr>');
			document.getElementById('ef_inputs['+obj.name+']').onchange = function(){ ef_calc(); }
			break;
		case 'text':
			document.write('<input type="text" name="ef_inputs['+obj.name+']" id="ef_inputs['+obj.name+']" size="'+obj.size+'" value="'+obj.defaultvalue+'">');
			if(obj.texttype == 'num'){
			if (document.implementation.hasFeature ('Events', '2.0')){
				document.getElementById('ef_inputs['+obj.name+']').onkeydown = function(event){
					if(event.keyCode==38) this.value++;
					else if(event.keyCode==40) (this.value==1)? this.value=1:this.value--;
					else this.value = this.value.replace(/[^\d]/g,'');
					ef_calc();
				}
			} else if (typeof window.event != 'undefined') {
				document.getElementById('ef_inputs['+obj.name+']').onkeydown = function(e){
					if(event.keyCode==38) this.value++;
					else if(event.keyCode==40) (this.value==1)? this.value=1:this.value--;
					else this.value = this.value.replace(/[^\d]/g,'');
					ef_calc();
				}
			}
			document.getElementById('ef_inputs['+obj.name+']').onkeyup = function(){
				this.value = this.value.replace(/[^\d]/g,'');
				ef_calc();
			}
			} else{
				document.getElementById('ef_inputs['+obj.name+']').onchange = function(){ ef_calc(); }
			}
			break;
	}
	document.write('</td></tr>');
}

function print_estimation_input(){
	document.write('<form name="ef" id="ef"><table id="EstimationInput">');
	for(var i in aes_items) addform(aes_items[i]);
	document.write('</table></form>');
}