// Funções Gerais - O Retorno

// Informações do navegador
function checkBrowser(){
	var T=this, b=navigator.appName, v=navigator.appVersion, u=navigator.userAgent;
	if (b=='Netscape'){	T.b='ns';	}
	else {	if(b=='Microsoft Internet Explorer'){	T.b='ie';	}
	else {	T.b=b;	}	}
	T.v=parseInt(v);
	T.ns=(T.b=='ns'&&T.v>=4);
	T.ns4=(T.b=='ns'&&T.v==4);
	T.ns5=(T.b=='ns'&&T.v==5);
	T.ns6=(T.b=='ns'&&T.v==5);
	T.ns72=(T.b=='ns'&&u.indexOf('rv:1.7.2')>0);
	T.ns80=(T.b=='ns'&&(u.indexOf('rv:1.7.5')>0||u.indexOf('Firefox/1.0.1')>0));
	T.ie=(T.b=='ie'&&T.v>=4);
	T.ie4=(u.indexOf('MSIE 4')>0);
	T.ie5=(u.indexOf('MSIE 5.0')>0);
	T.ie55=(u.indexOf('MSIE 5.5')>0);
	T.ie6=(u.indexOf('MSIE 6.0')>0);
	if(T.ie5)T.v=5;
	if(T.ie55)T.v=5.5;
	if(T.ie6)T.v=6;
	if(T.ns72)T.v=7.2;
	if(T.ns80)T.v=8.0;
	T.sa=(u.indexOf('Safari')>0);
	T.min=(T.ns||T.ie||T.sa);
	T.dom=(T.v>=5);
	T.win=(u.indexOf('Win')>0);
	T.mac=(u.indexOf('Mac')>0);
}

var is=new checkBrowser();
var d=document;

function _cria(linha) {	d.writeln(linha);	}
function _criam(linha) {	d.write(linha);	}

