var aro = new AjaxRenderObject();
var limitResults = 10;
var gStrNoData = ""

function getListino(value, fromDate, toDate, StrDal, StrAl, StrPrezzo, strNoData) {
	gStrNoData = '<div class="testo">' + strNoData + '</div>';
	aro.getContainer("listinoDiv")

	aro.wait = true;
	aro.url = "/ajax/json.aspx"
	
	aro.parameters = { 'op':'getlistinoxml', 'IDunita':value, 'fromDate':fromDate, 'toDate':toDate, 'cacheTicks':'', 'tipoPrezzo':'' }
	aro.loadingMessage = '<div class="testo">Loading data, please wait...</div>'
	aro.completeMessage = "Data loaded..."
	aro.timeout = 20000
	aro.timeoutMessage = "Error: request timed out."
	aro.onSuccess = processElements;
	aro.onError = requestError;
	aro.headerTemplateHTML = "<table class='prezzi' cellpadding='0' width='100%' cellspacing='0' border=1 bordercolor='#c1dad7'><tr class='headertable'><td>" + StrDal + "</td><td>" + StrAl + "</td><td>" + StrPrezzo + "</td></tr>"
	aro.itemTemplateHTML = "<tr ><td>#normalData#</td><td>#nextData#</td><td>&euro; #prezzo#</td></tr>"
	aro.alternatingItemTemplateHTML = "<tr class='alternating'><td>#normalData#</td><td>#nextData#</td><td>&euro; #prezzo#</td></tr>"
	aro.footerTemplateHTML = "</table>"
	aro.showContainer();
	aro.getData();
}

function transformDate(strDate) {
	return (strDate.substr(8,2) + "/" + strDate.substr(5,2) + "/" + strDate.substr(0,4))
}

function transformPrice(strPrice) {
	var dotIndex = strPrice.indexOf(".")
	if (dotIndex >= 0) {
		if (dotIndex == 0) {
			return ("0" + strPrice);
		} else {
			if (strPrice.length - dotIndex == 2) {
				strPrice = strPrice + "0";
			}
		}
		strPrice = strPrice.replace(".",",");
	} else {
		strPrice = strPrice + ",00";
	}
	return strPrice;
}


function processElements(req, aro) {
	if (aro.processedObject) {
		var o = aro.processedObject;

		var elements = new Array();

		if (o.listino.length) {
			var limit = o.listino.length;

			if (o.listino.length > 1) {
				var count = 0;
				for (var i=0;i<limit ;i++ ) {
					if (o.listino[i].listino.prezzo > 0) {
						var normalData = o.listino[i].listino.normalData;
						var nextData = o.listino[i].listino.nextData;
						var price = roundDecimal(o.listino[i].listino.prezzo * 7,2) + "";
						o.listino[i].listino.normalData = transformDate(normalData);
						o.listino[i].listino.nextData = transformDate(nextData);
						o.listino[i].listino.prezzo = transformPrice(price);
						elements[count] = o.listino[i].listino;
						count++;
					}
				}
				aro.container.innerHTML = aro.renderAll(elements);
			}
		} else if (o.listino.listino) {
			if (o.listino.listino.prezzo > 0) {
				o.listino.listino.normalData = transformDate(o.listino.listino.normalData);
				o.listino.listino.nextData = transformDate(o.listino.listino.nextData);
				aro.container.innerHTML = aro.renderAll(o.listino.listino);
			} else {
				requestError(req, aro)
			}
		}
		aro.showContainer();
	} else {
		aro.hideContainer();
	}

}

function requestError(req, aro) {
	aro.container.innerHTML = gStrNoData;
}

function roundDecimal(n, nd) {
	var c = Math.pow(10,nd);
	var diffCeil = Math.ceil(n) - n;
	var diffFloor = n - Math.floor(n);
	if (diffCeil > diffFloor) {
		n = Math.floor(n);
	} else {
		n = Math.ceil(n);
	}
	return Math.round(n*c)/c;
}
