function LoadSellServers()
{
	Game = $('Sell_Games').value;
	if (Game != 0)
	{
		var MyAjax = new Ajax.Request
		(
			'/Ajax/ExpressCheckout.php',
			{
				method: 'post',
				parameters: 'Action=SellServers' + 
							'&GameId=' + Game,
				onSuccess: LoadSellServersSuccess,
				onFailure: Failure
			}
		);
	}
	else
	{
		var i, NoOptions, NewOption, Select = $('Sell_Servers');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);
		
		var i, NoOptions, NewOption, Select = $('Sell_Blocks');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);
			
	}
	
	$('Sell_Blocks').disabled = false;
	if (Blocks[Game])
	{
		if (Blocks[Game].length == 0)
		{
			$('Sell_Blocks').disabled = true;
		}
	}
}

function LoadSellServersSuccess(response)
{
	eval('var Result=' + response.responseText);
	
	if (Result.Success)
	{
		//reloading the blocks dropdown
		var i, NoOptions, NewOption, Select = $('Sell_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;
			Select.appendChild(NewOption);
		}
		
		
		var i, NoOptions, NewOption, Select = $('Sell_Blocks');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);
	}
}

function LoadSellBlocks()
{
	Game = $('Sell_Games').value;
	if (Blocks[Game])
	{
		var i, NoOptions, NewOption, Select = $('Sell_Blocks');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);

		for (i = 0; i < Blocks[Game].length; i++)
		{
			NewOption = document.createElement('OPTION');
			NewOption.value = Blocks[Game][i];
			NewOption.innerHTML = Blocks[Game][i];

			Select.appendChild(NewOption);
		}
	}
	else
	{
		var i, NoOptions, NewOption, Select = $('Sell_Blocks');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);
			
		for (i = 0; i < DefaultAmount.length; i++)
		{
			NewOption = document.createElement('OPTION');
			NewOption.value = DefaultAmount[i];
			NewOption.innerHTML = DefaultAmount[i];

			Select.appendChild(NewOption);
		}
	}
	//alert(Blocks_eval(Game))
	//if (Blocks_Game)
}

function Sell()
{
	CEmail = Trim($('SellEmail').value);
	CMessId = ($('SellMessId').value != MessDefaultText) ? $('SellMessId').value : '';
	CMessType = '';
	$('SellError').className = 'error';
	$('SellError').innerHTML = '';
	
	if ($('Sell_MSN').checked)
	{
		CMessType = 'MSN';
	}
	else if ($('Sell_Yahoo').checked)
	{
		CMessType = 'Yahoo';
	}
	else if ($('Sell_AOL').checked)
	{
		CMessType = 'AOL';
	}
	else if ($('Sell_ICQ').checked)
	{
		CMessType = 'ICQ';
	}
	
	
	CGameId = $('Sell_Games').value;
	CServerId = $('Sell_Servers').value;
	CAmount = $('Sell_Blocks').value;
	CComments = $('Comments').value;
	
	$('EmailError').style.visibility = ((CEmail == '') || !CheckEmail(CEmail))? 'visible': 'hidden';
	$('MessError').style.visibility = (CMessId && (CMessType == ''))? 'visible': 'hidden';
	$('GameError').style.visibility = (CGameId == 0)? 'visible': 'hidden';
	$('ServerError').style.visibility = (CServerId == 0)? 'visible': 'hidden';
	$('CurrError').style.visibility = (CAmount == 0)? 'visible': 'hidden';

	if (CEmail == '') 
	{
		$('SellError').innerHTML = EmptyStringSubmit;
		return false;
	}

	if (!CheckEmail(CEmail))
	{
		$('SellError').innerHTML = LongEmailError;
		return false;
	}
	if (CMessId && CMessType == '')
	{
		$('SellError').innerHTML = EmptyStringSubmit;
		return false;
	}
	if (CGameId == 0)
	{
		$('SellError').innerHTML = EmptyStringSubmit;
		return false;
	}
	if (CServerId == 0)
	{
		$('SellError').innerHTML = EmptyStringSubmit;
		return false;
	}
	if (CAmount == 0)
	{
		$('SellError').innerHTML = EmptyStringSubmit;
		return false;
	}
	
	var MyAjax = new Ajax.Request
		(
			'/Ajax/SendEmails.php',
			{
				method: 'get',
				parameters: 'Action=Sell' + 
							'&Email=' + CEmail +
							'&MessId=' + CMessId +
							'&MessType=' + CMessType +
							'&GameId=' + CGameId +
							'&ServerId=' + CServerId +
							'&Amount=' + CAmount +
							'&Comment=' + encodeURIComponent(CComments) +
							'&bb=' + encodeURIComponent($('fbMark').value),
				onSuccess: SellSuccess,
				onFailure: Failure
			}
		);
}

function SellSuccess(response)
{
	eval('var Result=' + response.responseText);
	if (Result.Success)
	{
		$('SellDiv').style.display = 'none';
		$('MainDiv').style.display = 'none';
		$('SuccessDiv').style.display = 'block';

		if (Result.bbin && !(Result.bbin.length < 1))
		{
			var ifrm = document.getElementById('PageContent');
	        ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
	        ifrm.document.open();
	        ifrm.document.write('<html><body><script type="text/javascript">var s_bbin = "' + Result.bbin + '";var s_bbin_encoded = "' + Result.enbbin + '";var io_operation = "ioEnd";var io_bbout_element_id = "ioBlackBox";var io_install_stm = false;var io_wsdl_url = "' + Result.IovWSDL + '";</script><script type="text/javascript" src="' + Result.IovJS + '"></script></body></html>');
	        ifrm.document.close();
		}
	}
	else
	{
		$('SellError').innerHTML = Result.Message;
		$('SellError').className = 'error';
	}
}

