
var country;

window.addEvent('domready', function(){

	country = $('cboAdCountry');
	county = $('cboAdCounty');

	if (country != null)
	{
		country.addEvent('change', PopulateCounty);
		//PopulateCounty();
	}

	if (county != null)
	{
		county.addEvent('change', ShowCounty);
		//ShowCounty();
	}
});

function PopulateCounty()
{
	if (country == null)
		return;
	
	clearCombo (county);
	AddToOptionList (county, "0", "Loading...");

	AJAXrequest (countyUrl + '?c=' + country.value, ajaxRet);
}

function ajaxRet(obj)
{
	if (obj.status == '200')
	{
		clearCombo (county);
		var html = obj.responseText;
		if (html != '')
		{
			var lines = html.split('\r\n');
			for (var i = 0; i < lines.length; i++)
			{
				if (lines[i] != '')
				{
					var opt = document.createElement("OPTION");
					var xx = lines[i].split(',');
					opt.value = xx[0];
					opt.text = xx[1];
							
					AddToOptionList (county, xx[0], xx[1]);
					
					if (i == 0)
					{
						$('regionText').innerText = xx[2];
					}
				}
			}
			if (!constrain)
				AddToOptionList (county, '0', ' ADD YOUR REGION');
				$('regionText').innerText = 'Region';
		}
		else
		{
			if (!constrain)
			AddToOptionList (county, '0', ' ADD YOUR REGION');
			$('regionText').innerText = 'Region';
		}
	}
	else
	{
		clearCombo (county);
		AddToOptionList (county, "-1", "Error loading regions");
		$('regionText').innerText = 'Region';
	}

	if ($('CountryContainer'))
	{
		if (country.value == '0')
		{
			$('CountryContainer').style.display = 'block';
			$('fCountry').style.border = '1px solid #ff0000';
			$('fCountry').style.backgroundColor = '#FFCCCC';
			$('addCountry').style.color = '#ff0000';
			
		}
		else
		{
			$('CountryContainer').style.display = 'none';
		}
	}
		
	ShowCounty();
}

function ShowCounty()
{
	if (county == null)
		return;

	if ($('CountyContainer'))
	{
		if (county.value == '0')
		{
			
			$('CountyContainer').style.display = 'block';
			$('fCounty').style.border = '1px solid #ff0000';
			$('fCounty').style.backgroundColor = '#FFCCCC';
			$('addCounty').style.color = '#ff0000';
		}
		else
		{
			$('CountyContainer').style.display = 'none';
		}
	}
}

