var gid = new Array();
var gname = new Array();
var gprice = new Array();
var gnum = new Array();

function WMShopInit() {
	WMSetupGoods();
	WMUpdateCartInd();
}

function WMFormatCurrency( sn ) {
	var i,ln,rslt="";
	sn += "";
	ln = sn.length;
	for ( i=0; i<ln; i++ ) {
		if ( i%3==0 && i!=0 )
			rslt = "," + rslt;
		rslt = sn.charAt(ln-1-i) + rslt;
	}
	return "\\" + rslt + ".-";
}

function WMGetSubtotal() {
	var i,rslt=0;
	for ( i=0; i<gid.length; i++ ) {
		rslt += gprice[i]*gnum[i];
	}
	return rslt;
}

function WMGetTax() {
	var tax,rslt=0;
	tax = WMGetSubtotal()*wm_consumptionTaxRate;
	if ( tax >= 1 )
		rslt = parseInt( tax );
	return rslt;
}

function WMGetTotal() {
	var tax,rslt=WMGetSubtotal();
	tax = WMGetSubtotal()*wm_consumptionTaxRate;
	if ( tax >= 1 )
		rslt += parseInt( tax );
	return rslt;
}

function WMClearCart( msg ) {
	if ( WMIsStayCartGoods() == false ) {
		alert("カゴは空です。");
		return;
	}
	if ( msg == 1 ) {
		var ans = confirm("カゴを空にしますか?");
		if ( ans == false )
			return;
	}
	WMClearCartContents();
	WMUpdateCartInd();
	WMSyncGoodsData();
}

function WMAddCartData( g ) {
	var ans = confirm( "商品をカゴに入れてもいいですか?" );
	if ( ans == false )
		return;
	var idx;
	WMSyncGoodsData();
	idx = WMFindGoods( g.name );
	if ( idx == -1 ) {
		alert( "SYSERR: 商品が見つかりませんでした。" );
		return;
	}
	gnum[idx] = parseInt( gnum[idx]+1 );
	document.cookie = idx + "=" + gnum[idx];
	WMUpdateCartInd();
}

function WMAddCartDataID( id ) {
	var ans = confirm( "商品をカゴに入れてもいいですか?" );
	if ( ans == false )
		return;
	var idx;
	WMSyncGoodsData();
	idx = WMFindGoods( id );
	if ( idx == -1 ) {
		alert( "SYSERR: 商品が見つかりませんでした。" );
		return;
	}
	gnum[idx] = parseInt( gnum[idx]+1 );
	document.cookie = idx + "=" + gnum[idx];
	WMUpdateCartInd();
}

function WMRemoveCartData( g ) {
	var ans = confirm( "この商品を1つ削除しますか?" );
	if ( ans == false )
		return;
	var idx;
	WMSyncGoodsData();
	idx = WMFindGoods( g.name );
	if ( idx == -1 ) {
		alert("SYSERR: 商品が見つかりませんでした。");
		return;
	}
	gnum[idx] = parseInt(gnum[idx]-1);
	document.cookie = idx + "=" + gnum[idx];
	document.location.reload();
	WMUpdateCartInd();
}

function WMSearchControl( obj, n ) {
	var i;
	for ( i=0; i<obj.elements.length; i++ ) {
		if ( obj.elements[i].name == n )
			return true;
	}
	return false;
}

function WMUpdateCartInd() {
	var i, j, k, ind, fname="WMCartInd";
	WMSyncGoodsData();
	for ( i=0; i<document.forms.length; i++ ) {
		if ( document.forms[i].name == fname ) {
			ind = document.forms[i];
			if ( WMSearchControl( ind, "subtotal" ) ) ind.subtotal.value = WMFormatCurrency(WMGetSubtotal());
			if ( WMSearchControl( ind, "tax"      ) ) ind.tax.value      = WMFormatCurrency(WMGetTax());
			if ( WMSearchControl( ind, "total"    ) ) ind.total.value    = WMFormatCurrency(WMGetTotal());
		}
	}
	if ( parent ) {
		for ( i=0; i<parent.frames.length; i++ ) {
			for ( j=0; j<parent.frames[i].document.forms.length; j++ ) {
				if ( parent.frames[i].document.forms[j].name == fname ) {
					ind = parent.frames[i].document.forms[j];
					if ( WMSearchControl( ind, "subtotal" ) ) ind.subtotal.value = WMFormatCurrency(WMGetSubtotal());
					if ( WMSearchControl( ind, "tax"      ) ) ind.tax.value      = WMFormatCurrency(WMGetTax());
					if ( WMSearchControl( ind, "total"    ) ) ind.total.value    = WMFormatCurrency(WMGetTotal());
				}
			}
		}
	}
	if ( parent && parent.parent ) {
		for ( i=0; i<parent.parent.frames.length; i++ ) {
			for ( j=0; j<parent.parent.frames[i].document.forms.length; j++ ) {
				if ( parent.parent.frames[i].document.forms[j].name == fname ) {
					ind = parent.parent.frames[i].document.forms[j];
					if ( WMSearchControl( ind, "subtotal" ) ) ind.subtotal.value = WMFormatCurrency(WMGetSubtotal());
					if ( WMSearchControl( ind, "tax"      ) ) ind.tax.value      = WMFormatCurrency(WMGetTax());
					if ( WMSearchControl( ind, "total"    ) ) ind.total.value    = WMFormatCurrency(WMGetTotal());
				}
			}
		}
	}
	if ( parent ) {
		for ( i=0; i<parent.frames.length; i++ ) {
			for ( j=0; j<parent.frames[i].window.frames.length; j++ ) {
				for ( k=0; k<parent.frames[i].window.frames[j].document.forms.length; k++ ) {
					if ( parent.frames[i].window.frames[j].document.forms[k].name == fname ) {
						ind = parent.frames[i].window.frames[j].document.forms[k];
						if ( WMSearchControl( ind, "subtotal" ) ) ind.subtotal.value = WMFormatCurrency(WMGetSubtotal());
						if ( WMSearchControl( ind, "tax"      ) ) ind.tax.value      = WMFormatCurrency(WMGetTax());
						if ( WMSearchControl( ind, "total"    ) ) ind.total.value    = WMFormatCurrency(WMGetTotal());
					}
				}
			}
		}
	}
}

