// Varias funciones JVS útiles. // JJBM, 15/07/2004, ajuste de bisiestos sobre 100 y 400 años. function navegar( url ) { window.location.href = url.replace(/ /g,"+") } function email_correcto( c ) { var er_mail=/^[^@ <>=\\\/$]+@[^@ $\.]+(\.[^@ $\.]+)+$/ return er_mail.test(c) } function evalua_campo_vacio( campo, vacio ) { if( campo.value.length == 0 ) { campo.focus(); return true } return vacio } function fecha_correcta_dma( d, m, a ) { return a>1970&&m>0&&d>0&&m<13&&(d<=(m==2?28+(!(a%400)||(a%100&&!(a%4))):30+(m&1)^(m>7))) } function fecha_completa(strFecha) { var d = "", m = "", a = "", i = 0 while( i < strFecha.length && strFecha.charAt(i) != '/' ) d = d + strFecha.charAt(i++) if(i++>=strFecha.length) return false while( i < strFecha.length && strFecha.charAt(i) != '/' ) m = m + strFecha.charAt(i++) if(i++>=strFecha.length) return false while( i < strFecha.length && strFecha.charAt(i) != '/' ) a = a + strFecha.charAt(i++) if(a.length == 2) a = (a>49?"19":"20") + a if(a.length != 4) return false return (d+"/"+m+"/"+a) } function cheq_fecha( txtFecha ) { var d = "", m = "", a = "", i = 0 while( i < txtFecha.length && txtFecha.charAt(i) != '/' ) d = d + txtFecha.charAt(i++) if(i++>=txtFecha.length) return false if(d<1) return false while( i < txtFecha.length && txtFecha.charAt(i) != '/' ) m = m + txtFecha.charAt(i++) if(i++>=txtFecha.length) return false if(m<1) return false while( i < txtFecha.length && txtFecha.charAt(i) != '/' ) a = a + txtFecha.charAt(i++) if(a<1) return false if(a.length == 2) a = (a>49?"19":"20") + a if(a.length != 4) return false return fecha_correcta_dma(d,m,a) } function cheq_fecha2( f ) { var bln = cheq_fecha(f) if( ! bln ) alert( "Fecha incorrecta." ) return bln } function comp_fechas( a, b ) { var da = "", ma = "", aa = "", i var db = "", mb = "", ab = "" // Capturamos dia, mes y año de la fecha a: i = 0 while( i < a.length && a.charAt(i) != '/' ) da = da + a.charAt(i++) if(i++>=a.length) return false while( i < a.length && a.charAt(i) != '/' ) ma = ma + a.charAt(i++) if(i++>=a.length) return false while( i < a.length && a.charAt(i) != '/' ) aa = aa + a.charAt(i++) if(aa.length == 2) aa = (aa>49?"19":"20") + aa if(aa.length != 4) return false // Capturamos dia, mes y año de la fecha b: i = 0 while( i < b.length && b.charAt(i) != '/' ) db = db + b.charAt(i++) if(i++>=b.length) return false while( i < b.length && b.charAt(i) != '/' ) mb = mb + b.charAt(i++) if(i++>=b.length) return false while( i < b.length && b.charAt(i) != '/' ) ab = ab + b.charAt(i++) if(ab.length == 2) ab = (ab>49?"19":"20") + ab if(ab.length != 4) return false // Comparamos cual es mayor: if( aa*1 > ab*1 ) return true if( aa*1 < ab*1 ) return false if( ma*1 > mb*1 ) return true if( ma*1 < mb*1 ) return false if( da*1 > db*1 ) return true if( da*1 < db*1 ) return false return true } function opcion_checked( c ) { var intN = 0 while( !c[intN].checked ) intN++ return intN } /* JJBM, 09/05/2001 Devuelve TRUE si la cadena contiene algún caracter UNICODE. */ function Cheq_UNICODE( s ) { var i for( i = 0; i < s.length; i++ ) if( s.charCodeAt(i) > 255 ) return true return false } /* JJBM, 05/07/2000 Devuelve la cadena txtC, pero substituyendo todos aquellos caracteres no válidos para pasar por la URL en su represen- tación hexadecimal (precedida de %). Con datos UNICODE no funcionan bien. function paramURL( txtC ) { var txtR = "", i = 0, c while( i < txtC.length ) { c = txtC.charCodeAt(i++) txtR += (((c>44&&c<58)||(c>64&&c<91)||(c>94&&c<123))&&c!=47&&c!=96)? String.fromCharCode(c) : '%'+(c.toString(16).length<2?"0":"")+c.toString(16).toUpperCase() } return txtR } function paramURLhex( txtC ) { var txtR = "", i = 0 while( i < txtC.length ) txtR += '%' + txtC.charCodeAt(i++).toString(16).toUpperCase() return txtR } */ function CharCodeToHex( intN ) { var c = intN.toString(16) if( c.length == 1 ) return "%0" + c if( c.length == 2 ) return "%" + c if( c.length == 3 ) return "%0" + c.charAt(0) + "%" + c.charAt(1) + c.charAt(2) return "%" + c.charAt(0) + c.charAt(1) + "%" + c.charAt(2) + c.charAt(3) } function paramURL( txtC ) { var txtR = "", i for( i = 0; i < txtC.length; i++ ) switch( txtC.charAt(i) ) { case '+': case '&': case '=': case '?': case '%': case '#': case '/': case '\\': case '\n': case '\t': case ' ': txtR += CharCodeToHex(txtC.charCodeAt(i)) break; default: txtR += txtC.charAt(i) } return txtR } // function paramURLhex( txtC ) { return paramURL( txtC ) } function paramURLhex( txtC ) { var txtR = "", i = 0 while( i < txtC.length ) txtR += CharCodeToHex(txtC.charCodeAt(i++)) return txtR } // JJBM; true | false que el número es válido y está entre min y max. function es_numero_valido( n, min, max ) { if( isNaN( n ) ) return false return n >= min && n <= max } // JJBM; true | false si es un número natural. function es_numero_natural( n, max ) { // Verificamos que sea número: if( isNaN( n ) ) return false // Verificamos que sea natural: if( Math.round(n) != n || n < 1 ) return false // Miramos si hay que acotar: if( max != null && n > max ) return false // Es un número natural: return true } // JJBM; Pasa la moneda a un formato de número válido para JScript. function Moneda2Numero( m ) { return new Number( (m.replace(/\./g,'')).replace(/\,/g,'.') ) } // JJBM; no lo hace ni floor() ni ceil(), trunca los decimales. function Truncar( n ) { var k = '', i, m = n.toString() for( i = 0; i < m.length && m.charAt(i) != '.'; i++ ) k += m.charAt(i) return k*1.0 } // JJBM; Pasa el número al formato de moneda con 'd' decimales. function Numero2Moneda( m, d ) { if( isNaN(m) || !isFinite(m) ) return null; var g="" if(m<0) { g="-"; m*=-1 } var d10 = Math.pow( 10, d ) m = Math.round( m * d10 ) / d10 var n=String(parseInt(Math.round(m*d10))%parseInt(d10)),s=",",M if(!d){n=s=""} else while(n.length < d) n = "0" + n if((m=Truncar(m))<=0) return g+"0"+s+n do{ M=String(m%1000) m=Truncar(m/1000) if(m) while(M.length < 3) M="0"+M n=M+s+n s="." }while(m) return g+n } // JJBM; true | false que el valor pasado es una moneda válida. function EsDigito( d ) { return d >= 0 && d <= 9 } // JJBM, 15/09/2003, admita número negtivos. function EsMoneda( n ) { var l=n.length, i=0, e=0, c if( l<1 || ( n.charAt(i) != '-'.charAt(0) && !EsDigito(n.charAt(i))) ) return false while( ++i < l ) { c = n.charAt(i) switch(e) { case 0: e=7; if(c=='.') e=1; if(c==',') e=5; if(EsDigito(c)) e=0; break; case 1: case 2: case 3: EsDigito(c)?e++:e=7; break; case 4: e=7; if(c=='.') e=1; if(c==',') e=5; break; case 5: case 6: e=EsDigito(c)?6:7; break; case 7: return false } } return e==0 || e==6 || e==4 } function Enviar_Frm(objFrm) { objFrm.submit(); return false ; } /* JJBM: En Formato.ASP, Antonio ha creado una función que genera el control de entrada de fechas. Con esta función, se recupera la fecha completa de este control. Si la fecha no es válida, devuelve "NaD" y si no está puesta "". */ function CampoFecha(dia,mes,ano) { var fecha if( dia != "" ) { fecha = dia + "/" + mes + "/" + ano if(cheq_fecha(fecha)) return fecha else return "NaD" } else return "" } /* JJBM: Devuelve una cadena con un mínimo de N caracteres de anchura justificando a la derecha la de entrada. */ function CadenaW( s, N ) { var cad = new String while(s.toString().length< N)s=" "+s.toString() cad += s return cad.substr(0,N) } /* JJBM; Para pasar del margen económico a beneficio y biceversa. Beneficio € [ 0, +inf ] MargenEconomico € [ 0, 1 ] */ function MargenEconomico( Beneficio_ ) { return Beneficio_ / ( 1.0 + Beneficio_ ) } function Beneficio( MargenEconomico_ ) { return MargenEconomico_ / ( 1.0 - MargenEconomico_ ) } /* JJBM; calculan de dos valores el mínimo y el máximo */ function menor( a, b ) { return a < b ? a : b } function mayor( a, b ) { return a < b ? b : a } /* David, 07/11/2001 Funcion para resizar el tamaño de las ventanas de las ayudas segun lo que midan */ function Calcula_Ventana(){ var alto window.moveTo (0,0) alto = medida.offsetHeight if (alto > 500) alto = 500 window.resizeTo (medida.offsetWidth+30,alto+50) } function Insertar(clv, tipo, lista, switches){ switch(tipo){ case 1: window.open("../APL/Home_Page_Productos_Usar.asp?cod_prod=" + clv,'','width=300,height=150') break; case 2: window.open("../APL/Mantener_Producto_detalle.asp?acto=R&codprodrel=" + clv,'','width=300,height=150') break; case 3: window.open("../APL/Mantener_Producto_detalle.asp?acto=S&codprodrel=" + clv,'','width=300,height=150') break; case 4: window.open("../APL/Mantener_Presupuesto_Producto_Anadir.asp?codprod=" + clv,'','width=300,height=150') break; case 5: window.open("../APL/Mantener_Productos_Alternativos.asp?N=1&clv=" + clv,'','width=300,height=150') break; case 6: window.open('../APL/Mantener_Cliente_Precios_Busquedas.asp?M=1&sw=' + switches + '&prod_clv='+ clv,'','width=300,height=150') break; case 7: window.open('../APL/Mantener_Cliente_Precios_Busquedas.asp?M=2&sw=' + switches + '&lista_clv=' + lista + '&prod_clv='+ clv,'','width=300,height=150') break; case 8: window.open('../APL/Mantener_Grupos_Productos.asp?clv=' + clv,'','width=300,height=150') break; } } function dW(s){ document.write(s) } //Ordenamiento de combos function ocpt_DATO(a,b) { this.a = a this.b = b } function ocpt_ORDEN(u,v) { if( u.a < v.a ) return -1; if( u.a > v.a ) return 1; return 0 } function Ordenar_Combo_Por_Texto_i(combo,w) { var i = 0, v = new Array(), j for( j = w; j < combo.length; j++ ) v[i++] = new ocpt_DATO( combo.options[j].text, combo.options[j].value ) v.sort( ocpt_ORDEN ) combo.length = w for( j = 0; j < i; j++ ) combo.options[j+w] = new Option( v[j].a, v[j].b ) } function Ordenar_Combo_Por_Texto(combo) { Ordenar_Combo_Por_Texto_i(combo,0) } //Fin del ordenamiento de combos function uCboVaciar(o) { // Vacía el select pasado. while( o.length ) { o.options[o.length] = null o.length-- } } function uCboClv2Idx(o,c) { // Si c es value de algún option, devuelve el Index sino -1 var i for( i = 0; i < o.length; i++ ) if( o[i].value == c ) return i return -1 } function uCboTexto(o) { // Devuelve el texto asociado a la opción elegida. "" si -1 if( o.selectedIndex >= 0 ) return o[o.selectedIndex].text return "" } function uCboNewOption(o,Texto,Clv) { // Inserta un nuevo option. o.options[ o.length ] = new Option( Texto, Clv ) }