function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
	else 
	{
		window.onload = function() 
		{
			if (oldonload) 
			{
				oldonload();
			}
			func();
		}
	}
}


function getElementsByClass(searchClass,node,tag) 
{
	var classElements = new Array();
	if ( node == null )
	node = document;
	if ( tag == null )
	tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	j=0;
	for (i=0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) 
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


function insertAfter(newElement, targetElement) 
{
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) 
	{
		parent.appendChild(newElement);
	}
	else 
	{
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}



function IntercambiarDisplay(objeto)
{  		
	if(document.getElementById(objeto))	
	{
		if(document.getElementById(objeto).style.display == "none")	
			document.getElementById(objeto).style.display = ""
		else
			document.getElementById(objeto).style.display = "none"			
	}
}

function MostrarOcultar(objeto)
{  		
	if(document.getElementById(objeto))	
	{
		if(document.getElementById(objeto).style.visibility == "hidden" || document.getElementById(objeto).style.visibility == "")
		{	
				
			document.getElementById(objeto).style.visibility = "visible"	
			document.getElementById(objeto).style.display = "block"
		}
		else
		{
			document.getElementById(objeto).style.visibility = "hidden"
			document.getElementById(objeto).style.display = "none"				
		}
	}
}

function  ImprimirPropiedades(obj, objName) 
{
	var output = "" ;
	for (var prop in obj) 
	{
		output += objName + "." + prop + " = " + obj[prop] + "<br>" ;
	}
	
	document.write(output)
	return output ;
}

function FechaHora()
{	  
	var Digital = new Date()
	var horas = Digital.getHours()
	var minutos = Digital.getMinutes()
	var segundos=Digital.getSeconds()
	var dia=Digital.getDay();
	var mes=Digital.getMonth();
	var ano=Digital.getYear();
	
	var fh = new String(ano + "" + mes + dia + horas + "" + minutos + segundos);
	
	return fh;
} 

function FormatearCodigo(num, n, f, r){
	return n = new Array((++n, f ? (f = (num + "").length) < n ? n - f : 0 : n)).join(0), r ? num + n : n + num;
};


function DesplegableXml(iddesplegable, xml, campoid, camponombre)
{
    var id
    var nombre
    var rows = xml.getElementsByTagName("row");        
    var tamano = rows.length
    var desplegable = document.getElementById(iddesplegable)      
    desplegable.length = null               
                
    for(var i=0;i<tamano;i++)
    {                
        if (rows[i].nodeType == 1)
        {    
            for(j=0;j<rows[i].childNodes.length;j++)
            {
                if (rows[i].childNodes[j].nodeType == 1)
                {    
                    if(rows[i].childNodes[j].nodeName == campoid)
                        id = rows[i].childNodes[j].firstChild.nodeValue
                    
                    if(rows[i].childNodes[j].nodeName == camponombre)
                        nombre = rows[i].childNodes[j].firstChild.nodeValue
                }
            }
            
            var opcion = new Option(nombre, id,"","");              
            desplegable.appendChild(opcion) 
        }            
        
    }
}

function DesplegableCargando(iddesplegable)
{
    var desplegable = document.getElementById(iddesplegable)
    desplegable.length = null  
    
    var opcion = new Option("Cargando...", "desplegable_sin_valor","","");              
    desplegable.appendChild(opcion) 
}

function FormatearNumero(num)
{
    var numero = new FormatoNumero()
    numero.sepmiles = ','
    numero.dec = 2
    numero.miles = true
    numero.valor = num
    return numero.formato()
}           

function Mensaje(text, tipo)
{
   if(!document.getElementById("mensajeestado")) 
        $("body").prepend('<div id="mensajeestado"></div> ')
        
   $("#mensajeestado").removeClass("error ok")
   
   if(tipo != null)
   {
      $("#mensajeestado").addClass(tipo)  
   } 
    
   $("#mensajeestado").html(text);   
   $("#mensajeestado").fadeIn("normal");   
}

function CerrarMensaje(tiempo)
{
    if(tiempo == null)
        tiempo = 3000
    else
        tiempo = tiempo*1000
    
        
     //Esconder mensajes de estado
    setTimeout(TemporizadorLimpiarMensajes, tiempo);
}

function TemporizadorLimpiarMensajes(){
    $("#mensajeestado").fadeOut("normal");    
}


