var lastMessage=0;
var lastForumIndex=0;
var forumPref='';
var forumEstado = new Array(); // Cargas de 50 em 50
var AForum = new Array();
var AForumBusca = new Array();
var dataBusca='';
var ifrSndDocument;  // --- Dá acesso ao objeto "document" do iframe ifrSnd
var ACategorias = new Array('Todos','Geral','Mercado-Ext','Mercado-BR','Papel');
var msgExcOpe = 'Deseja Realmente Excluir a Operação? Todas as Operações Posteriores a Esta Data, Para o Papel, Serão Excluídas!';
var msgExc    = 'Deseja Realmente Excluir o Item Selecionado?';
var objHlp;

function altGrafico(x)
{
	var c = x.getElementById('chart');
	var p = x.getElementById('telaMain');
	if (c.style.display=='none')
	{
		c.style.display='';
		p.style.display='none';
	}
	else
	{
		c.style.display='none';
		p.style.display='';
	}
	return false;
}

function getObjById(id)
{
   return document.getElementById(id);
}

function abreHelp(objImgHlp, oriId, bscId)
{
	objOri = getObjById(oriId); 
	objBsc = getObjById(bscId);
	for (i=0;i<objOri.childNodes.length;i++)
	{
		if(objOri.childNodes[i].nodeName=='SELECT')
		{
		    objHlp = objOri.childNodes[i];
			break;
		}
	}
	
	if (objBsc.style.display=='none')
	{	alert(objImgHlp.alt); objBsc.style.display='';}
	else
	{	objBsc.style.display='none';}
	
	return false;
	
}

function selHlp(objTxtBox)
{
//alert('Value:'+ objTxtBox.value);
    nome = objTxtBox.value.toUpperCase();
    if (nome.length<2)
    {
		objHlp.selectedIndex = 0;
		return;
    }
	if (objHlp)
	{
		for (i=1;i<objHlp.options.length;i++)
		{
			if (objHlp.options[i].text.toUpperCase().indexOf(nome)>=0)
			{
//			   alert(objHlp.options[i].text);
			   objHlp.selectedIndex = i;
			   break;
			}
		}
	}
	
	return false;
}

function setLabelText(labelID, text)
{
	x = document.getElementById(labelID);
	if (x.textContent)   // -- FF
	{
		x.textContent = text;
	}
	else
	{
		x.innerHTML = text;
	}
}

function windowOpen(url, top, left, width, height, align, winname)
{
	parms  = 'width=' + width + ', height=' + height + ',scrollbars=no,toolbars=no';
	parms += ',resizable=no, location=no'+',top=' + top +',left=' + left;
	
	x = window.open(url, winname, parms);
	x.focus();
}

function countChar(obj,lim,objDspName)
{
	if (obj.value.length>lim)
	{
		obj.value = obj.value.substr(0,lim);
	}
	if (objDspName)
	{	
	   document.getElementById(objDspName).value = lim - obj.value.length - 1;
	}
}

function newRecado(msg)
{
	if (!msg) msg = '*Recado !*';
	parms  = 'width=100,height=60,scrollbars=no,toolbars=no';
	parms += ',resizable=no,location=no,top=500,left=10';
	
	x = window.open('', 'newRecado', parms);
	x.document.write('<DIV onClick="window.opener.focus();window.close();">');
	x.document.write(msg);
	x.document.write('</DIV>');
}

function catChange() 
{
	if (document.getElementById('ddlEveCat').selectedIndex==4) // Papel
	{
	   document.getElementById('tblPap').style.display='';
	   document.getElementById('txtPapCod').value='';
	}
	else
	{
	   document.getElementById('txtPapCod').value='****';
	   document.getElementById('tblPap').style.display='none';
	}
}


function getHoraAtu(ms) 
{
  curtime = new Date();
  curhour = curtime.getHours()   + 100;
  curmin  = curtime.getMinutes() + 100;
  cursec  = curtime.getSeconds() + 100;
  var time = curhour.toString().substring(1) + ':' + curmin.toString().substring(1) + ':' + cursec.toString().substring(1);
  return time;
}  


