String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
//

function jF(lpVar){
	try{
		document.getElementById(lpVar).focus();
		return true;
	} catch (e) {
		return true;
	} 	
}
//
function gv(lpVar){
	return document.getElementById(lpVar).value;
}
function sv(O,lpVar){
	return document.getElementById(O).value=lpVar;
}
//
function jFocus(lpVar){
	document.getElementById(lpVar).focus();
}
function jLen(lpVar){
	return lpVar.trim().length;
}
function jVal(lpVar){
	if(jLen(lpVar)==0){
		return 0;
	}
	if(isNaN(lpVar)){
		return 0;
	}
	return parseFloat(lpVar);
}
//
function replaceAll(str, find, replace) {
  return str.replace(new RegExp(find, 'g'), replace);
}
//
function GetFec(){
	var today = new Date(); 
	var dd = today.getDate(); 
	var mm = today.getMonth()+1;
	var yyyy = today.getFullYear();
	yyyy=yyyy-2000;
	if(dd<10){dd='0'+dd} 
	if(mm<10){mm='0'+mm} 
	return mm+'-'+dd+'-'+yyyy;
}
function fKillerEx(lpIndex){
	var Rpta = confirm("Esta seguro que desea eliminar?")		
	if (Rpta==false){
		return;
	}
	document.getElementById("TxtRegID").value=lpIndex;
	document.getElementById("TxtAc").value="KILLER";
	document.getElementById("Frm").submit();
}
function CreateAJAX(){
        var xmlhttp=false;
        try {
               xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
               try {
                  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
               } catch (E) {
                       xmlhttp = false;
               }
        } 
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
               xmlhttp = new XMLHttpRequest();
        }
        return xmlhttp;
}

function AX(C,P1,P2,P3,P4,P5,P6,P7,P8,P9){
	var s="?C="+C;
	if(P1!=undefined)s=s+"&P1="+P1;
	if(P2!=undefined)s=s+"&P2="+P2;
	if(P3!=undefined)s=s+"&P3="+P3;
	if(P4!=undefined)s=s+"&P4="+P4;
	if(P5!=undefined)s=s+"&P5="+P5;
	if(P6!=undefined)s=s+"&P6="+P6;
	if(P7!=undefined)s=s+"&P7="+P7;
	if(P8!=undefined)s=s+"&P8="+P8;
	if(P9!=undefined)s=s+"&P9="+P9;
	//
	var php="ajax.php"+s;
	//prompt(php,php);
	ajax=CreateAJAX();
	ajax.open("GET",php,false);	
	ajax.send(null);
	var StrHtml=ajax.responseText;
	return StrHtml.trim();
}


function GetHora(){
	var f= new Date();
	h=f.getHours()*3600;
	m=f.getMinutes()*60;
	s=f.getSeconds();
	return h+m+s;
}
function VerEnSunat(){
	var Ruc=document.getElementById("TxtRuc").value;
	if(Ruc.trim().length==0){
		alert("Ingrese numero de Ruc");
		document.getElementById("TxtRuc").focus();
		return;
	}
	var StrU="read.php?Ruc="+Ruc;
	OpenWin(StrU,600,500);
}

