var _UrlSitio = '';
var _EstaSobre = false;
var _SobreId = null;
var _Adentro = false;
function ToggleElemento(idElemento)
{
	var ele 		= document.getElementById(idElemento);
	var respuestas 	= Ext.query('.respuesta');
	i=0;
	while(i<respuestas.length)
	{
		if(respuestas[i].id != idElemento)
		{
			respuestas[i].style.display = 'none';
		}
		i++;
	}
	if (ele != null)
	{
		if (ele.style.display == 'block')
		{
			ele.style.display = 'none';
		}
		else
		{
			ele.style.display = 'block';
		}
	}
}

function OcultarBotonera(id)
{
	_EstaSobre = false;
	window.setTimeout('Ocultar('+id+')', 10);
	
}
function Ocultar(id)
{
	if(!_EstaSobre || _SobreId !== id)
	{
		var ele 		= Ext.get('subBotonera_' + id);
		var linkEle 	= Ext.get('btnlink_' + id);
		if(ele !== null)
		{
			ele.dom.style.display = 'none';
		}
			
		linkEle.removeClass('seleccionado');
		
		KillModalDivBackground(false);
	}
}
function AcomodarMenu(id)
{
	var ele 		= Ext.get('subBotonera_' + id);
	var linkEle 	= Ext.get('btnlink_' + id);
	var eleBotonera = Ext.get('botoneraPrincipal');
	var elePadre = Ext.get('btn_' + id);
	var widthSubBotonera 	= elePadre.getWidth();
	var leftSubBotonera 	= elePadre.getLeft();
	
	var anchoSite 			= 940;
	ele.setTop(elePadre.getTop() + 24);
	
	
}

function MostrarBotonera(id)
{
	_EstaSobre = true;
	_SobreId = id;
	_Adentro = true;
	if(_Adentro)
	{
		window.setTimeout('', 1000);
		_Adentro = false;
	}
	//ele es el menu desplegado
	//elepadre es el li
	var ele 		= Ext.get('subBotonera_' + id);
	var linkEle 	= Ext.get('btnlink_' + id);
	var eleBotonera = Ext.get('botoneraPrincipal');
	
	if (ele.dom.style.display == 'none')
	{
		//ele.dom.style.display = '';
		lis = ele.dom.children[0].children;
		width=45;
		i=0;
		width = width + 178*(lis.length - 1) + 214;
		if(linkEle.getLeft() + width > eleBotonera.getRight())
		{
			ele.setLeft(eleBotonera.getRight() - width - 21);
		}
		else
		{
			ele.setLeft(linkEle.getLeft());
		}
		ele.slideIn('t');
		linkEle.addClass('seleccionado');
		var flechitaAzul 	= Ext.query('#subBotonera_' + id + ' .subBotoneraFlechaSelec');
		flechitaAzul 		= Ext.get(flechitaAzul[0]);
		
		flechitaAzul.setLeft((linkEle.getLeft() - ele.getLeft()) + 15);
		//CreateModalBackground(undefined, 0, undefined, function(){MostrarBotonera(id)});
	}
	else
	{
//		ele.dom.style.display = 'none';
//		
//		linkEle.removeClass('seleccionado');
//		
//		KillModalDivBackground(false);
	}
}

var _idModalDivBackground = 'GlobalModalBackground';

function CreateModalBackground(id, opacity, zIndex, onClick, hideSelects)
{
	if (typeof(id) == 'undefined')
	{
		id = _idModalDivBackground;
	}
	
	if (typeof(opacity) == 'undefined')
	{
		opacity = 0.6;
	}
	
	if (typeof(zIndex) == 'undefined')
	{
		zIndex = 99;
	}
	
	if (typeof(onClick) == 'undefined' || onClick == null)
	{
		onClick = function(){ KillModalDiv(true); };
	}
	
	if (typeof(hideSelects) == 'undefined')
	{
		hideSelects = true;
	}
	
	siteBody 		= Ext.getBody();
	modalBackground = Ext.get(document.createElement('div'));
	sizes 			= GetMaxSizes();
	
	modalBackground.dom.setAttribute('id', id);
	modalBackground.addClass('GlobalModalBackground');
	modalBackground.setHeight(sizes.height);
	modalBackground.setOpacity(opacity);
	modalBackground.addListener('click', onClick);
	modalBackground.dom.style.zIndex = zIndex;
	
	if (hideSelects)
	{
		siteBody.addClass('showingModalBG');
	}
	
	if (!hideSelects)
	{
		body = GetBodySizes();
	
		modalBackground.setHeight(body.height - 30);
		modalBackground.setWidth(sizes.width - 30);
	}

	siteBody.appendChild(modalBackground);
}

