
function Check(fo){
var errmsg="";
var amount;
var op1;
var op2;
var op="";
var opflag=false;

if(fo.elements['amount']){
	if(fo.elements['amount'].tagName="INPUT"){
		amount=fo.elements['amount'].value;
	}
	else{
		amount=fo.elements['amount'].options[fo.elements['amount'].selectedIndex].value;
	}
	if(amount == "0" || amount == ""){
		errmsg+="個数を選択してください。\n";
	}
}

if(fo.elements['op1']){;
	op1=fo.elements['op1'].options[fo.elements['op1'].selectedIndex].value;
	if(op1 == ""){
		errmsg+="オプションが選択されていません。\n";
	}
	op+=op1;
	opflag=true;
}

if(fo.elements['op2']){
	op2=fo.elements['op2'].options[fo.elements['op2'].selectedIndex].value;
	if(op2 ==""){
		errmsg+="オプションが選択されていません。\n";
	}
	if(op){
		op+=" ";
	}
	op+=op2;
	opflag=true;
}


if(errmsg ==""){
	if(typeof(st) != "undefined"){
		errmsg+=StockCheck(fo,amount,opflag,op);
	}
}

if(errmsg){
	Alert(errmsg);
	return false;
}



}

function StockCheck(fo,amount,opflag,op){
var stock;
var msg="";
var number;
var name;

var number=fo.elements['number'].value;
amount=parseInt(amount);
var name=fo.elements['name'].value;

if(!opflag){
	stock=st[number];
	if(stock == ""){
		stock="0";
	}
	stock=parseInt(stock);
	if(stock == 0){
		msg="申し訳ありません。"+name+"は売り切れです。";
	}
	else if(amount > stock){
		msg="注文数が在庫数より多くなっています。"+name+"の在庫数は"+stock+"です。";
	}
}
else{
	if(st[number]){
		stock=st[number][op];
		stock=parseInt(stock);
		if(stock == 0){
			msg="申し訳ありません。"+number+"の"+op+"は売り切れです。";
		}
		else if(amount > stock){
			msg="注文数が在庫数より多くなっています。"+name+"の"+op+"の在庫数は"+stock+"です。";
		}
	}
}


return msg;
}



function StockWrite(n){
var key;
var msg="";
var flag=0;

if(typeof(st[n]) == "object"){
	var stocknum;
	for(key in st[n]){
		stocknum=st[n][key];
		stocknum=parseInt(stocknum);
		if(stocknum > 0){
			msg+=key+":"+"在庫あり ";
		}
		if(stocknum == 0){
			msg+=key+":"+"売り切れ ";
		}
	}
}
else{
	var stocknum=st[n];
	stocknum=parseInt(stocknum);
	if(stocknum > 0){
		msg="在庫あり";
	}
	if(stocknum == 0){
		msg="売り切れ";
	}

}

document.open();
document.write(msg);
document.close();

}


function Alert(e){
	alert(e);
}


function SetCookie(amount){
document.cookie="cart="+amount+"; path=/";
}

function ReadCookie(n){
var c=document.cookie;
var tmp = c.split(';');
var value;
for(var i=0;i<tmp.length;i++){
	var ary=tmp[i].split('=');
	var na=ary[0];
	na=na.replace(/ /g,'');
	if(na == n){
		var v=ary[1];
		v=v.replace(/\+/g,'%20');
		value=decodeURIComponent(v);
		break;
	}
}
return value;

}

function Cart(){
var amount=ReadCookie('cart');
if(typeof(amount) == "undefined"){
	amount="0";
}

var ob=document.getElementById("amount");
ob.innerHTML=amount;

}

function Spinner(act,ob){

var pa=ob.parentElement;
var inp=pa.previousElementSibling;

var cnt=parseInt(inp.value);
if(act == "up"){
	cnt++;
}
else{
	cnt--;
	if(cnt < 0){
		cnt=0;
	}
}

inp.value=cnt;

}

function Send(act,data,u){

var xhr=new XMLHttpRequest();

xhr.onreadystatechange = function(){
	switch(xhr.readyState){
		case 0:
			break;
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;
		case 4:
			if(xhr.status == 200 || xhr.status == 304) {
				var txt=xhr.responseText;
				var s=txt.substr(0,3);

				if(s == "ok:" || s == "er:"){
					var tmp=txt.split(":");
					if(tmp[0] == "ok"){
						ShowData(act,tmp[2],tmp[3]);
					}
					else{
						var msg_ob_tmp=GetMsgOb(act);
						var msg_ob=msg_ob_tmp[0];
						var ob_tmp=GetOb(act);
						var ob=ob_tmp[0];
						MsgErr(msg_ob,tmp[1]);
						ObErr(ob);
					}
				}
				else{
					var msg_ob_tmp=GetMsgOb(act);
					var msg_ob=msg_ob_tmp[0];
					var ob_tmp=GetOb(act);
					var ob=ob_tmp[0];
					MsgErr(msg_ob,"エラーが発生しました");
					ObErr(ob);
				}

			}
			break;
	}
}

xhr.open("POST",u);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send( EncodeHTMLForm(data));
}

function EncodeHTMLForm(data){
var params=[];
for(var name in data){
	var value=data[name];
	var param=encodeURIComponent(name)+'='+encodeURIComponent(value);
	params.push(param);
}
return params.join('&').replace(/%20/g,'+');
}


window.onload=Cart;