    /// <reference path="../../javascript/jquery-1.2.6-vsdoc.js" />

// start-up
var selected_country = '';

$(function(){
	// wire-up country change
	$('#country_select').change(handleCountrySelection);
	$('#state_select').change(handleStateProvinceSelection);
	$('#city_select').change(performSearch);
	
	$('#print_button').click(function(){
		window.print();
	});
	
	// preset selected values
	$('#country_select').val('');
	$('#US_select').val('');
	$('#CA_select').val('');
});

function handleCountrySelection(){
	selected_country = $('#country_select').val();
	$('#city_row').fadeOut();
	$('#results_div').fadeOut();
	$('#details_div').fadeOut();
	
	if (selected_country != ''){	
		if(selected_country == 'CA') {
			$('.state_prov_span').html('province');
		} else if(selected_country == 'US') {
			$('.state_prov_span').html('state');
		}

		$('#state_select')
			.loadSelect([{value: '', caption: 'Loading...'}]);
		$('#state_row').fadeIn();
		
		// load states and provinces
		$.ajax({
		    type: "POST",
		    url: "RequestHandler.aspx",
		    data: { type: 'get_states', id: selected_country },
		    dataType: "json",
		    success: function(response) {
		        $('#state_select').loadSelect(response);
		    },
		    error: function(response) {
		        showError(response.responseText);
		    }
		});
	} else {
		$('#state_select').val("");		
		$('#state_row').fadeOut();
		$('#search_row').fadeOut();
	}	
}

function handleStateProvinceSelection(){
	var state_prov = $('#state_select').val();
	$('#results_div').fadeOut();
	$('#details_div').fadeOut();
	
	if (state_prov != ''){
		$('#city_select')
			.loadSelect([{value: '', caption: 'Loading...'}]);
		$('#city_row').fadeIn();
		
		$.ajax({
			type: "POST",
			url: "RequestHandler.aspx",
			data: {type: 'get_cities', id: state_prov},
			dataType: "json",
			success: function(response){
				$('#city_select').loadSelect(response);
			},
			error: function(response){
			    showError(response.responseText);
			}
		});		
	} else {
		$('#city_row').fadeOut();
	}
}

function performSearch(){
	$('#details_div').fadeOut();
	
	var state_prov = $('#state_select').val();
	var city = $('#city_select').val();

	$.ajax({
	    type: "POST",
	    url: "RequestHandler.aspx",
	    data: { type: 'search_list', state: state_prov, city: city },
	    dataType: "html",
	    success: function(response) {
	        $('#company_list_div').html(response);
	        $('#results_div').fadeIn();
	    },
	    error: function(response) {
	        showError(response.responseText);
	    }
	});
}

function showDetails(id){
	$('#details_div').hide();
	
	$.ajax({
		type: "POST",
		url: "RequestHandler.aspx",
		data: {type: 'search_detail', id: id},
		dataType: "json",
		success: function(response){
			$('#company_name_label').html(response.company);
			$('#address_label').html(response.address);
			$('#phone_number_label').html(response.phone);
			
			$('#details_div').fadeIn();
		},
		error: function(response){
		    showError(response.responseText);
		}
	});
}

function showError(message) {
    $('#debugger').val(message);
    $('#debugger').show();
}