
// Funciones propias utilizadas por SiteCatalist

/*****************************************************
//  Funcion para registra el enlace en SiteCatalist
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"         
*****************************************************/
function registroEnlace(textoEnlace, enlace) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="prop18";
	s.prop18=textoEnlace;
	s.tl(enlace,'o',textoEnlace);
}

function registroBBVA(textoEnlace, enlace) {
    
                s_gi('bbvacomprod');
                s.linkTrackVars='prop28,eVar28';
                s.linkTrackEvents='event20';
                s.prop28= textoEnlace;
                s.eVar28=s.prop28;
                s.events='event20';
                s.tl(enlace,'e', textoEnlace);
}

/*****************************************************
//  Funcion para registrar la accion de guardar un pack
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - nombreProp: Nombre del Pack    
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroGuardaPack(textoEnlace, nombreProp, enlace) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="prop23,events";
	s.linkTrackEvents="event5";
	s.prop23=enlace;
	s.events = "event5";
	s.tl(enlace,'o',nombreProp + " " + textoEnlace);
}

/*****************************************************
//  Funcion para registrar la accion de imprimir un pack
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - nombreProp: Nombre del Pack    
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroImprimePack(textoEnlace, nombreProp, enlace) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="prop23,events";
	s.linkTrackEvents="event6";
	s.prop23=enlace;
	s.events = "event6";
	s.tl(enlace,'o',nombreProp + " " + textoEnlace);
}

/*****************************************************
//  Funcion para registrar la accion de publicar un pack
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - nombreProp: Nombre del Pack    
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroPublicaPack(textoEnlace, nombreProp, enlace) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="prop23,events";
	s.linkTrackEvents="event7";
	s.prop23=enlace;
	s.events = "event7";
	s.tl(enlace,'o',nombreProp + " " + textoEnlace);
}

/*****************************************************
//  Funcion para registrar la accion de enviar un pack
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - nombreProp: Nombre del Pack    
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroEnviaPack(textoEnlace, nombreProp, enlace) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="prop23,events";
	s.linkTrackEvents="event15";
	s.prop23=enlace;
	s.events = "event15";
	s.tl(enlace,'o',nombreProp + " " + textoEnlace);
}

/*****************************************************
//  Funcion para registrar la accion de solicitar un pack
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - nombreProp: Nombre del Pack    
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroSolicitaPack(textoEnlace, nombreProp, enlace) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="prop23,events";
	s.linkTrackEvents="event16";
	s.prop23=enlace;
	s.events = "event16";
	s.tl(enlace,'o',nombreProp + " " + textoEnlace);
}

/*****************************************************
//  Funcion para registrar el inicio de una simulación
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - nombreProp: Nombre del Pack    
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroIniciaSimulacion(textoEnlace, nombreProp, enlace, xml) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="products,events";
	s.linkTrackEvents="event11";
	s.events = "event11";
	if (nombreProp == "Hipoteca") {
		s.products = leeXMLHipoteca(xml)
	} else if (nombreProp == "Cuenta Corriente") {
		s.products = leeXMLCuentaCorriente(xml)
	} else if (nombreProp == "Inversion") {
		s.products = leeXMLInversion(xml)
	}
	s.tl(enlace,'o',nombreProp + " " + textoEnlace);
}

/*****************************************************
//  Funcion para registrar el final de una simulación (inicio solicitud)
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - nombreProp: Nombre del Pack    
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroFinalizaSimulacion(textoEnlace, nombreProp, enlace, xml) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="products,events";
	s.linkTrackEvents="event14";
	s.events = "event14";
	if (nombreProp == "Hipoteca") {
		s.products = leeXMLHipoteca(xml)
	} else if (nombreProp == "Cuenta Corriente") {
		s.products = leeXMLCuentaCorriente(xml)
	} else if (nombreProp == "Inversion") {
		s.products = leeXMLCuentaCorriente(xml)
	}
	s.tl(enlace,'o',nombreProp + " " + textoEnlace);
}

/*****************************************************
//  Funcion para registrar el abandono de una simulación (salir sin solicitar nada)
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - nombreProp: Nombre del Pack    
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroAbandonaSimulacion(textoEnlace, nombreProp, enlace) {
	var xml;
	xml = parent.construirXmlGuardar();
	if (window.ActiveXObject)
	{
		var doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML(xml);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else
	{
		var parser=new DOMParser();
		var doc=parser.parseFromString(xml,"text/xml");
	}// documentElement always represents the root node
	xml = doc;
	s=s_gi('bbvacomprod');
	s.linkTrackVars="products,events";
	s.linkTrackEvents="event14";
	s.events = "event14";
	if (nombreProp == "Hipoteca") {
		s.products = leeXMLHipoteca(xml)
	} else if (nombreProp == "Cuenta Corriente") {
		s.products = leeXMLCuentaCorriente(xml)
	} else if (nombreProp == "Inversion" || nombreProp == "Inversión") {
		nombreProp = "Inversion"
		s.products = leeXMLCuentaCorriente(xml)
	}
	s.tl(enlace,'o',nombreProp + " " + textoEnlace);
}

/*****************************************************
//  Funcion para leer el XML de Hipotecas
//  Parametros:
//    - xml: Extructura XML con productos
*****************************************************/
function leeXMLHipoteca(xml){
	var productos = "";
	$(xml).find("HIPOTECA_UNOE").each(function(){
		var categoria = "Hipoteca;";
		var importe = categoria + "Importe;" + $(this).find("IMPORTE").text();
		importe = importe.replace(",", ".");
		var plazo = "," + categoria + "Plazo;" + $(this).find("PLAZO").text();
		var seguroHogar = "," + categoria + "Seguro de Hogar;" + reemplazarCaracteres($(this).find("SEGURO_HOGAR").text());
		var seguroVida = "," + categoria + "Seguro de Vida;" + reemplazarCaracteres($(this).find("SEGURO_VIDA").text());
		var domNomina = "," + categoria + "Domiciliacion de Nomina;" + reemplazarCaracteres($(this).find("DOMI_NOMINA").text());
		var seguroVida = "," + categoria + "Domiciliacion de Recibos;" + reemplazarCaracteres($(this).find("DOMI_RECIBOS").text());
		var totalVentajas = categoria + "Total Ventajas;" + $(this).find("TOTAL_VENTAJAS").text();
		totalVentajas = ","  + totalVentajas.replace(",", ".");
		var cuotaMensual = categoria + "Cuota Mensual;" + $(this).find("CUOTA_MENSUAL").text();
		cuotaMensual = "," + cuotaMensual.replace(",", ".");
		if (productos == "") {
			productos = importe + plazo + seguroHogar + seguroVida + domNomina + seguroVida + totalVentajas + cuotaMensual;
		} else {
			productos = productos + "," + importe + plazo + seguroHogar + seguroVida + domNomina + seguroVida + totalVentajas + cuotaMensual;
		}
	})

	var categoria = "Hipoteca";

	if($(xml).find("CAMPANIAS").children().length != 0){
		$(xml).find("CAMPANIAS").each(function(){
			var nombre = reemplazarCaracteres($(this).children("NOMBRE").text());
			var importe = reemplazarCaracteres($(this).children("IMPORTE").text());
			importe = importe.replace(",",".");
			productos = productos + "," + categoria + ";" + nombre + ";" + importe
		});
	}

	if($(xml).find("REMUNERACION_MENSUAL").children().length != 0){
		$(xml).find("REMUNERACION_MENSUAL").each(function(){
			var nombre = reemplazarCaracteres($(this).children("NOMBRE").text());
			var porcentaje = reemplazarCaracteres($(this).children("PORCENTAJE").text());
			porcentaje = porcentaje.replace(',', '.');
			productos = productos + "," + categoria + ";" + nombre + ";" + porcentaje
		});
	}

	if($(xml).find("TARJETAS_DEBITO").children().length != 0){
		$(xml).find("TARJETAS_DEBITO").each(function(){
			var nombre = reemplazarCaracteres($(this).children("NOMBRE").text());
			var tipo = reemplazarCaracteres($(this).children("TIPO").text());
			tipo = tipo.replace(",",".");
			productos = productos + "," + categoria + ";" + nombre + ";" + tipo
		});
	}

	if($(xml).find("TARJETAS_CREDITO").children().length != 0){
		$(xml).find("TARJETAS_CREDITO").each(function(){
			var nombre = reemplazarCaracteres($(this).children("NOMBRE").text());
			var tipo = reemplazarCaracteres($(this).children("TIPO").text());
			tipo = tipo.replace(",",".");
			productos = productos + "," + categoria + ";" + nombre + ";" + tipo;
		});
	}

	productos = reemplazarCaracteres(productos);
	return productos;
}