function jL(lpVar){
	return lpVar.trim().length;
}
//------------------------------------------
function soloLetras(){
	 $(".lt").keydown(function (event) {
 	    var O=document.activeElement;var ti=O.tabIndex;	
		var k;k=(document.all) ? event.keyCode :event.which; 
		if(k==219){return false;
		}else{
			if(k==13){					
				GetNext(ti);return true;
			}
			return true;
		}
	});
}
function soloFechas(){
	 $(".fe").mask("99-99-99");
	 $(".fe").keydown(function (event) {
 	    var O=document.activeElement;var ti=O.tabIndex;	
		var k;k=(document.all) ? event.keyCode :event.which; 
		if(k==219){return false;
		}else{
			if(k==13){
				GetNext(ti);return true;
			}
			return true;
		}
	});
}
function FrmCls(lpF){
	frm = document.getElementById("Frm");
	if (!frm) return;var mayor=0;	
	for(i=0; i<frm.elements.length; i++){
		var t=frm.elements[i].type;
		if (  t=="text" || t=="password" ){			
			frm.elements[i].value="";
		}
		if ( t=="select-one"  ){			
			frm.elements[i].selectedIndex=0;
		}
		if ( t=="checkbox" ){
			frm.elements[i].checked=false;
		}		
	}	
	if(jLen(lpF)>0){
		jFocus(lpF);
	}
}
function GetNext(ti){
	frm = document.getElementById("Frm");
	if (!frm) return;var mayor=0;	
	for(i=0; i<frm.elements.length; i++){
		var t=frm.elements[i].type;
		if ( t=="checkbox" || t=="text" || t=="button" || t=="select-one" || t=="password" ){			
			if(frm.elements[i].disabled==false){					
				if(frm.elements[i].tabIndex==ti+1){
					frm.elements[i].focus();return;
				}else{
					mayor=frm.elements[i].tabIndex;
				}				
			}
		}		
	}
	if(mayor==ti)return;
}
//
function soloNumeros(){
	 $(".nd").keydown(function (event) { 
	 	var O=document.activeElement;var k;var v;v=trimm(O.value);
		var ti=O.tabIndex;		
		k=(document.all) ? event.keyCode :event.which;	
		if((k>=48 && k<=57) || k==8 || (k>=96 && k<=105) 
			|| k==110 || k==37 || k==39  || k==36 || k==35){
			if(k==110){
				if(v.length==0 ){
					return false;
				}else{
					if(v.indexOf(".")!=-1){
						return false;
					}
				}
			}				
			return true;
		}else{
			if(k==46 || k==9){
				return true;
			}
			if(k==13){
				GetNext(ti);return true;
			}else{
				return false;
			}
		}
		//
	});
}
function soloEnteros(){
	 $(".ne").keydown(function (event) { 
	 	var O=document.activeElement;var k;var v;v=trimm(O.value);
		var ti=O.tabIndex;
		k=(document.all) ? event.keyCode :event.which;
		if((k>=48 && k<=57) || k==8 || (k>=96 && k<=105) || 
			k==37 || k==39 || k==46 || k==36 || k==35){
			return true;
		}else{
			if(k==46 || k==9){
				return true;
			}
			if(k==13){
				GetNext(ti);return true;
			}else{
				return false;
			}
		}
	});
}
function Inicializar(){
   soloLetras();
   soloNumeros();
   soloEnteros();
   soloFechas();
}

//------------------------------------------



function fGetSunat(){
	var Ruc=document.getElementById("TxtRuc").value;
	if(Ruc.trim().length==0){
		alert("Ingrese numero de Ruc");
		document.getElementById("TxtRuc").focus();
		return;
	}
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=ST_RUC&TxtPar="+Ruc);	
	ajax.onreadystatechange=function() {
		   if (ajax.readyState==4) {
var StrD=ajax.responseText;	
if (StrD.indexOf("invalido") != -1) {						
	alert("Numero de ruc NO VALIDO");
}else{
	document.getElementById("DivSunat").innerHTML=StrD;
	document.getElementById("TxtNom").value=document.getElementById("T_RS").value;	
	document.getElementById("TxtDir").value=document.getElementById("T_DI").value;	
	document.getElementById("TxtTel").value=document.getElementById("T_TE").value;
	alert("Los datos han sido cargados CORRECTAMENTE");	
}				    
		   }
	}
    ajax.send(null);
}
//**************************************************************************
function GetKey(){
	var k;
	k=(document.all) ? e.keyCode :e.which; 
	return k;
}
//**************************************************************************
function OpenWin(url,ancho,alto)
{
var xPos=(screen.width-ancho)/2; 
var yPos=(screen.height-alto)/2;
window.open(url, 1, 'location=no, directories=no,status=no,menubar=no,scrollbars=yes,resizable=no, Left=' + xPos + ', Top=' + yPos + ',width=' + ancho + ',height=' + alto + '');       
}
function trimm(myString)
{
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}
//**************************************************************************
function formato_numero(numero, decimales, separador_decimal, separador_miles){ 
    numero=parseFloat(numero);
if(isNaN(numero)){
return "";
}
if(decimales!==undefined){     
numero=numero.toFixed(decimales);
}
separador_decimal=".";
numero=numero.toString().replace(".", separador_decimal!==undefined ? 	separador_decimal : ",");
return numero;
}

