// Definizione dell'oggetto Pagamento che gestiste
// le transazioni verso tutti i sistemi di pagamento

// Per ora č implementato solo paypal ma in futuro si puņ allargare
// la questione

var Pagamento = {
	//PayPalURL: "https://www.sandbox.paypal.com/cgi-bin/webscr",
	PayPalURL: "https://www.paypal.com/cgi-bin/webscr",
	//PayPalAccount: "negozi_1273844888_biz@lenoci.org",
	PayPalAccount: "info@merryautumn.com",
	Contatore: 0,
	PagamentoForm: '',
	PagamentoCommand: '',

	StartPagamento: function(contenitore)
		{
	
		//this.PagamentoForm = new Element('form',{'action':this.PayPalURL,'method':'post','id':'PagamentoForm','target':'_blank'});
		this.PagamentoForm = new Element('form',{'action':this.PayPalURL,'method':'post','id':'PagamentoForm'});
		this.PagamentoCommand = new Element('input',{'type':'hidden','name':'cmd','value':'_cart'});
		var Upload = new Element('input',{'type':'hidden','name':'upload','value':'1'});
		var Business = new Element('input',{'type':'hidden','name':'business','value':this.PayPalAccount});
		var Submit = new Element('input',{'id':'PagamentoSubmit','type':'submit','value':'PayPal'});		
		var Currency = new Element('input',{'type':'hidden','name':'currency_code','value':'EUR'});
		var Immagine = new Element('input',{'type':'hidden','name':'cpp_header_image','value':'http://www.merryautumn.com/images/MerryPayPal.png'});
		//var Costo = new Element('input',{'type':'hidden','id':'shipping','name':'shipping','value':'0.00'});
		
		this.PagamentoCommand.inject(this.PagamentoForm);
		Upload.inject(this.PagamentoForm);
		Business.inject(this.PagamentoForm);
		Submit.inject(this.PagamentoForm);
		Currency.inject(this.PagamentoForm);
		Immagine.inject(this.PagamentoForm);
		//Costo.inject(this.PagamentoForm);
		this.PagamentoForm.inject(contenitore);
	},
	
	StartSandBox:function (url,account)
		{
		this.PayPalURL = url;
		this.PayPalAccount = account;
	},
	
	AggiungiCostoSpedizione:function(costo)
		{	
		$('shipping_1').set('value',costo);
	},
	
	AggiungiOggetto: function(nome,valore)
		{
		this.Contatore = this.Contatore+1;
		var OggettoNome = new Element('input',{'type':'hidden','id':'item_name_' + this.Contatore,'name':'item_name_' + this.Contatore,'value':nome});
		var OggettoPrezzo = new Element('input',{'type':'hidden','id':'amount_' + this.Contatore,'name':'amount_' + this.Contatore,'value':valore});
		var Costo = new Element('input',{'type':'hidden','id':'shipping_' + this.Contatore,'name':'shipping_' + this.Contatore,'value':'0.00'});
		OggettoNome.inject(this.PagamentoForm);
		OggettoPrezzo.inject(this.PagamentoForm);
		Costo.inject(this.PagamentoForm);
	},
	
	AssegnaURLRitorno: function(nordine)
		{
		var URLRitorno = new Element('input',{'type':'hidden','name':'return','value':'http://www.merryautumn.com/index.php?status=paypalok&ord='+nordine});
		var URLRitornoCancel = new Element('input',{'type':'hidden','name':'cancel_return','value':'http://www.merryautumn.com/index.php?status=paypalcancel&ord='+nordine});
		var URLShop = new Element('input',{'type':'hidden','name':'shopping_url','value':'http://www.merryautumn.com/index.php?status=shop'});
		var NOrdine = new Element('input',{'type':'hidden','name':'invoice','value':nordine});
		
		URLRitorno.inject(this.PagamentoForm);
		URLRitornoCancel.inject(this.PagamentoForm);
		URLShop.inject(this.PagamentoForm);
		NOrdine.inject(this.PagamentoForm);
	},
	
	AvviaPagamento: function ()
		{
		this.PagamentoForm.submit();
	},
	
	RiempiDatiUtente: function(dati)
		{
		var Nome = new Element('input',{'type':'hidden','name':'first_name','value':dati.nome});
		var Cognome = new Element('input',{'type':'hidden','name':'last_name','value':dati.cognome});
		var Indirizzo= new Element('input',{'type':'hidden','name':'address1','value':dati.via +' '+dati.numero});
		var CAP = new Element('input',{'type':'hidden','name':'zip','value':dati.cap});
		var Citta = new Element('input',{'type':'hidden','name':'city','value':dati.citta});
		var Paese = new Element('input',{'type':'hidden','name':'country','value':dati.paese});
		var Email = new Element('input',{'type':'hidden','name':'email','value':dati.email});
		var Override = new Element('input',{'type':'hidden','name':'address_override','value':'1'});
		Nome.inject(this.PagamentoForm);
		Cognome.inject(this.PagamentoForm);
		Indirizzo.inject(this.PagamentoForm);
		CAP.inject(this.PagamentoForm);
		Citta.inject(this.PagamentoForm);
		Paese.inject(this.PagamentoForm);
		Email.inject(this.PagamentoForm);
		Override.inject(this.PagamentoForm);
	}
};
