var Z_INDEX_BLOQUEO_TOTAL=10000;
var Z_INDEX_BLOQUEO_PARCIAL=5000;

function DOM_siguiente(v_elemento)
{
	var siguiente=v_elemento.nextSibling;

	while(siguiente && siguiente.nodeType!=1)
	{
		siguiente=siguiente.nextSibling;
	};

	return siguiente;
}

function DOM_anterior(v_elemento)
{
	var anterior=v_elemento.previousSibling;
	
	while(anterior && anterior.nodeType!=1)
	{
		anterior=anterior.previousSibling;
	};

	return anterior;
}

function montar_elemento(tipo, clases, contenido, padre, id)
{
	var temporal=document.createElement(tipo);
	
	if(clases) temporal.className=clases;
	if(contenido) temporal.innerHTML=contenido;
	if(id) temporal.id=id;
	if(padre) padre.appendChild(temporal);

	return temporal;
}

function montar_imagen(src, title, clases, padre, id)
{
	var temporal=document.createElement('img');

	if(title)
	{
		temporal.title=title;
		temporal.alt=title;
	}
	
	if(clases) temporal.className=clases;
	if(src) temporal.src=src;
	if(id) temporal.id=id;
	if(padre) padre.appendChild(temporal);

	return temporal;
}

function montar_enlace(href, clases, contenido, padre, id, title)
{
	var temporal=document.createElement('a');
	if(title)
	{
		temporal.title=title;
		temporal.alt=title;
	}

	if(href) temporal.href=href;
	if(clases) temporal.className=clases;
	if(contenido) temporal.innerHTML=contenido;
	if(id) temporal.id=id;
	if(padre) padre.appendChild(temporal);

	return temporal;
}

function montar_input(tipo, nombre, valor, clase, id, padre)
{
	var elemento_input=null;

	switch(tipo)
	{
		case 'text':
			elemento_input=document.createElement('input');
			elemento_input.type='text';
			if(clase) elemento_input.className=clase;
			if(id) elemento_input.id=id;
			if(valor) elemento_input.value=valor;
		break;

		case 'textarea':
			elemento_input=document.createElement('textarea');
			if(clase) elemento_input.className=clase;
			if(id) elemento_input.id=id;
			if(valor) elemento_input.value=valor;
		break;

		case 'button':
			elemento_input=document.createElement('input');
			elemento_input.type='button';
			if(clase) elemento_input.className=clase;
			if(id) elemento_input.id=id;
			if(valor) elemento_input.value=valor;
		break;

		case 'file':
			elemento_input=document.createElement('input');
			elemento_input.type='file';
			if(clase) elemento_input.className=clase;
			if(id) elemento_input.id=id;
//			if(valor) elemento_input.value=valor;
		break;

		case 'hidden':
			elemento_input=document.createElement('input');
			elemento_input.type='hidden';
			if(id) elemento_input.id=id;
			if(valor) elemento_input.value=valor;
		break;

		case 'select':
			elemento_input=document.createElement('select');
			if(id) elemento_input.id=id;
			if(valor) elemento_input.value=valor;
		break;

		case 'checkbox':
			elemento_input=document.createElement('input');
			elemento_input.type='checkbox';
			if(clase) elemento_input.className=clase;
			if(id) elemento_input.id=id;
			if(valor) elemento_input.value=valor;
		break;
	}

	if(nombre) elemento_input.name=nombre;
	if(padre) padre.appendChild(elemento_input);

	return elemento_input;
}

function montar_iframe(v_clase, v_padre, v_src, v_nombre, v_id)
{
	var clase=v_clase ? v_clase : '';
	var src=v_src ? v_src : '';
	var nombre=v_nombre ? v_nombre : 'iframe';
	var id=v_id ? v_id : nombre;
	
	var frame=null;

	try 
	{		
		//Ie... otro mundo.
  		frame=document.createElement('<iframe name="'+nombre+'" class="'+clase+'" src="'+src+'" id="'+id+'">');

	} 
	catch(excepcion) 
	{
		frame=montar_elemento('iframe', clase, null, null, id);
		frame.src=src;
		frame.name=nombre;
	}

	if(v_padre)
	{
		v_padre.appendChild(frame);
	}

	return frame;
}

function insertar_opcion(valor, texto, select, marcado)
{
	var opcion = document.createElement('option'); 
	opcion.value=valor;
	opcion.text=texto;
	opcion.innerText=texto;

	if(marcado) opcion.selected=true;

	select.appendChild(opcion);
}

//Cuando tenemos demasiadas opciones en un select se nos puede colgar. Para eso
//tenemos esta función... Técnicamente incorrecto, pero razonablemente rápido.

function eliminar_contenido_rapido(v_elemento)
{
	v_elemento.innerHTML='';
}

function eliminar_contenido(elemento)
{
	var l=elemento.childNodes.length;
	var i=0;
	for(i=0; i<l; i++)
	{
		elemento.removeChild(elemento.childNodes[0]);
		try
		{
			delete(elemento.childNodes[0]);
			elemento.childNodes[0]=null;
		}
		catch(error) {}
	}
}

function eliminar_elemento(elemento)
{
	eliminar_contenido(elemento);
	elemento.parentNode.removeChild(elemento);
}