function ifrReload(){
 // alert('Oi-Dentro da Função');";
   parent.frames['ifrUpd'].location.href="/osardinha/User/oSaCheck.aspx";
}

// --- Funções para fazer sort() de arrays numéricos
function numOrdA(a, b){ return (a-b); }
function numOrdD(a, b){ return (b-a); } 

function setIFrame(frameName, url)
{
//	alert('frameName:' + frameName, ' - url:' + url);
	if (parent)
	{
//		alert(parent.document.getElementById(frameName).src);
		parent.document.getElementById(frameName).src = url;
	}
	else
	{
		location.href = '/osardinha/index.aspx';
	}
}

function ShowObj(obj)
{
	if (obj.style.display == "none")
	{
		obj.style.display = "";
	}
	else
	{
		obj.style.display = "none";
	}
}

function teste()
{
	alert('Teste NEW');
	var amsgs = new Array();
	amsgs[0] = new js_oSaMsg("Teste 1","Objeto 1");
	amsgs[1] = new js_oSaMsg("Teste 2","Objeto 2");
	amsgs[2] = new js_oSaMsg("Teste 3","Objeto 3");
	amsgs[3] = new js_oSaMsg("Teste 4","Objeto 4");
	amsgs[4] = new js_oSaMsg("Teste 5","Objeto 5");
	js_displayoSaMsgs(amsgs);
}

function js_oSaMsg(msg, objname)
{
	this.msg     = msg;
	this.objname = objname;
	
}

function js_displayoSaMsgs(amsgs)
{
//	alert('Entrou js_displayoSaMsgs - amsgs.length = '+amsgs.length);
	
	if (amsgs.length==0) return false;
	var divAux = document.getElementById('oSaDivMsg2');
	
	if (!divAux) 
	{
		divAux = document.createElement("div");
		divAux.id		= "oSaDivMsg2";
		divAux.className= "cls_oSaDivMsg2";
	}
	else
	{
		divAux.innerHTML = "";
	}

	var osaDiv = document.getElementById('oSaDivMsg');
	
	if (!osaDiv) 
	{
		osaDiv = document.createElement("div");
		osaDiv.id		= "oSaDivMsg";
		osaDiv.className= "cls_oSaDivMsg";
	}
	else
	{
		osaDiv.innerHTML = "";
	}
	
	
	osaTable	= document.createElement("table");
	osaTable.id	= "cls_oSaTableMsg";
	osaTable.className= "msgtableTabs";
	osaTBody = document.createElement("tbody");

// --- Cabeçalho
	osaRow   = document.createElement("tr");	
	osaCel   = document.createElement("td");
	osaRow.appendChild(osaCel);
	osaCel   = document.createElement("td");
	osaCel.innerHTML = '<b>Oooops!</b>';
	osaCel.className = 'msgtabsCenter';
	osaRow.appendChild(osaCel);
	
	osaCel   = document.createElement("td");
	osaCel.className = 'msgtabsCenter';
	osaCel.innerHTML = '<IMG width=13 src="/osardinha/images/fechar.gif" onclick="getObjById(\'oSaDivMsg2\').style.display=\'none\';" >';
	osaRow.appendChild(osaCel);
	
	//osaCel   = document.createElement("td");
	//osaCel.className = 'msgtabsRight';
	//osaRow.appendChild(osaCel);

	osaTBody.appendChild(osaRow);

	for (i=0;i<amsgs.length;i++)
	{
  	   if (amsgs[i]) 
  	   {
  			if (document.getElementById(amsgs[i].objname)!=null)
  			{
				funcao = 'js_destacaoSaMsg(this,\''+ amsgs[i].objname + '\');';  	   
			}
			else
			{
				funcao = 'a=1;';   // Só para não ficar em branco
			}
			
			osaRow   = document.createElement("tr");	
			// Left
			osaCel   = document.createElement("td");
			osaCel.className = 'msgtabsCenter';
			osaRow.appendChild(osaCel);
			
			// Msg - Center
			osaCel   = document.createElement("td");
			osaCel.innerHTML = "<SPAN onmouseover=" + funcao + " onmouseout=" + funcao + " >" + amsgs[i].msg + "</SPAN>";
			osaCel.className = 'msgtabsCenter';
			osaRow.appendChild(osaCel);

			// -- Right
			osaCel   = document.createElement("td");
			osaCel.className = 'msgtabsCenter';
			osaRow.appendChild(osaCel);

			osaTBody.appendChild(osaRow);
	   }	
	}

	osaTable.appendChild(osaTBody);
	osaDiv.appendChild(osaTable);
	divAux.appendChild(osaDiv);
	var objBody = document.getElementsByTagName("FORM")[0];
	objBody.appendChild(divAux);
	h = osaDiv.offsetHeight;
	w = osaDiv.offsetWidth ;
	xtifr = '<iframe frameborder=2 top=0 left=150px width=' + w + 'px height= ' + h + 'px></iframe>';
	divAux.innerHTML = xtifr + divAux.innerHTML;
	//alert('2---\n' + divAux.innerHTML);
	document.getElementById('oSaDivMsg2').style.display='';
	return false;	
}

