function buyPack(type)
{
	if ( type == "clintz" )
	{
		// Clintz pack has been disabled - both here and in the server code :)
		return;
		if ( playerCredits < 10 ) alert( getString( _("Unable to purchase a Clintz Pack because you only have @0@ credits, you are short by @1@ credits."), [playerCredits, 10 - playerCredits] ) );
		else
		{
			kill();
			darkenPopup( confirmPackPurchase.pass(type) );
		}
	}
	else if ( type == "fulldeck" )
	{
		if ( playerCredits < 50 ) alert( getString( _("Unable to purchase a Full Deck pack because you only have @0@ credits, you are short by @1@ credits."), [playerCredits, 50 - playerCredits] ) );
		else
		{
			kill();
			darkenPopup( confirmPackPurchase.pass(type) );
		}
	}
	else if ( type == "action" || type == "cool" || type == "danger" || type == "newbloods" || type == "rainbow")
	{
		if ( playerCredits < 20 ) alert( getString( _("Unable to purchase a Booster pack because you only have @0@ credits, you are short by @1@ credits."), [playerCredits, 20 - playerCredits] ) );
		else
		{
			kill();
			darkenPopup( confirmPackPurchase.pass(type) );
		}
	}
}

// Handle the changes on the shop auto deck checkbox, call ajax to change it.
function handleShopAutoDeckCheckboxChange()
{
	var shopAutoDeck = this.getProperty("checked");
	
	new Request({
		'url': "/ajax/shop/set_autodeck.php",
		'data': { shop_autodeck: shopAutoDeck },
		'onRequest': function () { $('shopAutoDeckLoading').setStyle('visibility', 'visible'); },
		'onComplete': function () { $('shopAutoDeckLoading').setStyle('visibility', 'hidden'); }
	}).send();	
}

function updateTotalAmount(cost)
{
	$('totalAmount').set('html',  cost * $('packAmount').get('value') );
}

function confirmPackPurchase(type)
{
	var imgWidth = 350;
	var imgHeight = 200;
	var tmpLeft = window.getScrollLeft() + (window.getWidth() - imgWidth) / 2;
	var tmpTop = window.getScrollTop() + (window.getHeight() - imgHeight) / 2;
	
	var confirmationDiv = new Element("div", {
		"id": "shopConfirmationDiv",
		"styles": {
			position: "absolute",
			textAlign: "center",
			padding: "4px",
			top: tmpTop + "px",
			left: tmpLeft + "px",
			width: imgWidth + "px",
			color: "#fff",
			backgroundColor: "#000",
			zIndex: 1001
		},
		"class": "boldText"
	});
	
	var confirmationDivContent = "<img src=" + staticsHost + "/img/v2/shop/" + type + ".jpg" + " style=\"width: 120px; height: 90px; border: 1px solid #888;\" />";
	confirmationDivContent += "<div id=\"confirmationDivContent\">";
	
	var cost = 0;
	if ( type == "clintz" )
	{
		confirmationDivContent += _("One Clintz pack cost 10 credits.");
		cost = 10;
	}
	else if ( type == "fulldeck" )
	{
		confirmationDivContent += _("One Full Deck pack cost 50 credits.");
		cost = 50;
	}
	else 
	{
		confirmationDivContent += getString(_("One @0@ booster cost 20 credits."), [type.ucfirst()]);
		cost = 20;
	}



	confirmationDivContent += "<br/>";
	
	confirmationDivContent += _("How many do you want to buy ?");
	
	confirmationDivContent += '<br/><br/>';
	confirmationDivContent += '<div class="mediumText" style="background-color: #222;">';
	confirmationDivContent += '<select name="packAmountSelect" id="packAmount" onChange="updateTotalAmount(' + cost + ')">';
	var packAmountOptions = [1, 2, 3, 4, 5];
	for (var i = 0; i < packAmountOptions.length; ++i)
	{
		if ( (packAmountOptions[i] * cost) <= playerCredits )
		{
			confirmationDivContent += '<option value="' + packAmountOptions[i] + '">' + packAmountOptions[i];
		}
	}
	confirmationDivContent += '</select> ';
	confirmationDivContent += getString(_("pack(s) for @0@ credits"), ['<span id="totalAmount">' + cost + '</span>']);
	confirmationDivContent += '</div>';
	confirmationDivContent += "<br/>";
	confirmationDivContent += "<a href=javascript:lastStepPackPurchase('" + type + "'); class=\"bigText\">" + _("Buy").ucfirst() + "</a>";
	confirmationDivContent += " - ";
	confirmationDivContent += "<a href=javascript:cancelPackPurchase(); class=\"bigText\">" + _("Cancel").ucfirst() + "</a>";

	confirmationDivContent += "</div>";
	confirmationDiv.set('html',  confirmationDivContent );
	
	getDarkDiv().getParent().adopt( confirmationDiv );
}

