var oMateriales;
var oAccionamientos;
var oTiposCapacidad;

function comboMateriales(){
	var sel=$('#MATERIAL').val();
	$('#MATERIAL').html(oMateriales.clone());
	var s=$('#SECTOR').find('option:selected').val();
	var sm='';
	var reset=true;
	
	$('#MATERIAL').find('option').each(function() {

		sm=parseInt($(this).val());
		if(!isNaN(sm)){
			if(materiales[sm]['idsector']==s || isNaN(s) || s=='' || s==0){
				$(this).show();
				if(sm==sel){
					$(this).attr('selected','selected');
					reset=false;
				}
			}
			else{
				//$(this).hide(); no funciona en IE
				$(this).remove();
			}
		}
		else{
			$(this).show();
		}
	});
	
	if(reset){
		$('#MATERIAL').val('');
	}

	
}

function comboAccionamientos(){
	var sel=$('#ACCIONAMIENTO').val();
	$('#ACCIONAMIENTO').html(oAccionamientos.clone());
	var m=$('#MATERIAL').find('option:selected').val();
	var a='';
	var reset=true;
	
	$('#ACCIONAMIENTO').find('option').each(function() {
		a=$(this).val();
		if(a!=''){
			if(materiales[m][a]){
				$(this).show();
				if(a==sel){
					$(this).attr('selected','selected');
					reset=false;
				}
			}
			else{
				//$(this).hide(); no funciona en IE
				$(this).remove();
			}
		
		}
		else{
			$(this).show();
		}
		
	});
	if($('#ACCIONAMIENTO').find('option').length==2){ //manci: nos cargamos la opcion 'indiferente' cuando sólo hay un accionamiento.
		$('#ACCIONAMIENTO').find('option[value=""]').remove();
	}
	if(reset){
		$('#ACCIONAMIENTO').val('');
	}

}

function tiposCapacidad(){
	var sel=$('#TIPOCAP').val();
	$('#TIPOCAP').html(oTiposCapacidad.clone());
	var m=$('#MATERIAL').find('option:selected').val();
	var tc='';
	var reset=true;
	
	$('#TIPOCAP').find('option').each(function() {
		tc=$(this).val();
		
		if(materiales[m]['toneladas'] && parseInt(materiales[m]['toneladas'])>0 && tc=='CM'){
			//$(this).hide(); no funciona en IE
			$(this).remove();
		}
		else{
			$(this).show();
					
			if(tc==sel){
				$(this).attr('selected','selected');
				reset=false;
			}
		}
		
	});

	if(reset){
		$('#TIPOCAP').val('');
	}
	
}

function densidadMax(){
	
	var m=$('#MATERIAL').find('option:selected').val();
	if(!isNaN(m)){
		$('#DENSIDAD').val(materiales[m]['densidad']);
	}
	
}


function mostrarInfo(seccion){
	$('.info_web').hide();
	$('#info_'+seccion.toLowerCase()).show();
	
}
function ocultarInfo(seccion){
	$('#info_'+seccion.toLowerCase()).hide();
}

$(document).ready(function() 
{
	$('#SECTOR').change(comboMateriales);
	$('#MATERIAL').change(comboAccionamientos);
	$('#MATERIAL').change(tiposCapacidad);
	$('#MATERIAL').change(densidadMax);
	oMateriales=$('#MATERIAL').find('option').clone();
	oAccionamientos=$('#ACCIONAMIENTO').find('option').clone();
	oTiposCapacidad=$('#TIPOCAP').find('option').clone();
	$('#SECTOR').change();
	//$('#MATERIAL').change();
	comboAccionamientos();
	tiposCapacidad();
	
});


