
/*------------------------------------------------------------------------------------
*	Rotina para postar dados para processamento em servidor
------------------------------------------------------------------------------------*/

function getXmlHttpRequest() {

	if ( window.XMLHttpRequest ){

		return new XMLHttpRequest();

	} else if ( window.ActiveXObject ){

		return new ActiveXObject( "Microsoft.XMLHTTP" );

	}

}

/*------------------------------------------------------------------------------------
*	Rotinas para controle dos redirecionamentos
------------------------------------------------------------------------------------*/

function vaPara( curl ){

	document.location.href = curl;

}

function processaLocalLeilao( clocal ){

	window.open( "http://maps.google.com/maps?q='" + clocal + "'", "MAPALEILAO" );

}

/*------------------------------------------------------------------------------------
*	Rotinas para controle do formulário de alteração de idioma
------------------------------------------------------------------------------------*/

function selecionaIdioma( ID ){

	if ( ID < 0 || ID > 2 ) {
		window.alert( "Idioma inválido!" );
		return false;
	}

	informaStatus();

	var xmlhttp = getXmlHttpRequest();

	var cdados = "IDIDIOMA=" + ID;

	xmlhttp.open( "POST", "processa-idioma.php?" + cdados, true );

	xmlhttp.setRequestHeader( "Cache-Control", "no-store, no-cache, must-revalidate" );
	xmlhttp.setRequestHeader( "Cache-Control", "post-check=0, pre-check=0" );
	xmlhttp.setRequestHeader( "Pragma", "no-cache" );
	xmlhttp.setRequestHeader( "Content-Type", "text/xml" );
	xmlhttp.setRequestHeader( "encoding", "ISO-8859-1" );
	xmlhttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	xmlhttp.setRequestHeader( "Content-length", cdados.length );

	xmlhttp.onreadystatechange = function(){

		if ( xmlhttp.readyState == 4 ){

			if ( xmlhttp.status == 200 ){

				var i         = 0;
				var clances   = "";
				var resultado = xmlhttp.responseXML;

				var alerta = resultado.getElementsByTagName( "alerta" );

				if ( alerta[0].childNodes[0].data !== "OK" ){

					fechaInformacao();

					window.alert( alerta[0].childNodes[0].data );

					return false;

				}

				document.location.reload();

			} else {

				fechaInformacao();

				window.alert( "Ocorreu um erro no momento da seleção do idioma. Tente novamene mais tarde!" );

			}

		}

	}

	xmlhttp.send( null );

}

/*------------------------------------------------------------------------------------
*	Rotinas para controle de pesquisa
------------------------------------------------------------------------------------*/

function processaBusca(){

	if ( document.getElementById( "txtbuscar" ).value == "" ){
		window.alert( "Por favor, informe o que deseja pesquisar." );
		document.getElementById( "txtbuscar" ).focus()
		return false;
	}

	if ( document.getElementById( "txtbuscar" ).value.length < 3 ){
		window.alert( "Por favor, informe a menos 3 caracteres." );
		document.getElementById( "txtbuscar" ).focus()
		return false;
	}

	document.location.href = "pesquisa.php?txtbuscar=" + document.getElementById( "txtbuscar" ).value;

}

function processaNovaBusca(){

	if ( document.getElementById( "txtpalavra" ).value == "" ){

		window.alert( "Por favor, informe o que deseja pesquisar." );

		document.getElementById( "txtpalavra" ).focus()

		return false;

	}

	if ( document.getElementById( "txtpalavra" ).value.length < 3 ){

		window.alert( "Por favor, informe a menos 3 caracteres." );

		document.getElementById( "txtpalavra" ).focus()

		return false;

	}

	document.location.href = "pesquisa.php?txtpalavra=" + document.getElementById( "txtpalavra" ).value + "&cbbcategoria=" + document.getElementById( "cbbcategoria" ).value + "&cbbcampo=" + document.getElementById( "cbbcampo" ).value

}

