function redondear(cantidad, decimales) {
	var cantidad = parseFloat(cantidad);
	var decimales = parseFloat(decimales);
	decimales = (!decimales ? 2 : decimales);
	return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
}

function mOvr(src,clrOver) {
	if (!src.contains(event.fromElement)) {
		src.style.cursor = 'hand';
		src.bgColor = clrOver;
	}
}

function mOut(src,clrIn) {
	if (!src.contains(event.toElement)) {
		src.style.cursor = 'default';
		src.bgColor = clrIn;
	}
}

function mClk(src) {
	if(event.srcElement.tagName=='TD'){
		src.children.tags('A')[0].click();
	}
}

// Checkea todos los RADIO o CHECKBOX
// oPadre: document, formulario1, etc...
// bEstado: true false
function CheckAll(oPadre,bEstado){
    var i;
    var typeName;
    if (!oPadre.all){return;}
    for(i=0; i < oPadre.all.length; i++){
        typeName = oPadre.all[i].type;
        typeName = typeName?typeName.toUpperCase():"";
        //Si el objeto del documento es del tipo RADIO o CHECKBOX
        if (typeName == 'RADIO' || typeName == 'CHECKBOX'){
            oPadre.all[i].checked=bEstado;
        }
    }
}

// Cantidad de RADIO o CHECKBOX checkeados
// oPadre: document, formulario1, etc...
function CantChecked(oPadre){
    var i;
    var typeName;
	var cant;
	cant = 0;
    if (!oPadre.all){return;}
    for(i=0; i < oPadre.all.length; i++){
        typeName = oPadre.all[i].type;
        typeName = typeName?typeName.toUpperCase():"";
        //Si el objeto del documento es del tipo RADIO o CHECKBOX
        if (typeName == 'RADIO' || typeName == 'CHECKBOX'){
            if(oPadre.all[i].checked) cant++;
        }
    }
	return cant;
}

function abrir_ventana(url,ancho,alto){
	var top = (screen.height/2) - (alto/2);
	var left = (screen.width/2) - (ancho/2);
	var ventana = window.open(url, "nombre", "scrollbars=1,resizable=1,width="+ancho+",height="+alto+",top="+top+",left="+left);
	ventana.focus();
}

function Ltrim(str){
    var i;
	if(str != null){
	    for (i=str.length;i>0 && str.substr(i-1,1) == " ";i--);
	}
	return str.substr(0,i); 
}

function Rtrim(str){
	if(str != null){
	    for (i=0;i<str.length && str.substr(i,1) == " ";i++);
	}
	return str.substr(i,str.length);
}

function trim(str){
	if(str != null){
		str = Rtrim(str);
		str = Ltrim(str);
	}
	return str
}

/*
Usar: onkeypress="solo_numerico(4)"
max = maxlength (0 para ignorarlo)
*/
function solo_numerico(max){
	if(window.event.keyCode == 46) return true; //tecla Delete
	if(window.event.keyCode == 8) return true; //tecla Suprimir
	if(window.event.keyCode == 37) return true; //tecla Izq
	if(window.event.keyCode == 38) return true; //tecla Arriba
	if(window.event.keyCode == 39) return true; //tecla Derecha
	if(window.event.keyCode == 40) return true; //tecla Abajo
	if(window.event.keyCode == 36) return true; //tecla Inicio
	if(window.event.keyCode == 35) return true; //tecla Fin
	if(window.event.keyCode == 18) return true; //tecla ALT izq
	if(window.event.keyCode == 17) return true; //tecla ctrl
	if(window.event.keyCode == 15) return true; //tecla shift
	if(window.event.keyCode == 9) return true; //tecla tab
	if(window.event.keyCode == 96) return true; //teclado numerico
	if(window.event.keyCode == 97) return true; //teclado numerico
	if(window.event.keyCode == 98) return true; //teclado numerico
	if(window.event.keyCode == 99) return true; //teclado numerico
	if(window.event.keyCode == 100) return true; //teclado numerico
	if(window.event.keyCode == 101) return true; //teclado numerico
	if(window.event.keyCode == 102) return true; //teclado numerico
	if(window.event.keyCode == 103) return true; //teclado numerico
	if(window.event.keyCode == 104) return true; //teclado numerico
	if(window.event.keyCode == 105) return true; //teclado numerico

   if(max > 0){
		if (!maxlength(window.event.srcElement, max)){
			//Anulo la tecla apretada
		    window.event.returnValue = 0;
			return;
		}
   }

   var s="0123456789";
   //Verifico que la tecla apretada este dentro de 
   //los caracteres permitidos
   var re=String.fromCharCode(window.event.keyCode);

   //Si no esta dentro de los caracteres permitidos
   if ( s.indexOf(re) == -1 ){
    //Anulo la tecla apretada
    window.event.returnValue = 0
	//alert('Solo se permiten números');
	return;
   }
}