function cancelPackPurchase()
{
	$("shopConfirmationDiv").dispose();
 	getDarkDiv().setStyle("display", "none");
}


function lastStepPackPurchase( type )
{
	var amount = 1;
	if ( $chk($('packAmount')) ) amount = $('packAmount').get('value');
	
	$("confirmationDivContent").set('html', "<img src=" + staticsHost + "/img/loading_negative.gif />");
	
	window.location = "/premium/buy_pack.php?amount=" + amount + "&type=" + type;
}

function setShopCountry(countryCode, firstTime)
{
	var myAjax = new Request({
		'url': "/ajax/get_country_billing.php",
		'data': { countryCode: countryCode }, 
		'onComplete': parseShopAjaxAnswer
	});

	myAjax.send();
}

function openCountrySelector()
{
	if ( $('countrySelectionDiv').getStyle('display') == 'block' )
	{
		$('countrySelectionDiv').setStyle('display', 'none');
	}
	else
	{
		if ( $('countrySelectionDiv').getElement('div').innerHTML == '' )
		{
			var content = '';
			for (var i = 0; i < countries.length; ++i)
			{
				content += '<a href="javascript:setShopCountry(\'' + countries[i]['code'] + '\');">';
				content += getFlagSpriteTag(countries[i]['code'], 24, false, Array('shopFlagImg', 'shopFlags'));
				content += '</a>';
			}
		
			$('countrySelectionDiv').getElement('div').set('html', content);
		}
	
		$('countrySelectionDiv').setStyle('display', 'block');
	}
}

function parseShopAjaxAnswer(responseText)
{
	var values = responseText.split(";");
	var countryCode = values[0];
	var countryName = values[1];

	getFlagSprite(countryCode, 24, false, Array('shopFlagImg'), 'shopFlag').replaces( $("shopFlag") );
	$("shopCountryName").set('html', countryName);

	for (var i = 0; i < billingOptions.length; ++i)
	{
		var support = values[i + 2];

		$("shop" + billingOptions[i]).removeEvents();

		if ( support == 1 )
		{
			$("shop" + billingOptions[i]).optionID = i;
			$("shop" + billingOptions[i]).src = staticsHost + "/img/shop/ui/" + billingOptions[i] + "_clair.gif?v2";
			$("shop" + billingOptions[i]).addEvent('mouseover', function () {
				this.src = staticsHost + "/img/shop/ui/" + billingOptions[this.optionID] + "_rollon.gif?v2";
				this.style.cursor = "pointer";
				popup( billingExplanations[this.optionID], 200 ); 
			});
			$("shop" + billingOptions[i]).addEvent('mouseout', function () {
				this.src = staticsHost + "/img/shop/ui/" + billingOptions[this.optionID] + "_clair.gif?v2";
				this.style.cursor = "default";
				kill();
			});
			$("shop" + billingOptions[i]).addEvent('mouseup', function () {
				window.location = "/shop/" + billingPages[this.optionID] + '?countryCode=' + countryCode + '&billingOption=' + billingOptions[this.optionID];
			});
		}
		else
		{
			$("shop" + billingOptions[i]).src = staticsHost + "/img/shop/ui/" + billingOptions[i] + "_sombre.gif?v=2";
			$("shop" + billingOptions[i]).addEvent('mouseover', function() {
				popup( _("This billing option is unfortunatly not available yet in your country. This may change in the future, so check back often."), 200 );
			});
			$("shop" + billingOptions[i]).addEvent('mouseout', function() {
				kill();
			});
		}
	
	}

	if ( $("countrySelectionDiv") ) $('countrySelectionDiv').setStyle('display', 'none');
}