function KillModalDivBackground(fade)
{
	if (fade != true)
	{
		fade = false;
	}
	
	var modalDivBG = Ext.get(_idModalDivBackground);
	
	if (modalDivBG != undefined)
	{
		if (fade)
		{
			modalDivBG.fadeOut({remove: true, callback: function(){Ext.getBody().removeClass('showingModalBG');}});
		}
		else
		{
			modalDivBG.remove();
			Ext.getBody().removeClass('showingModalBG');
		}
	}
}

function GetMaxSizes()
{
	var viewport 	= GetViewPortSizes();
	var body 		= GetBodySizes();
	
	if (viewport.width >= body.width)
	{
		var retWidth = viewport.width;
	}
	else
	{
		var retWidth = body.width;
	}
	
	if (viewport.height >= body.height)
	{
		var retHeight = viewport.height;
	}
	else
	{
		var retHeight = body.height;
	}
	
	return {width: retWidth, height: retHeight};
}

function GetViewPortSizes()
{
	if (typeof window.innerWidth != 'undefined')
	{
		var viewportWidth 	= window.innerWidth;
		var viewportHeight 	= window.innerHeight;
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		var viewportWidth 	= document.documentElement.clientWidth;
		var viewportHeight 	= document.documentElement.clientHeight;
	}
	
	return {width: viewportWidth, height: viewportHeight};
}

function GetBodySizes()
{
	return {width: Ext.getBody().getWidth(), height: Ext.getBody().getHeight()};
}

function Buscar(idInput)
{
	
	var ele = document.getElementById(idInput);
	
	if(ele.value != '')
	{
		if(_UrlSitio == '/')
		{
			_UrlSitio = '';
		}
		location.href = _UrlSitio + '/busqueda?q=' + encodeURIComponent(ele.value);
	}
	
	return false;
}

function Hide(selector)
{
	var items = Ext.query(selector);
	
	for (var i = 0; i < items.length; i++)
	{
		items[i].style.display = 'none';
	}
}

/*Funciones para mostrar errores en formularios*/
function MostrarErroresFormulario(errores, formId, cajaMensajesId)
{
	var erroresDiv = Ext.get(cajaMensajesId);
	var formulario = Ext.get(formId);
	
	if (formulario!=null && erroresDiv!=null)
	{
		limpiarSubForm(formId);
		
		erroresDiv.dom.innerHTML = '';
		
		for (var idError in errores)
		{
			if (typeof(errores[idError]) != 'function')
			{
				//Marcamos los elementos con error que tengan por nombre o por id a 'idError'
				formulario.select('[name='+idError+'],[id='+idError+']').each(function(){
					if (this.dom.type=='hidden')
					{
						//Veamos si tiene un parent q sea uno de esos divs que agrega ext al transformar los selects
						var contenedor = this.up('div[id^=ext-]');
						if (contenedor)
						{
							var inputContenedorSelect = this.next('input[type=text][id^=ext]');
							if (inputContenedorSelect)
							{
								inputContenedorSelect.addClass('colorError');
							}
						}
					}
					else
					{
						this.addClass('colorError');	
					}
				});
				
				for (var i=0; i<errores[idError].length; i++)
				{
					erroresDiv.dom.innerHTML += '<div>- ' + errores[idError][i]  + '</div>';
				}
			}
		}
		erroresDiv.removeClass('oculto');
	}
}

function MostrarMensajeOKFormulario(mensaje, formId, divErroresId)
{
	var formulario = Ext.get(formId);
	var erroresDiv = Ext.get(divErroresId);
	
	if (formulario!=null)
	{
		if (erroresDiv !=null)
		{
			erroresDiv.addClass('oculto');
		}
		limpiarSubForm(formId);
		//MostrarMensajeGlobal(mensaje);
		MostrarMensajeGlobalCandidato(mensaje);
	}
}

/**
 * Saca la clase error de los campos de un formulario
 * @param formId
 * @return void
 * 
 * @deprecated
 * @see limpiarSubForm
 */
function DesmarcarCamposConError(formId)
{
	var formulario 	= Ext.get(formId);

	if (formulario!=null)
	{
		formulario.select('[class~=colorError]').each(function(){
			this.removeClass('colorError');
		});
	}
}

