$(document).ready(function(){
	$('select#id_country').change(function(){
		updateState();
	}).change();
	
	
	$('select#id_state').change(function(){
		updateCounty();
	}).change();
	


 
//$(function(){
//  $("select#id_srotcCollState").function(){
//    $.getJSON("/schools.php",{id: $(this).val(), ajax: 'true'}, function(j){
//      var options = '';
//      for (var i = 0; i < j.length; i++) {
//        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
//      }
//      $("select#id_srotcCollState").html(options);
//    })
//  })
//})
});
function sortList()
{
var lb = document.getElementById('id_state');
arrTexts = new Array();
arrValues = new Array();
arrOldTexts = new Array();

for(i=0; i<lb.length; i++)
{
arrTexts[i] = lb.options[i].text;
arrValues[i] = lb.options[i].value;

arrOldTexts[i] = lb.options[i].text;
}

arrTexts.sort();

for(i=0; i<lb.length; i++)
{
lb.options[i].text = arrTexts[i];
for(j=0; j<lb.length; j++)
{
if (arrTexts[i] == arrOldTexts[j])
{
lb.options[i].value = arrValues[j];
j = lb.length;
}
}
}
}



function updateState()
{
	setTimeout('updateCounty()',300);
	$('select#id_state option:not(:first-child)').remove();
		var states = countries[$('select#id_country').val()];
		//states.sort();
		if( typeof(states) != 'undefined' )
		{
		
			for (indexState in states)
			{
				//ie bug fix
				if (indexState != 'indexOf')
					$('select#id_state').append('<option value="'+indexState+'"'+ (idSelectedCountry == indexState ? ' selected="selected' : '') + '">'+states[indexState]+'</option>');
			}
			$('p.id_state:hidden').slideDown('slow');
		}
		else
			$('p.id_state').slideUp('fast');
		
		sortList();	
}

function updateCounty()
{
$('select#id_county option:not(:first-child)').remove();	
var currentStateVal = $('select#id_state').val();
var currentCountryVal = $('selected#id_country').val();
     if (currentStateVal == '9'){
        $('select#id_county option').remove();
     	$('p.id_county:hidden').slideDown('slow');
     	$('select#id_county').append(countiesList);
     	
     	
     }
	 else {
	 	    
			$('p.id_county').slideUp('fast');
			$('select#id_county option').remove();	
			$('select#id_county').append('<option value="">-</option>');
			
}
}



