<!--
/*M001 12-04-2004 Cambia Funcionamiento Operaciòn Pago Ahora se Selecciona Servicio Luego Empresa*/
/*Modificación		: M002
Fecha Modificación	: 16/01/2006
Autor			: Paola Castro
Empresa			: Anticipa
Objetivo		: Se modificó fuction OnChangeBiller(select), levanta popup si pareja biller-servicio posee.
			  Se modificó fuction AgregarCuentasaPagar(), valida ingreso de identificador cuando tiene popup.
			  Se modificó fuction VerIdentificador(), valida  ventana ejemplo de identificador, no se muestra si tiene popup. 
			  		
Modificación		: M003
Fecha Modificación	: 29/06/2007
Autor			: Alonso Vega
Empresa			: Anticipa S.A
Objetivo		: Se modificó fuction onChangeBiller(select), para controlar el ingreso combinado de los Servicios de las Autopistas (Pago TAG)
			  y nuevo servicio Pago Total TAG. 

Modificación       : M004
Fecha Modificación : 28/08/2007
Autor              : Yasna Olmos A.
Empresa            : Anticipa S.A.
Objetivo           : Se modificó la función OnChangeBiller para incluir el filtro de Servicio al levantar el popup.				

Modificación       : M005
Fecha Modificación : 27/04/2010
Autor              : Gonzalo Gonzalez
Empresa            : Anticipa S.A.
Objetivo           : Se incorpora validacion popup, coreccion package 20100129_AVG_Mensaje_Elementos_Emergentes_T8193

Modificación       : M006
Fecha Modificación : 06/08/2010
Autor              : Edison Romero
Empresa            : Anticipa S.A.
Objetivo           : Se agrega altitud del pop up de las paginas de los pases diarios 
			  
*/

var arrayBillers		= new Array(); //Arreglo Empresas (Billers)
var arrayServicios		= new Array(); //Arreglo Servicios
var arrayCuentasaPagar	= new Array(); //Arreglo Cuentas a Pagar
var lix = 0;	
var indBillers = -1;
		
function Vacio(stext){
	Salida = '';
	for(ljx=0;ljx<stext.length;ljx++){
		if (stext.substring(ljx,ljx+1) != ' '){
			Salida = Salida + stext.substring(ljx,ljx+1)
		}
	}
	return Salida;
}	
	
/* Función Invocada desde Submit de Formulario */
function ValidaIngreso(){
	if (Vacio(document.formPagoCuentas.identificador.value) != ''){
		AgregarCuentasaPagar();		
	}
	return false;		
}
	
/* Función Invocada desde Submit de Formulario */
function ValidaIngresoCuentaNoInscrita(){
	if (Vacio(document.formPagoCuentas.identificador.value) != ''){
		AgregarCuentasNoInscritas();		
	}
	return false;		
}	
	
//Agrega Billers y Servicios a los respectivos Select
function CargarBillers(){
	var oForm = document.formPagoCuentas;		
	var tmpBiller = "";
	if (indBillers == -1){
		alert('Ocurrió Un Error en La carga de la Empresas');
		return;	
	}	
	for (lix=0;lix<arrayBillers.length;lix++){
		if (tmpBiller != arrayBillers[lix][1]){			
			// Formato Llamada - SelectAddOption(Select, NombreBiller, IdBiller);				
			SelectAddOption(oForm.billers, arrayBillers[lix][2], arrayBillers[lix][1]);
		}
		tmpBiller = arrayBillers[lix][1];
	}
}
	
/* M001 Carga Servicios */
function CargarServicios(){
	var oForm = document.formPagoCuentas;		
	var tmpBiller = "";
	if (indBillers == -1){
		alert('Ocurrió Un Error en La carga de Servicios');
		return;	
	}
	for (lix=0;lix<arrayBillers.length;lix++){
		if (tmpBiller != arrayBillers[lix][0]){
			// Formato Llamada - SelectAddOption(Select, NombreBiller, IdBiller);				
			SelectAddOption(oForm.servicios, arrayBillers[lix][3], arrayBillers[lix][0]);
		}
		tmpBiller = arrayBillers[lix][0];
	}
	//document.formPagoCuentas.billers.//modificación usuario registrado
	selectBillers = document.formPagoCuentas.billers;
	SelectClear(selectBillers);	//Limpia Select Billers
	SelectAddOption(selectBillers, "[Empresa]","0");		
}