function paginaPesquisa( ctexto, ipagina ){

	document.location.href = "pesquisa.php?txtpalavra=" + ctexto + "&ipagina=" + ipagina + "&cbbcategoria=" + document.getElementById( "cbbcategoria" ).value + "&cbbcampo=" + document.getElementById( "cbbcampo" ).value;

}

/*------------------------------------------------------------------------------------
*	Rotinas para recarregar documentos com caracteres gráficos
------------------------------------------------------------------------------------*/

function gerarOutraImagem(){

	document.location.reload();

}

/*------------------------------------------------------------------------------------
*	Rotinas para controle de formatações de campos e filtro de seus dados
------------------------------------------------------------------------------------*/

function paraMaiusculas( cid ){

	document.getElementById( cid ).value = document.getElementById( cid ).value.toUpperCase();

}

function paraMinusculas( cid ){

	document.getElementById( cid ).value = document.getElementById( cid ).value.toLowerCase();

}

function paraCasoVariavel( cid ){

	if ( document.getElementById( "itipopessoa" ).value == 1 ){

		paraMaiusculas( cid );

	} else {

		paraMinusculas( cid );

	}

}

function soNumeros( evt, cobjeto ){

	var tecla;

	if ( evt.which ){

		tecla = evt.which;

	} else {

		tecla = evt.keyCode;

	}

	if ( tecla == 13 ) {

		if ( cobjeto != '' ) { document.getElementById( cobjeto ).focus(); }

	}

	if ( tecla > 47 && tecla < 58 ){

		return true;

	} else {

		if ( tecla == 8 || ( tecla >= 35 && tecla <= 40 ) || tecla == 46 || tecla == 9 ) return true;

		return false;

	}

}

function soInteiros( evt, cobjeto ){

	var tecla;

	if ( evt.which ){

		tecla = evt.which;

	} else {

		tecla = evt.keyCode;

	}

	if ( tecla == 13 ) {

		if ( cobjeto != '' ) { document.getElementById( cobjeto ).focus(); }

	}

	if ( tecla > 47 && tecla < 58 ){

		return true;

	} else {

		if ( tecla == 8 || ( tecla >= 35 && tecla <= 40 ) || tecla == 9 ) return true;

		return false;

	}

}

function enterTab( evt, cobjeto ){

	var tecla;

	if ( evt.which ){

		tecla = evt.which;

	} else {

		tecla = evt.keyCode;

	}

	if ( tecla == 13 && cobjeto ){ document.getElementById( cobjeto ).focus(); }

	return true;

}

/*------------------------------------------------------------------------------------
*	Rotinas para tratamento de caracteres especiais antes de sua submissão
------------------------------------------------------------------------------------*/

function converteHexa( cvalor ){

	var cretorno = "";

	if ( cvalor == "" ) return( "" );

	for( i = 0; i < cvalor.length; i++ ){

		if ( cvalor.charCodeAt( i ) > 122 ){

			cretorno += "%" + cvalor.charCodeAt( i ).toString( 16 ).toUpperCase();

		} else {

			cretorno += cvalor.charAt(i);

		}

	}

	return( cretorno );

}

/*------------------------------------------------------------------------------------
*	Rotina para desabilitar o F5
------------------------------------------------------------------------------------*/

function unableF5( event ){

	var event;
	var key_f5 = 116;

	if ( event.which ){

		tecla = event.which;

	} else {

		tecla = event.keyCode;

	}

	if ( key_f5 == tecla ){

		if ( event.which ){

			//event.which = 0;

		} else {

			event.keyCode = 0;

		}

	}

	return false;

} 

/*------------------------------------------------------------------------------------
*	Rotinas para envio de lote a um amigo
------------------------------------------------------------------------------------*/

function processaEnviarLote( idleilao, idlote ){

	if ( idleilao == "" || idlote == "" ){
		return false;
	}

}

/*------------------------------------------------------------------------------------
*	Rotinas carregar tela de lote
------------------------------------------------------------------------------------*/