/*****************************************************
//  Funcion para leer el XML de Cuentas
//  Parametros:
//    - xml: Extructura XML con productos
*****************************************************/
function leeXMLCuentaCorriente(xml){
	var productos = "";
	var categoria = "Cuenta Corriente";

	var numCuenta = 0;
	if($(xml).find("CORRIENTE_UNOE").children().length != 0){
		$(xml).find("CORRIENTE_UNOE").each(function(){
			numCuenta++;
			var nombre = $(this).children("NOMBRE").text();
			var importe = $(this).children("IMPORTE").text();
			importe = importe.replace(",", ".");
			if (numCuenta > 1) {
				productos = productos + "," + categoria + ";" + nombre +";"+ importe;
			} else {
				productos = productos + categoria + ";" + nombre +";"+ importe;
			}
		});
	}

	$(xml).find("AHORRO_UNOE").each(function(){
		var nombre = $(this).children("NOMBRE").text();
		var porcentaje = $(this).children("PORCENTAJE").text();
		porcentaje = porcentaje.replace(",", ".");
		if (productos == "") {
			productos = categoria + ";" + nombre +";"+ porcentaje
		} else {
			productos = productos + "," + categoria + ";" + nombre +";"+ porcentaje;
		}
	});

	$(xml).find("TARJETAS_DEBITO").each(function(){
		var nombre = reemplazarCaracteres($(this).children("NOMBRE").text());
		var tipo = reemplazarCaracteres($(this).children("TIPO").text());
		productos = productos + "," + categoria + ";" + nombre +";"+ tipo;
	});
	
	if($(xml).find("TARJETAS_CREDITO").children().length != 0){
		$(xml).find("TARJETAS_CREDITO").each(function(){
			var nombre = reemplazarCaracteres($(this).children("NOMBRE").text());
			var tipo = reemplazarCaracteres($(this).children("TIPO").text());
			productos = productos + "," + categoria + ";" + nombre +";"+ tipo;
			});
	}

	productos = reemplazarCaracteres(productos);
	return productos;
}

