function respuesta (nrespuesta, desrespuesta, desrespuestaprecios, npregunta, nomrespuesta, valor, alias, deslargarespuesta)
{
	this.nrespuesta			= nrespuesta;
	//Alias del id de la respuesta
	this.nomrespuesta		= nomrespuesta;
	//Alias del id de la respuesta
	if (alias == null)	this.alias = nomrespuesta;
	else this.alias	= alias;

	this.desrespuesta		= desrespuesta;

	if (desrespuestaprecios==null)	this.desrespuestaprecios=desrespuesta;
	else this.desrespuestaprecios= desrespuestaprecios;	

	this.deslargarespuesta	= deslargarespuesta;
	this.valor				= valor;
	this.npregunta			= npregunta;				
}


function pregunta (npregunta, despregunta,icono)
{
	this.npregunta		= npregunta;
	this.despregunta	= despregunta;
	this.icono			= icono;
}

function incompatibilidades (respuestaincomp, preguntaincomp)
{
	//Array de respuestas incompatibles
	this.respuestaincomp	= respuestaincomp.toString().split(',');
	this.preguntaincomp		= preguntaincomp;
}

function producto (nombreproducto, precioalta, textoalta, preciolinea, preciopromo, preciodespues, productodecimal, promociones, finpromocion, cuotadesco, notas)
{
	this.nombreproducto		= nombreproducto;
	this.precioalta			= redondeo(precioalta,2);
	this.textoalta			= textoalta;
	this.preciolinea		= redondeo(preciolinea,2);
	this.preciopromo		= redondeo(preciopromo,2);
	this.preciodespues		= redondeo(preciodespues,2);
	this.productodecimal	= productodecimal;
	this.promociones		= promociones;
	this.finpromocion		= finpromocion;
	this.cuotadesco			= redondeo(cuotadesco,2);
	
	if (this.cuotadesco.toString() == '0')
		this.cuotadescodespues = redondeo('6.5',2);
	else if (this.cuotadesco != null && this.cuotadesco.toString() != '' )
		this.cuotadescodespues = redondeo(this.cuotadesco,2)
		
	this.notas				= notas;
}

var apreguntas			= new Array();
var arespuestas			= new Array();
var aincompatibilidades	= new Array();
var aproductos			= new Array();

var npreg=0;
var irep=0;
var nrep=0;

//9
apreguntas['tipoimagenio'] = new pregunta(++npreg,'Imagenio','/on/images/ico_tv1.gif');
arespuestas[++irep] = new respuesta(++nrep,'Imagenio Familiar',null,npreg,'tieneimageniofamiliar','100','familiar','Más de <strong>70 canales</strong>.');
arespuestas[++irep] = new respuesta(++nrep,'Imagenio Básico',null,npreg,'tieneimageniobasico','010','basico','Más de <strong>30 canales</strong>.');
arespuestas[++irep] = new respuesta(++nrep,'Imagenio Conexión',null,npreg,'tieneimagenioconexion','001','conexion','Acceso a Videoclub, Fútbol y demás servicios de Imagenio.');


//Metodos
function get_respuestasXpregunta (ipregunta) 
{
	//Quita lo valores undefined del array y busca los elementos del array que cumplan con el criterio de búsqueda
	return arespuestas.compact().findAll(function(erespuesta) 
	{			
		return erespuesta.npregunta == apreguntas[ipregunta].npregunta;
	})	
}

function get_respuestasXpregunta_sinnotiene (ipregunta) 
{
	//Quita lo valores undefined del array y busca los elementos del array que cumplan con el criterio de búsqueda
	return arespuestas.compact().findAll(function(erespuesta) 
	{			
		return (erespuesta.npregunta == apreguntas[ipregunta].npregunta && erespuesta.nrespuesta != 0);
	})	
}

function valorRespuesta (nomrespuesta, sCadena) 
{	
	var valor = false;
	arespuestas.compact().each(function (erespuesta){
		if (erespuesta.nomrespuesta == nomrespuesta)
		{	
			if (sCadena.charAt(erespuesta.nrespuesta-1) == "0")
			{
				valor = false;
				throw $break;
			}
			else 
			{
				if (sCadena.charAt(erespuesta.nrespuesta-1) == "1") 
				{
					valor = true;
					throw $break;				
				}
				else
				{
					valor = false;
					throw $break;
				}
			}
		}		
	});

	return valor;
}

