//----------  For cookies  -----------
function setCookie(_name, _value)
{
var exdate=new Date();
exdate.setTime(exdate.getTime() + (120 * 60 * 1000)); // expire in 60 mins
document.cookie=_name+ "=" + escape(_value) + "; expires = " + exdate.toGMTString();
}

function getCookie(_name)
{
	if (document.cookie.length>0)
  	{
		c_start=document.cookie.indexOf(_name + "=");
	  	if (c_start!=-1)
    		{ 
    			c_start=c_start + _name.length+1; 
    			c_end=document.cookie.indexOf(";",c_start);
    			if (c_end==-1) c_end=document.cookie.length;
    			return unescape(document.cookie.substring(c_start,c_end));
    		} 
  	}

	return '';
}


function addBasket(_game, _idx)
{
	var _name  = document.getElementById('item' + _idx +'_name').value;
	var _point = document.getElementById('item' + _idx +'_point').value;
	//var _img   = document.getElementById('item' + _idx +'_img').getAttribute('src').replace(/\//g, "\\u002f");
	var _img   = document.getElementById('item' + _idx +'_img').value;
	var _param1 = document.getElementById('item' + _idx +'_param1').value;
	var _param2 = document.getElementById('item' + _idx +'_param2').value;
	var _param3 = document.getElementById('item' + _idx +'_param3').value;
	var _param4 = document.getElementById('item' + _idx +'_param4').value;


	var _quantity = 1;

	if ( _game == "nana" )
	{
		alert ("點數暫時停止對換");
		return;
	}

	if ( _game == "dco" )
	{
		if ( _param1 == 1 )
		{
			if ( _point == 450 )
			{
				location = "shop_checkout_dco.php?dcocach=150";
			}
			else if ( _point == 600 )
			{
				location = "shop_checkout_dco.php?dcocach=200";
			}
			else
			{
				location = "shop_checkout_dco.php?dcocach=" + (_point / 3.5) ;
			}
			return;
		}
		else if ( _param1 == 2 )
		{
			location = "shop_checkout_dco_virtualpack.php?itemidx="+ _idx +"&cardid=" + _param2 + "_" + _param3 + "&usageid=" + _param4;
		}
		else
		{
			//alert (_param1);
		}
		return;
	}

	if ( _game == "nindou" )
	{
		if ( _param1 == 1 )
		{
			location = "shop_checkout_nindou.php?nindoucash="+_param2;
		}
		return;
	}

	if ( _game == "dcube" )
	{
		if ( _param1 == 1 ) 
		{
			location = "./shop_dcube/shop_checkout_dcube.php?param2="+_param2;
		}
		return;
	}
	
	if ( _game == "u1world" )
	{
		if ( _param1 == 1 ) 
		{
			location = "./shop_u1world/shop_checkout_u1world.php?idx="+_idx+"&param2="+_param2;
		}
		return;
	}
	
	if ( _game == "hlo" )
	{
		if ( _param1 == 1 ) 
		{
			 location = "./shop_hlo/shop_checkout_hlo.php?idx="+_idx+"&param2="+_param2;
			//alert ( "為了提供更好的服務質素，點數兌換暫時停止，不便之處，敬請原諒。" );
		}
		return;
	}

	if ( _game == "tartaros" )
	{
		if ( _param1 == 1 ) 
		{
			location = "./shop_tartaros/shop_checkout_tartaros.php?idx="+_idx+"&param2="+_param2;
		}
		else if ( _param1 == 2 )
		{
			location = "shop_checkout_tartaros_virtualpack.php?itemidx="+ _idx +"&cardid=" + _param2 + "_" + _param3 + "&usageid=" + _param4;
		}
		return;
	}
	
	if ( _game == "efo" )
	{
		if ( _param1 == 1 ) 
		{
			location = "./shop_efo/shop_checkout_efo.php?idx="+_idx;
		}
		return;
	}
	
	if ( _game == "sto" )
	{
		if ( _param1 == 1 ) 
		{
			location = "./shop_sto/shop_checkout_sto.php?idx="+_idx;
		}
		return;
	}
	if (_quantity > 0)
	{
		var basket_name = _game + '_basket';
		var old_basket = getCookie(basket_name);

		if (old_basket.length > 0)
		{
			var new_basket = '';
			var found = false;
			var items = old_basket.split('##');

			for(i in items)
			{
				if (items[i].length > 0)
				{
					item = items[i].split('//');
					if (item[0] == _idx)
					{
						new_item = _idx + '//' + (_quantity + parseInt(item[1])) + '//' + _name + '//' + _point + '//' + _img + '##' ;
						found = true;
						new_basket += new_item;
					}
					else
					{
						new_basket += items[i] + '##';
					}
				}
			}

			if (!found)
			{
				new_basket += _idx + '//' + _quantity + '//' + _name + '//' + _point + '//' + _img + '##' ;
			}
			setCookie(basket_name, new_basket);
		}
		else
		{
			var item;
			item = _idx + '//' + _quantity + '//' + _name + '//' + _point + '//' + _img + '##' ;
			setCookie(basket_name, item);
		}
	}

	alert('物品已加到購物籃');
	updateSummary();
}