function OcultarCajaMensajesFormulario(cajaMensajesId)
{
	var mensajesDiv = Ext.get(cajaMensajesId);
	
	if (mensajesDiv != null)
	{
		mensajesDiv.addClass('oculto');
	}
}

/**
 * Muestra los distintos errores para cada subformulario de alguno de los pasos que poseen esta caracteristica
 * @param errores
 * @return void
 */
function mostrarSubFormErrores(errores)
{
	for (var id in errores)
	{
		var subFormId		= id.substring(id.lastIndexOf('.')+1);
		var inputName		= id.substring(0, id.lastIndexOf('.'));
		var subFormDiv 		= Ext.get(subFormId);
		var mensajesDiv 	= subFormDiv.child('div[class~=contenedorMensajeError]');
		
		//Muestro el div de mensajes
		mensajesDiv.removeClass('oculto');
		
		mensajesDiv.dom.innerHTML += '<div>'+ errores[id] +'</div>';
		
		if (typeof(errores[id]) != 'function')
		{
			//Marcamos los elementos con error que tengan por nombre o por id a 'idError'
			var input = subFormDiv.child('[name='+inputName+']');
			if (input)
			{
				if (input.dom.type=='hidden')
				{
					//Veamos si tiene un parent q sea uno de esos divs que agrega ext al transformar los selects
					var contenedor = input.up('div[id^=ext-]');
					if (contenedor)
					{
						var inputContenedorSelect = input.next('input[type=text][id^=ext]');
						if (inputContenedorSelect)
						{
							inputContenedorSelect.addClass('colorError');
						}
					}
				}
				else
				{
					input.addClass('colorError');
				}
			}
		}
	}
}

/**
 * Quita la clase error de los campos que esten contenidos en el subform
 * y oculta el div de errores
 * @param subFormId
 * @return void
 */
function limpiarSubForm(subFormId)
{
	var subformDiv 	= Ext.get(subFormId);
	if (subformDiv)
	{
		var mensajesDiv 	= subformDiv.child('div[class~=contenedorMensajeError]');
		if (mensajesDiv)
		{
			mensajesDiv.addClass('oculto');
			mensajesDiv.dom.innerHTML = '';
		}
		subformDiv.select('*').each(function(){
			this.removeClass('colorError');			
		});
	}
}

function AddStyleSheet(path)
{
	isLoaded = StyleSheetIsLoaded(path);
	
	if (!isLoaded)
	{
		styleSheetTag = document.createElement('link');
		styleSheetTag.setAttribute('rel', 'stylesheet');
		styleSheetTag.setAttribute('type', 'text/css');
		styleSheetTag.setAttribute('href', path);
		
		headLoc = document.getElementsByTagName('head').item(0);
		headLoc.appendChild(styleSheetTag);
	}
}

function StyleSheetIsLoaded(path)
{
	styleSheets = document.getElementsByTagName('link');
	
	for (x = 0; x < styleSheets.length; x++)
	{
		src = styleSheets[x].getAttribute('href');
		
		if (src != undefined)
		{
			if (src == path)
			{
				return true;
			}
		}
	}
	
	return false;
}

function EnviarAmigo(idPagina)
{
	var buttons = [];
	
	buttons[0] = {text: 'Enviar', action: ActionEnviarAmigo, actionParams: [idPagina], closeAfterAction: false};
	buttons[1] = {text: 'Cancelar', type: 'cancel'};
	
	var modal = loadAjaxPageModal('/ajax/enviaramigo.php', {method: 'post', width: 350, title: 'Enviar por e-mail', buttons: buttons, buttonsAlignment: 'center'});
}

function ActionEnviarAmigo(idPagina)
{
	DesmarcarCamposConError('frmEnviarAmigo');
	
	ajax('/ajax/enviaramigo.php', FinActionEnviarAmigo, {method: 'post', form: 'frmEnviarAmigo', ajaxParams: {idPagina: idPagina, enviar: true}});
}

function FinActionEnviarAmigo(o)
{
	var respuesta = eval('(' + o + ')');
	
	if (respuesta.estado == 'ERROR')
	{
		MostrarErroresFormulario(respuesta.errores, 'frmEnviarAmigo', 'erroresEnviarAmigo');
	}
	else
	{
		CreateActionsModalDiv('<b>El mensaje ha sido enviado.</b>', null, 'Aceptar', 'Enviar por e-mail', undefined, undefined, 300);
	}
}