function js_destacaoSaMsg(objMsg,nomeObj)
{
   obj = document.getElementById(nomeObj);
   
   if (!obj.currentStyle) return;
   
   if (obj.currentStyle.backgroundColor)
   {
		color = obj.currentStyle.backgroundColor;
   }
   else
   {
   
		color = obj.style.backgroundColor;
   }
   if (color=='transparent')
   {
		color = obj.document.bgColor;
   }
   var newColor = getRGBCompColor(color);
//   if (confirm('Dump?')) dumpProps(obj,nomeObj);
   obj.style.background = newColor ;
   if (objMsg.style.cursor == 'hand')
   	   objMsg.style.cursor = 'default';
   else 
   	   objMsg.style.cursor = 'hand';
}

getRGBCompColor = function (hexaColorCode) {
	var hexaGrade = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
	var color = hexaColorCode.toUpperCase().replace("#", "").split("");
	var newColor = [];
	var currentColor, inverseColor

	for(var i=0; i<color.length; i++) {
		currentColor = color[i];
		inverseColor = hexaGrade[Math.abs(hexaGrade.find(currentColor)+1-hexaGrade.length)];
		newColor.push(inverseColor);
	}

	return("#" + newColor.join(""));
};
Array.prototype.find = function () {
	for(var i=0; i<this.length; i++) {
		if(this[i].toString() == arguments[0].toString()) return(i);
	}
	return(false);
}


// --------------- AUXILIARES

function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object 
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to 
      // build the message. Message includes i (the object's property name) 
      // then the object's property value on a new line 
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they 
      // click "CANCEL" then quit this level of recursion 
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object 
      if (typeof obj[i] == "object") { 
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}

function getStyle(elementID){
    var elstyle;
	if (document.getElementById(elementID).currentStyle) 
		elstyle = document.getElementById(elementID).currentStyle; 
	else 
	if (window.getComputedStyle)
	{
		elstyle = window.getComputedStyle(document.getElementById(elementID), ""); 
	}
	return elstyle;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function verificaEvento()
{
	var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.nodeName != 'DIV') return false;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return false;
	
	return true;
	// Mouseout took place when mouse actually left layer
	// Handle event
}