function carregarLote( cleilao, clote ){

	document.location.href="./leiloes-lote.php?idleilao=" + cleilao + "&idlote=" + clote;

}

/*------------------------------------------------------------------------------------
*	Rotina para carregar teste de conexão
------------------------------------------------------------------------------------*/

function carregarTesteConexao(){

	window.open( "./teste-conexao.php", "testeconexao", "left=20,top=10,height=420,width=620,statusbar=no,scrollbars=no" );

}

/*------------------------------------------------------------------------------------
*	Rotinas para conversões
------------------------------------------------------------------------------------*/

function float2moeda( num ){

   x = 0;

   if( num < 0 ){

      num = Math.abs( num );
      x = 1;

   }

   if( isNaN( num ) ) num = "0";
      cents = Math.floor( ( num * 100 + 0.5 ) %100 );

   num = Math.floor( ( num * 100 + 0.5 ) / 100 ).toString();

   if( cents < 10 ) cents = "0" + cents;
      for( var i = 0; i < Math.floor( ( num.length - ( 1 +i ) ) /3 ); i++ )
         num = num.substring( 0, num.length - ( 4 * i +3 ) ) + '.' + num.substring( num.length - ( 4 * i +3 ) );

   ret = num + ',' + cents;

   if ( x == 1 ) ret = ' - ' + ret;

   return ret;

}

function moeda2float( moeda ){

   moeda = moeda.replace( ".", "" );
   moeda = moeda.replace( ",", "." );

   return parseFloat( moeda );

}

function CleanUp( valor, validos ){

	var result = "";
	var aux;

	for( var i = 0; i < valor.length; i++ ){

		aux = validos.indexOf( valor.substring( i, i +1 ) );

		if ( aux >= 0 ){

			result += aux;

		}

	}

	return result;

}

/* Formata número tipo moeda usando o evento onKeyDown */

function FormatNumber( campo, tammax, teclapres, decimal ){

	var tecla = teclapres.keyCode;

	vr  = CleanUp( campo.value, "0123456789" );

	tam = vr.length;
	dec = decimal;

	if ( tam < tammax && tecla != 8 ){

		tam = vr.length +1;

	}

	if ( tecla == 8 ){

		tam = tam -1;

	}

	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){

		if ( tam <= dec ){

			campo.value = vr;

		}

		if ( ( tam > dec ) && ( tam <= 5 ) ){

			campo.value = vr.substr( 0, tam -2 ) + "," + vr.substr( tam -dec, tam );

		}

		if ( ( tam >= 6 ) && ( tam <= 8 ) ){

			campo.value = vr.substr( 0, tam -5 ) + "." + vr.substr( tam -5, 3 ) + "," + vr.substr( tam -dec, tam );

		}

		if ( ( tam >= 9 ) && ( tam <= 11 ) ){

			campo.value = vr.substr( 0, tam -8 ) + "." + vr.substr( tam -8, 3 ) + "." + vr.substr( tam -5, 3 ) + "," + vr.substr( tam -dec, tam );

		}

		if ( ( tam >= 12 ) && ( tam <= 14 ) ){

			campo.value = vr.substr( 0, tam -11 ) + "." + vr.substr( tam -11, 3 ) + "." + vr.substr( tam -8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam -dec, tam );

		}

		if ( ( tam >= 15 ) && ( tam <= 17 ) ){

			campo.value = vr.substr( 0, tam -14 ) + "." + vr.substr( tam -14, 3 ) + "." + vr.substr( tam -11, 3 ) + "." + vr.substr( tam -8, 3 ) + "." + vr.substr( tam -5, 3 ) + "," + vr.substr( tam -2, tam );

		}

	}

}

function roundNumber( rnum ){

	return Math.round( rnum * Math.pow( 10, 2 ) ) / Math.pow( 10, 2 );

}

function adicionaFavorito( stitulo, scaminho ){

	if( document.all ){

		window.external.AddFavorite( scaminho, stitulo );

	} else if( window.sidebar ){

		window.sidebar.addPanel( stitulo, scaminho, "" );

	}

}

