jQuery(document).ready(function(){
	jQuery('#srcType,#destType').change(function(){
		var elmObj = jQuery(this);
		var showId = elmObj.attr('id');
		var model = elmObj.val();
		var regionId = jQuery('#'+showId+'-reg .rgselect').val();
		if (model == '' || model == 'Factory'){
			jQuery('#'+showId+'-reg').hide();
			jQuery('#'+showId+'-ref').hide();
		} else {
			jQuery.ajax({
				type: 'POST',
				url: siteUrl+'stocks/getModelOptions.json',
				data: {reModel:model,region:regionId },
				dataType: 'json',
				beforeSend: function(){
					elmObj.after('<img src="'+siteUrl+'img/loading20.gif" class="loading" />');
				},
				success: function(data){
					if ( model != 'Distributor'){
					jQuery('#'+showId+'-reg').show();
					}
					jQuery('#'+showId+'-ref').show();
					jQuery('.loading').remove();
					$('#'+showId+'-ref dd').html(data.label); 
					$('#'+showId+'-ref .wdsel').fillSelect(data.options);
				},
				statusCode: {
					404: function() {
						alert( message.noPage );
					}
				}
			});
		}
		return false;
		
	});
	
	//jQuery('#dest_region_id, #dest_region_id').
	
	
	
	jQuery( document ).on( "change", "#src_region_id, #dest_region_id", function() {
		var elmObj = jQuery(this);
		
		
		var regionId = elmObj.val();
		var showId = elmObj.attr('rel');
		var model = jQuery('#'+showId).val();
		
		jQuery.ajax({
			type: 'POST',
			url: siteUrl+'stocks/getModelOptions.json',
			data: {reModel:model,region:regionId },
			dataType: 'json',
			beforeSend: function(){
				elmObj.after('<img src="'+siteUrl+'img/loading20.gif" class="loading"/>');
			},
			success: function(data){
				jQuery('#'+showId+'-reg').show();
				jQuery('#'+showId+'-ref').show();
				jQuery('.loading').remove();
				$('#'+showId+'-ref dd').html(data.label); 
				$('#'+showId+'-ref .wdsel').fillSelect(data.options);
			},
			statusCode: {
				404: function() {
					alert( message.noPage );
				}
			}
		});
		
		return false;
	});
});