﻿var TabIndexes = new Array();

function hideSelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT")
				document.forms[i].elements[e].style.visibility="hidden";
		} // for e
	} // for i
};

function displaySelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT")
				document.forms[i].elements[e].style.visibility="visible";
		} // for e
	} // for i
};

function show_lightbox_from_element(oElem, oParent) {
	/*if (typeof isBodyLoaded == 'undefined') {
		alert('Espere a que la página se carge completamente');
		return;
	}*/
	
	var eFade = document.createElement('div');
	eFade.id = 'fade';
	eFade.className = 'black_overlay';
	var eLight = document.createElement('div');
	eLight.id = 'light';
	eLight.className = 'white_content';
	eLight.appendChild(oElem);
	
	oParent.insertBefore(eLight, oParent.childNodes[0]);
	oParent.insertBefore(eFade, oParent.childNodes[0]);
	
	hideSelectBoxes();

	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block';

	window.onscroll = function(event) {scrollTo(0,0);};
}

var lightbox_content = '';

function show_lightbox_from_div(sDiv) {
	var oDiv = document.getElementById(sDiv);
	lightbox_content = oDiv.innerHTML;
	show_lightbox();
}

function show_lightbox_from_param(content) {
	lightbox_content = content;
	show_lightbox();
}

function show_lightbox() {

	if (typeof isBodyLoaded == 'undefined') {
		setTimeout("show_lightbox()", 500);
		return;
	}
	
	var eFade = document.createElement('div');
	eFade.id = 'fade';
	eFade.className = 'black_overlay';
	var eLight = document.createElement('div');
	eLight.id = 'light';
	eLight.className = 'white_content';
	eLight.innerHTML = lightbox_content;
	
	document.body.insertBefore(eLight, document.body.childNodes[0]);
	document.body.insertBefore(eFade, document.body.childNodes[0]);
	
	hideSelectBoxes();

	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block';

	window.onscroll = function(event) {scrollTo(0,0);};
}

function get_close_btn() {
	return '<div id="admin_container" class="lightbox_close" style="">' +
				'<div class="btn">' +
					'<div class="btn_left"></div>' +
					'<div class="btn_mid">' +
						'<a href="javascript:close_lightbox();" style="color:#000;">cerrar</a>' +
					'</div>' +
					'<div class="btn_right"></div>' +
				'</div>' +
			'</div>';
}

function close_lightbox_no_clean(sUrl) {

	displaySelectBoxes();

	if (document.getElementById('light')) document.getElementById('light').style.display='none';
	if (document.getElementById('fade')) document.getElementById('fade').style.display='none';

	window.onscroll = null;
	
	if (sUrl) {
		document.location.href = sUrl;
	}
}

function close_lightbox(sUrl) {

	displaySelectBoxes();

	clean_lightbox_elems(document.getElementById('light'));
	clean_lightbox_elems(document.getElementById('fade'));
	
	if (document.getElementById('light')) document.getElementById('light').style.display='none';
	if (document.getElementById('fade')) document.getElementById('fade').style.display='none';

	window.onscroll = null;
	
	if (sUrl) {
		document.location.href = sUrl;
	}
}

function clean_lightbox_elems(oDiv) {
	if (oDiv) {
		while (oDiv.childNodes.length > 0) {
			var oTmp = oDiv.childNodes[0];
			clean_lightbox_elems(oTmp)
			oDiv.removeChild(oTmp);
		}
	}
}

function attachEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}


function attachImgOnError(img_id, img_path)
{
	var img = document.getElementById(img_id);

	/*	if (!img.complete) {
			img.src = img_path;
		}*/
	
	if (img)		
		attachEvent( img, 'error', 
			function(){
				img.src = img_path;
			}
		);

		//img.src = img.src;		
}

function attachImgOnErrorCode(img_id, code)
{
	var img = document.getElementById(img_id);
	
	if (img)		
		attachEvent( img, 'error', 
			function(){				
				eval(code);
			}
		);
		
}

function setRequired(input_id, regex, compareWith)
{
	  var input = document.getElementById(input_id);
	  if (input) {
		input.setAttribute("required", "1")

		if (regex != null){
			input.setAttribute("expresionRegular", regex)
		}

		if (compareWith != null){
			input.setAttribute("compareWith", compareWith)
		}
	  }
}
