/******************************************************************************/
/* Arquivo:		Functions.js  												  					*/
/* Descrição: 	Contém funções JavaScript genéricas									  	*/
/* Autor: 		Wellington Cardador - 29/10/2003							 				*/
/******************************************************************************/

/******************************************************************************/
/* Função:		isNum																				*/
/* Descrição:	Permite a digitação somente de números em um campo INPUT HTML	*/
/*					números de 0 a 10																*/
/*	Exemplo:		evento onKeyPress="return isNum(window.event.keyCode);"			*/
/******************************************************************************/
//document.onkeydown = disableAltCtrl; 

function isNum(key) 
{

	if ( key == 0 || key == 8 ) //tab, backspace
		return true;

	if ( key == 32 ) //space 
		return false;
		
	if ( key<48  ||  key>57  )  
		return false;
}

function isNum2(key) //número com espaços
{
	if(key==32)
		return true;
	if ( key<48  ||  key>57  )  
		return false;
}

function isNumAlfa(key) 
{
	/*	permitir digitação de números e letras somente (sem hífens, pontos, espaços)
		útil para campos do tipo RG */
	
	if ( key == 0 || key == 8 || key == 32 ) //tab, backspace, arroba
		return true;

	if (  ( key<48 || key>57 )  && ( key<97 || key >122 ) && ( key<65 || key >90 ) )  
		return false;
}

function isNumAlfa2(key) 
{
	/*	permitir digitação de números e letras somente (sem hífens, pontos, espaços)
		útil para campos do tipo RG */
	
	if ( key == 0 || key == 8 ) //tab, backspace, arroba
		return true;

	if ( key == 32 )
		return false;
		
	if (  ( key<48 || key>57 )  && ( key<97 || key >122 ) && ( key<65 || key >90 ) )  
		return false;
}

function isEmail(key) 
{
	/*permitir caracteres de email somente*/
	//alert(key);
	if ( key == 0 || key == 8 || key == 32 || key == 64 || key == 46 || key == 95) //tab, backspace, space (netscape), ponto(.)
		return true;
		
	if (  ( key<48 || key>57 )  && ( key<97 || key >122 ) && ( key<65 || key >90 ) )  
		return false;
}


function isReal(key) //função para valor em R%
{
	//48 a 57 numbers
	//44 comma
	//46 dot
		
	if ( !( ( key>=48  &&  key<=57 ) || ( key == 44 ) || ( key == 46 ) )  )  
		return false;
}

function isAlfa(key) 
{
	/* permitir digitação somente de letras 
		Bloqueia números 
		Bloqueia caracteres especiais
		Libera acentuação
	*/
	
	keyOk = true;
	
	if ( key>=33  &&  key<=64  )  
		keyOk = false;
		
	if ( key>=91 && key <=96 ) 
		keyOk = false;

	if ( key>=123 && key <=134 ) 
		keyOk = false;

	if ( key==180 ) 
		keyOk = false;

	//alert(key);	
	return keyOk;

}

function isDateFormat ( key ) //data com barras
{
	if ( key == 0 || key == 8 || key == 32 ) //tab, backspace, space (netscape)
		return true;

	if ( ( ( key<47 ) || ( key>57 ) ) )  
		return false;
}

function dateMask( date,field ) 
{ 
   var dateAtual;
   tamanho = date.length;
   if ( ( tamanho == 8 ) && ( date.indexOf("/") <= 0 ) )
   {
	   //dia e mês com um dígito e ano com quatro dígitos
	   if ( date.length == 8 ) 
		{
	      dateAtual = date.substring(0,2) + '/' + date.substring(2,4)+ '/' + date.substring(4,8);
	      field.value =  dateAtual;
			field.blur();			
	   }
	}
} 

/******************************************************************************/
/* Função:		disableAltCtrl																	*/
/* Descrição:	Desabilita operações de CTRL / ALT (Cut and copy)					*/
/*             Esta função deverá ser chamada algum evento Onkey (onkeypress, */
/*					onkeydown 																		*/
/*					use também na tag body  onkeydown="disableAltCtrl();				*/
/******************************************************************************/

function disableAltCtrl()
{
	if ( (window.event.altKey) || (window.event.altLeft) ||  (window.event.ctrlKey) || (window.event.ctrlLeft) ) 
	{
		window.event.keyCode = 0;
		alert("Opção indisponível!");		
		return false;
	}
}


function mouseDown(e) 
{
	if (parseInt(navigator.appVersion)>3) {
		var clickType=1;
	if (navigator.appName=="Netscape") clickType=e.which;
		else clickType=event.button;

	if (clickType!=1) 
	{ 
		//self.status='Right button!'
		alert("Opção indisponível!");
	};
 }
 return true;
}

