
var PriceTool = {};

PriceTool.getCountries = function() {
	return getJSon('data/countries.jsp',null);
}

PriceTool.getCountries_geoinfo = function() {
	return getJSon('data/countries_geoinfo.jsp',null);
}

PriceTool.getPrices = function(country,commodity) {
	return getJSon('data/prices.jsp',{country_id:country,commodity_id:commodity});
}

PriceTool.getLocations_geoinfo = function(country) {
	return getJSon('data/locations_geoinfo.jsp',{country_id:country});
}

PriceTool.getLocations = function(country,commodity,price_type) {
	return getJSon('data/locations.jsp',{country_id:country,commodity_id:commodity,pricetype:price_type});
}

PriceTool.getCommodities = function(country) {
	return getJSon('data/commodities.jsp',{country_id:country});
}

PriceTool.getLocalWeights = function(location,commodity) {
	return getJSon('data/weights.jsp',{location_id:location,commodity_id:commodity});
}

PriceTool.getLocalCurrencies = function(location) {
	return getJSon('data/currencies.jsp',{location_id:location});
}

PriceTool.getDates = function(location,commodity) {
	return getJSon('data/dates.jsp',{location_id:location,commodity_id:commodity});
}

PriceTool.getSeries = function(series,date_minimum,date_maximum) {
	return getJSon('data/series.jsp',{min_date:date_minimum_string,max_date:date_maximum_string,series_array:series});


}

PriceTool.saveCSV = function(csv) {
	location.href="data/save_csv.jsp?csv_content="+csv;
}

getJSon = function(myUrl,myParameters) {
		var variable = null;
        dojo.xhrGet ({
            url: myUrl,
            handleAs: 'json',
            sync: true,
            content: myParameters,
            load: function(json_server_data) {
        		variable = new dojo.data.ItemFileReadStore(
        				{
        					data: {
        						identifier: "id",
        						items: json_server_data
        					}
        				}
        		);
        	},
            error: function (error) {
                console.error('Error: ', error);
            }
        });
/*		dojo.rawXhrPost({
			url: myUrl,
			handleAs: 'json',
			sync:true,
			postData: myParameters,
            load: function(json_server_data) {
    		variable = new dojo.data.ItemFileReadStore(
    				{
    					data: {
    						identifier: "id",
    						items: json_server_data
    					}
    				}
    			);
    		}
		});*/
        return variable;
}