//JavaScript Document

function getPosicaoElemento(elemID)
{
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function NSC_defineValor(varForm,varCampo,varItem)
{
	eval("var item = document."+varForm+"."+varCampo+"_"+varItem+"_item.value");
	eval("var valor = document."+varForm+"."+varCampo+"_"+varItem+"_valor.value");

	if(eval("document."+varForm+"."+varCampo)!=null){eval("document."+varForm+"."+varCampo+".value=valor");}
	if(document.getElementById("display_"+varCampo)!=null){document.getElementById("display_"+varCampo).innerHTML=item;}
}

function NSC_showList(cmdList,imgRef1,imgRef2)
{
	if((document.getElementById(imgRef1)!=null)&&(document.getElementById(cmdList)!=null))
	{
		if(document.getElementById(cmdList).style.display=='none')
		{
			document.getElementById(cmdList).style.display='block';

			var imgRef1Pos = getPosicaoElemento(imgRef1);
	
			var posLeft = imgRef1Pos.left;
			var posTop = Math.round(imgRef1Pos.top)-1;
			
			var AlturaCamada = document.getElementById(cmdList).clientHeight;
			var AlturaConteu = document.body.clientHeight;
			
			if((AlturaCamada+posTop)>AlturaConteu){posTop = posTop-(AlturaCamada+19);}
			
			var Largura01 = document.getElementById(imgRef1).clientWidth;
			var Largura02 = document.getElementById(imgRef2).clientWidth;
			var LaguraFinal = 0;
			
			if(Largura01<Largura02){LaguraFinal=Largura02;}else{LaguraFinal=Largura01;}
	
			document.getElementById(cmdList).style.left=posLeft+'px';
			document.getElementById(cmdList).style.top=posTop+'px';
			document.getElementById(cmdList).style.width=LaguraFinal+'px';
		}
		else
		{
			document.getElementById(cmdList).style.display='none';
		}
	}	
}

function NSC_hideList(cmdList)
{
	if(document.getElementById(cmdList)!=null)
	{
		document.getElementById(cmdList).style.display='none';
	}	
}

function NSC_showListByTD(cmdList)
{
	if(document.getElementById(cmdList)!=null)
	{
		document.getElementById(cmdList).style.display='block';
	}
}


var NSelectCLass = function(FormID,CampoID,CampoWidth,MaxSelectItens)
{
	
	if(isNaN(MaxSelectItens)){MaxSelectItens=0;}
	if(MaxSelectItens==0){MaxSelectItens=5;}
	
	this.imgSrc = "images/img_select.png"; //Imagem para o botão de selecionar
	this.imgEsqSrc = "images/b_select.png";
	this.imgBgSelect = "images/bg_select.png";
	this.imgRefSrc = "images/spacer.gif"; //Imagem para referencia de largura da lista
	this.borderColor = "transparent"; //Cor com #
	this.corBack = "#FFFFFF";
	this.corOver = "#ffebd9";
	this.CssClass = "principal";
	
	this.strFormID = FormID;
	this.strCampoID = CampoID;
	this.strCampoWidth = CampoWidth;
	this.strMaxSelectItens = Math.round(MaxSelectItens);
	this.NSItens = new Array();
	this.NSValues = new Array();
	this.NSSelected = new Array();
}

NSelectCLass.prototype =
{
	addItem : function(item,valor,selected)
	{
		this.NSItens.push(item);
		this.NSValues.push(valor);
		this.NSSelected.push(selected);
	},
	
	geraSelect : function(idCmd)
	{
		if(document.getElementById(idCmd)!=null)
		{
			var SelectedItemVL = 0;
			var SelectedItemID = this.strCampoID;
			var SelectedFormID = this.strFormID;
			var StyleOverFlow = "";

			if(this.NSItens.length > this.strMaxSelectItens)
			{
				var SelectHeight = this.strMaxSelectItens*24;
				StyleOverFlow = "overflow:auto;height:"+SelectHeight+"px;";
			}
			
			var RetList = "";
				
				RetList += "<table width='"+this.strCampoWidth+"' cellpadding='0' cellspacing='0' border='0'  ><tr><td>";
				
					RetList += "<div style='position:absolute;z-index:10;' >";
						RetList += "<table width='"+this.strCampoWidth+"' cellpadding='0' cellspacing='0' border='0'  ><tr>";
						RetList += "<td width=0%  ><img src='"+this.imgEsqSrc+"' /></td>";
						RetList += "<td width=100% style='border-bottom:1px solid "+this.borderColor+"; background:url("+this.imgBgSelect+") repeat-x;' >";
						RetList += "<input type='hidden' name='"+this.strCampoID+"' id='"+this.strCampoID+"' value='' />";
						RetList += "<div name='display_"+this.strCampoID+"' id='display_"+this.strCampoID+"' style='border:0px;width:100%;height=16;font-size:11px;overflow:hidden;white-space:nowrap;' class=principal >&nbsp;</div>";
						RetList += "</td><td width='0%' >";
						RetList += "<img src='"+this.imgSrc+"' />";
						RetList += "</td></tr><tr><td colspan='3' width='100%' ><img id='imgRef1"+this.strCampoID+"' src='"+this.imgRefSrc+"' width='100%' height=1 ></td></tr></table>";
					RetList += "</div>";
					
					RetList += "<div style='position:relative;z-index:15;width:"+this.strCampoWidth+"px;height:20px;' onclick=javascript:NSC_showList('cmdSelect"+this.strCampoID+"','imgRef1"+this.strCampoID+"','imgRef2"+this.strCampoID+"'); onmouseover=javascript:this.style.cursor='pointer'; onMouseOut=JavaScript:NSC_hideList('cmdSelect"+this.strCampoID+"'); ><img src='images/spacer.gif' width='100%' height='100%' /></div>";				
				
				RetList +="</td></tr></table>";
				
					RetList += "<div id='cmdSelect"+this.strCampoID+"' style='position:absolute;display:none;z-index:500000;backgound-color:#FFFFFF;"+StyleOverFlow+";width:"+this.strCampoWidth+"px' onMouseOver=JavaScript:NSC_showListByTD('cmdSelect"+this.strCampoID+"'); onMouseOut=JavaScript:NSC_hideList('cmdSelect"+this.strCampoID+"'); >";
					RetList +="<img id='imgRef2"+this.strCampoID+"' src='"+this.imgRefSrc+"' width='100%' height='1' />";
					RetList +="<table bgcolor='#FFFFFF' width=100% cellspacing=0 cellpadding=5 border=0  class=principal  >";
					for(i=0;i<this.NSItens.length;i++)
					{
						if(this.NSSelected[i])
						{
							SelectedItemVL = i;
						}
						
						RetList += "<input type='hidden' name='"+this.strCampoID+"_"+i+"_item' id='"+this.strCampoID+"_"+i+"_item' value='"+this.NSItens[i]+"' style='border:0px;width:100%;' readonly />";
						RetList += "<input type='hidden' name='"+this.strCampoID+"_"+i+"_valor' id='"+this.strCampoID+"_"+i+"_valor' value='"+this.NSValues[i]+"' style='border:0px;width:100%;' readonly />";
						
						RetList+="<tr><td nowrap bgcolor='"+this.corBack+"' style='border-left:1px solid #e9e6e0;border-right:1px solid #e9e6e0;border-bottom:1px solid #e9e6e0;'";
						RetList+=" onmouseover=javascript:this.style.cursor='pointer';this.style.backgroundColor='"+this.corOver+"'; ";
						RetList+=" onmouseout=JavaScript:this.style.backgroundColor='"+this.corBack+"'; ";
						RetList+=" onclick=javascript:NSC_defineValor('"+this.strFormID+"','"+this.strCampoID+"',"+i+");NSC_hideList('cmdSelect"+this.strCampoID+"'); ";
						RetList+=" >"+this.NSItens[i]+"</td></tr>";
					}
					RetList +="</table>";
					RetList +="</div>";
					
			document.getElementById(idCmd).innerHTML=RetList;
			
			NSC_defineValor(SelectedFormID,SelectedItemID,SelectedItemVL);
			
		}
	}
}