function WMSyncGoodsData() {
	var i, sn;
	for ( i=0; i<gid.length; i++ ) {
		sn = WMGetCartContents(i);
		if ( sn == "" )
			sn = 0;
		gnum[i] = parseInt(sn);
	}
}

function WMClearCartContents() {
	var i;
	if ( gid.length == 0 )
		WMSetupGoods();
	for ( i=0; i<gid.length; i++ ) {
		gnum[i] = 0;
		document.cookie = i + "=0"
	}
}

function WMGetCartGoodsCount() {
	var i, count = 0;
	if ( gid.length == 0 )
		WMSetupGoods();
	WMSyncGoodsData();
	for ( i=0; i<gid.length; i++ ) {
		count += gnum[i];
	}
	return count;
}

function WMIsStayCartGoods() {
	var i, bStay = false;
	if ( gid.length == 0 )
		WMSetupGoods();
	WMSyncGoodsData();
	for ( i=0; i<gid.length; i++ ) {
		if ( gnum[i] != 0 ) {
			bStay = true;
			break;
		}
	}
	return bStay;
}

function WMGetCartContents( id ) {
	var i, p, s, rslt="";
	p = document.cookie.split(";");
	for ( i=0; i<p.length; i++ ) {
		if ( p[i].charAt(0) == " " ) {
			p[i] = p[i].substring( 1, p[i].length );
		}
	}
	for ( i=0; i<p.length; i++ ) {
		if ( p[i].indexOf(id+"=") != -1 ) {
			s = p[i].indexOf("=");
			if ( p[i].substring(0, s) == id ) {
				rslt = p[i].slice(s+1);
				return rslt;
			}
		}
	}
	return rslt;
}

function WMValidationForm( f ) {
	var i, n, v, tmp;
	for ( i=0; i<f.elements.length; i++ ) {
		if ( f.elements[i].type
			&& ((f.elements[i].type == "text")
				|| (f.elements[i].type == "select-one")) ) {
			n = f.elements[i].name;
			v = f.elements[i].value;
			if ( n.length ) {
				if ( n.charAt(0) == "@" ) {
					if ( (f.elements[i].type == "select-one") ) {
						if ( f.elements[i].selectedIndex == 0 ) {
							tmp = "\'";
							tmp += n.substring( 1, n.length );
							tmp += "\' は必須項目です。";
							alert( tmp );
							f.elements[i].focus();
							return false;
						}
					} else {
						if ( v.length == 0 ) {
							tmp = "\'";
							tmp += n.substring( 1, n.length );
							tmp += "\' は必須項目です。";
							alert( tmp );
							f.elements[i].focus();
							return false;
						}
					}
				}
			}
		}
	}
	return true;
}

function WMFindGoods( id ) {
	var i;
	if ( id.length > 0 ) {
		for ( i=0; i<gid.length; i++ ) {
			if ( gid[i] == id )
				return i;
		}
	}
	return -1;
}



