
/* CLASE gestorAtributos *****************************************/
function gestorAtributos(){
	this.atributs = new Array
	this.atributTemp = null
	this.precioBase = null
	this.campoPrecioBase = null
	this.campoPrecio = null
	this.moneda = "€"
	
	this.calculaPrecioTotal = function(){
		var precio
		precio = this.precioBase
		if(precio==null) precio=0
		
		for(var i =0;i<this.atributs.length;i++)
			precio = this.atributs[i].calcula(precio)
		
		
		this.campoPrecio.value = formatPreu(precio, this.moneda)
	}
	this.recojeIds = function(){
		var arrTemp = new Array
		
		for(var i =0;i<this.atributs.length;i++){
			if(this.atributs[i].idElejido()!='')
				arrTemp.push(this.atributs[i].idElejido())
		}
		
		return arrTemp.join(',')
	}
	
	this.recojeValors = function(){
		var arrTemp = new Array
		
		for(var i =0;i<this.atributs.length;i++){
			if(this.atributs[i].valorElejido().valor!='')
				arrTemp.push(this.atributs[i].valorElejido().valor)
		}
		
		return arrTemp.join(',')
	}
	
	this.pinta = function(){
		//pintamos el precio base si es necesario
		//alert(this.atributs[0].valors[0].accio) || this.atributs[0].valors[0].accio=='='
		if(this.precioBase == null)
			this.campoPrecioBase.style.display='none'
		else
			this.campoPrecioBase.value = formatPreu(this.precioBase, this.moneda)
		
		for(var i =0;i<this.atributs.length;i++){
			if(!this.atributs[i].padre) this.atributs[i].padre = this
			this.atributs[i].pinta()
		}
	}
}
/* FI CLASE gestorAtributos *****************************************/


/* CLASE atribut *****************************************/

function atribut(nouNom, nouObligatori){
	this.nom = nouNom
	this.obligatori = nouObligatori
	this.desplegable = null
	this.valors = new Array()
	this.moneda = "€";
	
	this.calcula = function(precio){
		var elejido = this.valorElejido()
		
		if(elejido.accio=='' || elejido.accio=='N')
			return precio
		else
			if(elejido.accio=='+')
				return parseFloat(precio) + parseFloat(elejido.cost)
			else
				if(elejido.accio=='=')
					return elejido.cost
	}
	
	this.valorElejido = function(){
		return this.valors[this.desplegable.selectedIndex]
	}
	
	this.idElejido = function(){
		return this.valorElejido().id
	}
	

	this.pinta = function(){
		this.desplegable = document.createElement("select")
		this.desplegable.name=this.nom
		this.desplegable.id=this.nom
		this.desplegable.onchange= function(){ga.calculaPrecioTotal()}
		
		for(var i=0;i < this.valors.length; i++){
			textOption = this.valors[i].valor+' '
			if(this.valors[i].accio=='+')
				textOption += '+'
			else
				if(this.valors[i].accio!='') textOption += formatPreu(this.valors[i].cost, this.moneda)
			
			this.desplegable.options[this.desplegable.options.length] = new Option(textOption,this.valors[i].id)
		}
		
		etiqueta = document.createElement("span")
		etiqueta.className='nomAtribut'
		etiqueta.innerHTML=' '+this.nom+':'
		
		bloc = document.createElement("span")
		bloc.className='atribut'
		bloc.appendChild(etiqueta)
		bloc.appendChild(this.desplegable)
		
		lloc.appendChild(bloc)
	}
	
}
/* FI CLASE atribut *****************************************/

/* CLASE valor **********************************************/
function valor(nouId, nouValor, nouCost, nouAccio){
	this.id=nouId
	this.valor=nouValor
	this.cost=nouCost
	this.accio=nouAccio
	this.selected=false
}
/* FI CLASE valor *******************************************/

function formatPreu(preu, moneda){
	if(preu!=''){
		preu = (Math.round(preu*100)/100).toString()
		
		if(preu.indexOf('.')<0)
			preu=preu+'.00'
		else{
			preu=preu+'00'
			preu=preu.substring(0,preu.indexOf('.')+3)
		}
		
		preu += ' ' + moneda;
	}
	
	return preu
}
