	$(document).ready(function(){ 
		if($('#gasType').val() != 'adhoc'){
			defaultValues();
			calculateWobbeIndex();
		}
		checkInputs();
		checkUnits();
		$("div.conversorUnidades input[name='type']").change(function(){
			$('.result').css('display', 'none');
			var typeSelected = $(this).val();
			var values = emvValues;
			var etiqs = emvEtiqs;
			if(typeSelected == '2'){
				values = flujoValues;
				etiqs = flujoEtiqs;
			}
			if(typeSelected == '3'){
				values = precioValues;
				etiqs = precioEtiqs;
				$('.dolarConversion').fadeIn('slow');
				$('.dolarConversion input').removeAttr('disabled');
			}else{
				$('.dolarConversion').css('display', 'none');
				if($('#euroConversion').val()=='')
					$('#euroConversion').val($("input[name='defaultDolarEuro']").val());
				if($('#poundConversion').val()=='')
					$('#poundConversion').val($("input[name='defaultDolarPound']").val());
			}
			var optionOInputUnit = $("#inputUnit option[value='']").text();
			$("#inputUnit option").remove();
			$("#inputUnit").append("<option value=''>"+optionOInputUnit+"</option>");
			var optionOOutputUnit = $("#outputUnit option[value='']").text();
			$("#outputUnit option").remove();
			$("#outputUnit").append("<option value=''>"+optionOOutputUnit+"</option>");
			for (count in values){
				$("#inputUnit").append("<option value='"+values[count]+"'>"+etiqs[count]+"</option>");
				$("#outputUnit").append("<option value='"+values[count]+"'>"+etiqs[count]+"</option>");
			}
			checkUnits();
			$('.result').fadeIn('slow');
		});
		$('div.conversorUnidades #gasType').change(function(){
			defaultValues();
			checkInputs();
			calculateWobbeIndex();
		});
		$('#volume, #density, #highHeating').change(function(){calculateWobbeIndex();});
		$("div.conversorUnidades select[name='inputUnit']").change(function(){checkUnits();});
	});
		
	function calculateWobbeIndex(){
		try{
			var wobbeIndex = $('#highHeating').val() / Math.sqrt($('#density').val() / $('#volume').val() / 1.22417640572981);
			wobbeIndex = ((wobbeIndex*100) | 0) / 100; 
			if (!isNaN(wobbeIndex)&&wobbeIndex!=0)
				$('#WobbeIndex').val(wobbeIndex);
		} catch(err) {}
	}
	
	function defaultValues(){
		$('div.conversorUnidades #volume').val($("input[name='volume"+$('#gasType').val()+"']").val());
		$('div.conversorUnidades #density').val($("input[name='density"+$('#gasType').val()+"']").val());
		$('div.conversorUnidades #highHeating').val($("input[name='highHeating"+$('#gasType').val()+"']").val());
	}
	
	
	function checkUnits(){
		if($("div.conversorUnidades #inputUnit").val() == ''){
			$("div.conversorUnidades #outputUnit").val('');
			$("div.conversorUnidades label[for='outputUnit']").attr('class', 'inactivo');
			$("div.conversorUnidades #outputUnit").attr('disabled', 'disabled');
		}else{
			if($("div.conversorUnidades #outputUnit").val()==$("div.conversorUnidades #inputUnit").val())
				$("div.conversorUnidades #outputUnit").val('');
			$("div.conversorUnidades #outputUnit").removeAttr('disabled');
			$("div.conversorUnidades label[for='outputUnit']").attr('class', '');
			$("div.conversorUnidades #outputUnit option").removeAttr('disabled');
			$("div.conversorUnidades #outputUnit option[value='"+$('#inputUnit').val()+"']").attr('disabled', 'disabled');
		}
	}
	
	function checkInputs(){
		var enable = $('div.conversorUnidades #gasType').val() == 'adhoc';
		checkInput(enable, 'volume');
		checkInput(enable, 'density');
		checkInput(enable, 'highHeating');
	}
	
	function checkInput(enable, id){
		if(enable)	{
			$('div.conversorUnidades #'+id+"hidden").remove();
			$('div.conversorUnidades #'+id).removeAttr('readonly');
			$("div.conversorUnidades label[for='"+$('#'+id).attr('name')+"']").attr('class','');
		}else{
			$('div.conversorUnidades #'+id).after($("<input type='hidden' value='"+$('#'+id).val()+"' name='"+$('#'+id).attr('name')+"' id='"+id+"hidden'/>"));
			$('div.conversorUnidades #'+id).attr('readonly', 'readonly');
			$("div.conversorUnidades label[for='"+$('#'+id).attr('name')+"']").attr('class','inactivo');
		}
	}