function incBasket(idx)
{
	var p = document.getElementById( 'quantity' + idx );
	var q = parseInt ( p.innerHTML );
	++q ;

	updateBasket2(idx, q);
	
	p.innerHTML = q;

}

function decBasket(idx)
{
	var p = document.getElementById( 'quantity' + idx );
	var q = parseInt ( p.innerHTML );
	--q ;

	if (q < 0)
		q = 0;

	updateBasket2(idx, q);
	
	p.innerHTML = q;

}

function updateBasket2(idx, q)
{
	var _quantity = q;
	var _game = getCookie('currgame');
	var basket_name = _game + '_basket';
	var old_basket = getCookie(basket_name);

	if (old_basket.length > 0)
	{
		var new_basket = '';
		var items = old_basket.split('##');

		for(i in items)
		{
			if (items[i].length > 0)
			{
				var item = items[i].split('//');
				if (item[0] == idx)
				{
					if (_quantity > 0)
					{
						new_item = idx + '//' + _quantity + '//' + item[2] + '//' + item[3] + '//' + item[4] +'##' ;
						new_basket += new_item;
					}
				}
				else
				{
					new_basket += items[i] + '##';
				}
			}
		}

		setCookie(basket_name, new_basket);
	}

	updateSummary();
}


function updateBasket(idx)
{
	var _quantity = document.getElementById('quantity' + idx).value;
	var _game = getCookie('currgame');
	var basket_name = _game + '_basket';
	var old_basket = getCookie(basket_name);

	if (old_basket.length > 0)
	{
		var new_basket = '';
		var items = old_basket.split('##');

		for(i in items)
		{
			if (items[i].length > 0)
			{
				var item = items[i].split('//');
				if (item[0] == idx)
				{
					if (_quantity > 0)
					{
						new_item = idx + '//' + _quantity + '//' + item[2] + '//' + item[3] + '//' + item[4] +'##' ;
						new_basket += new_item;
					}
				}
				else
				{
					new_basket += items[i] + '##';
				}
			}
		}

		setCookie(basket_name, new_basket);
	}

	viewBasket();
	updateSummary();
}

