﻿// Prestige Software

/*
+----------------------------------------------------------------------
+ .s. COMBOS
+ .s. JAVASCRIPT
+ .s. MÉTODOS PÚBLICOS: 
+		CbFix(pEdText,pEdId,pVTx,pVId,pEventFunc)
+
+ .s. pEdText -> Como string del ID
+ .s. En el ed de Id se devuelve el selectedIndex
+----------------------------------------------------------------------
*/

var _Cb_bShowed				= false;
var _Cb_oEdText;
var _Cb_oEdId;
var _Cb_iWidth;
var _Cb_iMaxHeight			= 125;
var _Cb_oDivMain;
var _Cb_sEventoFuncion		= "";

function CbFix(pEdText,pEdId,pVTx,pVId,pEventFunc)
{
	if (_Cb_bShowed)
	{	
		document.body.removeChild(document.getElementById("divCbFix"));
		//document.getElementById("divCbFix").style.display = "none";
		_Cb_bShowed = false;
		return;
	}
	
	_Cb_oEdText			= document.getElementById(pEdText);
	_Cb_oEdId			= document.getElementById(pEdId);
	_Cb_iWidth			= _Cb_oEdText.offsetWidth;
	_Cb_sEventoFuncion	= pEventFunc;
	
	if (_Cb_oEdText.disabled)
		return;
	
	_Cb_DoHTML(pVTx,pVId);
}

/*
+----------------------------------------------------------------------
+ .s. Espera un array de items. Un item = objeto.Text, objeto.Value
+
+----------------------------------------------------------------------
*/

function Cb(pEdText,pEdId,pVCbItems,pEventFunc)
{
	if (pVCbItems == null) return;
	
	if (_Cb_bShowed)
	{	
		document.body.removeChild(document.getElementById("divCbFix"));
		//document.getElementById("divCbFix").style.display = "none";
		_Cb_bShowed = false;
		return;
	}
	
	_Cb_oEdText			= document.getElementById(pEdText);
	_Cb_oEdId			= document.getElementById(pEdId);
	_Cb_iWidth			= _Cb_oEdText.offsetWidth;
	_Cb_sEventoFuncion	= pEventFunc;
	
	if (_Cb_oEdText.disabled)
		return;
	
	_Cb_DoHTMLCbItems(pVCbItems);
}

/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/

function _Cb_DoHTMLCbItems(pVCbItems)
{	
	var sHTML = "";
	
	for (i = 0;i < pVCbItems.length; i++)
	{
		var sText = pVCbItems[i].Text.replace(/"/gi,"´");
		
		sHTML += "<a href='javascript:;' class=\"Cb_Item\" onclick=\"_Cb_SendInfo('" + sText + "','" + pVCbItems[i].Value + "','" + i + "')\"><div>" + sText + "</div></a>";
	}
	
	_Cb_Do(sHTML);	
}


/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/

function _Cb_DoHTML(pVTx,pVId)
{	
	var sHTML = "";
	
	for (i = 0;i < pVId.length; i++)
	{
		var sText = pVTx[i].toString();
			sText = sText.replace(/"/gi,"´");
			
		sHTML += "<a href='javascript:;' class=\"Cb_Item\" onclick=\"_Cb_SendInfo('" + sText + "','" + pVId[i] + "','" + i + "')\"><div>" + sText + "</div></a>";
	}
	
	_Cb_Do(sHTML);	
}

/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/

function _Cb_Do(sHTML)
{
	
	if (document.getElementById("divCbFix"))
	{
		_Cb_oDivMain = document.getElementById("divCbFix")
	}
	else
	{
		_Cb_oDivMain = document.createElement("DIV");
		document.body.appendChild(_Cb_oDivMain);
		_Cb_oDivMain.id				= "divCbFix";
		_Cb_oDivMain.className		= "Cb_divMain";
		_Cb_oDivMain.style.position	= "absolute";
	}
		
	var Punto = _Cb_Posicion(_Cb_oEdText)
	_Cb_oDivMain.style.left		= Punto.x + 4 + "px";
	_Cb_oDivMain.style.top		= Punto.y + _Cb_oEdText.offsetHeight + 4 + "px";	
	
	
	_Cb_oDivMain.style.width	= (_Cb_iWidth - 2)+ "px";	
	_Cb_oDivMain.innerHTML		= sHTML;
	_Cb_oDivMain.style.display	= "";
	_Cb_oDivMain.style.height	= "";
	
	_Cb_oDivMain.style.KHTMLOpacity		= 1;
    _Cb_oDivMain.style.MozOpacity		= 1; 
	_Cb_oDivMain.style.opacity			= 1; 
   // _Cb_oDivMain.style.filter			= "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";

	if (_Cb_oDivMain.offsetHeight > _Cb_iMaxHeight)
	{
		_Cb_oDivMain.style.height	= _Cb_iMaxHeight + "px";
		_Cb_oDivMain.style.width	= (_Cb_iWidth -2)+ "px";
	}
	
	//.s. Control para que aparezca por encima del edit
	if ((_Cb_oDivMain.offsetHeight + _Cb_oDivMain.offsetTop) >= document.body.offsetHeight)
	{
		_Cb_oDivMain.style.top = _Cb_oDivMain.offsetTop - _Cb_oDivMain.offsetHeight - _Cb_oEdText.offsetHeight + "px";
	}
 
	_Cb_oDivMain.onmouseout = _Cb_MouseOut;
	
	_Cb_bShowed = true;		

}

/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/

function _Cb_MouseOut(e)
{
	if (!e) var e = window.event;
	
	//.s. El origen me da lo mismo
	//var oOrigen = (window.event) ? e.srcElement : e.target;
	//if (oOrigen.nodeName != 'DIV') return;
	
	var oDestino = (e.relatedTarget) ? e.relatedTarget : e.toElement;

	while (oDestino != null && oDestino.nodeName != "DIV" && oDestino.nodeName != "BODY")
		oDestino = oDestino.parentNode
	
	if ( oDestino == null || oDestino.nodeName == "BODY")
	{
		document.getElementById("divCbFix").style.display = "none"
		_Cb_bShowed = false;
		return;
	}	
	
	//.s. Exista otra capa por ahí	
//	if (oDestino.nodeName == "DIV" && oDestino.id != "divCbFix")
//	{
//		document.all("divCbFix").style.display = "none"
//		return false;
//		_Cb_bShowed = false;
//	}
	
	
}

/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/

function _Cb_Posicion(obj)
{
	var Punto = new Object();
	
	Punto.x = 0;
	Punto.y = 0;

	while ((obj.tagName != "BODY") && (obj.tagName != "HTML"))
	{
		Punto.x += (obj.offsetLeft - obj.scrollLeft);
		Punto.y += (obj.offsetTop - obj.scrollTop);
		
		obj = obj.offsetParent;
	}
	
	return Punto;
}

/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/

function _Cb_SendInfo(sTx,sId,sIndex)
{
	_Cb_oEdText.value				= sTx;
	_Cb_oEdId.value					= sId;
	_Cb_oEdId.selectedIndex			= sIndex;
	
	document.body.removeChild(document.getElementById("divCbFix"));
	//_Cb_oDivMain.style.display		= "none";
	
	_Cb_bShowed				= false;
	
	if (_Cb_sEventoFuncion)
	{
		eval(_Cb_sEventoFuncion);
		_Cb_sEventoFuncion = null;
	}

}