var GestorCartera = Class.create();
GestorCartera.prototype = {
	initialize: function (tablaCartera, idAnadirCartera, inversion, autocompletar) {
		this.tablaCartera = tablaCartera;
		this.anadirCartera = $(idAnadirCartera);
		this.inversion = inversion;
		this.autocompletar = autocompletar;
		this.autocompletar.addEventListener("selected", this.onfondoseleccionado.bindAsEventListener(this)); 
		this.preparadoInsertar = false;
		Event.observe(this.anadirCartera, "click", this.anadirInversion.bindAsEventListener(this));
		this.anadirCartera.hide()
		Event.observe(this.inversion.participacionesField, "keydown", this.onKeyUpParticipaciones.bindAsEventListener(this));
		Event.observe(this.inversion.dineroInvertidoField, "keydown", this.onKeyUpEuros.bindAsEventListener(this));
		
	},
	onKeyUpParticipaciones: function (ev) {
		if (ev.keyCode ==  Keys.ENTER) {
			this.inversion.onparticipaciones();
			if (this.tablaCartera._rowEdit >= 0) {
				this.tablaCartera.save();
			}
			else {
				this.anadirInversion();
			}
		}
		return true;
	},
	onKeyUpEuros: function (ev) {
		if (ev.keyCode ==  Keys.ENTER) {
			this.inversion.ondineroinvertido();
			if (this.tablaCartera._rowEdit >= 0) {
				this.tablaCartera.save();
			} else {
				this.anadirInversion();
			}
		}
		return true;
	},
	onfondoseleccionado: function () {
		this.anadirCartera.show();
		this.preparadoInsertar = true;
	},
	
	anadirInversion: function () {
		if (this.preparadoInsertar) {
			this.anadirCartera.hide();
			
			//alert(this.inversion.participacionesField.getValue());
			this.tablaCartera._data.push([this.inversion.fundSelected.FundISIN,
										this.inversion.fundSelected.FundDescription,
										this.inversion.numberValorLiquidativo.getValue(),
										this.inversion.numberParticipaciones.getValue(),
										this.inversion.numberDinero.getValue()]);
							
			this.tablaCartera.sortColumnNoChange(this.tablaCartera._colOrder);
			this.tablaCartera.pintar();
			this.inversion.reset();
			this.autocompletar.reset();
			this.autocompletar.ele.focus();
			this.preparadoInsertar = false;
		}
	}
}