function crear_estilo_css(texto_css, es_archivo, v_id)
{
	if(!es_archivo)
	{
		var temp=document.createElement('style');
		temp.type="text/css";

		if(temp.styleSheet)
		{
			temp.styleSheet.cssText=texto_css;
		}
		else
		{
			temp.appendChild(document.createTextNode(texto_css));
		}
	}
	else
	{
		var temp=document.createElement('link');
		temp.rel='stylesheet';
		temp.type='text/css';
		temp.href=texto_css;
	}

	document.getElementsByTagName('head')[0].appendChild(temp);

	if(v_id) temp.id=v_id;

	return temp;
}

function detener_propagacion(v_evento)
{
	if(!v_evento)	//Para Explorer: Si no hay evento se maneja con window.Event.
	{
		var evento=window.event;
		evento.cancelBubble=true;	//También para para explorer....
		evento.returnValue=false;
	}
	else var evento=v_evento;

	if(evento.stopPropagation) 	//Y ahora para fireFox...
	{
		evento.stopPropagation();
		evento.preventDefault();
	}
}

function asociar_calendario(input, boton)
{
	boton.onclick=function()
	{
		new Selector_fecha_vf(input, boton);
	}
}


function getElementsByClassName(v_elemento, v_clase)
{
	var resultado=new Array();

	if(v_elemento.className && v_elemento.className.search(v_clase)!=-1)
	{
		resultado.push(v_elemento);
	}

	if(v_elemento.hasChildNodes())
	{
		var i;
		var l=v_elemento.childNodes.length;
		for(i=0; i<l; i++)
		{
			var encontrados=getElementsByClassName(v_elemento.childNodes[i], v_clase);
			if(encontrados instanceof Array && encontrados.length)
			{	
				resultado=resultado.concat(encontrados);
			}
		}
	}

	return resultado;
}

function getElementByName(v_elemento, v_name)
{
	var resultado=null;

	if(v_elemento.name && v_elemento.name==v_name)
	{
		resultado=v_elemento;
	}
	else if(v_elemento.hasChildNodes())
	{
		var i;
		var l=v_elemento.childNodes.length;
		for(i=0; i<l; i++)
		{
			resultado=getElementByName(v_elemento.childNodes[i], v_name);
			if(resultado!=null) break;
		}
	}

	return resultado;
}

function obtener_dimensiones_chrome()
{
	var alto;
	var ancho;

	if(window.innerWidth) 
	{
		ancho=window.innerWidth;
		alto=window.innerHeight;
	} 
	else if(document.documentElement && document.documentElement.clientWidth) 
	{
		ancho=document.documentElement.clientWidth;
		alto=document.documentElement.clientHeight;
	} 
	else if(document.body) 
	{
		ancho=document.body.clientWidth;
		alto=document.body.clientHeight;
	}

	var resultado=Array(ancho, alto);

	return resultado;
}

function crear_script_js(archivo, v_id, v_callback, v_ambito_callback)
{
	var temp=document.createElement('script');
	temp.type='text/javascript';
	temp.src=archivo;	
	document.getElementsByTagName('head')[0].appendChild(temp);
	if(v_id) temp.id=v_id;

	if(document.all) //IE
	{
		temp.onreadystatechange=function() 
		{
			if(temp.readyState=='loaded')
			{
				if(v_ambito_callback) v_callback.call(v_ambito_callback);
				else v_callback();
			}
		}
	}
	else
	{
		temp.onload=function() 
		{
			if(v_ambito_callback) v_callback.call(v_ambito_callback);
			else v_callback();
		}
	}
}

function es_descendiente_de(v_sujeto, v_padre)
{
	if(!v_padre || !v_sujeto) return false;
	else
	{
		if(v_sujeto==v_padre) return true;
		else if(v_sujeto!=document.body && v_sujeto.parentNode) return es_descendiente_de(v_sujeto.parentNode, v_padre);
		else return false;
	}
}

function transparentar(v_elemento, v_desde, v_hasta, v_callback, v_ambito_callback, v_salto, v_velocidad)
{
	var salto=v_salto ? v_salto : 5;
	var velocidad=v_velocidad ? v_velocidad : 5;
	var direccion=v_desde > v_hasta ? -1 : 1;
	var actual=v_desde;

	function establecer_transparencia(v_elemento, v_valor)
	{
		var standard=parseFloat(v_valor / 100);

		if(standard < 0) standard=0;
		else if(standard > 1) standard=1;

		var ie=parseInt(standard);
		
		v_elemento.style.opacity=standard;
		v_elemento.style.filter='alpha(opacity='+ie+')';
	}

	function procesar()
	{		
		var continuar=false;

		if(direccion==1)
		{
			actual=actual+salto;
			if(actual < v_hasta) continuar=true;
		}
		else
		{
			actual=actual-salto;
			if(actual > v_hasta) continuar=true;
		}

		establecer_transparencia(v_elemento, actual);

		if(continuar) setTimeout(function() {procesar()} , velocidad);
		else 
		{
			if(v_callback)
			{
				if(v_ambito_callback) v_callback.call(v_ambito_callback);
				else v_callback();
			}
		}
	}

	establecer_transparencia(v_elemento, actual);
	setTimeout(function() {procesar()} , velocidad);
}

