var counter = 0;
var incounter = 1;
function AddTableRows(){
	// カウンタを回す
	counter+=3;
	incounter++;

	var buytbl = document.getElementById("buytbl");
	var row1 = buytbl.insertRow(counter);
	var row2 = buytbl.insertRow(counter + 1);
	var row3 = buytbl.insertRow(counter + 2);
	var cell1 = row1.insertCell(0);
	var cell2 = row1.insertCell(1);
	var cell3 = row2.insertCell(0);
	var cell4 = row2.insertCell(1);
	var cell5 = row3.insertCell(0);
	var cell6 = row3.insertCell(1);

	// class の付与は UserAgent によって
	// 挙動が違うっぽいので念のため両方の方法で
	cell1.setAttribute("class","tb_left_in_"+incounter);
	cell2.setAttribute("class","tb_right_in");
	cell3.setAttribute("class","tb_left_in_"+incounter);
	cell4.setAttribute("class","tb_right_in");
	cell5.setAttribute("class","tb_foot_left_in_"+incounter);
	cell6.setAttribute("class","tb_foot_right_in");
	cell1.className = 'tb_left_in_'+incounter;
	cell2.className = 'tb_right_in';
	cell3.className = 'tb_left_in_'+incounter;
	cell4.className = 'tb_right_in';
	cell5.className = 'tb_foot_left_in_'+incounter;
	cell6.className = 'tb_foot_right_in';
	cell6.colSpan = 3;
	
	var HTML1 = '商品名<span class="form_font1"> ※</span>\n';
	var HTML2 = '<input name="item' + incounter + '" id="item' + incounter + '" size="50" maxlength="255" value="" type="text">\n';
	var HTML3 = '在庫数<span class="form_font1"> ※</span>\n';
	var HTML4 = '<input name="num' + incounter + '" id="num' + incounter + '" size="8" maxlength="255" value="" type="text">\n';
	var HTML5 = '商品写真\n';
	var HTML6 = '<input type="file" name="upfile_' + incounter + '" size="30">\n';
	cell1.innerHTML = HTML1;
	cell2.innerHTML = HTML2;
	cell3.innerHTML = HTML3;
	cell4.innerHTML = HTML4;
	cell5.innerHTML = HTML5;
	cell6.innerHTML = HTML6;

	if(incounter>=3){
		var addform_btn = document.getElementById("addform");
		addform_btn.disabled = true; 
	}

	var delform_btn = document.getElementById("delform");
	delform_btn.disabled = false; 

}

function DelTableRows(){

	var buytbl = document.getElementById("buytbl");
	var row1 = buytbl.deleteRow(counter);
	var row2 = buytbl.deleteRow(counter);
	var row3 = buytbl.deleteRow(counter);

	// カウンタを回す
	counter-=3;
	incounter--;

	if(incounter==1 || counter==0){
		var delform_btn = document.getElementById("delform");
		delform_btn.disabled = true; 
	}

	var addform_btn = document.getElementById("addform");
	addform_btn.disabled = false; 
}

function check_atc( obj, text) {
	obj.style.border = "";
	if (obj.value == "") {
		obj.style.border = "2px solid #FF0000";
		window.alert(text + "　※必須を入力してください");
		obj.focus();
		return false;
	}

	return true;
}

function check_ctc( obj1, obj2, text) {
	obj1.style.border = "";
	obj2.style.border = "";
	if (obj1.value == "" && obj2.value == "") {
		obj1.style.border = "2px solid #FF0000";
		obj2.style.border = "2px solid #FF0000";
		window.alert(text + "　※どちらかを入力してください");
		obj1.focus();
		obj2.focus();
		return false;
	}

	return true;
}

function form_check() {
	myform = window.document.formmail;

	if ( check_atc( myform.name, '■お名前') == false){ ret = false;}
	if ( check_atc( myform.mail, '■メールアドレス') == false){ ret = false;}
	if ( check_atc( myform.message, '■内容') == false){ ret = false;}
	
	return ret;
}

function buyform_check() {
	myform = window.document.formmail;

	ret = true;

	//if ( check_atc( myform.inc, '■会社名') == false){ ret = false;}
	//if ( check_atc( myform.name, '■お名前') == false){ ret = false;}
	if ( check_ctc( myform.mail, myform.tel, '■メールアドレス 電話番号') == false){
		ret = false;
	}
	if ( check_atc( myform.item1, '■商品名') == false){ ret = false;}
	if ( check_atc( myform.num1, '■在庫数') == false){ ret = false;}

	if( incounter > 1){
		if ( check_atc( myform.item2, '■商品名') == false){ ret = false;}
		if ( check_atc( myform.num2, '■在庫数') == false){ ret = false;}
		if( incounter > 2){
			if ( check_atc( myform.item3, '■商品名') == false){ ret = false;}
			if ( check_atc( myform.num3, '■在庫数') == false){ ret = false;}
		}
	}

	//if ( check_atc( myform.message, '■内容') == false){ ret = false;}
	
	return ret;
}

function saleform_check() {
	myform = window.document.formmail;

	ret = true;

	if ( check_atc( myform.inc, '■会社名') == false){ ret = false;}
	if ( check_atc( myform.name, '■お名前') == false){ ret = false;}
	if ( check_atc( myform.mail, '■メールアドレス') == false){ ret = false;}
	if ( check_atc( myform.tel, '■電話番号') == false){ ret = false;}
	//if ( check_atc( myform.message, '■内容') == false){ ret = false;}
	
	return ret;
}