/* M001 */
function onChangeServicio(select){
	selectBillers = document.formPagoCuentas.billers;
	SelectClear(selectBillers);	//Limpia Select Billers
	if (select.value == 0){
		SelectAddOption(selectBillers, "[Empresa]","0");
		document.formPagoCuentas.identificador.value = "";
		document.formPagoCuentas.identificador.disabled = true;			
		return;
	}
	SelectAddOption(selectBillers, "[Empresa]","0");
	for (lix=0;lix<arrayBillers.length;lix++){
		if (arrayBillers[lix][0] == select.value){
			SelectAddOption(selectBillers, arrayBillers[lix][2], arrayBillers[lix][1]);
			document.formPagoCuentas.billers.focus();
		}
	}		
}

function onChangeBiller(select){
	var pagina = document.formPagoCuentas.pagina_origen.value;//variable que indica a que página corresponde 
//	var opciones = "toolbar=no,location=no,directories=no,resizable=no,width=525,height=510";
	var opciones = "toolbar=no,location=no,directories=no,resizable=no,width=525,height=550";//propiedades ventana //M006
	var serv_sel = document.formPagoCuentas.servicios.options[document.formPagoCuentas.servicios.selectedIndex].value;//M004
	if (select.value != 0){
	  	for (lix=0;lix<arrayBillers.length;lix++){
			// M003
			if (arrayCuentasaPagar.length > 0){
				for(ljx=0;ljx<arrayCuentasaPagar.length;ljx++){
					if ((arrayCuentasaPagar[ljx][0] == 130) && ((select.value == 677) || (select.value == 669) || (select.value == 617) || (select.value == 833))){
						alert('Ya tiene seleccionado Pago Total TAG,\npor este motivo no puede seleccionar una Autopista.\n\nSi desea seleccionar una Autopista,\ndebe desmarcar el Pago Total TAG');
						document.formPagoCuentas.servicios.selectedIndex = 0;		
						onChangeServicio(document.formPagoCuentas.servicios);
						return;	
					}
					if (((arrayCuentasaPagar[ljx][0] == 677) || (arrayCuentasaPagar[ljx][0] == 669) || (arrayCuentasaPagar[ljx][0] == 617) || (arrayCuentasaPagar[ljx][0] == 833)) && (select.value == 130)){
						alert('Ya tiene seleccionada una Autopista,\npor este motivo no puede seleccionar Pago Total TAG.\n\nSi desea seleccionar Pago Total TAG,\ndebe desmarcar todas las Autopistas');
						document.formPagoCuentas.servicios.selectedIndex = 0;		
						onChangeServicio(document.formPagoCuentas.servicios);							
						return;	
					}
				}
			}
			// M003
			if (arrayBillers[lix][1] == select.value){ //M002
				if (arrayBillers[lix][8] == 1){ //M002(levanta pagina ingreso de datos)
					if (arrayBillers[lix][0] == serv_sel){ //M004
					   var url = arrayBillers[lix][9];//M002
					   //var hora = arrayBillers[lix][10];//M002
					   document.formPagoCuentas.identificador.value = "";//M002
					   document.formPagoCuentas.identificador.disabled = true;//M002
				     //Inicio M005
				     var valpop = window.open(url+"&opcion="+pagina,"ventana",opciones);//M002
				     if (!valpop)
							{
							 	alert('Estimado Cliente, la configuración actual de su navegador no permite el correcto funcionamiento de Servipag.com, Usted debe tener habilitados los elementos emergentes o pop-ups para pagar sus cuentas en Servipag.com, Ante cualquier duda con respecto a cómo habilitar estos elementos solicitamos se dirija a nuestro contáctenos.');
							}
							else
							{
								if (!valpop.name){
									alert('Estimado Cliente, la configuración actual de su navegador no permite el correcto funcionamiento de Servipag.com, Usted debe tener habilitados los elementos emergentes o pop-ups para pagar sus cuentas en Servipag.com, Ante cualquier duda con respecto a cómo habilitar estos elementos solicitamos se dirija a nuestro contáctenos.');
								}		 		
							}
							//Fin M005
				     //window.open("/browse.asp?pagina=web/popup_pase_diario.htm&opcion="+pagina ,"",opciones);
					   return;
		 	     }
				}
			}else{
			        document.formPagoCuentas.identificador.disabled = false;
			 	document.formPagoCuentas.identificador.focus();
		    	}
		   }
	}else{
		document.formPagoCuentas.identificador.value = "";
		document.formPagoCuentas.identificador.disabled = true;
	}
}	

