var isIE = document.all?true:false;
var isNS = document.layers?true:false;
function onlyDigits(e,decReq) {
	var key = (isIE) ? window.event.keyCode : e.which;
	var obj = (isIE) ? event.srcElement : e.target;
	var isNum = (key > 47 && key < 58) ? true:false;
	var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;
	window.event.keyCode = (!isNum && !dotOK && isIE) ? 0:key;
	e.which = (!isNum && !dotOK && isNS) ? 0:key;
	return (isNum || dotOK);
}

//funcion para validar campos con expresiones regulares
function evaluar_expresion(sVAR,sTipo) {
    var er_fecha = /^(\d{4})(-)(0[1-9]|1[012])\2([012][0-9]|3[0|1])$/   //aaaa-mm-dd
    var er_hora = /^(0[0-9]|1\d|2[0-4]):([0-5]\d):([0-5]\d)$/ //hh:mm:ss
    var er_decimal = /(\d{,2})\.?(\d{,2})/ //00.00
    var er_numero = /^\d+/ //00
    var er_car_p = /[&<>@"|]+/ // caracteres prohibidos: &<>@"|

     
    switch(sTipo)
    {
    case "fecha":
        if (!er_fecha.test(sVAR)) {
          return false;
        }
        break;
    case "hora":
        if (!er_hora.test(sVAR)) {
          return false;
        }
        break;
    case "decimal":
        if (!er_decimal.test(sVAR)) {
            if(!er_numero.test(sVAR)){
                return false;  
            }
        }
        break;
    case "prohibido":
        if (!er_car_p.test(sVAR)) {
                return false;  
        }
        break;
    } 
    return true;
}

function sustituir_expresion(sCampo,sTipo,sCaracterSustituto) {
    var er_fecha = /^(\d{4})(-)(0[1-9]|1[012])\2([012][0-9]|3[0|1])$/   //aaaa-mm-dd
    var er_hora = /^(0[0-9]|1\d|2[0-4]):([0-5]\d):([0-5]\d)$/ //hh:mm:ss
    var er_decimal = /(\d{,2})\.?(\d{,2})/ //00.00
    var er_numero = /^\d+/ //00
    var er_car_p = /[&<>@"|?]+/ // caracteres prohibidos: <
    var er_general = "";
    var sVAR = eval(sCampo+".value;");
    
    switch(sTipo)
    {
    case "fecha":
        er_general = er_fecha;
        break;
    case "hora":
        er_general = er_hora;
        break;
    case "decimal":
        er_general = er_decimal;
        break;
    case "prohibido":
        er_general = er_car_p;
        break;
    } 
    
    var contador=0;
    while(evaluar_expresion(sVAR,sTipo) && contador<1000){
        var nuevo_valor = sVAR.replace(er_general,sCaracterSustituto,"gi");
        //alert(contador);
        eval(sCampo+".value='"+nuevo_valor+"';");
        sVAR = eval(sCampo+".value;");
        contador=contador+1;
    }
}
