/**
 * Arquivo contendo funções que manipulam XML.
 *
 *
 */


function main(){
	//Configurando as ilhas de dados para trabalharem sincronizadas
	xmlImagens.async = true;
	xmlCampos.async = true;
	xmlDados.async = true;
	
	//Configurando as ilhas para aceitarem consultas com XPath.
	xmlImagens.setProperty("SelectionLanguage", "XPath");	
	xmlCampos.setProperty("SelectionLanguage", "XPath");	
	xmlDados.setProperty("SelectionLanguage", "XPath");	

}


function MontaBtsAcao(td)
{
	LimpaBotoes();	
	setTimeout('CriaBotoes("'+ td.children[0].value +'")', 600);	
	   
}


function CriaBotoes(ID)
{	
	

	var xslbtsAcao;
	var strTD;		
	var xslBotoes = new ActiveXObject("Microsoft.XMLDOM");
	var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	xmlHTTP.open("GET","/PSV/Comum/XSL/CriaHtmlImgBotoes.xsl", false);
	xmlHTTP.send();
	//alert(xmlHTTP.responseText);
	xslBotoes.loadXML(xmlHTTP.responseText);
	
	
	//alert(xslBotoes.xml);
	//var nodeBotao = xmlDados.selectSingleNode("//root/row[@ID='"+ ID +"']")
	CampoValidaImg = xmlDados.selectSingleNode("//root/row[@ID='"+ ID +"']").attributes.getNamedItem("ID").text
	
		

	var nodelist = xmlCampos.selectNodes("//CAMPOS/CAMPO[@CodTipoCampoLista='3']");			

	xmlImagens.loadXML('<CAMPOS></CAMPOS>');

	//adicionando o valor 
	for(var i=0;i < nodelist.length;i++)
	{		
		var node = nodelist[i].cloneNode(true);	
		_ValorFuncao = xmlDados.selectSingleNode("//root/row[@ID='"+ ID +"']").attributes.getNamedItem(node.getAttribute("ValorFuncao")).text
		Postid = xmlDados.selectSingleNode("//root/row[@ID='"+ ID +"']").attributes.getNamedItem(node.getAttribute("NomeCampo")).text
					
		node.setAttribute("ValorFuncao",_ValorFuncao);	
		node.setAttribute("NomeCampo",Postid);
		xmlImagens.documentElement.appendChild(node);

	}
		
	
	var trs = document.all['trImagens'];
	var i;
	
	//alert(trs.length);
		
	
	if (trs.length>0)
	{
		for(i=0 ;i < trs.length ;i++)
			{
				if(ID==trs[i].children[0].value)	
				{	
					strTD = trs[i].innerHTML;
					strTD += xmlImagens.transformNode(xslBotoes.documentElement);
					trs[i].innerHTML = strTD;
				}
			}
	}
	else
	{ 
		strTD = trs.innerHTML;
		strTD += xmlImagens.transformNode(xslBotoes.documentElement);
		trs.innerHTML = strTD;
	}	
	

}

function LimpaBotoes()
{


	var trs = document.all['trImagens']
	var i;
	if (trs.length>0)
	{
		for(i=0 ;i < trs.length ;i++)
		{									
			trs[i].innerHTML ='<input type=hidden  DATAFLD="ID">'
				
		}
	
	}
	else
	{
		trs.innerHTML ='<input type=hidden  DATAFLD="ID">'
	}
			
			
}




function carregarListagem() 
{
	var xmlCampos = document.all("xmlCampos");
	var xslCampos = document.all("xslCampos");	

	//alert(xmlCampos.transformNode(xslCampos.documentElement));
	divConteudo.innerHTML = xmlCampos.transformNode(xslCampos.documentElement);
	
	//fazendo backup dos dados
	xmlDadosBackup.loadXML(xmlDados.xml)	
}  


var strCampo;
var strSentido;
function SortBy(strField){   

	strSentido  = ((strCampo==strField)?((strSentido==true)?false:true):true);
	strCampo	=  strField;

	LimpaBotoes();   
try{

	//sortXML(objXML, select, isDescending, strType, isLowerFirst)
	xmlDados.loadXML(sortXML(xmlDados,'@'+strField, strSentido));			  
		
	}catch(e)
		{
			alert(e.message);
		}

}


function Filtrar(select, texto) { 
	try { xmlDados.loadXML(filtrar(xmlDados, '@'+select, texto)); 
		} catch(e) 
		{ 
			alert(e.message); 
		} 
} 


