DattaCalc = new Calculadora();

function Calculadora() {
	this.a_root = new Array(); // Array('label','period','precio','cant','setup');
	this.Suma = 0;
	this.SumaSetup = 0;
	
	this.GetValueByType = GetValueByType;
	this.GetLabel = GetLabel;
	this.GetPeriod = GetPeriod;
	this.GetPrecio = GetPrecio;
	this.GetCant = GetCant;
	this.GetDesc = GetDesc;
	this.GetSetup = GetSetup;
	this.ChPeriod = ChPeriod;
	this.ChPrecio = ChPrecio;
	this.ChCant = ChCant;
	this.ChDesc = ChDesc;
	this.ChSetup = ChSetup;
	this.Add = Add;
	this.AddX = AddX;
	this.Del = Del;
	this.DelX = DelX;
	this.Reset = Reset;
	this.IsEmpty = IsEmpty;
	this.Build = Build;
	this.Show = Show;
	this.Hide = Hide;

	function GetValueByType(o_obj){
		switch(o_obj.type){
				case 'checkbox':
					return o_obj.value;
					break;
				case 'radio':
					for (e=0;e<o_obj.length;e++){
						if (o_obj[e].checked){
							return o_obj[e].value;
						}
					}
					break;
				case 'select-one':
					return o_obj.options[o_obj.selectedIndex].value;
					break;
				case 'input':
					return o_obj.value;
					break;
				case 'text':
					return o_obj.value;
					break;
				case 'password':
					return o_obj.value;
					break;
				default:
					return o_obj.value;
					break;
		}
	}

	function GetLabel(i){
		return this.a_root[i][0];
	}

	function GetPeriod(i){
		return this.a_root[i][1];
	}

	function GetPrecio(i){
		return this.a_root[i][2];
	}
	
	function GetCant(i){
		return this.a_root[i][3];
	}

	function GetDesc(i){
		return this.a_root[i][4];
	}
	
	function GetSetup(i){
		return this.a_root[i][5];
	}

	function ChPeriod(period,i){
		this.a_root[i][1] = period;
		return;
	}

	function ChPrecio(precio,i){
		this.a_root[i][2] = precio;
		return;
	}

	function ChDesc(desc,i){
		this.a_root[i][4] = desc;
		return;
	}
		
	function ChCant(cant,i){
		this.a_root[i][3] = cant;
		return;
	}
	
	function ChSetup(setup,i){
		this.a_root[i][5] = setup;
		return;
	}

	function Add(s_label,s_period,s_precio,s_desc,i_cant,s_setup){
		var agregada = false;
		var no_existe = true;
		if(i_cant==null) i_cant = 1;
		if(s_setup==null) s_setup = 0;
		for (var i=0;i<this.a_root.length;i++){
			if (this.GetLabel(i) == s_label){
					this.ChPeriod(s_period,i);
					this.ChPrecio(s_precio,i);
					this.ChDesc(s_desc,i);
					this.ChCant(i_cant,i);
					this.ChSetup(s_setup,i);
					no_existe = false;
					agregada = true;
			}
		}
		if(no_existe){
			this.a_root.push(Array(s_label,s_period,s_precio,i_cant,s_desc,s_setup));
			agregada = true;
		}

		if(agregada){
			this.Build();
		}
		return agregada;
	}

	function AddX(label,period,precio,desc,cant,setup){
		
		var o_label = xGetElementById(label);
		if(o_label==null) var s_label = label;
		else {
			var s_label = this.GetValueByType(o_label);
		}
		
		var o_period = xGetElementById(period);
		if(o_period==null) var s_period = period;
		else {
			var s_period = this.GetValueByType(o_period);
		}
		
		var o_precio = xGetElementById(precio);
		if(o_precio==null) s_precio = precio;
		else {
			var s_precio = this.GetValueByType(o_precio);
		}
		
		var o_desc = xGetElementById(desc);
		if(o_desc==null) s_desc = desc;
		else {
			var s_desc = this.GetValueByType(o_desc);
		}
		
		if(cant==null) i_cant = 1;
		else {
			var o_cant = xGetElementById(cant);
			if(o_cant==null) i_cant = cant;
			else {
				var i_cant = this.GetValueByType(o_cant);
			}
		}
		
		if(setup==null) s_setup = 0;
		else {
			var o_setup = xGetElementById(setup);
			if(o_setup==null) s_setup = setup;
			else {
				var s_setup = this.GetValueByType(o_setup);
			}
		}
		
		this.Add(s_label,s_period,s_precio,s_desc,i_cant,s_setup);
		
		return;
	}

	function Del(s_label){
		var deleted = false;
		if(this.a_root.length == 0) {
		}else{
			for (var i=0;i<this.a_root.length;i++){
				if (this.GetLabel(i) == s_label){
					this.a_root.splice(i,1);
					deleted = true;
				}
			}
		}
		if(deleted){
			this.Build();
		}
		return deleted;
	}

	function DelX(label){
		
		var o_label = xGetElementById(label);
		if(o_label==null) return ;
		else {
			var s_label = this.GetValueByType(o_label);
		}
		this.Del(s_label);
		return;
	}

	function Reset(){
		this.a_root = new Array(); // Array('label','period','precio','desc','cant','setup');
		this.Suma = 0;
		this.SumaSetup = 0;
		this.Build();
	}
	
	function IsEmpty(){
		if(this.a_root.length > 0){
			return false;
		}else{
			return true;
		}
	}

	function Build(){
			var Listado='';
			this.Suma = 0;
			this.SumaSetup = 0;
			var montoTotal = 0;
			Listado = '<table width="100%" border="0" cellpadding="0" cellspacing="1">\n';
			Listado += ' <tr>\n  <td align="left" valign="middle" class="Calculadora_Listado_td_head"><strong>Cant.</strong></td>\n  <td align="left" valign="middle" class="Calculadora_Listado_td_head"><strong>Producto</strong></td>\n  <td align="center" valign="middle" class="Calculadora_Listado_td_head"><strong>Periodo</strong></td>\n </tr>\n';
			if(this.a_root.length > 0){
				for(var i=0;i<this.a_root.length;i++){
					this.Suma += (this.GetPrecio(i)/1)*this.GetCant(i);
					this.SumaSetup += this.GetSetup(i);
					var DescCompleta;
					if((this.GetDesc(i) == undefined) || (this.GetDesc(i) == null)){DescCompleta=this.GetLabel(i);}else{DescCompleta=this.GetLabel(i)+'<br />'+this.GetDesc(i);}
					Listado += ' <tr>\n  <td align="center" valign="middle" class="Calculadora_Listado_td_list">'+this.GetCant(i)+'</td>\n  <td align="left" valign="middle" class="Calculadora_Listado_td_list">'+DescCompleta+'</td>\n  <td align="center" valign="middle" class="Calculadora_Listado_td_list">'+this.GetPeriod(i)+'</td>\n </tr>\n';
				}
			}else{
				Listado += ' <tr>\n  <td align="left" valign="middle" class="Calculadora_Listado_td_list" colspan="3">No hay productos seleccionados</td>\n </tr>\n';
			}		
			Listado += '</table>\n';
			montoTotal = this.Suma + this.SumaSetup;
			xGetElementById('Calculadora_PrecioDetalle_setup').innerHTML = FormatPrecio(this.SumaSetup);
			xGetElementById('Calculadora_PrecioDetalle_periodico').innerHTML = FormatPrecio(this.Suma);
			if(this.SumaSetup > 0){
				show('Calculadora_PrecioDetalle');
			}else{
				hide('Calculadora_PrecioDetalle');
			}
			xGetElementById('Calculadora_PrecioNumero').innerHTML = FormatPrecio(montoTotal);
			xGetElementById('Calculadora_Listado').innerHTML = Listado;
			if(this.a_root.length > 0){this.Show();} else {this.Hide();}
	}

	function Show(){
		if(xGetElementById('Calculadora').style.display == 'none'){
			fadeOutIn('Calculadora','in');
		}
		return;
	}

	function Hide(){
		fadeOutIn('Calculadora','out');
		return;
	}

}