// VALIDA EMAIL
// ============
function valida_email(obj) {
  var objRegExp  = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/i;
  if (!objRegExp.test(obj.value)){
	  alert("Email inválido!");
      obj.focus();
      obj.select();
      return false;
  }
  return true;
}

//PERMITE APENAS VALORES NUMERICOS NO CAMPO
// ==========================================

function onlynum(){
   if (event.keyCode < 48 || event.keyCode > 57){
      event.returnValue = false;
   }
}
// ==================================

//coloca o focus no primeiro campo do form ao carregar a pagina
function placeFocus() {
   if (document.forms.length > 0) {
      var field = document.forms[0];
      for (i = 0; i < field.length; i++) {
         if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
            document.forms[0].elements[i].focus();
            break;
         }
      }
   }
}

// Formatacao e Validacao de Data
// ==============================

function mascara_data(data,evento){ 

  var tecla = (window.Event) ? evento.which : evento.keyCode;
  var mydata = ''; 
  mydata = mydata + data.value; 

  if (tecla == 127 || tecla == 8) {
     if ((mydata.length == 2) || (mydata.length == 5)) {
        mydata.indexOf("/",mydata.length);
        data.value = mydata;
     }
  } else{
     if ( ((mydata.length == 2) || (mydata.length == 5)) && (mydata.length <= 10) ){ 
        mydata = mydata + '/'; 
        data.value = mydata; 
     }
  }
}
// ==================================

function verifica_data(data){ 

   dia = (data.value.substring(0,2)); 
   mes = (data.value.substring(3,5)); 
   ano = (data.value.substring(6,10)); 
   situacao = ""; 

   // verifica formato de data
   if (data.length > 10) {
      alert("O formato da data esta invalido ! \n Exemplo: dd/mm/aaaa");
      data.focus();
      data.select();
      return false;
   }

   // verifica o dia valido para cada mes 
   if ((dia < 1)||(dia < 1 || dia > 30) && (  mes == 4 || mes == 6 || mes == 9 || mes == 11 ) || dia > 31) { 
       situacao = "falsa"; 
   } else
      // verifica se o ano e inferior a 1900
      if (ano < 1900) {
         alert("Ano menor que 1900 !");
         data.focus();
         data.select();
         return false;
      } else
         // verifica se o mes e valido 
         if (mes < 01 || mes > 12 ) { 
            situacao = "falsa"; 
         } else
            // verifica se e ano bissexto 
            if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
               situacao = "falsa"; 
            } else
               if (data.value == "") { 
                  situacao = "falsa"; 
               }
   if (situacao == "falsa") { 
      alert("Data invalida!"); 
      data.focus(); 
      data.select();
      return false;
   } 
   return true;
} 

// Formatacao e Validacao de Hora
// ==============================

function mascara_hora(hora){ 

     var myhora = ''; 
     myhora = myhora + hora; 

     if (myhora.length == 2){ 
         myhora = myhora + ':'; 
         hora.value = myhora; 
     } 
 }
// ==================================

function verifica_hora(hora){

   hrs = (hora.value.substring(0,2)); 
   min = (hora.value.substring(3,5)); 
   situacao = ""; 

   // verifica data e hora 
   if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){ 
      situacao = "falsa"; 
   } 
   
   if (hora.value == "") { 
      situacao = "falsa"; 
   } 
   
   if (situacao == "falsa") { 
      alert("Hora invalida!"); 
      hora.focus(); 
   } 
}

// Formatacao Decimal
// ==================

function mascara_decimal(campo,tammax,teclapres,numcasas) {

	var tecla = teclapres.keyCode;
	vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ",", "" );
	tam = vr.length;

	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 <= numcasas ){ 
			campo.value = vr; }
		if ( (tam > numcasas) && (tam <= numcasas + 3) ){
			campo.value = vr.substr( 0, tam - numcasas ) + ',' + vr.substr( tam - numcasas ) ; }
		if ( (tam >= numcasas + 4) && (tam <= numcasas + 6) ){
			campo.value = vr.substr( 0, tam - (numcasas + 3) ) + '.' + vr.substr( tam - (numcasas + 3), 3 ) + ',' + vr.substr( tam - numcasas ) ; }
		if ( (tam >= numcasas + 7) && (tam <= numcasas + 9) ){
			campo.value = vr.substr( 0, tam - (numcasas + 6) ) + '.' + vr.substr( tam - (numcasas + 6), 3 ) + '.' + vr.substr( tam - (numcasas + 3), 3 ) + ',' + vr.substr( tam - numcasas ) ; }
		if ( (tam >= numcasas + 10) && (tam <= numcasas + 12) ){
			campo.value = vr.substr( 0, tam - (numcasas + 9) ) + '.' + vr.substr( tam - (numcasas + 9), 3 ) + '.' + vr.substr( tam - (numcasas + 6), 3 ) + '.' + vr.substr( tam - (numcasas + 3), 3 ) + ',' + vr.substr( tam - numcasas ) ; }
		if ( (tam >= numcasas + 13) && (tam <= numcasas + 15) ){
			campo.value = vr.substr( 0, tam - (numcasas + 12) ) + '.' + vr.substr( tam - (numcasas + 12), 3 ) + '.' + vr.substr( tam - (numcasas + 9), 3 ) + '.' + vr.substr( tam - (numcasas + 6), 3 ) + '.' + vr.substr( tam - (numcasas + 3), 3 ) + ',' + vr.substr( tam - numcasas ) ;}
	}
}