/******************************************************************************/
/* Função:		checkCpf																			*/
/* Descrição:	Verifica se o cpf é valido retornando true ou false				*/
/******************************************************************************/
function checkCpf(cpf)
{
	dig_1 = 0;
	dig_2 = 0;
	controle_1 = 10;
	controle_2 = 11;
	lsucesso = 1;

	if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" ||    cpf == "33333333333" || cpf == "44444444444" || 
        cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
	{
        	return false;
	}
	else
	{
		for ( i=0 ; i < 9 ; i++ )
		{
			dig_1 = dig_1 + parseInt(cpf.substring(i, i+1) * controle_1);
			controle_1 = controle_1 - 1;
		}

		resto = dig_1 % 11;
		dig_1 = 11 - resto;

		if ( (resto == 0) || (resto == 1) )
		dig_1 = 0;

		for ( i=0 ; i < 9 ; i++)
		{
			dig_2 = dig_2 + parseInt(cpf.substring(i, i + 1) * controle_2);
			controle_2 = controle_2 - 1;
		}

		dig_2 = dig_2 + 2 * dig_1;
		resto = dig_2 % 11;
		dig_2 = 11 - resto;

		if ( (resto == 0) || (resto == 1) ) dig_2 = 0;

		dig_ver = (dig_1 * 10) + dig_2;

		if (dig_ver != parseFloat(cpf.substring(cpf.length-2,cpf.length)))
		{
			return false;
		}
		else
		{
			return true;
		}
	}	
}

/*
function checkPisPasep( PisPasep )
{

	if ( PisPasep.length < 11 || PisPasep.lenght > 11  )
	{
		return false;
	}
	
	ftab = "3298765432";
	total = 0;
	
	for ( i = 0 ; i <= 9 ; i++ ) 
	{
		total += parseInt( PisPasep.substring(i, i + 1) ) *  parseInt( ftab.substring(i,i + 1) )  ;
	}
	
	resto = parseInt(total % 11);

	if ( resto != 0 ) 
	{
		resto = parseInt( 11 - resto );
	}

	if ( resto != PisPasep.substring(10,11 ) )
	{
		return false;
	}
	else
	{
		return true;
	}

}
*/


function checkPisPasep( pis )
{
var ftap="3298765432";
var total=0;
var i;
var resto=0;
var numPIS=0;
var strResto="";

total=0;
resto=0;
numPIS=0;
strResto="";

	numPIS=pis;
			
	if (numPIS=="" || numPIS==null)
	{
		return false;
	}
	
	for(i=0;i<=9;i++)
	{
		resultado = (numPIS.slice(i,i+1))*(ftap.slice(i,i+1));
		total=total+resultado;
	}
	
	resto = (total % 11)
	
	if (resto != 0)
	{
		resto=11-resto;
	}
	
	if (resto==10 || resto==11)
	{
		strResto=resto+"";
		resto = strResto.slice(1,2);
	}
	
	if (resto!=(numPIS.slice(10,11)))
	{
		return false;
	}
	
	return true;
}




function elementFocus(element)
{

	if ( element.type == "text" || element.type == "radio" )
	{
		element.select();
		element.focus();	
	}

	if ( element.type == "select-one")
	{
		element.focus();	
	}
	
}