//Agrega Opciones a la Lista
function SelectAddOption(select, text, value){
	select.options[select.length] = new Option(text, value, false, false);
}
	
//Limpia Elementos de la Lista
function SelectClear(select){
	select.length = 0;
}
	
//Verifica Si cuenta Para el mismo Biller y Servicio Existe
function ExisteCuenta(idBiller, idServicio, Cuenta){
	existe = false;		
	if (arrayCuentasaPagar.length > 0){
		for(ljx=0;ljx<arrayCuentasaPagar.length;ljx++){					
		       //Solo Verifica en Cuentas Activas
			if ((idBiller == arrayCuentasaPagar[ljx][0]) && (idServicio == arrayCuentasaPagar[ljx][1]) && (Cuenta == arrayCuentasaPagar[ljx][2]) && arrayCuentasaPagar[ljx][4] == 'A'){
				existe = true;
				break;
			}
		}
	}
	return existe;
}
	
/*Agrega Cuentas a Pagar*/
function AgregarCuentasaPagar(){
	var selBiller	 = document.formPagoCuentas.billers.options[document.formPagoCuentas.billers.selectedIndex].value;
	var selServicio = document.formPagoCuentas.servicios.options[document.formPagoCuentas.servicios.selectedIndex].value;
	/* M001 */
	if (selServicio == "0"){
		alert('Seleccione Servicio');
		document.formPagoCuentas.servicios.focus();
		return;
	}		
	if (selBiller == "0"){
		alert('Seleccione Empresa');
		document.formPagoCuentas.billers.focus();
		return;
	}
	document.formPagoCuentas.identificador.value = Vacio(document.formPagoCuentas.identificador.value);
	if (document.formPagoCuentas.identificador.value == ''){
		if (document.formPagoCuentas.identificador.disabled == true){
			document.formPagoCuentas.servicios.selectedIndex = 0;//M002	
			onChangeServicio(document.formPagoCuentas.servicios);//M002
			alert('Seleccione Servicio');
			return;
		}else{
			alert('Ingrese Identificador de la Cuenta');
			document.formPagoCuentas.identificador.focus();
			return;
		}
	}
	/*Verifica si Cuenta ya Existe*/
	if (ExisteCuenta(selBiller, selServicio, document.formPagoCuentas.identificador.value)){
		alert('La Cuenta ya Fue Ingresada');
		if (document.formPagoCuentas.identificador.disabled == true){
			document.formPagoCuentas.servicios.selectedIndex = 0;//cambio		
			onChangeServicio(document.formPagoCuentas.servicios);//cambio
			return;
		}else{
			document.formPagoCuentas.identificador.value = '';
			document.formPagoCuentas.identificador.focus();
			return;
		}
	} 
         /*Agrega Cuenta a Select Cuentas*/
	ValorAgregar = selBiller;
        /*Texto que se muestra en el Select de Cuentas Seleccionadas*/
	TextoAgregar = document.formPagoCuentas.billers.options[document.formPagoCuentas.billers.selectedIndex].text;
	SelectAddOption(document.formPagoCuentas.CuentasSeleccionadas, TextoAgregar, ValorAgregar);
        /*Agrega a Array de Cuentas a Pagar*/
	lenarrayCuentasaPagar = arrayCuentasaPagar.length;		
	arrayCuentasaPagar[lenarrayCuentasaPagar] = new Array();
	arrayCuentasaPagar[lenarrayCuentasaPagar][0] = document.formPagoCuentas.billers.options[document.formPagoCuentas.billers.selectedIndex].value; //Id Biller
	arrayCuentasaPagar[lenarrayCuentasaPagar][1] = document.formPagoCuentas.servicios.options[document.formPagoCuentas.servicios.selectedIndex].value; //Id Servicio
	arrayCuentasaPagar[lenarrayCuentasaPagar][2] = document.formPagoCuentas.identificador.value; //Identificador
	arrayCuentasaPagar[lenarrayCuentasaPagar][3] = TextoAgregar; //Texto Que se Muesta en Select
	arrayCuentasaPagar[lenarrayCuentasaPagar][4] = 'A'; //Estado Cuenta Seleccionada (Activa - Desactiva)
	arrayCuentasaPagar[lenarrayCuentasaPagar][5] = document.formPagoCuentas.billers.options[document.formPagoCuentas.billers.selectedIndex].text; //Nombre Biller
	arrayCuentasaPagar[lenarrayCuentasaPagar][6] = document.formPagoCuentas.servicios.options[document.formPagoCuentas.servicios.selectedIndex].text; //Nombre Servicio
	/* Verifica si se ingresaron cuentas para el mismo biller */
	ExisteBillerEnCuentas(arrayCuentasaPagar[lenarrayCuentasaPagar][0], 'I');
	/* M001 */
	document.formPagoCuentas.servicios.selectedIndex = 0;		
	onChangeServicio(document.formPagoCuentas.servicios);
	document.formPagoCuentas.identificador.value = "";
	document.formPagoCuentas.identificador.disabled = true;
}
	
