var AmountObj;

function LoadServers()
{
	if ($('Games'))
	{
		Game = $('Games').value;
		if (Game != 0)
		{
			var MyAjax = new Ajax.Request
			(
				'/Ajax/ExpressCheckout.php',
				{
					method: 'post',
					parameters: 'Action=LoadServers' + 
								'&GameId=' + Game,
					onSuccess: LoadServersSuccess,
					onFailure: Failure
				}
			);
		}
		else
		{
			var i, NoOptions, NewOption, Select = $('Servers');
			while (Select.options.length > 1)
				Select.removeChild(Select.options[Select.options.length - 1]);
				
			var i, NoOptions, NewOption, Select = $('Blocks');
			while (Select.options.length > 1)
				Select.removeChild(Select.options[Select.options.length - 1]);
				
			$('BlockPrice').innerHTML = '';	
			if ($('TotalText'))
			{
				$('TotalText').innerHTML = '';
			}
		}
	}
}

function LoadServersSuccess(response)
{
	eval('var Result=' + response.responseText);
	
	if (Result.Success)
	{
		//reloading the blocks dropdown
		var i, NoOptions, NewOption, Select = $('Servers');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);

		for (i = 0; i < Result.Data.length; i++)
		{
			NewOption = document.createElement('OPTION');
			NewOption.value = Result.Data[i].Id;
			NewOption.innerHTML = Result.Data[i].Name;
			NewOption.setAttribute('redlink', Result.Data[i].Url);
			Select.appendChild(NewOption);
		}
	
		if ($('SelectedServer') && $('SelectedServer').value)
		{
			Select.value = $('SelectedServer').value;
			$('SelectedServer').value = '0';
			LoadBlocks();
		}	
		
		var i, NoOptions, NewOption, Select = $('Blocks');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);
			
		$('BlockPrice').innerHTML = '';
		if ($('TotalText'))
		{
			$('TotalText').innerHTML = '';
		}
		
	}
}

function LoadBlocks()
{
	Server = $('Servers').value;
	
	if (Server != 0)
	{
		var MyAjax = new Ajax.Request
		(
			'/Ajax/ExpressCheckout.php',
			{
				method: 'post',
				parameters: 'Action=LoadBlocks' + 
							'&ServerId=' + Server +
							'&CouponId=' + ($('CouponId') ? $('CouponId').value : 0),
				onSuccess: LoadBlocksSuccess,
				onFailure: Failure
			}
		);
	}
	else
	{
		var i, NoOptions, NewOption, Select = $('Blocks');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);
			
		$('BlockPrice').innerHTML = '';
		if ($('TotalText'))
		{
			$('TotalText').innerHTML = '';
		}
	}
}

function LoadBlocksSuccess(response)
{
	eval('var Result=' + response.responseText);
	
	if (Result.Success)
	{
		AmountObj = Result.Data;
		//reloading the blocks dropdown
		var i, NoOptions, NewOption, Select = $('Blocks');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);

		for (i = 0; i < Result.Data.length; i++)
		{
			NewOption = document.createElement('OPTION');
			NewOption.value = Result.Data[i].Id;
			NewOption.innerHTML = Result.Data[i].NameText + '&nbsp;&nbsp;&nbsp;&nbsp;' + Result.Data[i].PriceText;

			Select.appendChild(NewOption);
		}
		if ($('SelectedBlock') && $('SelectedBlock').value)
		{
			Select.value = $('SelectedBlock').value;
			$('SelectedBlock').value = '0';
		}
		LoadPrice();
	}
}


function Buy()
{
	if ($('Games').value == 0)
	{
		return false;
	}
	if ($('Servers').value == 0)
	{
		window.location = $('Games').options[$('Games').selectedIndex].getAttribute('redlink');
		return false;
	}
	if ($('Blocks').value == 0)
	{
		window.location = $('Servers').options[$('Servers').selectedIndex].getAttribute('redlink');
		return false;
	}
	
	var MyAjax = new Ajax.Request
	(
		'/Ajax/CartCmd.php',
		{
			method: 'get',
			parameters: 'Action=Buy' + 
						'&ServerId=' + $('Servers').value +
						'&Block=' + $('Blocks').value +
						'&ExpressCheck=1' + 
						'&CouponCode=' +  ($('CouponCode') ? $('CouponCode').value : ''),
			onSuccess: BuySuccess,
			onFailure: Failure
		}
	);
}

function BuySuccess(response)
{
	eval('var Result=' + response.responseText);

	if (Result.Success)
	{
		window.location = Result.Link;
	}
	else
	{
		$('BlockPrice').className = 'error';
		$('BlockPrice').innerHTML = Result.Message;
	}
}

function LoadPrice()
{
	if ($('Blocks').value != 0)
	{
		$('BlockPrice').className = '';
		for (i = 0; i < AmountObj.length; i++)
		{
			if (($('Blocks').value) == AmountObj[i].Id)
			{
				if ($('CouponId') && AmountObj[i].AcceptsCoupon)
				{
					$('BlockPrice').innerHTML = '<b>' + AmountObj[i].Price + '</b><br />' + AmountObj[i].DiscountDetail;
					$('TotalText').innerHTML = AmountObj[i].TotalPrice;
				}
				else
				{
					$('BlockPrice').innerHTML = AmountObj[i].Price;
					if ($('TotalText'))
						$('TotalText').innerHTML = '';
				}
			}
		}
	}
	else
	{
		$('BlockPrice').innerHTML = '';
		if ($('TotalText'))
		{
			$('TotalText').innerHTML = '';
		}
	}
}