/*****************************************************
//  Funcion para leer el XML de Inversion
//  Parametros:
//    - xml: Extructura XML con productos
*****************************************************/
function leeXMLInversion(xml){

	var productos = "";
	var categoria = "Inversion";

	var numDeposito = 0;
	var depositos = "";
	$(xml).find("DEPOSITO").each(function(){
		numDeposito ++;
		var nombreD= $(this).find("NOMBRE").text();
		var importe = $(this).find("IMPORTE").text();
		importe = importe.replace(",", ".");
		var plazo = $(this).find("PLAZO").text();
		if (numDeposito > 1)
		{
			depositos = depositos + "," + categoria + ";" + nombreD + ";" + importe + ";" + plazo
		} else {
			depositos = categoria + ";" + nombreD + ";" + importe + ";" + plazo
		}
	});
	
	var fondosContratados = " ";
	$(xml).find("FONDOS").each(function(){
		var nombre = $(this).children("NOMBRE_CARTERA").text();
		var importe = $(this).children("IMP_CARTERA").text();
		importe = importe.replace(",", ".");
		var esCartera = false;

		if(nombre!=""){
			esCartera = true;
		}else{
			$(this).find("FONDO").each(function(){
				nombre = $(this).children("NOMBRE").text();
				importe = $(this).children("IMPORTE").text();
				importe = importe.replace(",", ".");
			});
		}

		if(nombre!=""){
			fondosContratados = categoria + ";" + nombre + ";" + importe;
		} else {
			fondosContratados = "";
		}
	});
	
	var planesContratados = "";
	$(xml).find("PLANES").each(function(){
		var nombre = $(this).find("NOMBRE").text();
		var importe = $(this).find("IMPORTE").text();
		importe = importe.replace(",", ".");
		if (nombre != "") {
			planesContratados = categoria + ";" + nombre + ";" + importe;
		} else {
			planesContratados = "";
		}
	});

	if (depositos != "") {
		productos = depositos;
	}
	
	if (fondosContratados != "") {
		if (productos != "") {
			productos = productos + "," + fondosContratados;
		} else {
			productos = fondosContratados;
		}
	}
	
	if (planesContratados!= "") {
		if (productos != "") {
			productos = productos + "," + planesContratados;
		} else {
	productos = planesContratados;
		}
	}

	if($(xml).find("CAMPANIAS").children().length != 0){
		$(xml).find("CAMPANIAS").each(function(){
			var nombre = $(this).children("NOMBRE").text();
			var importe = $(this).children("IMPORTE").text();
			importe = importe.replace(",",".");
			productos = productos + "," + categoria + ";" + nombre + ";" + importe
		});
	}

	if($(xml).find("REMUNERACION_MENSUAL").children().length != 0){
		$(xml).find("REMUNERACION_MENSUAL").each(function(){
			var nombre = reemplazarCaracteres($(this).children("NOMBRE").text());
			var porcentaje = reemplazarCaracteres($(this).children("PORCENTAJE").text());
			porcentaje = porcentaje.replace(',', '.');
			productos = productos + "," + categoria + ";" + nombre + ";" + porcentaje
		});
	}

	if($(xml).find("TARJETAS_DEBITO").children().length != 0){
		$(xml).find("TARJETAS_DEBITO").each(function(){
			var nombre = reemplazarCaracteres($(this).children("NOMBRE").text());
			var tipo = reemplazarCaracteres($(this).children("TIPO").text());
			tipo = tipo.replace(",",".");
			productos = productos + "," + categoria + ";" + nombre + ";" + tipo
		});
	}

	if($(xml).find("TARJETAS_CREDITO").children().length != 0){
		$(xml).find("TARJETAS_CREDITO").each(function(){
			var nombre = reemplazarCaracteres($(this).children("NOMBRE").text());
			var tipo = reemplazarCaracteres($(this).children("TIPO").text());
			tipo = tipo.replace(",",".");
			productos = productos + "," + categoria + ";" + nombre + ";" + tipo;
		});
	}

	productos = reemplazarCaracteres(productos);
	return productos;
}

