$("<div id='loading' style='display:none;'></div>").prependTo($(document.body).children()[0]);
dwrDisableLoadingMessage = function() {
	dwr.engine.setPreHook(function() { });
	dwr.engine.setPostHook(function() { });
}
dwrEnableLoadingMessage = function() {
	dwr.engine.setPreHook(function() { $('#loading').show(); });
	dwr.engine.setPostHook(function() { $('#loading').hide(); });
}
$(document).ready(function() {
	$().ajaxStart(function() { $('#loading').show(); });
	$().ajaxStop(function() { $('#loading').hide(); });
}); 

dwrEnableLoadingMessage();

simulatePrice = function(locale) {
	simulator.simulatePrice(locale, $('#numberOfVechicles').val(), $('#numberOfUsers').val(), function(data) {
		$('#calculatePriceResult').html(data);
	});
}

simulateRoi = function(locale) {
	if (!isEmptyField('#vehicles') && !isEmptyField('#standardConsumption') && !isEmptyField('#traveledDistancePerVehiclesInMonth') && !isEmptyField('#fuelCostPerQuantityOfIt') && !isEmptyField('#improvementsFuelConsumptionPercentage')) {
		simulator.simulateRoi(locale, $('#vehicles').val(), $('#standardConsumption').val(), $('#traveledDistancePerVehiclesInMonth').val(), $('#fuelCostPerQuantityOfIt').val(), $('#improvementsFuelConsumptionPercentage').val(), function(data) {
			$('#fuelQuantityPerYear').html(data.fuelQuantityPerYear);
			$('#fuelCostsPerYear').html(data.fuelCostsPerYear);
			$('#averageConsumptionGoal').html(data.averageConsumptionGoal);
			$('#annualFuelSaving').html(data.annualFuelSaving);
			$('#monthFuelSaving').html(data.monthFuelSaving);
			$('#fuelSavingPerVechicleInMonth').html(data.fuelSavingPerVechicleInMonth);
		});
	}
}

toLabel = function(aField) {
	aField.value = aField.value.toLowerCase();
	aField.value = replaceAll(aField.value,' ', '_');
} 

isEmptyField = function(theFieldName) { return $(theFieldName).val() == null || $(theFieldName).val() == ''; }

replaceAll = function(str, de, para){
    var pos = str.indexOf(de);
    while (pos > -1){
		str = str.replace(de, para);
		pos = str.indexOf(de);
	}
    return (str);
}

isHidden = function(element) { return document.getElementById(element).style.display == 'none'; }

go = function(dest) { window.location.assign(dest); }