function ff(numero, decimales, separador_decimal, separador_miles){ 
    numero=parseFloat(numero);
if(isNaN(numero)){
return "";
}
if(decimales!==undefined){     
numero=numero.toFixed(decimales);
}
separador_decimal=".";
numero=numero.toString().replace(".", separador_decimal!==undefined ? 	separador_decimal : ",");
return numero;
}
//**************************************************************************
function fSiguiente(){		
	document.getElementById("TxtAc").value="MSig";
	document.getElementById("Frm").submit();
}
function fAnterior(){
	document.getElementById("TxtAc").value="MAnt";
	document.getElementById("Frm").submit();
}
function fPrimero(){
	document.getElementById("TxtAc").value="MPri";
	document.getElementById("Frm").submit();
}
function fUltimo(){
	document.getElementById("TxtAc").value="MUlt";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function fVolver(StrUrl){
	window.location = StrUrl;
}
function fBuscar(StrUrl){
	window.location = StrUrl;
}
//**************************************************************************
function fActivar(reg){	
	document.getElementById("TxtId").value=reg;	
	document.getElementById("TxtAc").value="AR";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function fFiltrar(){	
	document.getElementById("TxtAc").value="F";
	document.getElementById("Frm").submit();
}
function fPDF(){
	document.getElementById("TxtAc").value="PDF";
	document.getElementById("Frm").submit();
}
function ffGuardar(){
	document.getElementById("TxtAc").value="G";
	document.getElementById("Frm").submit();
}
function fEnviarPDF(){
	var Rpta = confirm("Esta seguro que desea enviar un email?")		
	if (Rpta==true){
		document.getElementById("TxtAc").value="ENV_PDF";
		document.getElementById("Frm").submit();
	}
}
function fEliminar(){
	var Rpta = confirm("Esta seguro que desea eliminar?")		
	if (Rpta==true){
		document.getElementById("TxtAc").value="E";
		document.getElementById("Frm").submit();
	}
}
function fAnular(){
	var Rpta = confirm("Esta seguro que desea anular?")		
	if (Rpta==true){
		document.getElementById("TxtAc").value="A";
		document.getElementById("Frm").submit();
	}
}
function fValidar(){
	var Rpta = confirm("Esta seguro que desea validar?")		
	if (Rpta==true){
		document.getElementById("TxtAc").value="VAL";
		document.getElementById("Frm").submit();
	}
}
/**************************************************************************
	onKeyDown="return SoloTexto(event);" 
***************************************************************************/
function SoloTexto(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which; 
	if(k==219){
		return false;
	}else{
		return true;
	}
}
function SoloTextoEx(e,Sig){	
	var k;
	k=(document.all) ? e.keyCode :e.which; 
	if(k==219){
		return false;
	}else{
		if(k==40){
			EnFocar();
		}
		if(k==13){
			document.getElementById(Sig).focus();
			return true;
		}
		return true;
	}
}
function SoloTextoExX(e,Sig){	
	var k;
	k=(document.all) ? e.keyCode :e.which; 
	if(k==219){
		return false;
	}else{
		if(k==40){
			EnFocar();
		}
		if(k==13){
			document.getElementById(Sig).focus();
			return true;
		}
		return true;
	}
}
function EnFocar(){
	try {		
		var t=document.getElementById("LbDatos").length;
		if(t>0){
			document.getElementById('LbDatos').options[0].selected = true;
			document.getElementById("LbDatos").focus();
			document.getElementById("LbDatos").scrollTop=0;
			/*
			document.getElementById("LbDatos").selectedIndex=0;
			document.getElementById("LbDatos").selectedIndex=-1;
			
			*/
		}
		return true
	} catch (e) {
		return false;          
	} 
}
//

//
function SoloTextoExF(e,Sig){	
	var k;
	k=(document.all) ? e.keyCode :e.which; 
	if(k==219){
		return false;
	}else{
		if(k==13){
			document.getElementById("TxtAc").value="F";
			document.getElementById("Frm").submit();
			return true;
		}
		return true;
	}
}
function SoloTextoEx2(e,Sig,Sig2){	
	var k;
	k=(document.all) ? e.keyCode :e.which; 
	if(k==219){
		return false;
	}else{
		if(k==13){
			if(document.getElementById(Sig).readOnly==false){
			document.getElementById(Sig).focus();
			}else{
			document.getElementById(Sig2).focus();
			}
			return true;
		}
		return true;
	}
}
/**************************************************************************
	onKeyDown="return SoloNum(this, event);"
***************************************************************************/
function SoloNum1(O,e){	
	var k;
	var v;
	v=trimm(O.value);
	k=(document.all) ? e.keyCode :e.which;	
	if((k>=48 && k<=57) || k==8 || (k>=96 && k<=105) 
		|| k==110 || k==37 || k==39  || k==36 || k==35){
		if(k==110){
			if(v.length==0 ){
				return false;
			}else{
				if(v.indexOf(".")!=-1){
					return false;
				}
			}
		}				
		return true;
	}else{
		if(k==46 || k==9){
			return true;
		}
		if(k==13){
			return true;
		}else{
			return false;
		}
	}
}
function SoloNum(O,e,Sig){	
	var k;
	var v;
	v=trimm(O.value);
	k=(document.all) ? e.keyCode :e.which;	
	if((k>=48 && k<=57) || k==8 || (k>=96 && k<=105) 
		|| k==110 || k==37 || k==39  || k==36 || k==35){
		if(k==110){
			if(v.length==0 ){
				return false;
			}else{
				if(v.indexOf(".")!=-1){
					return false;
				}
			}
		}				
		return true;
	}else{
		if(k==46 || k==9){
			return true;
		}
		if(k==13){
			document.getElementById(Sig).focus();
			return true;
		}else{
			return false;
		}
	}
}
//**************************************************************************
function SoloFec(O,e,Sig){	
	var k;
	var v;
	v=trimm(O.value);
	k=(document.all) ? e.keyCode :e.which;
	if((k>=48 && k<=57) || k==8 || (k>=96 && k<=105) 
		||  k==37 || k==39 || k==109 || k==36 || k==35){
		if(k==109){
			if(v.length==0 ){
				return false;
			}else{
				p1=v.indexOf("-");
				p2=v.lastIndexOf("-");
				if(p1==-1 && p2==-1){
					return true;
				}
				if(p1==p2){
					return true;
				}
				if(p1!=p2){
					return false;
				}
				if(v.indexOf("-")!=-1){
					return false;
				}
			}
		}
		return true;
	}else{
		if(k==46 || k==9){
			return true;
		}
		if(k==13){
			document.getElementById(Sig).focus();
			return true;
		}else{
			return false;
		}
	}
}
/**************************************************************************
	onKeyDown="return SoloEnteros(this, event);"
***************************************************************************/
function SoloEnteros(O,e){	
	var k;
	var v;
	v=trimm(O.value);
	k=(document.all) ? e.keyCode :e.which;
	if((k>=48 && k<=57) || k==8 || (k>=96 && k<=105) || k==37 || k==39){
		return true;
	}else{
		if(k==13){
			return true;
		}else{
			return false;
		}
	}
}
//**************************************************************************
function SoloEnterosEx(O,e,Sig){	
	var k;
	var v;
	v=trimm(O.value);
	k=(document.all) ? e.keyCode :e.which;
	if((k>=48 && k<=57) || k==8 || (k>=96 && k<=105) || 
		k==37 || k==39 || k==46 || k==36 || k==35){
		return true;
	}else{
		if(k==13){
			document.getElementById(Sig).focus();
			return true;
		}else{
			return false;
		}
	}
}
//**************************************************************************
function SoloEnterosEx2(O,e,Sig,Sig2){	
	var k;
	var v;
	v=trimm(O.value);
	k=(document.all) ? e.keyCode :e.which;
	if((k>=48 && k<=57) || k==8 || (k>=96 && k<=105) || 
		k==37 || k==39 || k==46 || k==36 || k==35){
		return true;
	}else{
		if(k==13){
			if(document.getElementById(Sig).readOnly==false){
				document.getElementById(Sig).focus();
			}else{
				document.getElementById(Sig2).focus();
			}
			return true;
		}else{
			return false;
		}
	}
}
//**************************************************************************
function EsFecha(fecha){
	var dtCh= "-";
	var minYear=1900;
	var maxYear=2100;
	function isInteger(s){
		var i;
		for (i = 0; i < s.length; i++){
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		return true;
	}
	function stripCharsInBag(s, bag){
		var i;
		var returnString = "";
		for (i = 0; i < s.length; i++){
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}
	function daysInFebruary (year){
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}
	function DaysArray(n) {
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
			if (i==2) {this[i] = 29}
		}
		return this
	}
	function isDate(dtStr){
		var daysInMonth = DaysArray(12)
		var pos1=dtStr.indexOf(dtCh)
		var pos2=dtStr.indexOf(dtCh,pos1+1)
		var strDay=dtStr.substring(0,pos1)
		var strMonth=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1)
		strYr=strYear
		strYear=String(parseInt(strYear)+2000)
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)		
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}		
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr) +2000
		
		if (pos1==-1 || pos2==-1){
			return false
		}
		if (strMonth.length<1 || month<1 || month>12){
			return false
		}
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			return false
		}
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
			return false
		}
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
			return false
		}
		return true
	}
	if(isDate(fecha)){
		return true;
	}else{
		return false;
	}
}
function selecciona_value(objInput) { 
    var valor_input = objInput.value; 
    var longitud = valor_input.length; 

    if (objInput.setSelectionRange) { 
        objInput.focus(); 
        objInput.setSelectionRange (0, longitud); 
    } 
    else if (objInput.createTextRange) { 
        var range = objInput.createTextRange() ; 
        range.collapse(true); 
        range.moveEnd('character', longitud); 
        range.moveStart('character', 0); 
        range.select(); 
    } 
} 


function fRound(numero) {
	var original = parseFloat(numero);
	var result = Math.round(original * 100) / 100;
	return result;
}