// JavaScript Document
//funciones de acciones sobre la informacion

function Asc(String){
    return String.charCodeAt(0);
}

function Chr(AsciiNum){
    return String.fromCharCode(AsciiNum)
}

var cr= new Array(" ","A","B","C","D","E","F","G","H","I","J","K","L","M","N","*","O","P","Q","R","S","T","U","V","W","X","Y","Z","'","(",")","=","_","|","0","1","2","3","4","5","6","7","8","9",".",",");
	
function eCr(lcCadena){
	v1=7; v2=1;
	tam=cr.length;
	lcCadena=lcCadena.toUpperCase().trim();
	cryp="";	
	for (i=0;i<lcCadena.length;i++){
		numx=0;
		let=lcCadena.substring(i, i+1);
		numx=cr.indexOf(let);
		resop=v1*numx+v2;
		md=resop%tam;
		cryp=cryp+""+cr[md];
	}
	return cryp;
}

function dCr(lcCadena){
	v1=7; v2=1;
	tam=cr.length;
	lcCadena=lcCadena.toUpperCase().trim();
	inv=33;
	dcryp="";	
	for (i=0;i<lcCadena.length;i++){
		numx=0;
		let=lcCadena.substring(i, i+1);
		numx=cr.indexOf(let);
		resop=(numx-v2)*inv;
		md=resop%tam;
		if(md<0){		
		  while(md<0){
			md=md+tam;
		  }
		}
		dcryp=dcryp+""+cr[md];
	}
	
	return dcryp;
}

function dCrip(lcCadena,lcPatron){
    lcR=""
    if(lcPatron==""){
       lcPatron = "ABCDEFGHIJ"
    }
    
    lcCadena = lcCadena.replace( "__________", Chr(39))
    lcCadena = lcCadena.replace("++++++++++", Chr(0))

    for(lnI = 0; lnI< lcCadena.length;lnI++){
        lnC1 = Asc(lcCadena.substring(lnI, lnI+1))
        if(lnI > lcPatron.length-1){
            lnPos = (lnI % (lcPatron.length-1))
            lnC2 = Asc(lcPatron.substring(lnPos, lnPos+1))
        }
        else{
            lnC2 = Asc(lcPatron.substring(lnI, lnI+1))
        }
        lnC1 = lnC1 - lnC2 - 64
        if(lnC1 < 0){
            lnC1 = 256 + lnC1
        }
        lcR = lcR + Chr(lnC1)
    }
    return lcR
}
        
