var costibn=new Array(4);
costibn[0]=new Array(0.021,0.027,0.031,0.037);
costibn[1]=new Array(0.022,0.028,0.032,0.038);
costibn[2]=new Array(0.028,0.032,0.038,0.042);
costibn[3]=new Array(0.048,0.053,0.058,0.069);
var costiplastificazione=new Array(0,1,1);
var costicol=new Array(0.1,0.16,0.2,0.3);
var rs = 0;
var tot = 0;

function get_radio_value(radio){
	for (var i=0; i < radio.length; i++){
		if (radio[i].checked)
			return radio[i].value;
   }
   return -1;
}

function totale(a){
	var npagine = document.modulo.npagine.value;
	var ncopie = document.modulo.ncopie.value;
	if ((isNaN(npagine)) || (npagine == "") || (npagine == "undefined")) {
		alert("Il campo numero pagine deve essere numerico ed obbligatorio.");
		return '';
	} else if ((isNaN(ncopie)) || (ncopie == "") || (ncopie == "undefined") || (ncopie < parseInt(50))) {
		alert("Il campo numero copie deve essere numerico ed obbligatorio. \n Il numero di copie non dev'essere minore di 50.");
		return ((a) * document.modulo.ncopie.value) + 100;
	} else if(ncopie >= parseInt(100) && ncopie <= parseInt(199)){
		var sconto = ((((a) * document.modulo.ncopie.value) + 100) / 100) * 25;
		return (((a) * document.modulo.ncopie.value) + 100) - sconto;
	} else if(ncopie >= parseInt(200) && ncopie <= parseInt(499)){
		var sconto = ((((a) * document.modulo.ncopie.value) + 100) / 100) * 35;
		return (((a) * document.modulo.ncopie.value) + 100) - sconto;
	} else if(ncopie >= parseInt(500)){
		var sconto = ((((a) * document.modulo.ncopie.value) + 100) / 100) * 45;
		return (((a) * document.modulo.ncopie.value) + 100) - sconto;
	}
	else return ((a) * document.modulo.ncopie.value) + 100;
}

function errorMsg(msg){
	alert(msg)
	xInnerHtml("bod_totale","-");
	xInnerHtml("bod_prezzo_copia","-");
	return false;
}

function calcolaPreventivo(){
	var npaginebn = document.modulo.npagine.value;
	var npaginecol = document.modulo.npagine_colori.value;
	var ncopie = document.modulo.ncopie.value;
	var formato = get_radio_value(document.modulo.formato);
	var plastificazione = 1;//get_radio_value(document.modulo.plastificazione);
	var carta = get_radio_value(document.modulo.carta);
	var totale=0;
	var prezzocopia=0;
	var costo_fisso=90;	//Cambiato costo fisso in data 4 dicembre 2007
	if (ncopie>1000){
		xInnerHtml("bod_prezzo_copia","-");
		return errorMsg("Il massimo di copie stampabili e' 1000!");
	}
	if(formato==-1)
		return errorMsg("Selezionare il formato");
	if(plastificazione==-1)
		return errorMsg("Selezionare l'eventuale plastificazione");
	if(carta==-1)
		return errorMsg("Selezionare il tipo di carta");
	if (((isNaN(npaginebn)) || (npaginebn == "") || (npaginebn == "undefined"))&&((isNaN(npaginecol)) || (npaginecol == "") || (npaginecol == "undefined")))
		return errorMsg("Immettere il numero di pagine");
	if ((npaginebn==0)&&(npaginecol==0))
		return errorMsg("Immettere il numero di pagine");
	if ((isNaN(ncopie)) || (ncopie == "") || (ncopie == "undefined"))
		return errorMsg("Immettere il numero di copie");
	if (ncopie<50)
		return errorMsg("Il numero di copie deve essere almeno 50");
	totale=(costiplastificazione[plastificazione] + costibn[formato][carta]*npaginebn + costicol[formato]*npaginecol)*ncopie;
	//Aggiungi costi copertina e rilegatura (2 Euro a copia)
	totale+=(ncopie*2);
	totale+=costo_fisso;
	if(ncopie>=800){
		xInnerHtml("bod_prezzo_copia","-");
		totale*=0.60; //60% di sconto
	}else if(ncopie>=500)
		totale*=0.75; //50% di sconto
	else if(ncopie>=200)
		totale*=0.88; //40% di sconto
	else if(ncopie>=100)
		totale*=0.95; //30% di sconto
	else if(ncopie>=50)
		totale*=0.98; //20% di sconto
	xInnerHtml("bod_totale",totale.toFixed(2));
	prezzocopia=totale/ncopie;
	xInnerHtml("bod_prezzo_copia",prezzocopia.toFixed(2));
}