function FormataData(campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
			document.form[campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document.form[campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	}
}

function abre(url,janela,larg,alt,scroll){
	window.open(url,janela,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+scroll+",resizable=no,copyhistory=no,width="+larg+",height="+alt);
}

function Abre_PopUp(lista){
	window.open(lista.value+'','new','width=450,height=450,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
}

function cria_separador() {
	_cria("<table width=\"50\" height=\"3\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
	_cria("	<tr>");
	_cria("		<td height=\"10\"></td>");
	_cria("	</tr>");
	_cria("</table>");
}

function cria_linha() {
	_cria("	<tr>");
	_cria("		<td class=\"BordaTabela01\"></td>");
	_cria("		<td class=\"BordaTabela01\"></td>");
	_cria("		<td class=\"BordaTabela01\"></td>");
	_cria("	</tr>");
}

function mesma_windowEx(pagina,alvo) 
{
	window.open(pagina,alvo);
}

function mesma_window(pagina) 
{
	mesma_windowEx(pagina,'_self');
}

function limpa_lista(lista) {
	var i;
	for (i = lista.options.length; i >= 0; i--) {
		lista.options[i] = null; 
	}
}

function _compara_txt(a, b) {
	var x, s, e, res, ta, tb;
/*
	s="áàâãéèêíóôõúüç";
	e="aaaaeeeiooouuc";
	s=s+s.toUpperCase();
	e=e+e.toUpperCase();
*/	ta=a;
	tb=b;
/*	for (x=0; x<s.length; x++) {	ta=ta.replace(s.charAt(x),e.charAt(x));	}
	for (x=0; x<s.length; x++) {	tb=tb.replace(s.charAt(x),e.charAt(x));	}
*/	res=0;
	if (ta<tb) {	res=-1;	}
	if (ta>tb) {	res=1;	}
	return res;
}

function ife(condicao,valor_true,valor_false) {
	if (condicao) { return valor_true } else { return valor_false }
}

function ImprimeEmail(conta, dominio) {
	_criam(conta);
	_criam("@");
	_criam(dominio);
}

function PoeTexto(campo, texto) 
{
	if (campo!=null) 
	{
		if (campo.type=="select-one") {
			var x;
			for (x=0; x<campo.options.length; x++) {	campo.options[x].selected=(campo.options[x].value==texto);	}
			if (campo.onchange!=null) {	campo.onchange();	}
		} else if (campo.type=="text") {
			campo.value=texto;
			if (campo.onchange!=null) {	campo.onchange();	}
		} else if (campo.type=="checkbox") {
			var x;
			if (campo.length==null) {
				if (campo.value==texto) {
					campo.checked=true;
					if (campo.onclick!=null) {	campo.onclick();	}
				}
			} 
			else 
			{
				for (x=0;x<campo.length;x++)
				{
					if (campo[x].value==texto) 
					{
						campo[x].checked=true;
						if (campo.onclick!=null) 
						{	campo.onclick();	}
					}
				}
			}
		} else if (campo.type=="hidden") 
		{
			campo.value=texto;
		} 
		else if (campo.type=="textarea") 
		{
			campo.value=texto;
		} 
		else 
		{
			campo.value=texto;
		}
	}
}

function PoeFoco(campo) {
	if (campo.type=="select-one") {
		campo.focus();
		if (campo.onfocus!=null) {	campo.onfocus();	}
	} else if (campo.type=="text") {
		campo.focus();
		if (campo.onfocus!=null) {	campo.onfocus();	}
		campo.select();
		if (campo.onselect!=null) {	campo.onselect();	}
	}
}

function EsperaMili(mili) {
	var x=new Date(), t=new Date();
	x.setTime(x.getTime()+mili);
	while (x>t) {
		t=new Date();
	}
}

function InStr(posi, str, ch) {
	var x, res=-1, f, t;
	f=str.length-(ch.length-1);
	for (x=posi;x<f;x++) {
		t=str.substr(x,ch.length);
		if ((res==-1) && (t==ch)) {	res=x;	}
	}
	return res;
}

function Parte(str, num, c) {
	var x, res, posi=0, y=1, posf;
	while (y<num) {
		posi=InStr(posi, str, c)+c.length;
		y+=1;
	}
	posf=InStr(posi, str, c);
	if (posf==-1) {
		res=str.substr(posi);
	} else {
		res=str.substr(posi, posf-posi);		
	}
	return res;
}

function ContaParte(str, ch) {
	var x, res=1, f, t;
	f=str.length-(ch.length-1);
	for (x=0;x<f;x++) {
		t=str.substr(x,ch.length);
		if (t==ch) {	res+=1;	}
	}
	return res;
}

function GravaCookie(chave, valor) {
	var coo=document.cookie, x, cv, ci, ok=false, t1, t2, npart, ncoo="", nvalor=escape(valor);
	cv="=";
	ci="; ";
	npart=ContaParte(coo,ci);
	for (x=1;x<=npart;x++) {
		t1=Parte(coo, 1, cv);
		t2=Parte(coo, 2, cv);
		if (t1==chave){
			t2=nvalor;
			ok=true;
		}
		ncoo+=ife(ncoo.length>0,ci,"")+t1+cv+t2;
	}
	if (!ok) {
		ncoo+=ife(ncoo.length>0,ci,"")+chave+cv+nvalor;
	}
	document.cookie=ncoo;
}

function PoeRotulo(lista_origem, obj_destino) {
	function ListaOnChange(e) {
		if (lista_onchange!=null) {	lista_onchange(e);	}
		obj_destino.value=lista_origem.options[lista_origem.selectedIndex].text;
	}
	var lista_onchange;
	lista_onchange=lista_origem.onchange;
	lista_origem.onchange=ListaOnChange;
}

function Clica_CheckBox(check, valor) {
	var x;
	if(check!=null)
		if(check.length!=null)
			for(x=0; x<check.length; x++) {
				if(check[x].value==valor) {
					check[x].checked=!check[x].checked;
					if(check[x].onclick!=null) check[x].onclick(null);
				}
			}
		else if(check.value==valor) {
			check[x].checked=!check[x].checked;
			if(check[x].onclick!=null) check[x].onclick(null);
		}
}

function Liga_Campos01()
{
	var xeker=arguments[0];
	
	function liga_text(ori, des)
	{
		var ori_change, ori_keyup;
		
		function ori_onchange(e)
		{
			if(ori_change!=null) ori_change(e);
			if(xeker.checked){
				des.value=ori.value;
				if(des.onchange!=null) des.onchange(e);
			}
		}
		
		function ori_onkeyup(e)
		{
			if(ori_keyup!=null) ori_keyup(e);
			if(xeker.checked){
				des.value=ori.value;
				if(des.onkeyup!=null) des.onkeyup(e);
			}
		}
		
		ori_change=ori.onchange;
		ori.onchange=ori_onchange;
		ori_keyup=ori.onkeyup;
		ori.onkeyup=ori_onkeyup;
	}
	
	function liga_select_one(ori, des)
	{
		var ori_change;
		
		function ori_onchange(e)
		{
			if(ori_change!=null) ori_change(e);
			if(xeker.checked){
				des.options[des.selectedIndex].selected=false;
				des.options[ori.selectedIndex].selected=true;
				if(des.onchange!=null) des.onchange(e);
			}
		}
		
		ori_change=ori.onchange;
		ori.onchange=ori_onchange;
	}
	
	var x=1, obj_origem, obj_destino;
	while(x<arguments.length)
	{
		obj_origem=arguments[x++];
		obj_destino=arguments[x++];
		if(obj_origem.type=='text') {	liga_text(obj_origem, obj_destino);	}
		else if(obj_origem.type=='select-one') {	liga_select_one(obj_origem, obj_destino);	}
	}
}

function Liga_Campos02(botao, origem, destino)
{
	var botao_click;
	
	function botao_onclick(e)
	{
		if(botao_click!=null) botao_click(e);
		if(origem.type=='text')
		{
			destino.value=origem.value;
		}
		else if(origem.type=='select-one')
		{
			destino.options[destino.selectedIndex].selected=false;
			destino.options[origem.selectedIndex].selected=true;
		}
		if(destino.onchange!=null) destino.onchange(e);
	}
	
	botao_click=botao.onclick;
	botao.onclick=botao_onclick;
}

function FormSubmit(f)
{
	if(f.onsubmit!=null) f.onsubmit(arguments[0]);
	f.submit();
}

function Persiste(nomeForm, tempo) {
	tempo+=1000;
	setTimeout("Persiste("+nomeForm.name+","+tempo.toString()+");",tempo-1000);
	nomeForm.submit();
}

function SubmitPersistente(nomeForm) {
	Persiste(nomeForm, 3000);
}