function viewBasket()
{
	var _game = getCookie('currgame');
	var basket_name = _game + '_basket';
	var curr_basket = getCookie(basket_name);
        var ret         = '';
	var img         = '';
	var item;
	var j;

	if (curr_basket.length > 0)
	{
		var items = curr_basket.split('##');

		j = 0;
		ret += '<TABLE cellspacing="0" cellpadding="0" style="border-collapse: collapse; border-width: 1px;">'

		for(i in items)
		{
			if (items[i].length > 0)
			{
				item = items[i].split('//');
				eval  ( 'img = "' + item[4] + '"' );
				if (j % 4 == 0)
					ret += '<TR>';

				ret += '<TD style="border-width: 1px; border-style: dashed; border-color: red;"><TABLE border = 0 cellpadding =0 cellspacing = 0><TR><TD valign="top"><TABLE border = 0 cellpadding = 0 cellspacing = 0 width = 150><TR height = 100><TD align = center valign = center>';
				if (img.search(/w3k\/HAMMER/) > 0 || img.search(/w3k\/GUNLANCE/) > 0)
				{
					ret += '<IMG src="pointcard/'+ img +'" height=90 border="0">';
				}
				else
				{
					ret += '<IMG src="pointcard/'+ img +'" border="0">';
				}
				ret += '</TD></TR></TABLE></TD></TR>';		
				ret += '<TR><TD valign="top"><TABLE border = 0 cellspacing = 0 cellpadding = 0><TR><TD colspan = 2><B>' + item[2] + '</B></TD></TR><TR></TR><TD>\u50F9\u9322\uFF1A</TD><TD>' + item[3] + ' \u9EDE</TD></TR><TR><TD>\u4EF6\u6578\uFF1A</TD><TD><TABLE border = 0 cellpadding=0 cellspacing = 0><TR><TD><INPUT class=Input_White type="button" value="←" onClick="decBasket('+ item[0] +');"></TD><TD width=20 align=center><SPAN id="quantity' + item[0] +'">'+ item[1] +'</SPAN></TD><TD><INPUT class=Input_White type="button" value="→" onClick="incBasket('+ item[0] +');"></TD></TR></TABLE></TD></TR></TABLE></TD></TR></TABLE></TD>';
				if (j % 4 == 3)
					ret += '</TR>';
				++j;
			}
		}

		if (j % 4 != 0)
		{
			while (j % 4 != 0)
			{
				ret += '<TD  style="border-width: 1px; border-style: dashed; border-color: red;" width = 150>&nbsp;</TD>';
				++j;
			}
			ret += '</TR>';
		}

		ret += '</TABLE>'
	}
	else
	{
		ret = '<TR><TD>\u6C92\u6709\u7269\u54C1</TD></TR>';
	}

        ret = '<TABLE border="0" cellspacing="4" cellpadding="2">' + ret + '</TABLE>';
	document.getElementById('basket_view').innerHTML = ret;

	updateSummary();
}

//----------  For general -----------
function goShop()
{
	var game   = getCookie('currgame');
	var loc    = String(location);
	var pos    = loc.lastIndexOf("/");
	var new_location = loc.substring(0, pos);


	location = new_location + '/shop_lfo.htm';
	// location = new_location + '/shop_itemlist.php?game=' + game;
}

//----------  For summary -----------
function updateSummary()
{
	var quantity = 0;
	var total    = 0;

	var _game   = getCookie('currgame');
	var sBal    = getCookie('accbalance');

	if (sBal == '')
		balance = 0;
	else
		balance = parseInt(sBal);

	var basket_name = _game + '_basket';
	var old_basket = getCookie(basket_name);

	if (old_basket.length > 0)
	{
		var items = old_basket.split('##');

		for(i in items)
		{
			if (items[i].length > 0)
			{
				var sub_quantity;
				var sub_total;
				var item;

				item = items[i].split('//');
				sub_quantity = parseInt(item[1]);
				sub_total    = parseInt(item[3]) * sub_quantity;

				quantity += sub_quantity;
				total    += sub_total;
			}
		}
	}

	document.getElementById('basket_total').innerHTML = total;
	document.getElementById('basket_balance').innerHTML = balance;
}