function eCrip(lcCadena,lcPatron){
    lcR=""
   if(lcPatron == ""){
      lcPatron = "ABCDEFGHIJ"
   }
    for(lnI = 0;lnI<lcCadena.length;lnI++){
        lnC1 = Asc(lcCadena.substring(lnI,lnI+1))
    
        if(lnI > lcPatron.length-1){
            lnPos = (lnI % (lcPatron.length-1))
            lnC2 = Asc(lcPatron.substring(lnPos, lnPos+1))
        }
        else {
            lnC2 = Asc(lcPatron.substring(lnI, lnI+1))
        }
        lnC1 = lnC1 + lnC2 + 64
        if(lnC1 > 255) {
            lnC1 = lnC1 - 256
        }
       
        switch(lnC1){
            case 39 :
                lcR = lcR + "__________"
                break;
            case 0 :
                lcR = lcR + "++++++++++"
                break;
            default:
                lcR = lcR + Chr(lnC1)
        }
    }
    return lcR
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function btnNuevoColorbox(grilla,info){
	if( info.nom_pag.indexOf("#") !== 0){pager = "#"+info.nom_pag;}else{pager=info.nom_pag;}	
	grilla.navSeparatorAdd(pager);
	
	var editurl=grilla.jqGrid('getGridParam','editurl')

	grilla.navButtonAdd(pager,{
		  caption:"", 
		  buttonicon : 'ui-icon-plus', 
		  title:info.title,
		  onClickButton: function(){ 
			$.fn.colorbox({
			  href: editurl + info.param,
			  width:  info.ancho,
			  height: info.alto,
			  title: info.title,
			  transition:'elastic', 
			  iframe:info.iframe,
			  onClosed:function(){
				  if(info.recarga){
					  grilla.trigger("reloadGrid");
				  }
			  }
        	});	
		  },
		 position:"last"	
	});	
	$(pager).find(".ui-icon-plus").css({"background-image":"url('images/nuevo.png')", "background-position":"0","width":"16px","height":"16px","cursor":"pointer"});
}

function btnEditarColorbox(grilla,info){
	if( info.nom_pag.indexOf("#") !== 0){pager = "#"+info.nom_pag;}else{pager=info.nom_pag;}	
	grilla.navSeparatorAdd(pager);
	var editurl=grilla.jqGrid('getGridParam','editurl')

	grilla.navButtonAdd(pager,{
	  caption:"", 
	  buttonicon : 'ui-icon-pencil', 
	  title:info.title,
	  onClickButton: function(){ 
		var gr = jQuery("#"+info.ide_tabla).getGridParam("selrow"); 
		if( gr != null ){ 
			$.fn.colorbox({
				  href: editurl+info.param+"&codigo="+gr,
				  width:  info.ancho,
				  height: info.alto,
				  title: info.title,
				  transition:'elastic', 
				  iframe:info.iframe,
				  onClosed:function(){
					  if(info.recarga){
					  	grilla.trigger("reloadGrid");
					  }
			  	  }
			});	
		}else{
			jAlert(info.msg_sel,info.empresa); 
		}
	  },
	 position:"last"	
	});	
	$(pager).find(".ui-icon-pencil").css({"background-image":"url('images/editar.png')", "background-position":"center","width":"16px","height":"16px","cursor":"pointer"});
}

function btnEliminar(grilla,info){
	if(!info.adicional){
		info.adicional=null;	
	}
	if( info.nom_pag.indexOf("#") !== 0){pag = "#"+info.nom_pag;}else{pag=info.nom_pag;}	
	grilla.navSeparatorAdd(pag);
	
	var pager =grilla.jqGrid('getGridParam','pager')
	var modulo=info.modulo;
	var editurl="";
	if (info.isgrid){
		editurl=grilla.jqGrid('getGridParam','editurl')
	}else{
		editurl=info.url;
	}
	grilla.navButtonAdd(pag,{
	  caption:"", 
	  buttonicon : 'ui-icon-trash', 
	  title:info.title,
	  onClickButton: function(){ 
		var gr = jQuery("#"+info.ide_tabla).getGridParam("selrow"); 
		if( gr != null ){ 
			jConfirm(info.msg_confirma,info.empresa,function(r){
				if (r){
					$.ajax({
						type:"POST",
						url: editurl,
						data:{oper:'del',id:gr},
						async:false,
						success:function(rta){
							if (rta!=0){
								if (rta=='session'){
									modulo='global'
								}
								jAlert(getMsg(modulo,rta),info.empresa,function(){
									if(rta=='session'){
										location.reload();	
									}	
								});
							}else{
								if(info.recarga){
									grilla.trigger('reloadGrid');
									notifica_ok(info.msgExito);	
								}
								if($.isFunction(info.adicional)){
									info.adicional();
								}	
							}	
						}
					});
				}	
			});	
		}else{
			jAlert(info.msg_sel,info.empresa); 
		}
	  },
	 position:"last"	
	});	
	$(pag).find(".ui-icon-trash").css({"background-image":"url('images/eliminar.png')", "background-position":"center","width":"16px","height":"16px","cursor":"pointer"});
}

/*-------------------------------------------------------------------------------------------------------------------------------------------------------
	Coloca un boton para agregar un nuevoregistro  deacuaerdo al formato que arma  la grilla
-------------------------------------------------------------------------------------------------------------------------------------------------------*/
function btnNewReginLine(grilla, info){
	var new_id=Math.random()
	if( info.nom_pag.indexOf("#") !== 0){pag = "#"+info.nom_pag;}else{pag=info.nom_pag;}
	grilla.navButtonAdd(pag,{
		  caption:"", 
		  buttonicon : 'ui-icon-plus', 
		  title:info.title,
		  onClickButton: info.Click,
		  position:"last"
	});
	$(pag).find(".ui-icon-plus").css({"background-image":"url('images/nuevo.png')", "background-position":"center","width":"16px","height":"16px","cursor":"pointer"});	
}

/*-------------------------------------------------------------------------------------------------------------------------------------------------------
	Coloca un boton para agregar un nuevoregistro  deacuaerdo al formato que arma  la grilla
-------------------------------------------------------------------------------------------------------------------------------------------------------*/

function btnNuevoNor(grilla,info){
	if( info.nom_pag.indexOf("#") !== 0){pag = "#"+info.nom_pag;}else{pag=info.nom_pag;}
		
	grilla.navSeparatorAdd(pag);
	var pager =grilla.jqGrid('getGridParam','pager')
	var modulo =info.modulo;
	grilla.navButtonAdd(pag,{
		  caption:"", 
		  buttonicon : 'ui-icon-plus', 
		  title:info.title,
		  onClickButton: function(){ 
			jQuery("#"+info.ide_tabla).jqGrid('editGridRow','new',{
				reloadAfterSubmit:info.recarga,
				modal:info.modal,
				width:info.ancho,
				closeAfterAdd:true,
				recreateForm : true, //buelve a crear el formulario
				beforeSubmit:function(){
					if($(":input").hasClass('campos_error')){
						jAlert(info.msg_cmps_oblg,info.empresa)
					}else{
						return [true]
					}
				},
				afterSubmit:function(xhr,postdata){ 
					rta=xhr.responseText;
					if(rta==0){ 
						notifica_ok(info.msgExito);	
						return [true]
					}else{
						if(rta=='session')
							modulo='global'
						jAlert(getMsg(modulo,rta),info.empresa,function(){
							if(rta=='session')
								location.reload();
						})
						return [true];	
					}
				},
			});
		 },
		 position:"last"	
	});
	$(pag).find(".ui-icon-plus").css({"background-image":"url('images/nuevo.png')", "background-position":"center","width":"16px","height":"16px","cursor":"pointer"});	
}

/*-------------------------------------------------------------------------------------------------------------------------------------------------------
//funcion para agreagar un boton de editar a la grilla; este boston se crea con respecto a los datos y formato de la grilla
-------------------------------------------------------------------------------------------------------------------------------------------------------*/

function btnEditarNor(grilla,info){
	if( info.nom_pag.indexOf("#") !== 0){pag = "#"+info.nom_pag;}else{pag=info.nom_pag;}	
	grilla.navSeparatorAdd(pag);
	
	var modulo=info.modulo;
	grilla.navButtonAdd(pag,{
	  caption:"", 
	  buttonicon : 'ui-icon-pencil', 
	  title:info.title,

	  onClickButton: function(){ 
			var gr = jQuery("#"+info.ide_tabla).getGridParam("selrow"); 
			if( gr != null ) { 
				jQuery("#"+info.ide_tabla).jqGrid('editGridRow',gr,{
					reloadAfterSubmit:info.recarga,
					modal:info.modal,
					recreateForm : true,
					width:info.ancho,
					closeAfterEdit:true,
					beforeSubmit:function(){
						if($(":input").hasClass('campos_error')){
							jAlert(info.msg_cmps_oblg,info.empresa)
							return 	
						}else{
							return [true]
						}
					},
					afterSubmit:function(xhr,postdata){ 
						rta=xhr.responseText;
						if(rta==0){ 
								notifica_ok(info.msgExito);
								return [true]
						}else{
							if(rta=='session')
								modulo='global'
							jAlert(getMsg(modulo,rta),info.empresa,function(){
								if(rta=='session')
									location.reload();
							})
							return [true];	
						}
					},
				});
		   }else{
			   jAlert(info.msg_sel,info.empresa); 
		  }; 
		 },
	 position:"last"	
	});	
	$(pag).find(".ui-icon-pencil").css({"background-image":"url('images/editar.png')", "background-position":"center","width":"16px","height":"16px","cursor":"pointer"});
}

function btnEliminarNor(grilla,info){
	if( info.nom_pag.indexOf("#") !== 0){pag = "#"+info.nom_pag;}else{pag=info.nom_pag;}	
	grilla.navSeparatorAdd(pag);
	var modulo=info.modulo;
	grilla.navButtonAdd(pag,{
          caption:"", 
		  buttonicon : 'ui-icon-trash',
		  title:info.title ,
		  onClickButton: function(){ 
				var gr = jQuery("#"+info.ide_tabla).getGridParam("selrow"); 
				if( gr != null ) { 
					jQuery("#"+info.ide_tabla).jqGrid('delGridRow',gr,{
						reloadAfterSubmit:info.recarga,
						msg:info.msg_confirma,
						bSubmit: "Si",
						bCancel: "No",
						modal:info.modal,
						width:info.ancho,
						resize:false,
						afterSubmit:function(xhr,postdata){ 
							rta=xhr.responseText;
							if(rta==0){ 
								return [true]
								notifica_ok(info.msgExito);	
							}else{
								if(rta=='session')
									modulo='global'
								jAlert(getMsg(modulo,rta),info.empresa,function(){
									if(rta=='session')
										location.reload();
								})
								return [true];	
							}
						},
					});
			   }else{ 
			   	jAlert(info.msg_sel,info.empresa); 
			  }; 
	         },
		 position:"last"	
});
}

/*-------------------------------------------------------------------------------------------------------------------------------------------------------
funcion para cambiar los iconos basiscos de la grilla como : paginadores, busqueda y reset
-------------------------------------------------------------------------------------------------------------------------------------------------------*/

function cambiar_iconos(pager){
	$("#"+pager).find(".ui-icon-search").css({"background-image":"url('images/buscar.png')", "background-position":"center","width":"16px","height":"16px"});
	$("#"+pager).find(".ui-icon-refresh").css({"background-image":"url('images/refresh.png')", "background-position":"center","width":"16px","height":"16px"});
	
	//btn Siguiente
	$("#"+pager).find(".ui-icon-seek-first").css({"background-image":"url('images/first.png')", "background-position":"center","width":"16px","height":"16px"});
	//btn Anterior
	$("#"+pager).find(".ui-icon-seek-prev").css({"background-image":"url('images/previous.png')", "background-position":"center","width":"16px","height":"16px"});
	
	//btn Siguiente
	$("#"+pager).find(".ui-icon-seek-next").css({"background-image":"url('images/next.png')", "background-position":"center","width":"16px","height":"16px"});
	//btn Ultimo
	$("#"+pager).find(".ui-icon-seek-end").css({"background-image":"url('images/last.png')", "background-position":"center","width":"16px","height":"16px"});
	
}
/*-------------------------------------------------------------------------------------------------------------------------------------------------------
//funcion para mandar a guardar informacion de un formulario a la base de datos  (Guardar o editar) 
-------------------------------------------------------------------------------------------------------------------------------------------------------*/

function acciones(info){
	$(document).ready(function(){
		if(!$(":input, :selected").hasClass('campos_error') ){
			$.ajax({
				type: "POST",
				url:  info.url,
				data: info.data,
				dataType: "html",
				beforeSend: function(){
					$.blockUI({message:info.msg_espere});
				},
				success: function(rta){
					$.unblockUI();
					if (rta!=0){
						if (rta=='session'){
							modulo='global'
						}
						jAlert(getMsg(info.modulo,rta),info.empresa,function(){
							if(rta=='session'){
								$.fn.colorbox.close();
								location.reload();	

							}	
						});
					}else{
						if(info.recarga){
							$.fn.colorbox.close();
							$("#"+info.id_grilla).trigger('reloadGrid');
							notifica_ok(info.msgExito);	
						}	
					}					
				},
			});
		}else{
			jAlert(info.msg_cmps_oblg,info.empresa)
		}
	});
}