function adjustLayout(){ 
	var lHeight = xHeight("left"); 
	var rHeight = xHeight("right");
	var cHeight = xHeight("center");
	var maxHeight =  Math.max(cHeight,Math.max(rHeight,lHeight)) + 20;
	xHeight("left", maxHeight);
	xHeight("right", maxHeight);
	xHeight("center", maxHeight);
}

//AJAX cart functions begin

function deleteCart(delete_url){
	var obj = document.getElementById('basket_short'); 
	obj.innerHTML = 'Loading...';
	http('GET', delete_url, ajax_response);
}

function addToCart(product_id){
	var submitTo = 'index.php';
//	alert('addToCart' + product_id);
	product_form=xGetElementById('addtocart' + product_id);
	url ='index.php?option=com_virtuemart';
	url += '&product_id='+product_form.product_id.value;
	url += '&task='+product_form.task.value;
	url += '&quantity='+product_form.quantity.value;
	url += '&page='+product_form.page.value;
	url += '&ch=json';
	url += '&func='+product_form.func.value;
	url += '&Itemid='+product_form.Itemid.value;
	url += '&lang='+product_form.lang.value;
	url += '&category_id='+product_form.category_id.value;
	url += '&option='+product_form.option.value;
	/*
	*/
	//product_form=eval('document.addtocart' + product_id);
	//alert(product_form.toString());
	product_form.ch.value='json';
	if(product_form){
	  	//location.href = submitTo; //uncomment if you need for debugging
//	  	if(task=='view'){
	  		//Loading
			var obj = document.getElementById('basket_short'); 
			obj.innerHTML = 'Loading...';
//		}
		//alert(product_form.toString());
		http('GET', url, ajax_response);
	}
//	alert("****" + product_id);
}

function ajax_response(responseData){
	var functionName=responseData.option + "_" + responseData.task;
	if((responseData.option.toLowerCase()=="com_virtuemart") && ((responseData.task.toLowerCase()=="cartadd")||(responseData.task.toLowerCase()=="cartdelete"))){
		com_virtuemart_basket_short(responseData.result);
	}
}

function com_virtuemart_basket_short(result){
	var htmlResult="";
	//alert("Product list length:" + result.product_list.length);
	var labels = result.labels;
	if(result.product_list.length > 0){
		htmlResult="<table class=\"cart_short\">";
		htmlResult += "<tr><th class=\"name\">" + labels["name_header"] + "</th>";
		htmlResult += "<th class=\"quantity\">" + labels["quantity"] + "</th>";
		htmlResult += "<th class=\"subtotal\">" + labels["subtotal_header"] + "</th>";
		htmlResult += "<th class=\"delete\"></th></tr>";
		var totQuantity=0;
		for (var i = 0; i < result.product_list.length; i++){
			var productName = result.product_list[i].product_name;
			var productQty = result.product_list[i].product_quantity;
			totQuantity += parseInt(productQty);
			var productPrice = result.product_list[i].product_price;
			var productSubtotal = result.product_list[i].subtotal;
			var deleteForm = result.product_list[i].delete_form;
			var deleteButton = "<a href=\"" + result.product_list[i].delete_button["link_url"] + "\"";
				deleteButton +=  " onClick=\"deleteCart('" + result.product_list[i].delete_button["link_url"] + "&ch=json');return false;\"";
				deleteButton +=  " title=\"" + result.product_list[i].delete_button["link_title"] + "\">";
				deleteButton += "<img src=\"" + result.product_list[i].delete_button["image_url"];
				deleteButton += "\" alt=\"" + result.product_list[i].delete_button["image_alt"] + "\" /></a>";
			htmlResult += "<tr class=\"cart_product_row\" >";
			htmlResult += "<td class=\"name\">" + productName + "</td>";
			htmlResult += "<td class=\"quantity\">" + productQty + "</td>";
			htmlResult += "<td class=\"subtotal\">" + productSubtotal + "</td>";
			htmlResult += "<td class=\"delete\">" + deleteButton + "</td>";
			htmlResult += "</tr>";
		};
		htmlResult += "<tr><td colspan=\"4\">" + totQuantity + " " + labels["products"] +"</td><td></td></tr>";
		htmlResult += "<tr><td class=\"value\" colspan=\"3\">" + result.total + "</td><td></td></tr>";
		htmlResult+="</table>";
	}else{
		htmlResult=labels["empty_cart"];	
	}
	var obj = document.getElementById('basket_short');
	obj.innerHTML = htmlResult; 
}
//AJAX cart functions end

window.onload = function(){ 
	adjustLayout();
}