//----------  For item listing -----------
function displayItem(_loc, _id, _name, _img, _point, _param1, _param2, _param3, _param4)
{
	var item = document.getElementById('item' + _loc);
	var game   = getCookie('currgame');
	var str;

	if (_img.search(/w3k\/HAMMER/) > 0 || _img.search(/w3k\/GUNLANCE/) > 0)
	{
		var str  = "";
		str += "<input type=hidden id=item" + _id + "_name value = \"" + _name + "\">";
		str += "<input type=hidden id=item" + _id + "_point value = \"" + _point + "\">";
		str += "<input type=hidden id=item" + _id + "_img value = \"" + _img + "\">";

		if ( game == "dco" )
		{
			alert("buying dco");
 			str += "<TABLE border = 0 cellspacing = 0 cellpadding = 0 width = 148><TBODY><TR height = 134><TD valign = center align = center bgcolor = #fff3a5>"
				+ "<A href=\"account/account_dco_usercash.php\"><IMG src=\"pointcard/" + _img 
				+ "\" height = 90 border = 0 ></A></TD></TR><TR><TD align = center valign = top>" 
				+ _name + "<BR>"+_point +" \u9EDE<BR></TD></TR></TBODY></TABLE>";

		}
		else
		{
 			str += "<TABLE border = 0 cellspacing = 0 cellpadding = 0 width = 148><TBODY><TR height = 134><TD valign = center align = center bgcolor = #fff3a5>"
				+ "<A href=\"javascript:alert('物品已加到購物籃');\"><IMG src=\"pointcard/" + _img 
				+ "\" height = 90 border = 0 ></A></TD></TR><TR><TD align = center valign = top>" 
				+ _name + "<BR>"+_point +" \u9EDE<BR></TD></TR></TBODY></TABLE>";
		}

	}
	else
	{
		var str  = "";

		str += "<input type=hidden id=item" + _id + "_name value = \"" + _name + "\">";
		str += "<input type=hidden id=item" + _id + "_point value = \"" + _point + "\">";
		str += "<input type=hidden id=item" + _id + "_img value = \"" + _img + "\">";
		str += "<input type=hidden id=item" + _id + "_param1 value = \"" + _param1 + "\">";
		str += "<input type=hidden id=item" + _id + "_param2 value = \"" + _param2 + "\">";
		str += "<input type=hidden id=item" + _id + "_param3 value = \"" + _param3 + "\">";
		str += "<input type=hidden id=item" + _id + "_param4 value = \"" + _param4 + "\">";

		//str += "<TABLE border = 0 cellspacing = 0 cellpadding = 0 width = 150><TBODY><TR height = 134><TD valign = center align = center bgcolor=#fff3a5>"
		//	+ "<A href=\"javascript:addBasket('" + game + "', " + _id + ");\"><DIV style=\"position:relative; width:150; height:134;\"><DIV style=\"position:relative; width:150; height:134;\"><IMG src=\"pointcard/" + _img 
		//	+ "\" border = 0 ></DIV><DIV style=\"position:absolute; top:0; left:0; z-index:4;\"><IMG SRC=\"pointcard/shop_image/common/slide_2.gif\" border = 0></DIV></DIV></A></TD></TR><TR><TD align = center valign = top>" 
		//	+ _name + "<BR>"+_point +" \u9EDE<BR></TD></TR></TBODY></TABLE>";

		str += "<DIV style=\"position:relative\">";
		str += "<DIV style=\"position:relative; top:0; left:0;\">";
		str += "<TABLE border = 0 cellspacing = 0 cellpadding = 0 width = 150><TBODY><TR height = 134><TD valign = center align = center bgcolor=#fff3a5>"
			+ "<IMG src=\"pointcard/" + _img 
			+ "\" border = 0 ></A></TD></TR><TR><TD align = center valign = top>" 
			+ _name + "<BR>"+_point +" \u9EDE<BR></TD></TR></TBODY></TABLE>";
		str += "</DIV>";
		str += "<DIV style=\"position:absolute; top:0; left:0\">";
		str += "<A href=\"javascript:addBasket('" + game + "', " + _id + ");\">" + "<IMG SRC=\"pointcard/shop_image/common/slide_2.gif\" border = 0>" + "<\A>";
		str += "</DIV>";
		str += "</DIV>";
	}

	item.innerHTML = str;
}