/*****************************************************
//  Funcion para reemplazar caracteres especiales
//  Parametros:
//    - cadena: String con caracteres especiales
*****************************************************/
function reemplazarCaracteres (cadena){
	var carEspeciales = new Array('Ñ','ñ','Á','á','À','à','Â','â','Ã','ã','Ä','ä','É','é','È','è','Ê','ê','Ë','ë','Ì','ì','Í','í','Î','î','Ï','ï','Ò','ò','Ó','ó','Õ','õ','Ô','ô','Ö','ö','Ù','ù','Ú','ú','Û','û','Ü','ü','Ç','ç','º','ª','¡','!','¿','?');
	var carNormales  =  new Array('N','n','A','a','A','a','A','a','A','a','A','a','E','e','E','e','E','e','E','e','I','i','I','i','I','i','I','i','O','o','O','o','O','o','O','o','O','o','U','u','U','u','U','u','U','u','C','c',' ',' ',' ',' ',' ',' ');
	var numCaractEspeciales = carEspeciales.length;
	for (i=0;i<numCaractEspeciales;i++) 
	{ 
		cadena = cadena.replace(carEspeciales[i],carNormales[i]); 
	} 
	return cadena; 	
}

/*****************************************************
//  Funcion para registrar la accion de descargar un PDF
//  Parametros:
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registraPDF(enlace) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="prop19";
	s.prop19=enlace;
                var nombre = reemplazarCaracteres($(enlace).text());
	s.tl(enlace,'o',nombre);
}

/*****************************************************
//  Funcion para registrar la accion de iniciar un formulario
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroIniciaForm(textoEnlace, enlace) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="events";
	s.linkTrackEvents="event20";
	s.events = "event20";
	s.tl(enlace,'o',textoEnlace);
}

/*****************************************************
//  Funcion para registrar la accion de iniciar un formulario
//  Parametros:
//    - textoEnlace: Texto que aparecera en SiteCatalist para este enlace
//    - enlace: Objeto enlace. Normalmente la funcion que llame pondra "this"
*****************************************************/
function registroEnviaForm(textoEnlace, enlace) {
	s=s_gi('bbvacomprod');
	s.linkTrackVars="events";
	s.linkTrackEvents="event21";
	s.events = "event21";
	s.tl(enlace,'o',textoEnlace);
}