function checkReal(value) //verifica formato do valor em reais
{
	len = parseInt(value.length);
	commaPos = len - 3;
	dotPos   = commaPos + 3;
	
	if ( commaPos == value.indexOf(",") )
	{
		return true;
	}
	else
	{
		return false;
	}
	
	if ( len > 6 ) //verifica vírgula e ponto
	{
	
		if ( dotPos == value.indexOf(".") )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	

	/*
	alert(value);
	alert("dot" + value.indexOf("."));	
	alert("comma" + value.indexOf(","));
	*/
	/*
	if ( value.indexOf(".") < 1 )
	{
		return false;
	}
	*/

}

function checkEmail(email)
{
	email = email.value;
	
	if ( email.length < 7	)
		return false;
	
	if ( email.indexOf("@") < 1 )
	{
		return false;
	}
	
	domain = email.substring(email.indexOf("@")+1,email.length);
	
	if ( domain.indexOf(".") < 1 )
	{
		return false;
	}
	
	return true;
	
}

function trim ( element ) 
{

	txt = removeEspacosDeixaUmSo(element.value.toUpperCase());
//	txt = element.value.toUpperCase();

	while ( txt.charAt(0) == " " || txt.charAt(0) == "'" ||  txt.charAt(0) == "\"" )
	{
		txt = txt.substring(1,txt.length);
	}
	
	/*
	while ( txt.charAt( txt.length ) == " " ) 
	{
		alert("'" + txt + "'" );
		txt = txt.substring( 0 ,txt.length-1 );
		alert("'" + txt + "'" );		
	}
	*/
	element.value = txt;

}

function trimLCase ( element ) 
{

	txt = element.value.toLowerCase();

	while ( txt.charAt(0) == " " || txt.charAt(0) == "'" ||  txt.charAt(0) == "\"" )
	{
		txt = txt.substring(1,txt.length);
	}
	
	element.value = txt;

}

//verificação da datas  
function checkDate(date) { 

   day   = parseInt(date.substring(0,2), 10); 
   a     = date.substring(2,3);  
   month = parseInt(date.substring(3,5), 10);
   b     = date.substring(5,6);
   year  = date.substring(6,10);
	
  	//alert(day + "/" + month + "/" + year);

   //verifica se o formato é valido
   if (a != "/") return false;

   if (b != "/") return false;

   //verifica se foram informados dias em formato correto
   if (isNaN(day)) return false;

   if (isNaN(month)) return false;

   if ((isNaN(year)) || (year.length != 4)) return false;
	
   if ((isNaN(year)) || (year.length != 4)) return false;

   // verifica o dia valido para cada mes 
   if ((day < 1)||(day < 1 || day > 30) && (  month == 4 || month == 6 || month == 9 || month == 11 ) || day > 31)  
      return false; 

    // verifica se o mes e valido
    if (month < 1 || month > 12 )
      return false; 

    if (year < 1920 || year > 1996)
        return false;
		
   // verifica se e ano bissexto 
   if (month == 2 && ( day < 1 || day > 29 || ( day > 28 && (parseInt(year / 4) != year / 4))))  
      return false; 
 
   return true;

} 

//verificação da datas  
function checkDateMaior ( date ) 
{ 

   day   = parseInt(date.substring(0,2), 10); 
   a     = date.substring(2,3);  
   month = parseInt(date.substring(3,5), 10);
   b     = date.substring(5,6);
   year  = date.substring(6,10);
	
  	//alert(day + "/" + month + "/" + year);

   //verifica se o formato é valido
   if (a != "/") return false;

   if (b != "/") return false;

   //verifica se foram informados dias em formato correto
   if (isNaN(day)) return false;

   if (isNaN(month)) return false;

   if ((isNaN(year)) || (year.length != 4)) return false;
	
   if ((isNaN(year)) || (year.length != 4)) return false;

   // verifica o dia valido para cada mes 
   if ((day < 1)||(day < 1 || day > 30) && (  month == 4 || month == 6 || month == 9 || month == 11 ) || day > 31)  
      return false; 

   // verifica se o mes e valido 
   if (month < 1 || month > 12 ) 
      return false; 

	if ( year < 1920 )	
		return false;
		
   // verifica se e ano bissexto 
   if (month == 2 && ( day < 1 || day > 29 || ( day > 28 && (parseInt(year / 4) != year / 4))))  
      return false; 
 
   return true;

} 

function checkDateMaioridade ( date ) 
{ 

   day   = parseInt(date.substring(0,2), 10); 
   a     = date.substring(2,3);  
   month = parseInt(date.substring(3,5), 10);
   b     = date.substring(5,6);
   year  = date.substring(6,10);
	
  	//alert(day + "/" + month + "/" + year);

   //verifica se o formato é valido
   if (a != "/") return false;

   if (b != "/") return false;

   //verifica se foram informados dias em formato correto
   if (isNaN(day)) return false;

   if (isNaN(month)) return false;

   if ((isNaN(year)) || (year.length != 4)) return false;
	
   if ((isNaN(year)) || (year.length != 4)) return false;

   // verifica o dia valido para cada mes 
   if ((day < 1)||(day < 1 || day > 30) && (  month == 4 || month == 6 || month == 9 || month == 11 ) || day > 31)  
      return false; 

   // verifica se o mes e valido 
   if (month < 1 || month > 12 ) 
      return false; 

	if ( year < 1920 )	
		return false;
		
   // verifica se e ano bissexto 
   if (month == 2 && ( day < 1 || day > 29 || ( day > 28 && (parseInt(year / 4) != year / 4))))  
      return false; 
 
   // verifica se o candidato tem mais de 18 anos
   if ( year > 1993 )	
      return false;

   return true;

} 

function ltrim (texto) {
	while (texto.charAt(0) == " ") {
		texto = texto.substring(1,51)
	}
	return (texto.toUpperCase())
}

function removeaspas (texto)	{
	var antes = ""
	var depois = ""
	var limpa = texto
	
	while (limpa.indexOf("'", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf("'"))
		depois = limpa.substring(limpa.indexOf("'")+1,51)
		limpa = dummy.concat(antes,depois)
	}
	while (limpa.indexOf(String.fromCharCode(34), 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf(String.fromCharCode(34)))
		depois = limpa.substring(limpa.indexOf(String.fromCharCode(34))+1,51)
		limpa = dummy.concat(antes,depois)
	}
	return(limpa)
}

function removelixo (texto)	{
	var antes = ""
	var depois = ""
	var limpa = texto
	while (limpa.indexOf(".", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf("."))
		depois = limpa.substring(limpa.indexOf(".")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}
	while (limpa.indexOf(",", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf(","))
		depois = limpa.substring(limpa.indexOf(",")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}
	while (limpa.indexOf("-", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf("-"))
		depois = limpa.substring(limpa.indexOf("-")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}
	while (limpa.indexOf("/", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf("/"))
		depois = limpa.substring(limpa.indexOf("/")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}

	while (limpa.indexOf(" ", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf(" "))
		depois = limpa.substring(limpa.indexOf(" ")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}

	return (limpa)
}

function removelixo2 (texto)	{
	var antes = ""
	var depois = ""
	var limpa = texto;
	
	alert(texto);

	while (limpa.indexOf(".", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf("."))
		depois = limpa.substring(limpa.indexOf(".")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}
	while (limpa.indexOf(",", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf(","))
		depois = limpa.substring(limpa.indexOf(",")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}
	while (limpa.indexOf("-", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf("-"))
		depois = limpa.substring(limpa.indexOf("-")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}
	while (limpa.indexOf("/", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf("/"))
		depois = limpa.substring(limpa.indexOf("/")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}

	while (limpa.indexOf(" ", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf(" "))
		depois = limpa.substring(limpa.indexOf(" ")+1,texto.length)
		limpa = dummy.concat(antes,depois)
	}

	return (limpa)
}

function t_campo (objeto, tammin) {
	with (document.p1) {
	    numElement = 0;
	    if (elements[0].name == "PHPSESSID") {
	       numElement = 1;
	    } 
	    objeto += numElement;
		elements[objeto].value = ltrim(elements[objeto].value.toString())
		elements[objeto].value = removeaspas(elements[objeto].value.toString())
		if (objeto == 1 || objeto == 7 || objeto == 11 || objeto == 16 || objeto == 17 || objeto == 18) {
			elements[objeto].value = removelixo(elements[objeto].value.toString())
		} //if
		if ( (objeto == 7) && (erro == -1) ) {
			t_data (objeto)
		} //if
	}//with
}

function MM_openBrWindow(theURL,winName,features) 
{ //v2.0]
  window.open(theURL,winName,features);
}

//retorna a tecla pressionada - Internet Explorer e Netscape
function getkey(e)
{
	if (window.event)
	   key = window.event.keyCode;
	else
	   key = e.which;

	return key;
}

function showConsultaCEP()
{
	   window.open("busca_cep.php","ConsultaCEP","scrollbars=no,width=360,height=350");
}


function removeEspacosDeixaUmSo (texto)	{
	var antes = ""
	var depois = ""
	var limpa = texto

	while (limpa.indexOf("  ", 0) != -1) {
		var dummy = ""
		antes = limpa.substring(0,limpa.indexOf("  "))
		depois = limpa.substring(limpa.indexOf("  ")+2,texto.length)
		limpa = dummy.concat(antes, " " +depois)
	}

	return (limpa)
}



function verificaNome(nome){
	var limpa = nome;
	var novo;
	
	antes = limpa.substring(0,limpa.indexOf(" "))
	depois = limpa.substring(limpa.indexOf(" ")+1,limpa.length)
	/*verifica num nome de duas partes se tem pelo menos 2 letras */
	if  ( ( antes.length < 2 )	|| (depois.length < 2) || depois==""){
		return false;
	}

	//verifica se tem muitos espaços próximos numa string. Ex; A A A A A A A
	total = 0;
	for (i=0;i<=limpa.length;i++ ){
		//não permite nome com menos de 1 caracter
		//previne a digitação de espaços desnecessários. Ex: J O A O 
		if( limpa.substring(0+i, 1+i) == " " && limpa.substring(2+i, 3+i) == limpa.substring(0+i, 1+i) ){
			total++;
		}
	}
	
	if ( total > 2 ){
		return false;
	}else{
		return true;	
	}
	

}



/*Função loadIframe - efetua um reload no conteúdo do iframe para consultar o CEP*/
function loadIframe(iframeName, url) {

  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
	//alert(url);
    return false;
  }
  else return true;
}