// Checa estado do Radio-set CPF-CNPJ
// ==================================

function status_rs(){
	if(document.all.w_tipo_pessoa[0].checked){
		document.all.w_cnpj.disabled = true;
		document.all.w_cpf.disabled = false;
	}else{
		document.all.w_cnpj.disabled = false;
		document.all.w_cpf.disabled = true;
	}
}

// Formatacao e Validacao de CPF
// ==============================

function mascara_cpf(campo,evento){

	var tecla = (window.Event) ? evento.which : evento.keyCode;
	var mycpf = '';
	mycpf = mycpf + campo.value;

	if (tecla != 46 && tecla != 8){			
		if ((mycpf.length == 3) || (mycpf.length == 7)) {
			mycpf = mycpf + '.';
			campo.value = mycpf;
		}
		if (mycpf.length == 11) {
			mycpf = mycpf + '-';
			campo.value = mycpf;
		}
		if (mycpf.length == 14) {
		}
	}
}
// ==================================

function valida_cpf(campo) {

	var erro = "no";
	var CPF = campo.value;
	CPF = CPF.replace( ".", "" );
	CPF = CPF.replace( ".", "" );
	CPF = CPF.replace( "-", "" );

	if(campo.disabled == false){

		if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" || 
			CPF == "22222222222" || CPF == "33333333333" || CPF == "44444444444" ||
			CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
			CPF == "88888888888" || CPF == "99999999999")
			erro = "yes";
		soma = 0;
		for (i=0; i < 9; i ++) 
			soma += parseInt(CPF.charAt(i)) * (10 - i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11)
			resto = 0;
		if (resto != parseInt(CPF.charAt(9)))
			erro = "yes";
		soma = 0;
		for (i = 0; i < 10; i ++) 
			soma += parseInt(CPF.charAt(i)) * (11 - i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11)
			resto = 0;
		if (resto != parseInt(CPF.charAt(10)))
			erro = "yes";

		if (erro == "yes"){
			alert("CPF inválido!");
			campo.focus(); 
			campo.select();
			return false;
		}	
		return true;
	}
}

// Formatacao e Validacao de CNPJ
// ==============================

function mascara_cnpj(campo, evento){

   var tecla = evento.keyCode;

   vr = campo.value;
   vr = vr.replace(".", "");
   vr = vr.replace(".", "");
   vr = vr.replace("/", "");
   vr = vr.replace("-", "");

   tam = vr.length + 1 ;
   
   if (tecla != 46 && tecla != 8){
      if (tam > 2 && tam < 6)
         campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
      if (tam >= 6 && tam < 9)
         campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
      if (tam >= 9 && tam < 13)
         campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
      if (tam >= 13 && tam < 15)
         campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
      }
}
// ==================================

function valida_cnpj(campo)
{
	var i;
	var s = campo.value;
	s = limpa_string(s);
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	var erro = "no";

	if(campo.disabled == false){

		for (i = 0; i < 12; i++){
			d1 += c.charAt(11-i)*(2+(i % 8));
		}
		if (d1 == 0) {
			erro = "yes";
		}
		d1 = 11 - (d1 % 11);
		if (d1 > 9) d1 = 0;
		if (dv.charAt(0) != d1) {
			erro = "yes";
		}
		d1 *= 2;
		for (i = 0; i < 12; i++){
			d1 += c.charAt(11-i)*(2+((i+1) % 8));
		}
		d1 = 11 - (d1 % 11);
		if (d1 > 9) d1 = 0;
		if (dv.charAt(1) != d1)	{
			erro = "yes";
		}
		if (erro == "yes"){
			alert("CNPJ inválido!");
			campo.focus(); 
			campo.select();
			return false;
		}	
		return true;
	}
}
// ==================================

function limpa_string(S){
	// Deixa so' os digitos no numero
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";

	for (var i=0; i<S.length; i++)	{
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0)	{
			temp=temp+digito	}
	}
	return temp
}

// Formatacao de Telefone
// (0xx99) 9999.9999
// ======================

function mascara_fone(campo,evento){

	var tecla = (window.Event) ? evento.which : evento.keyCode;
	var myfone = '';
	myfone = myfone + campo.value;

	if (tecla != 46 && tecla != 8) {		
		if (myfone.length == 1) {
			myfone = '(0xx' + myfone;
			campo.value = myfone;
		}
		if (myfone.length == 6) {
			myfone = myfone + ') ';
			campo.value = myfone;
		}
		if (myfone.length == 12) {
			myfone = myfone + '.';
			campo.value = myfone;
		}		
	}
}

// Formatacao de CEP
//	99.999-999
// =================

function mascara_cep(campo,evento){

	var tecla = (window.Event) ? evento.which : evento.keyCode;
	var mycep = '';
	mycep = mycep + campo.value;

	if (tecla != 46 && tecla != 8){			
		if (mycep.length == 2) {
			mycep = mycep + '.';
			campo.value = mycep;
		}
		if (mycep.length == 6) {
			mycep = mycep + '-';
			campo.value = mycep;
		}
	}
}
