// Função que retorna o endereço a partir do cep
function _ajaxRetornaEndereco()
{
	if( ( trim( $( "#CEP" ).val() ) != "" ) && ( $( "#CEP" ).val() != "_____-___" ) )
	{
		$("#ajaxCEP").show();
		$.getScript( "http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep="+$("#CEP").val(),function()
		{
			if(resultadoCEP["resultado"])
			{
				$("#Endereco").val(unescape(resultadoCEP["tipo_logradouro"]) + " " + unescape(resultadoCEP["logradouro"]) + ", nº " );
				$("#Bairro").val(unescape(resultadoCEP["bairro"]));
				$("#Cidade").val(unescape(resultadoCEP["cidade"]));
				$("#UF").val(unescape(resultadoCEP["uf"]));
			}
			$( "#ajaxCEP" ).hide();							
		});
	}	
}

// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  return foundObj;
}


// Example: preloadImages('file.gif', 'http://www.x.com/y.gif');
function preloadImages()
{
  if(document.images)
  {
    if(!document.imageArray) document.imageArray = new Array();
    var i,j = document.imageArray.length, args = preloadImages.arguments;
    for(i=0; i<args.length; i++)
    {
      if (args[i].indexOf("#")!=0)
      {
        document.imageArray[j] = new Image;
        document.imageArray[j++].src = args[i];
      }
    }
  }
}


// Example: showHideLayers(Layer1,'',1,...);
// arg 1: simple name of a layer object, such as "Layer1"
// arg 2: ignored (for backward compatibility)
// arg 3: 0 = hide or 1 = show
function showHideLayers()
{ 
  var i, visStr, obj, args = showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3)
  {
    if ((obj = findObj(args[i])) != null)
    {
      visStr = args[i+2];
      if (obj.style)
      {
        obj = obj.style;
        if(visStr == 1) visStr = 'visible';
        else if(visStr == 0) visStr = 'hidden';
      }
      obj.visibility = visStr;
    }
  }
}


//-------- Paginação ----------------------------
function pagPrimeira()
{
	$( "#cPagina" ).val( 0 );
	return true;
}	

function pagUltima()
{
	$( "#cPagina" ).val( parseInt( $( "#cTotalPaginas" ).val() ) );
	return true;
}

function pagProxima()
{
	if ( parseInt( $( "#cPagina" ).val() ) <  ( parseInt( $( "#cTotalPaginas" ).val() ) ) )
	{
		$( "#cPagina" ).val( parseInt( $( "#cPagina" ).val() ) + 1 );
		return true;
	}
	return false;
}	

function pagAnterior()
{
	if ( parseInt( $( "#cPagina" ).val() ) > 0 )
	{
		$( "#cPagina" ).val( parseInt( $( "#cPagina" ).val() ) - 1 );		
		return true;
	}
	return false;
}	

function pagIr()
{
	if ( ( parseInt( $( "#cPaginaAtual" ).val() ) > 0 ) && ( parseInt( $( "#cPaginaAtual" ).val() ) <= parseInt( $( "#cTotalPaginas" ).val() ) + 1 ) )
	{
		$( "#cPagina" ).val( parseInt( $( "#cPaginaAtual" ).val() ) - 1 );
		return true;		
	}
	return false;
}				

function Limpa_Filtro()
{
	$( "#filtro" ).val( "" );
	return true;
}


// ------------------------------------------------------
function trim(stringToTrim)
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim)
{
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim)
{
	return stringToTrim.replace(/\s+$/,"");
}


function escureceTela()
{
	$('<div id="_div_blackout"></div>').appendTo("body");	

	dAltura = $(document).height();
	dLargura = $(document).width();

	$( "#_div_blackout" ).css( "top", 0 );
	$( "#_div_blackout" ).css( "left", 0 );					
	$( "#_div_blackout" ).css( "z-index", "100000" );
	$( "#_div_blackout" ).css( "position", "absolute" );
	$( "#_div_blackout" ).css( "background-color", "#000000" );
	$( "#_div_blackout" ).css( "display", "none" );
	
	$( "#_div_blackout" ).height( dAltura );
	$( "#_div_blackout" ).width( dLargura );
	
	$('embed, object, select').hide();	
	
	$( "#_div_blackout" ).show();
	$( "#_div_blackout" ).fadeTo("fast", 0.7);
}