function cbItemList_full(_text)
{
	var info = eval( '(' + _text + ')' );

	var game   = getCookie('currgame');

	var pageinfo = info[0];
	var itemList = info[1];
	var subType  = info[2];
	var i;
	var str = '';
	var str2 = '';
	var p, p2;

	p = document.getElementById('pagenumber');

	if (pageinfo[1] == 1)
	{
		p.innerHTML = "\u4E0A\u4E00\u9801 1 \u4E0B\u4E00\u9801";
	}
	else
	{
		if (pageinfo[0] == 1)
		{
	 		str = '\u4E0A\u4E00\u9801 ';
		}
		else
		{
			str = '<a href="javascript: getItemList(\''+ game +'\', '+ (pageinfo[0] - 1) + ');">\u4E0A\u4E00\u9801</a>';
		}
			
		for(i = 1; i <= pageinfo[1]; ++i)
		{
			if (pageinfo[0] == i)
			{
				str += ' | <B>' + i + '</B>';
			}
			else
			{
				if (i == 1 || i == pageinfo[1])
				{
					str += ' | <a href="javascript: getItemList(\''+ game +'\', '+ i + ');">' + i + '</a>';
				}
				else if ( (pageinfo[0] - i) > 2 )
				{
					str += ' | ... ';
					i = pageinfo[0] - 3;
				}
				else if ( (pageinfo[0] - i) < -2 )
				{
					str += ' | ... ';
					i = pageinfo[1] - 1;
				}
				else
				{
					str += ' | <a href="javascript: getItemList(\''+ game +'\', '+ i + ');">' + i + '</a>';
				}

			}
		}

		if (pageinfo[0] == pageinfo[1])
			str += ' | \u4E0B\u4E00\u9801';
		else
			str += ' | <a href="javascript: getItemList(\''+ game +'\', ' + ( 1 + parseInt(pageinfo[0]) ) + ');">\u4E0B\u4E00\u9801</a>';

		p.innerHTML = str;
	}

	for(i = 0; i < 12; ++i)
	{
		try
		{
			var item = document.getElementById('item' + i);
			item.innerHTML = "&nbsp;";
		}
		catch ( err )
		{

		}
	}

	if (itemList.length == 0)
	{
		var item = document.getElementById('item1');
		item.innerHTML = "\u66AB\u6642\u6C92\u6709\u8CA8\u500B\u53EF\u4F9B\u8CFC\u8CB7";
	}

	for(i in itemList)
	{
		displayItem(i, itemList[i]['idx'], itemList[i]['name'], itemList[i]['img'], itemList[i]['point'], itemList[i]['param1'], itemList[i]['param2'], itemList[i]['param3'], itemList[i]['param4']);
	}

	getRanking(game);

}

function getItemList2(_game, _page, _subtype, _name)
{
	setCookie('subtypename', _name);
	setCookie('subtype', _subtype);
	getURL = 'pointcard/itemlist.php?game='+_game+'&page='+_page+'&subtype='+_subtype;

	ajaxGetText(getURL, cbItemList);

}

function getItemListSearch(_game, _keyword, _page)
{
	// getURL = 'pointcard/itemlist_full.php?game='+_game+'&page='+_page;
        getURL = 'pointcard/itemlist_full.php?game='+_game+'&page='+_page+'&key='+_keyword;
	ajaxGetText(getURL, cbItemList_full);
}

function getItemList(_game, _page)
{
	var searchKey   = getCookie('searchKey');
	getItemListSearch(_game, searchKey, _page);
}

function Search()
{
	var _game = getCookie('currgame');
	var p = document.getElementById('sSearchKeyword');
	var keyword = p.value;

	if ( keyword.length == 0 )
	{
		return;
	}
	else if ( keyword.length < 2 )
	{
		alert("請輸入兩個字以上的搜尋字");
		return;
	}
	else
	{
		setCookie('searchKey', keyword);
		getItemListSearch(_game, keyword, 1);
	}
}