var PagAtual = 1;
function MonitorPaginacao(action)
{
	LimpaBotoes(); 
    var totalDados = xmlDados.recordset.recordcount;
    var regPPagina = tblDados.dataPageSize;    
    var TotalPag = (Math.round(totalDados/regPPagina)>=(totalDados/regPPagina)? Math.round(totalDados/regPPagina):(Math.round(totalDados/regPPagina)+1)); 
    document.all['RegPag'].innerText = regPPagina; 
    document.all['TotalReg'].innerText = totalDados;
    document.all['TotalPag'].innerText = TotalPag;	
    switch (action.toLowerCase())
    {
        case 'firstpage':
            PagAtual = 1;
            document.all['firstpage'].className ='PSV_trans_01';
            document.all['firstpage'].disabled=true;
            document.all['previouspage'].className ='PSV_trans_01';
            document.all['previouspage'].disabled=true;

            document.all['lastpage'].className ='';
            document.all['lastpage'].disabled=false;
            document.all['nextpage'].className ='';
            document.all['nextpage'].disabled=false;

        break;
        case 'previouspage':
            PagAtual --;

            document.all['lastpage'].className ='';
            document.all['lastpage'].disabled=false;
            document.all['nextpage'].className ='';
            document.all['nextpage'].disabled=false;
            
            
            if (PagAtual<=1)
            {
                document.all['firstpage'].className ='PSV_trans_01';
                document.all['firstpage'].disabled=true;
                document.all['previouspage'].className ='PSV_trans_01';
                document.all['previouspage'].disabled=true;
            }

        break;
        case 'nextpage':
            PagAtual ++;

            document.all['firstpage'].className ='';
            document.all['firstpage'].disabled=false;
            document.all['previouspage'].className ='';
            document.all['previouspage'].disabled=false;

            if (PagAtual==TotalPag)
            {
                document.all['lastpage'].className ='PSV_trans_01';
                document.all['lastpage'].disabled=true;
                document.all['nextpage'].className ='PSV_trans_01';
                document.all['nextpage'].disabled=true;
            }

        break;
        case 'lastpage':
            PagAtual = TotalPag;
                document.all['firstpage'].className ='';
                document.all['firstpage'].disabled=false;
                document.all['previouspage'].className ='';
                document.all['previouspage'].disabled=false;

                document.all['lastpage'].className ='PSV_trans_01';
                document.all['lastpage'].disabled=true;
                document.all['nextpage'].className ='PSV_trans_01';
                document.all['nextpage'].disabled=true;

        break;
        case 'ordenando':
            PagAtual = 1;
            document.all['firstpage'].className ='PSV_trans_01';
            document.all['firstpage'].disabled=true;
            document.all['previouspage'].className ='PSV_trans_01';
            document.all['previouspage'].disabled=true;

            document.all['lastpage'].className ='';
            document.all['lastpage'].disabled=false;
            document.all['nextpage'].className ='';
            document.all['nextpage'].disabled=false;

        break;
        case 'redimencionando':
            PagAtual = 1;
            document.all['firstpage'].className ='PSV_trans_01';
            document.all['firstpage'].disabled=true;
            document.all['previouspage'].className ='PSV_trans_01';
            document.all['previouspage'].disabled=true;

            document.all['lastpage'].className ='';
            document.all['lastpage'].disabled=false;
            document.all['nextpage'].className ='';
            document.all['nextpage'].disabled=false;

        break;
        case 'filtrando':
            PagAtual = 1;
            document.all['firstpage'].className ='PSV_trans_01';
            document.all['firstpage'].disabled=true;
            document.all['previouspage'].className ='PSV_trans_01';
            document.all['previouspage'].disabled=true;

            document.all['lastpage'].className ='';
            document.all['lastpage'].disabled=false;
            document.all['nextpage'].className ='';
            document.all['nextpage'].disabled=false;

        break;
    }
    
    if (TotalPag==1)
    {
            document.all['lastpage'].className ='PSV_trans_01';
            document.all['lastpage'].disabled=true;
            document.all['nextpage'].className ='PSV_trans_01';
            document.all['nextpage'].disabled=true;
            document.all['firstpage'].className ='PSV_trans_01';
            document.all['firstpage'].disabled=true;
            document.all['previouspage'].className ='PSV_trans_01';
            document.all['previouspage'].disabled=true;
            
    }
 document.all['PagAtual'].innerText = PagAtual; 
 
}



function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0

  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+= 3) if((obj=MM_findObj(args[i]))!=null) { v=
     args[i+2]; if (obj.style){ obj=obj.style;v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; 
    }
}


function Detalhes(obj,link,Parametros,target)
{

//alert(link + '?PostID='+ obj.children[0].value + ((Parametros)?'&':'') + Parametros);
	if (target.toLowerCase() == 'self')
	( 	
		location.href = link + '?PostID='+ obj.children.ValorFuncao.value + ((Parametros)?'&':'') + Parametros
	)
	else
	(
		window.open(link+'?PostID='+ obj.children.ValorFuncao.value + ((Parametros)?'&':'') + Parametros , 'Popup2')

	)
		   
}


function RetornaValorOverlib(obj,strCaption,nomeCampo)
{
		return eval('overlib( obj.children.'+ nomeCampo +'.value , CAPTION, strCaption)');	
		   
}



function ExibePopupSASC(obj,link,Parametros,tw,th) {
	window.open(link+'?PostID='+ obj.children.ValorFuncao.value + ((Parametros)?'&':'') + Parametros , 'PopuP', 'width='+ tw +',height='+ th +',center=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes')
}



function ExibePopup(obj,link,Parametros,tw,th) {
	window.open(link+'?PostID='+ obj.children[0].value + ((Parametros)?'&':'') + Parametros , 'PopuP', 'width='+ tw +',height='+ th +',center=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes')
}