function IsMacintoshIE()
{
	if (( navigator.vendor=='Netscape6' || navigator.appName.search('Microsoft Internet Explorer')!=-1 ) && navigator.appVersion.search('Macintosh') > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function WMSetOrderData()
{
	if(IsMacintoshIE()==false)
		return;
	if ( WMIsStayCartGoods()==false )
		return; 
	str = new String();
	if(navigator.appName.search('Microsoft Internet Explorer')!=-1)
	{
		for(i=0;i<document.forms['WMOrderInputForm'].length;i++)
		{
			if(document.forms['WMOrderInputForm'].elements[i].type!="button" &&
				document.forms['WMOrderInputForm'].elements[i].type!="submit" &&
				document.forms['WMOrderInputForm'].elements[i].type!="reset")
			{
				document.cookie=escape(document.forms['WMOrderInputForm'].elements[i].name)+"="+
				escape(document.forms['WMOrderInputForm'].elements[i].value);
			}
		}
	}
	else
	{
		for(i=document.forms['WMOrderInputForm'].length-1;i>-1;i--)
		{
			if(document.forms['WMOrderInputForm'].elements[i].type!="button" &&
				document.forms['WMOrderInputForm'].elements[i].type!="submit" &&
				document.forms['WMOrderInputForm'].elements[i].type!="reset")
			{
				document.cookie=escape(document.forms['WMOrderInputForm'].elements[i].name)+"="+
								escape(document.forms['WMOrderInputForm'].elements[i].value);
			}
		}
	}
}

function WMCanSubmitOrder()
{
	var i,orderForm,fname="WMOrderInputForm";
	for ( i=0; i<document.forms.length; i++ ) {
		if ( document.forms[i].name == fname ) {
			orderForm = document.forms[i];
			break;
		}
	}
	if ( orderForm ) {
		if(WMValidationForm( orderForm )==false)
			return false;
		if ( WMIsStayCartGoods() ) {
			return true;
		} else {
			alert("カゴは空です。");
			return false;
		}
	}
	return false;
}

function WMGetGoodsString()
{
	var i, rslt = "";
	idx = 1;
	for ( i=0; i<gid.length; i++ ) {
		if ( gnum[i] != 0 ) {
			tmp = "商品ID（" + gid[i] + "）　商品名（" + gname[i] + "）　数量（" + gnum[i] + "）　単価（" + 		
					WMFormatCurrency(gprice[i]) + "）　金額（" + WMFormatCurrency(gprice[i] * gnum[i]) + "）";
			rslt+=tmp+ "\n";
			idx++;
		}
	}
	rslt += "\n";
	rslt += "合計 : "+ WMFormatCurrency(WMGetSubtotal());
	rslt += "\n\n";
	
	return rslt;
}

function SplitCookie(str)
{
	str=unescape(str);
	strRet=new String();
	strRet=WMGetGoodsString();
	strTmp=str.split("; ");
	
	i=0;
	reNum = new RegExp("[0-9]=","i");
	while(i<strTmp.length)
	{
		if(strTmp[i].match(reNum)==null)
		{
			strRet+=strTmp[i]+"\n";
		}
		i++;
	}
	return strRet;
}

function WMInitOrderSheet()
{
	if(WMIsStayCartGoods()==false)
	{
		document.WMOrderForm.elements['ORDER'].value="";
		return;
	}
	str2=new String();
	if(IsMacintoshIE()==false)
	{
		str2=WMGetGoodsString();
		str=new String(location.search);
		arg = GetArg2();
		for(i in arg) {
			str2+=unescape2(i+" : " )+unescape2(arg[i])+"\n";
		}
		document.WMOrderForm.elements['ORDER'].value=str2;
	}
	else
	{
		str2=SplitCookie(document.cookie);
		document.WMOrderForm.elements['ORDER'].value=str2;
	}
}

function unescape2(str)
{
	sub = str;
	ret = '';
	while ( (n = sub.search(/(%u([\dA-F]{4})|%([\dA-F]{2}))/i)) >= 0 )
	{
		ret += sub.substring(0, n) +
		String.fromCharCode(parseInt('0x' + RegExp.$2 + RegExp.$3));
		sub = sub.substring(n + RegExp.$1.length, sub.length);
	}
	ret += sub;
	return ret;
}

function GetArg() {
	var        p, pstr;
	if(location.search=="")
		return;
	str = new String(window.document.applets[0].url2uni(location.search));

	p = str.indexOf("?", 0);
	if(p == -1) {
		pstr = "";
	}
	else {
		pstr = str.substring(p + 1, str.length);
	}
	return(pstr.split("&"));
}

function GetArg2() {
	arg = new Array();
	arg_ = GetArg();
	for(i in arg_) {
		str = new String(arg_[i]);
		p = str.split("=");
		if ( p[0].charAt(0) == "@" ) {
			p[0] = p[0].substring( 1, p[0].length );
		}

		arg[p[0]] = p[1];
	}
	return(arg);
}

function WMResetOrder()
{
	if(WMIsStayCartGoods()==true)
	{
		WMClearCart(0);
		return true;
	}
	else
	{
		alert("カゴが空です。");
		document.WMOrderForm.elements['ORDER'].value="";
		return false;
	}
}