function obtenerNumeroRespuesta (nomrespuesta)
{
	var valor=0;

	arespuestas.compact().each(function (erespuesta)
	{			
		if (erespuesta.nomrespuesta == nomrespuesta)
		{	
			valor = erespuesta.nrespuesta;
			throw $break;
		}
	});
	return valor;
}

function obtenerRespIncompatibles(sCadena)
{
	var aRespIncompatibles;

	try{aRespIncompatibles = aincompatibilidades[sCadena];}
	catch (e){aRespIncompatibles = null;}
	return aRespIncompatibles;
}

function obtenerProducto(sCadena)
{
	var aInfoProducto;

	try{aInfoProducto = aproductos[sCadena];}
	catch (e){aInfoProducto = null;}
	return aInfoProducto;
}

function buscarRespuestaEnIncompatibles(var_alistadoIncompatibles, var_numrespuesta)
{
	if (var_alistadoIncompatibles == null) return false;
	if (var_alistadoIncompatibles.toString().indexOf(var_numrespuesta) >= 0) return true;
	else return false;
}

//FUNCIONES DE FECHA
function getFechaSajona (fechaEuropea) {
	var array_fecha = fechaEuropea.split("/");
	var dia		= array_fecha[0];
	var mes		= array_fecha[1];
	var anyo	= array_fecha[2];
	var fechaSajona = mes+"/"+dia+"/"+anyo;

	return fechaSajona;
}

//FUNCIONES COMUNES.
function formateo_decimales(x,decimales)
{
	if (x.indexOf(',')==-1)
		x = x + "," + '0'.times(decimales);
	else
	{
		if (x.substr(x.indexOf(',')+1,x.length).length<decimales)
			x = x + '0'.times(decimales-x.substr(x.indexOf(',')+1,x.length).length);
	}
	return x;
}

function paso_a_comas(precio)
{
	if (precio==null || precio.toString()=='' || precio.toString()=='0')
		return precio;

	precio = precio.toString().replace('.',',');
	precio = formateo_decimales(precio,2);
	return (precio);
}

function paso_a_puntos(precio)
{
	if (precio==null || precio.toString()=='' || precio.toString()=='0')
		return precio;

	precio = precio.toString().replace(',','.');
	return (precio);
}

function redondeo(cantidad, decimales) 
{
	if (cantidad==null || cantidad.toString()=='')
		return cantidad;
	else if (cantidad.toString()=='0')
		return parseInt(cantidad,10);
		
	var cantidad = parseFloat(paso_a_puntos(cantidad));
	var decimales = parseFloat(decimales);
	decimales = (!decimales ? 2 : decimales);
	return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
} 

function fncTieneImagenio (sCadena) 
{
	if (valorRespuesta('tieneimageniofamiliar',sCadena) || valorRespuesta('tieneimageniobasico',sCadena))
		return true;
	else
		return false;
}

function fncTieneAdsl (sCadena) 
{
	if (valorRespuesta('tieneadsl3mb',sCadena) || valorRespuesta('tieneadsl1mb',sCadena) || valorRespuesta('tieneadslmini',sCadena))
		return true;
	else
		return false;
}

function fncTieneLlamadas (sCadena) 
{
	if (valorRespuesta('tienellamadas',sCadena))
		return true;
	else
		return false;
}

//¡IMPORTANTE!: Sumar 1 cuando se meta un paquete o canal nuevo (son las posiciones reservadas para paquetes y canales)
var numero_bits_tematicos				=12;
var numero_bits_alacarta				=13;
var numero_bits_serviciosadicionales	=0;
var numero_bits_suscripciones			=3;

var v_respuestas = getParametro('v_respuestas');
var tipo_seleccion_oferta="";

//Se usa por si hay que poner precios distintos para alta y configuración
if (v_respuestas != null)
{
	if (valorRespuesta('tieneimageniofamiliar',v_respuestas) || valorRespuesta('tieneimageniobasico',v_respuestas))
		tipo_seleccion_oferta = "configuracion";
	else
		tipo_seleccion_oferta = "alta";
}