/* Elimina Cuentas a Pagar */
function EliminarCuentasaPagar(){
	if (arrayCuentasaPagar.length == 0){
			alert('No ha agregado Ninguna Cuenta para Pagar');
	}else if (document.formPagoCuentas.CuentasSeleccionadas.selectedIndex == -1){
			alert('Seleccione la cuenta que desea eliminar');
	}else{
		arrayCuentasaPagar[document.formPagoCuentas.CuentasSeleccionadas.selectedIndex][4] = "D";			
		/*Actualizar Select Cuentas a Pagar (Para mas de un Biller)*/
		ExisteBillerEnCuentas(arrayCuentasaPagar[document.formPagoCuentas.CuentasSeleccionadas.selectedIndex][0], 'E');
		RefreshSelectCuentasaPagar();			
	}
}
	
/*Actualiza Select CuentasaPagar, luego de Eliminar un elemento*/
function RefreshSelectCuentasaPagar(){
	arrayTemp = new Array();		
	liTmp	= -1;
	for(ljx=0;ljx<arrayCuentasaPagar.length;ljx++){
		if (arrayCuentasaPagar[ljx][4] == 'A'){
			liTmp++;				
			arrayTemp[liTmp] = arrayCuentasaPagar[ljx];				
		}
	}		
	document.formPagoCuentas.CuentasSeleccionadas.length = 0;		
	arrayCuentasaPagar.length = 0;
	arrayCuentasaPagar = arrayTemp;
	for(ljx=0;ljx<arrayCuentasaPagar.length;ljx++){
		SelectAddOption(document.formPagoCuentas.CuentasSeleccionadas, arrayCuentasaPagar[ljx][3], arrayCuentasaPagar[ljx][0]);
	}		
}
	
