// Abre um popup
function popUP(sURL, sName, iWidth, iHeight, scrollb) { 
	posHoriz = parseInt((screen.availWidth / 2) - parseInt(iWidth / 2));
	posVert = parseInt((screen.availHeight / 2) - parseInt(iHeight / 2));
	
	open (sURL, sName, "status=no, scrollbars="+scrollb+", left=" + posHoriz + ", top=" + posVert + ", width="+iWidth+", height="+iHeight)
}

function MascaraCEP (formato, evento, objeto)
{
	var keypress = evento.keyCode ? evento.keyCode : evento.which ? evento.which : evento.charCode;
	
	if(keypress == 8 || keypress == 37 || keypress == 39 || keypress == 46)
	{
		return true;	
	}
	
	if(!(keypress > 47 && keypress < 58))
	{
		return false;
	}
	
	campo = eval (objeto);
	if (formato=='CEP')
	{
		caracteres = '01234567890';
		separacoes = 1;
		separacao1 = '-';
		conjuntos = 2;
		conjunto1 = 5;
		conjunto2 = 3;
		if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < (conjunto1 + conjunto2 + 1))
		{
			if (campo.value.length == conjunto1) 
			campo.value = campo.value + separacao1;
		}
		else 
			return true;
	}
}


function FormataCPF(pForm,pCampo,pTamMax,pPos1,pPos2,pPosTraco,pTeclaPres){
	var wTecla, wVr, wTam;
	
	wTecla = pTeclaPres.keyCode ? pTeclaPres.keyCode : pTeclaPres.which ? pTeclaPres.which : pTeclaPres.charCode;
	wVr = pForm[pCampo].value;
	wVr = wVr.toString().replace( "-", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( "/", "" );
	wTam = wVr.length ;
	
	if (wTam < pTamMax && wTecla != 8) { 
		wTam = wVr.length + 1 ; 
	}
	
	if (wTecla == 8 ) { 
		wTam = wTam - 1 ; 
	}
	
	if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
		if ( wTam <= 2 ){
			pForm[pCampo].value = wVr ;
		}
		if (wTam > pPosTraco && wTam <= pTamMax) {
			wVr = wVr.substr(0, wTam - pPosTraco) + '-' + wVr.substr(wTam - pPosTraco, wTam);
		}
		if ( wTam == pTamMax){
			wVr = wVr.substr( 0, wTam - pPos1 ) + '.' + wVr.substr(wTam - pPos1, 3) + '.' + wVr.substr(wTam - pPos2, wTam);
		}

		if(apenas_numero(pTeclaPres) == false)
		{
			pForm[pCampo].value = pForm[pCampo].value;
		}else{
			pForm[pCampo].value = wVr;
		}
	}
}

function apenas_numero(evento)
{
	var keypress = evento.keyCode ? evento.keyCode : evento.which ? evento.which : evento.charCode;
	
	if(keypress == 8 || keypress == 37 || keypress == 39 || keypress == 46)
	{
		return true;	
	}
	
	if((keypress > 47 && keypress < 58))
	{
		return true;
	}
	
	return false;
}
//Funcao que retorna o cep em ajax
//foi desenvolvido para pagina cadastro_pf.php
function getEndereco( Cep ){
	$.post(
		"ajax.php" ,
		{ "getCep":"True" , "cep":Cep },
		function( Res ){
			eval(Res);
			document.getElementById("bairro").value = unescape( t.bairro );
			document.getElementById("cidade").value = unescape( t.cidade );
			document.getElementById("endereco").value = unescape( t.endereco );
			
			try{
				estado = document.getElementById("estado");			
				for( x=0; x < estado.options.length;x++){
					if( estado.options[ x ].innerHTML.toString().indexOf( t.estado ) != -1 ){
						est = estado.options[x];
						est.selected = true;
					}
				}
			}catch(e){
				document.getElementById("estado").value = unescape(t.estado);
			}
			
			try{
			document.getElementById("numero").focus();
			}catch(e){}
			
		}
	);
}
	
Object.prototype.nextObject = function() 
{	
	var n = this;
	do n = n.nextSibling;
	while (n && n.nodeType != 1);
	return n;
}
 
Object.prototype.previousObject = function() {
	var p = this;
	do p = p.previousSibling;
	while (p && p.nodeType != 1);
	return p;
}

Object.prototype.nextElement = function( elem ) 
{	
	var n = this;
	do
	{
		n = n.nextSibling;
		if( n.nodeType == elem ){
			return n;
		}
	}
	while( n && n.nodeType != 1 );
	
	return n;
}
 
Object.prototype.previousElement = function( elem ){
	var p = this;
	do{
			p = p.previousSibling;
			
			if( p.nodeType == elem ){
				return p;
			}
	}
	while( p && p.nodeType != 1);
	return p;
}