var store_companies = {1: true, 8: true, 13: true};

function not_listed_link() {
	$('not_listed_box').show();
	if($('dsc_id_blank')) $('dsc_id_blank').selected = true;
	$('not_listed_link').hide();
	return false;
}

function company_change(dsc_id, store_id, company_select) {
	company_id = $F(company_select);
	if(store_companies[company_id]) {
		$('store_row').show();
		load_store_selector(company_id, dsc_id);
		load_dsc_selector(company_id, dsc_id, 0);
	} else {
		$('store_row').hide();
		if($('store_id_blank')) $('store_id_blank').selected = true;
		load_dsc_selector(company_id, dsc_id);
	}
}

function load_store_selector(company_id, dsc_id, store_id) {
	if(!store_companies[company_id]) $('store_row').hide();
	
	$('store_id_box').update('Loading Stores...');
	
	var url = '/ajax/dsc.php?do=stores&company_id=' + company_id + '&store_id=' + store_id + '&blank=1';
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			$('store_id_box').update(transport.responseText);
			Event.observe('store_id', 'change', function (evt) {
				store_change(company_id, dsc_id, $('store_id'));
			});
		}
	});
}

function store_change(company_id, dsc_id, store_select) {
	store_id = $F(store_select);
	load_dsc_selector(company_id, dsc_id, store_id);
}

function load_dsc_selector(company_id, dsc_id, store_id) {
	$('dsc_id_box').update('Loading DSCs...');
	
	if(store_id != null)
		var url = '/ajax/dsc.php?do=dscs&company_id=' + company_id + '&store_id=' + store_id + '&dsc_id=' + dsc_id + '&blank=1';
	else
		var url = '/ajax/dsc.php?do=dscs&company_id=' + company_id + '&dsc_id=' + dsc_id + '&blank=1';
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			$('dsc_id_box').update(transport.responseText);
			Event.observe('dsc_id', 'change', function (evt) {
				if($('dsc_id').value) {
					$('not_listed_link').show();
					$('not_listed_box').hide();
				}
			});
		}
	});
	
	if(company_id == '0' || store_id == '0') {
		$('not_listed_link').hide();
		$('not_listed_box').hide();
	} else if($F($('rep_name')) == '') {
		$('not_listed_link').show();
		if($('not_listed_box')) $('not_listed_box').hide();
	} else {
		if($('not_listed_link')) $('not_listed_link').hide();
		$('not_listed_box').show();
	}
}

function init(company_id, dsc_id, store_id) {
	load_store_selector(company_id, dsc_id, store_id);
	load_dsc_selector(company_id, dsc_id, (store_companies[company_id] ? store_id : null));
}