// Functions used by text fiels to accept numbers only.
function IsDigit( e )
{
	var iCode;
	if (!e) {
		e= window.event ;
		iCode= ( e.keyCode ) ;
	} else {
		iCode = ( e.charCode ) ;
	}

	e.returnValue =
		(
			( iCode >= 48 && iCode <= 57 )		// Numbers
			|| iCode == 44						// ,
			|| iCode == 46						// .
			|| iCode == 0						// non-char
		) ;

	return e.returnValue ;
}

var prLoad=window.onload;
onload=Iniciar;

function Iniciar() {
	var f=document.formulario;

	if (!f) return;
	if (!f.autocomplete) f.autocomplete="off";
	var el=f.elements;
	var clase, obj;

	//validar que solo introducen números y comas
	for(var i=0; (i<el.length); i++) {
		obj=el[i];
//		clase=obj.className;
		if (obj.type=="text" && obj.className != "texto") {
//				obj.onchange=function () {cambioCelda(this)};
//				obj.onfocus=function () {dentroCelda(this)};

					obj.onkeypress=function(event) {return IsDigit(event)};

		}
	}

	if (typeof(prLoad)=="function") prLoad()
}

function cambia_combo(o) {}

function cambioMeses(obj, mn, mx) {
	if (mn===undefined) mn=0;
	if (mx===undefined) mx=12;

	var v=ValidarDbl(obj, 0, 1);
	if (v<mn) obj.value=mn.toString();
	if (v>mx) obj.value=mx.toString();
}

function comprobarRangoEntero(obj, mn, mx) {
	if (mn===undefined) mn=0;
	if (mx===undefined) mx=12;

	var v=ValidarInt(obj, 0);
	if (v<mn) obj.value=mn.toString();
	if (v>mx) obj.value=mx.toString();
}

//como parametros opcionales los otros campos a validar.
function cambioPorcentaje(obj) {
	var aCampos=new Array();
	aCampos.push(obj)
	for(var i=1; i<arguments.length; i++) {
		aCampos.push(document.getElementById(arguments[i]));
	}
//tenemos todos los campos, ahora los tratamos genéricamente.
	var aValores=new Array();
	var total=0;
	var valor;
	for (var i=0; i<aCampos.length; i++) {
		valor=ValidarDbl(aCampos[i], 0, 2);
		if ((valor<0) || (valor>100)) {
			if (aCampos.length==1)
				valor=100;
			else
				valor=0;
			aCampos[i].value=FormatNum(valor, 2);
		}
		if (total+valor>100) {
			valor=100-total;
			aCampos[i].value=FormatNum(valor, 2);
		}
		total+=valor;
	}

	if ((total<100) && (aCampos.length>1)) {
		var i=aCampos.length-1
		total-=ValidarDbl(aCampos[i], 0, 2);
		valor=100-total;
		aCampos[i].value=FormatNum(valor, 2);
	}
}

//valida que el campo obj sea mayor o igual que el valor de ref
function comprobarMayor(obj, ref) {
	var v=ValidarInt(obj, 0);
	var vRef=ValidarInt(document.getElementById(ref), 0);
	if (v<vRef) obj.value=vRef;
}

function ValorCombo(nombre) {
	var elmto=document.getElementById(nombre);
	if (-1==elmto.selectedIndex) return 0

	if (elmto.item(elmto.selectedIndex))
		return elmto.item(elmto.selectedIndex).value;

	return 0
}