function maxlength(obj,max){
	if(window.event.keyCode == 46) return true; //tecla Delete
	if(window.event.keyCode == 8) return true; //tecla Suprimir
	if(window.event.keyCode == 37) return true; //tecla Izq
	if(window.event.keyCode == 38) return true; //tecla Arriba
	if(window.event.keyCode == 39) return true; //tecla Derecha
	if(window.event.keyCode == 40) return true; //tecla Abajo
	if(window.event.keyCode == 36) return true; //tecla Inicio
	if(window.event.keyCode == 35) return true; //tecla Fin

	if(obj.value.length >= max){
		//alert("El máximo es de "+max+" caracteres");
		return false;
	}else{
		return true;
	}
}

function borrarTodos(select){
	document.all.item(select).length = 0;
}

//selecciona todos los items de un select
function selAll(select){
	for (i=0; i< document.all.item(select).length; i++) {
		document.all.item(select).options.item(i).selected = true;
	}
}

function checkCampoVacio(obj,nombre){
	var str = obj.value;
	str = trim(obj.value);
	if(str.length == 0){
		alert("Complete el campo "+nombre);
		obj.focus();
		return false;
	}else{
		return true;
	}
}

function checkCampoMin(obj,nombre,min){
	var str = obj.value;
	if(obj.type != 'password'){
		str = trim(obj.value);
	}
	if(str.length < min){
		alert("El campo "+nombre+" debe tener al menos "+min+" caracteres");
		obj.focus();
		return false;
	}else{
		return true;
	}
}

//---------------------------------------------------------------
//     Funcion : show_hide_ext(obj)
// Descripción : Muestra u oculta un objeto y cambia una imagen
//  Parametros : obj, imagen cuando está VISIBLE, imagen cuando está INVISIBLE
//         Nota: El id de la imagen tiene que ser ( id=img_xxx )
//           Ej: <td onclick="show_hide_menu(servicios, 'imagenes/flechita_abajo.gif', 'imagenes/flechita_der.gif')" style="cursor:hand;"><img id=img_servicios src="imagenes/flechita_der.gif"> 
//---------------------------------------------------------------
function show_hide_ex(obj, img_show, img_hidden){
	if(obj.style.display=='none'){
		document.all.item('img'+'_'+obj.id).src=img_hidden;
		obj.style.display='block';
	}else{
		document.all.item('img'+'_'+obj.id).src=img_show;
		obj.style.display='none';
	}
}

//-------------------------------------------------------------
//     Funcion : show_hide(obj)
// Descripción : Muestra u oculta un objeto
//  Parametros : obj
//-------------------------------------------------------------
function show_hide(obj){
	if(obj.style.display=='none'){
		obj.style.display='block';
	}else{
		obj.style.display='none';
	}
}

//-------------------------------------------------------------
//     Funcion : remoteScripting(url)
// Descripción : Carga un documento del servidor (remote scripting)
//  Parametros : la url
//-------------------------------------------------------------
var IFrameObj; // our IFrame object
function remoteScripting(url){
  if (!document.createElement) {return true};
  var IFrameDoc;
  var URL = url;
  if (!IFrameObj && document.createElement) {
    // create the IFrame and assign a reference to the
    // object to our global variable IFrameObj.
    // this will only happen the first time 
    // callToServer() is called
    var tempIFrame=document.createElement('iframe');
    tempIFrame.setAttribute('id','RSIFrame');
    tempIFrame.style.border='0px';
    tempIFrame.style.width='0px';
    tempIFrame.style.height='0px';
    IFrameObj = document.body.appendChild(tempIFrame);
  
    if (document.frames) {
      // this is for IE5 Mac, because it will only
      // allow access to the document object
      // of the IFrame if we access it through
      // the document.frames array
      IFrameObj = document.frames['RSIFrame'];
    }
  }
  
  if (navigator.userAgent.indexOf('Gecko') !=-1 
    && !IFrameObj.contentDocument) {
    // we have to give NS6 a fraction of a second
    // to recognize the new IFrame
    setTimeout('callToServer()',10);
    return false;
  }
  
  if (IFrameObj.contentDocument) {
    // For NS6
    IFrameDoc = IFrameObj.contentDocument; 
  } else if (IFrameObj.contentWindow) {
    // For IE5.5 and IE6
    IFrameDoc = IFrameObj.contentWindow.document;
  } else if (IFrameObj.document) {
    // For IE5
    IFrameDoc = IFrameObj.document;
  } else {
    return true;
  }
  
  IFrameDoc.location.replace(URL);
  return false;
}


/*
Selecciona un item de un SELECT
Parametros:
	select: Se puede enviar solo en nombre del select como string o el objeto
	str: el string del texto que busca
	en_donde: "value" , "text" o "id" (¿en la propiedad value o text de los OPTION?)
*/
function seleccionarItem(select, str, en_donde){
	var obj_sel = document.all.item(select);
	if(obj_sel==null) var obj_sel = select;

	var len = obj_sel.options.length;
	for(i=0; i < len; i++){
		if(en_donde == "value"){
			if(obj_sel.options(i).value==str){obj_sel.options(i).selected=true;break;}
		}else if(en_donde == "text"){
			if(obj_sel.options(i).text==str){obj_sel.options(i).selected=true;break;}
		}else if(en_donde == "id"){
			if(obj_sel.options(i).id==str){obj_sel.options(i).selected=true;break;}
		}
	}
}