//----------  For ranking -----------
function cbRanking(text)
{
	var chinese_digit = '\u4E00\u4E8C\u4E09\u56DB\u4E94\u516D\u4E03\u516B\u4E5D\u5341';
	var rank = eval( '(' + text + ')' );
	var p, i, j, str, r;

	str = '';
	j = 0;

	try
	{
		p = document.getElementById('rankView');

		str += '<TABLE border="0" cellspacing="0" cellpadding="0" width="274" background="./image/ranking/border_02a.gif">';
		str += '<TR><TD colspan="5"><IMG src="./image/ranking/border_01a.gif"></TD></TR>';
                
		for (i in rank)
		{
			r = chinese_digit.substring (j, j + 1);

                        str += '<TR><TD width="5"></TD><TD>第' + r + '位</TD><TD>' + rank[i].name + '</TD><TD align="right">' + rank[i].u1points +'</TD><TD width="20"></TD></TR>';

			++j;
		}

		str += '<TR><TD colspan="5"><IMG src="./image/ranking/border_03a.gif"></TD></TR>';
		str += "</TABLE>";

		p.innerHTML = str;
	}
	catch (err)
	{
		alert ( "Ranking Errr" );
	}
}

function getGameRanking()
{
	var game   = getCookie('currgame');
	getRanking(game);
}

function getRanking(_game)
{
	getURL = 'pointcard/shop_top_ten.php?game=' + _game ;
	ajaxGetText(getURL, cbRanking);
}

//----------  For item description -----------
function cbItemDesc(_text)
{
	var desc = eval( '(' + _text + ')' );
	var game   = getCookie('currgame');

	p = document.getElementById('name');
	p.innerHTML = desc['name'];
	p = document.getElementById('u1point');
	p.innerHTML = desc['u1point'];
	p = document.getElementById('desc');
	p.innerHTML = desc['desc'];
	p = document.getElementById('img');
	
	if (desc['img'].search(/w3k\/HAMMER/) > 0 || desc['img'].search(/w3k\/GUNLANCE/) > 0)
	{
		p.innerHTML = '<table border = 0 width = 150 cellpadding = 0 cellspacing = 0><tr height = 134><td  background="./image/shop/slide.jpg" align = center valign = center><IMG id = item_img src="'+  desc['img'] +'" height = "90" border="0"></td></tr></table>';
	}
	else
	{
		p.innerHTML = '<table border = 0 width = 150 cellpadding = 0 cellspacing = 0><tr height = 134><td  background="./image/shop/slide.jpg" align = center valign = center><IMG id = item_img src="'+  desc['img'] +'" border="0"></td></tr></table>';
	}

	getRanking(game);
}

function getItemDesc(_game, _idx)
{
	getURL = 'pointcard/itemdesc.php?game='+_game+'&idx='+_idx;
	ajaxGetText(getURL, cbItemDesc);
}

function setGame(_game)
{
	var p1, p2, p3, p4;

	p1 = document.getElementById('lfo_but');
	p2 = document.getElementById('w3k_but');
	p3 = document.getElementById('nana_but');
	p4 = document.getElementById('dco_but');

	p1.src = "./image/tab/lfo_off.gif";
	p2.src = "./image/tab/w3k_off.gif";
	p3.src = "./image/tab/nana_off.gif";
	p4.src = "./image/tab/dco_off.gif";

	if (_game == 'lfo')
	{
		p1.src = "./image/tab/lfo_items.gif";
	}
	else if (_game == 'w3k')
	{
		p2.src = "./image/tab/w3k_items.gif";
	}
	else if (_game == 'nana')
	{
		p3.src = "./image/tab/nana_items.gif";
	}
	else
	{
		p4.src = "./image/tab/dco_items.gif";
	}

	setCookie('currgame', _game);
	setCookie('searchKey', '');
	getItemList(_game, 1);
	updateSummary();
}