//Verifica Si Existe Biller ya Ingresado en Cuentas Seleccionadas
function ExisteBillerEnCuentas(idBiller, accion){
	arrayCuantosBillers = new Array();
	CuantosBillers = 0;
	BillerParaAgregar = idBiller;
	if (arrayCuentasaPagar.length > 1){
		for(ljx=0;ljx<arrayCuentasaPagar.length;ljx++){
			if (arrayCuentasaPagar[ljx][4] == 'A'){
				if (arrayCuentasaPagar[ljx][0] == BillerParaAgregar){
					arrayCuantosBillers[CuantosBillers] = ljx;
					CuantosBillers++
				}
			}
		}
	}				
	if (CuantosBillers > 1){
		for(ljx=0;ljx<arrayCuantosBillers.length;ljx++){
			posNro = arrayCuentasaPagar[arrayCuantosBillers[ljx]][3].search("_N°");
			if (posNro > -1){
				arrayCuentasaPagar[arrayCuantosBillers[ljx]][3] = arrayCuentasaPagar[arrayCuantosBillers[ljx]][3].substring(0,posNro);
			}
			arrayCuentasaPagar[arrayCuantosBillers[ljx]][3] = arrayCuentasaPagar[arrayCuantosBillers[ljx]][3]+'_N°'+(ljx+1);			
		}
		if (accion != 'E')	RefreshSelectCuentasaPagar();
	}else if (CuantosBillers == 1){
		posNro = arrayCuentasaPagar[arrayCuantosBillers[0]][3].search("_N°");
		if (posNro > -1){
			arrayCuentasaPagar[arrayCuantosBillers[0]][3] = arrayCuentasaPagar[arrayCuantosBillers[0]][3].substring(0,posNro);
		}			
	}
	arrayCuantosBillers.length = 0;
}
	
//Busca Posición en Array "arrayBillers" información para el Biller - Servicio Seleccionado
function BuscaUbicacionBillerServicio(){
	var SelBiller		= document.formPagoCuentas.billers.options[document.formPagoCuentas.billers.selectedIndex].value;
	var SelServicio	= document.formPagoCuentas.servicios.options[document.formPagoCuentas.servicios.selectedIndex].value;
	var Ubicacion		= -1;		
	for(i=0;i<arrayBillers.length;i++){
		if ((arrayBillers[i][1] == SelBiller) && (arrayBillers[i][0] == SelServicio)){
			Ubicacion = i;
			break;
		}
	}
	return Ubicacion;
}
	
// Ver Identificador de la Cuenta
function VerIdentificador(){
	var Params	= '';
	var Pos		= 0;
	select		= document.formPagoCuentas.billers;
	if (select.selectedIndex == 0){
		alert('Seleccione la Empresa y el Servicio para el cual desea ver el identificador');
	}else{
		Pos = BuscaUbicacionBillerServicio();			
		Params += '&Biller='+CambiaEspaciosPorMas(arrayBillers[Pos][2]);
		Params += '&IdentificadorPrimario='+CambiaEspaciosPorMas(arrayBillers[Pos][5]);						
		Params += '&EjemploIdentificador='+CambiaEspaciosPorMas(arrayBillers[Pos][7]);			
		Params += '&ImagenBoleta='+CambiaEspaciosPorMas(arrayBillers[Pos][4]);
		Params += '&IdentificadorSecundario='+CambiaEspaciosPorMas(arrayBillers[Pos][6]);
		var bloquea_iden = CambiaEspaciosPorMas(arrayBillers[Pos][8]);//M002
	        if (bloquea_iden != 1){//M002
		 Options = "'resizable=no,menubar=no,location=no,toolbar=no,status=no,scrollbars=yes,directories=no,width=525,height=440,left=0,top=0'";
		 window.open('/browse.asp?pagina=web/identificador_cuenta.htm'+Params, 'IdentificadorCuenta', Options);
		}
	}
}
	