function centralizaObj( Objeto )
{

	wAltura = $(window).height();
	wLargura = $(window).width();
	tDeslocamento = $(document).scrollTop();
	lDeslocamento = $(document).scrollLeft();

	Altura = $(Objeto).height();
	Largura = $(Objeto).width();

	Topo = tDeslocamento + ( ( wAltura - Altura ) / 2 ); 
	Esquerda = lDeslocamento + ( wLargura - Largura ) / 2;

	$(Objeto).css( "top", Topo );
	$(Objeto).css( "left", Esquerda );					
	$(Objeto).css( "z-index", "1000001" );
	$(Objeto).css( "position", "absolute" );
}

function Verifica_CPF( CPF )
{
	// Verifica se o campo é nulo
	if (CPF == '')
	{ return true; }

// Aqui começa a checagem do CPF
	var POSICAO, I, SOMA, DV, DV_INFORMADO;
	var DIGITO = new Array(10);
	DV_INFORMADO = CPF.substr(9, 2); // Retira os dois últimos dígitos do número informado
	
	// Desemembra o número do CPF na array DIGITO
	for (I=0; I<=8; I++)
	{ DIGITO[I] = CPF.substr( I, 1); }
	
	// Calcula o valor do 10º dígito da verificação
	POSICAO = 10;
	SOMA = 0;
	for (I=0; I<=8; I++)
	{ 
		SOMA = SOMA + DIGITO[I] * POSICAO;
		POSICAO = POSICAO - 1;
	}
	DIGITO[9] = SOMA % 11;
	if (DIGITO[9] < 2)
	{ DIGITO[9] = 0; }
	else
	{ DIGITO[9] = 11 - DIGITO[9]; }
	
	// Calcula o valor do 11º dígito da verificação
	POSICAO = 11;
	SOMA = 0;
	for (I=0; I<=9; I++)
	{
		SOMA = SOMA + DIGITO[I] * POSICAO;
		POSICAO = POSICAO - 1;
	}
	DIGITO[10] = SOMA % 11;
	if (DIGITO[10] < 2 )
	{ DIGITO[10] = 0; }
	else
	{ DIGITO[10] = 11 - DIGITO[10]; }
	
	// Verifica se os valores dos dígitos verificadores conferem
	DV = DIGITO[9] * 10 + DIGITO[10];
	if (DV != DV_INFORMADO)
	{ 
		alert( 'CPF inválido.' );
		formulario.CPF.value = '';
		formulario.CPF.focus();
		return false;
	} 
}

function montaFlash(swf, width, height, wmode, cache)
{
	noCache = cache || cache == undefined ? "" : "?" + new Date();
	monta_swf = "";
	monta_swf += "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\""+ width +"\" height=\""+ height +"\" title=\"\">";
	monta_swf += "<param name=\"movie\" value=\""+ swf + noCache +"\" />";
	monta_swf += "<param name=\"quality\" value=\"high\" />";
	monta_swf += "<param name=\"wmode\" value=\"transparent\" />";
	monta_swf += "<embed src=\""+ swf + noCache +"\" quality=\"high\" wmode=\""+ wmode +"\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+ width +"\" height=\""+ height +"\"></embed>";
	monta_swf += "</object>";
	document.write(monta_swf);
}

function Nova_Action( cAction ) 
{ 
  document.formulario.action = cAction; 
  document.forms.formulario.submit(); 
} 

function randomiza( maximo, minimo )
{
  if (typeof(minimo)!="number")
	 { minimo=0; }
  return( Math.floor(Math.random()*(maximo-minimo))+minimo );
}

function Checa_Email( campo )
{
	if (campo.value == "") {
		alert("Informe seu e-mail.");
		return false;
	} else {
		prim = campo.value.indexOf("@")
		if(prim < 2) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf("@",prim + 1) != -1) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf(".") < 1) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf(" ") != -1) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf(".@") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf("@.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf(".com.br.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf("/") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf("[") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf("]") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf("(") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf(")") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
		if(campo.value.indexOf("..") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return false;
		}
	}
		return true;
}

function clareiaTela()
{
	$( "#_div_blackout" ).hide();
	$('embed, object, select').show();		
}

function limpaSinais( $cObjeto )
{
	$cAux = $( $cObjeto ).val();
	$cAux = $cAux.replace( "-", "" );
	$cAux = $cAux.replace( ".", "" );
	$cAux = $cAux.replace( "/", "" );
	$( $cObjeto ).val( $cAux );
	retrun( $cAux );
}