// init displays
var bShowCountry = false;
var bShowState = false;
var bShowCurrency = false;
var bShowSubmit = false;

function showHideControls(){
	//console.log("showHideControls: Cty: %s, Sta: %s, Cur: %s, Sub: %s", bShowCountry, bShowState, bShowCurrency, bShowSubmit );
		
	$("#regionBlock").show();
	
	if ( bShowCountry ) 
		$("#countryBlock").show();
	else
		$("#countryBlock").hide();

	if ( bShowState )
		$("#stateBlock").show();
	else
		$("#stateBlock").hide();

	if ( bShowCurrency )
		$("#currencyBlock").show();
	else
		$("#currencyBlock").hide();

	if ( bShowSubmit )
		$("#submitBlock").show();
	else
		$("#submitBlock").hide();
		
}	

function LoadForRegion(iRegion){

	// upd display
	$('#listCurrency').val('');
	//$('#countryList').val('');

	var formval = { regionid: iRegion };
	//console.log("LoadForRegion: %o", formval );
	$.post("request_processor.cfm", 
			formval, 
			function(response){
				$("#countryList").html(response);
				
				var iCountry = $("#countryList").val();
				//console.log("LoadForRegion: Cty Loaded %d", iCountry );
				if ( iCountry > 0 ){
					LoadForCountry(iCountry);
				}
				//console.log("call showHideControls: LfRp" );				
				showHideControls();

			},
			"html");

	bShowCountry = true;
	bShowState = false;
	bShowCurrency = false;
	bShowSubmit = false;
	
}

function LoadForCountry(iCountry){

	$('#currencyLabel').html('');
	var formval = { getcurrency: iCountry};
	//console.log("LoadForCountry: %o", formval );
	$.post("request_processor.cfm", 
			formval, 
			function(response){
				$('#currencyLabel').html(response);
			},
			"html");

	bShowCurrency = true;

	var formval = { countryid: iCountry};	
	//console.log("LoadForCountry: %o", formval );
	$.post("request_processor.cfm", 
			formval, 
			function(response){
				$('#stateList').html(response);
				
				var iState = $("#stateList").val();
				if ( iState > 0 ){
					LoadForState(iState);
				}
				//console.log("call showHideControls: LfCp" );
				showHideControls();

			},
			"html");

	if (iCountry == 38 || iCountry == 226 || iCountry == 13){

		bShowState = true;
		bShowSubmit = false;
	
		if( sBAMLabelProv === undefined){
			sBAMLabelProv = "Your Province";
			sBAMLabelState = "Your State";
		}
		if (iCountry == 38){
			$("#labelStates").text(sBAMLabelProv);
		} else {
			$("#labelStates").text(sBAMLabelState);	
		}
		
	} else {			
		bShowState = false;
		bShowSubmit = true;
	}

	//console.log("call showHideControls: LfC" );
	showHideControls();
}

function LoadForState(iState){
	//console.log("LoadForState: %s", iState );
	bShowState= true;
}


$(document).ready(function(){
	//console.log("onReady" );

	// test special show conditions for specific countries
	var iRegion = $("#regionList").val();
	var iCountry = $("#countryList").val();
	var iState = $("#stateList").val();
	//console.log("check onstart: Reg: %s, Cty: %s, Sta: %s", iRegion, iCountry, iState );
	
	if( iRegion > 0) {
		LoadForRegion(iRegion);
	}
	//console.log("call showHideControls: r" );
	showHideControls();


	$("#regionList").change( function() {
		var iRegion = $(this).val();
		LoadForRegion( iRegion );
		//console.log("call showHideControls: rc" );
		showHideControls();
	});


	$("#countryList").change( function () {
		var iCountry = $(this).val();
		LoadForCountry(iCountry);
		//console.log("call showHideControls: cc" );
		showHideControls();
	});

	$("#stateList").change( function () {
		bShowSubmit = true;
		//console.log("call showHideControls: sc" );
		showHideControls();
	});


});