//Cambia Valor de Cadena donde encuentre " " lo reemplaza por "+"
function CambiaEspaciosPorMas(StrCambio){
	var Salida = '';
	var Cambio
	Cambio = / /gi;		
	Salida	= StrCambio.replace(Cambio,"+");		
	Cambio = /á/gi;
	Salida	= Salida.replace(Cambio,"a");
	Cambio = /é/gi;
	Salida	= Salida.replace(Cambio,"e");
	Cambio = /í/gi;
	Salida	= Salida.replace(Cambio,"i");
	Cambio = /ó/gi;
	Salida	= Salida.replace(Cambio,"o");
	Cambio = /ú/gi;
	Salida	= Salida.replace(Cambio,"u");
	return Salida;	
}
	
/*Envia a Página Busqueda Pagos desde Home Logueado*/
function iniciarpago(){
	if (arrayCuentasaPagar == ""){
		location.replace('https://'+document.domain+'/browse.asp?pagina=web/busqueda_cuentas.htm&IntTipoBusqueda=1');
	}else{
		enviar();
	}
}
	
/*Envia Datos seleccionados Búsqueda Pagos Solo desde La Home*/
function enviar(){
	var idenCuentas = '';
	if (arrayCuentasaPagar == ""){
		alert('No ha seleccionado cuentas a Pagar');
	}else{
		for(ljx=0;ljx<arrayCuentasaPagar.length;ljx++){
			idenCuentas = idenCuentas+arrayCuentasaPagar[ljx][1]+", "+arrayCuentasaPagar[ljx][0]+", '"+arrayCuentasaPagar[ljx][2]+"',"
		}
		idenCuentas = idenCuentas.substring(0,idenCuentas.length-1);			
		document.formPagoCuentas.arrayCuentas.value = idenCuentas;
		/* Envio de Datos con SSL */
		document.formPagoCuentas.action = 'https://'+document.domain+'/browse.asp';
		document.formPagoCuentas.submit();		
	}
}
	
//Envia Datos seleccionados Para Registrar
function enviar_registro(){
	var idenCuentas = '';
	if (arrayCuentasaPagar == ""){
		alert('No ha seleccionado cuentas a Pagar');
	}else{
	        for(ljx=0;ljx<arrayCuentasaPagar.length;ljx++){
			idenCuentas = idenCuentas+arrayCuentasaPagar[ljx][0]+", "+arrayCuentasaPagar[ljx][1]+", '"+arrayCuentasaPagar[ljx][2]+"',"
		}
		idenCuentas = idenCuentas.substring(0,idenCuentas.length-1);			
		document.formPagoCuentas.arrayCuentas.value = idenCuentas;			
		document.formPagoCuentas.submit();			
	}
}
	
//Envia Datos Seleccionados Agrega Cuentas No Inscritas (Página Boleta)
function AgregarCuentasNoInscritas(){
	var idenCuentas = '';
	if (document.formPagoCuentas.billers.selectedIndex == 0){
		alert('No ha seleccionado cuentas a Pagar');
		return;
	}
	document.formPagoCuentas.identificador.value = Vacio(document.formPagoCuentas.identificador.value);
	if (document.formPagoCuentas.identificador.value == ''){
		alert('Ingrese Identificador de la Cuenta');
		document.formPagoCuentas.identificador.focus();
		return;
	}
	if (document.formPagoCuentas.TipoConsulta.value == 'CLIENTE') document.formPagoCuentas.intTipoBusqueda.value = 1;		
	document.formPagoCuentas.IdBiller.value = document.formPagoCuentas.billers.options[document.formPagoCuentas.billers.selectedIndex].value;
	document.formPagoCuentas.IdServicio.value = document.formPagoCuentas.servicios.options[document.formPagoCuentas.servicios.selectedIndex].value;		
	document.formPagoCuentas.arrayCuentas.value = document.formPagoCuentas.IdServicio.value+", "+document.formPagoCuentas.IdBiller.value+", '"+document.formPagoCuentas.identificador.value+"'";
	document.formPagoCuentas.submit();
}
//-->
