//Event.observe(window, 'load', LoadServers, true);
var LastGame = 0;

function LoadServers()
{
	if ($('woweu').checked)
	{
		GameId = 66;
	}
	else if ($('wowus').checked)
	{
		GameId = 65;
	}
	else
	{
		GameId = 0;
	}
	
	if ((GameId != 0) && (GameId != LastGame))
	{
		var MyAjax = new Ajax.Request
		(
			'/PriceCompare/Ajax/LoadPrices.php?',
			{
				method: 'post',
				parameters: 'Action=LoadServers' + 
							'&GameId=' + GameId,
				onSuccess: LoadServersSuccess,
				onFailure: Failure
			}
		);
	}
	
	LastGame = GameId;
}

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

		for (i = 0; i < Result.Data.Servers.length; i++)
		{
			NewOption = document.createElement('OPTION');
			NewOption.value = Result.Data.Servers[i].Id;
			NewOption.innerHTML = Result.Data.Servers[i].Name;
			Select.appendChild(NewOption);
		}
		
		var i, NewOption, Select = $('Blocks');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);
		
		var i, NewOption, Select = $('Realms');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);

		for (i = 0; i < Result.Data.Realms.length; i++)
		{
			NewOption = document.createElement('OPTION');
			NewOption.value = Result.Data.Realms[i].Id;
			NewOption.innerHTML = Result.Data.Realms[i].Name;
			Select.appendChild(NewOption);
		}
		
		$('PriceTable').innerHTML = '';
		$('CurrencyName').innerHTML = Result.Data.Currency;
	}
}

function Failure()
{
	//alert(2)
	//$('GeneralMessage').innerHTML = ConnectionError;
}

function Compare()
{
	GameText = '';
	ServerText = '';
	BlockText = '';
	
	if ($('woweu').checked)
	{
		GameText = 'wow-eu';
	}
	else if ($('wowus').checked)
	{
		GameText = 'wow-us';
	}
	else
	{
		GameText = '';
	}
	
	SerSel = $('Servers');
	if ($('Servers').value)
	{
		for(var i=0;i<SerSel.options.length;i++)
		{
			if (SerSel.options[i].selected)
				ServerText = SerSel.options[i].text;
		}
	}
	
	BlockSel = $('Blocks');
	if ($('Blocks').value)
	{
		for(var i=0;i<BlockSel.options.length;i++)
		{
			if (BlockSel.options[i].selected)
				BlockText = BlockSel.options[i].text;
		}
	}
	
	RealmSel = $('Realms');
	if ($('Realms').value)
	{
		for(var i=0;i<RealmSel.options.length;i++)
		{
			if (RealmSel.options[i].selected)
				RealmText = RealmSel.options[i].text;
		}
	}
	
	if ($('Realms').value != 0)
		Url = '/pricecomparison/' + GameText + '/' + ServerText + '/' + RealmText + '/' + BlockText + '/' + $('Servers').value;
	else if ($('Realms').value == 0 && (RealmSel.options.length <= 1))
		Url = '/pricecomparison/' + GameText + '/' + ServerText + '/' + BlockText + '/' + $('Servers').value;
	else
	{	
		$('NoBlocksError').innerHTML = 'Please select a faction.';
		$('PriceTable').innerHTML = '';
		return;
	}
		
	if (GameText == '')
	{	
		$('NoBlocksError').innerHTML = 'Please select a game.';
		$('PriceTable').innerHTML = '';
		return;
	}
		
	if ($('Servers').value == 0)
	{	
		$('NoBlocksError').innerHTML = 'Please select a server.';
		$('PriceTable').innerHTML = '';
		return;
	}
	
	if ($('Blocks').value == 0)
	{	
		$('NoBlocksError').innerHTML = 'Please select a block.';
		$('PriceTable').innerHTML = '';
		return;
	}
		
		
	Url = Url.toLowerCase().replace(/\s/g, '-');
	window.location = Url;
	//alert($('Servers').options[$('Servers').value]);
}

function LoadBlocks()
{
	Server = $('Servers').value;
	Realm = $('Realms').value;
	if (Server != 0)
	{
		var MyAjax = new Ajax.Request
		(
			'/PriceCompare/Ajax/LoadPrices.php?',
			{
				method: 'post',
				parameters: 'Action=LoadBlocks' + 
							'&ServerId=' + Server +
							'&RealmId=' + Realm,
				onSuccess: LoadBlocksSuccess,
				onFailure: Failure
			}
		);
	}	
}

function LoadBlocksSuccess(response)
{
	eval('var Result=' + response.responseText);
	if (Result.Success)
	{
		var i, 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];
			NewOption.innerHTML = Result.Data[i];
			Select.appendChild(NewOption);
		}
	}
	else
	{
		$('NoBlocksError').innerHTML = Result.Message;
		var i, NewOption, Select = $('Blocks');
		while (Select.options.length > 1)
			Select.removeChild(Select.options[Select.options.length - 1]);
	}
}