function is_Date(obj)
{
	data = obj.value;
	dia = data.substring(0,2);
	mes = data.substring(3,5);
	ano = data.substring(6,10);
	barra1 = data.substring(2,3);
	barra2 = data.substring(5,6);

	hoje = new Date()

	if ((barra1 != "/") || (barra2 != "/")) {
		return false;
	}
	if ( isNaN(dia) || (dia < 1) || (dia > 31)) {
	return false;
	}
	else if (isNaN(mes) || (mes < 1) || (mes > 12) ) {
	return false;
	}
	else if (isNaN(ano)) {
	return false;
	}
	else if (data.length != 10) {
	return false;
	}
	quociente = (eval(ano) % 4);
	if (quociente == 0)
	biss = true;
	else
	biss = false;
	if (ano < 1900) {
	return false;
	}
	if (mes == 2) {
	if ((dia == 29) && (biss == false)) {
	return false;
	}
	else if ((dia == 30) || (dia == 31)) {
	return false;
	}
	}
	else if ((mes == 4) && (dia == 31)) {
	return false;
	}
	else if ((mes == 6) && (dia == 31)) {
	return false;
    }
    else if ((mes == 9) && (dia == 31)) {
    return false;
    }
    else if ((mes == 11) && (dia == 31)) {
    return false;
    }
return true;
}


// ---- AJAX  ---- AJAX---- AJAX---- AJAX---- AJAX---- AJAX---- AJAX---- AJAX---- AJAX---- AJAX---- AJAX---- AJAX
function ajax_Call(url, ajaxHandler){ 
	ajaxMsgWait(true); 
	
    xmlHttp = GetXmlHttpObject(); 
    
    if (xmlHttp)
    {
        xmlHttp_Get(xmlHttp, url, ajaxHandler); 
	}
	 else
	{
	    alert('xmlHttp : Não foi possível criar o objeto!'); 
	}
} 

//stateChangeHandler will fire when the state has changed, i.e. data is received back 
// This is non-blocking (asynchronous) 

// XMLHttp send GET request 
function xmlHttp_Get(xmlhttp, url, handler) { 
	xmlhttp.onreadystatechange = handler;
    xmlhttp.open('GET', url, true); 
    xmlhttp.send(null); 
} 

function xmlHttp_Post(xmlhttp, url, handler, dados) { 

//    xmlhttp.setRequestHeader('content-type','text/html; charset=UTF-8');
	xmlhttp.onreadystatechange = handler;
    xmlhttp.open('POST', url, true); 
    xmlhttp.send(dados); 
} 

function GetXmlHttpObject() 
{ 
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) 
    {
    	try 
    	{
			req = new XMLHttpRequest();
        } catch(e) 
        {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else 
    {
      if(window.ActiveXObject) 
		{
       		try 
       		{
	        	req = new ActiveXObject("Msxml2.XMLHTTP");
		  	} catch(e) 
		  	{
        		try 
        		{
          			req = new ActiveXObject("Microsoft.XMLHTTP");
	        	} catch(e) 
	        	{
          			req = false;
        		}
			}
	   }
	}
    return req; 
} 

var templateWait = 'Atualizando...';

function ajaxMsgWait(mostra,txt)
{
    var x = document.getElementById('ajaxWait');
	if (!x)
	{
		x = document.createElement('div');
		x.id = 'ajaxWait';
		x.className = 'ajaxWait';
		x.innerHTML = templateWait;
		document.getElementsByTagName('body')[0].appendChild(x);
	}
	
	if (txt)
  	   x.innerHTML = txt;
  	else
	   x.innerHTML = templateWait;

	if (mostra)
		x.style.display = '';
	else
		x.style.display = 'none';
}


function mudarDados(objFlash, strXML) 
{
    //This function updates the data of a FusionCharts present on the page
    //Get a reference to the movie 
    var FCObject = getObj(objFlash);
    //Set the data
    //Set dataURL to null
    FCObject.SetVariable('_root.dataURL',"");
    //Set the flag
    FCObject.SetVariable('_root.isNewData',"1");
    //Set the actual data
    FCObject.SetVariable('_root.newData',strXML);
    //Go to the required frame
    FCObject.TGotoLabel('/', 'JavaScriptHandler'); 
}
