<!--
// INFORMADO
function informado(text, label) {
   if (text.value == "") {
     alert("Informe o campo \"" + label + "\".");
     text.focus();
     return (false);
   }
   else
      return (true);
}

// CONTEUDO
function conteudo(text, checkOK) {
  var checkStr = text.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }
  if (!allValid)
    return (false);
  else
    return (true);
}

// EDIGITOS
function edigitos(text, label) {
  var checkOK = "0123456789.";
  if (!conteudo(text, checkOK)) {
    alert("Informe apenas números no campo \"" + label + "\".");
    text.focus();
    return (false);
  }
  else
    return (true);
}

// EMOEDA
function emoeda(text, label) {
  var checkOK = "0123456789.";
  var checkStr = text.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
    if (ch == ",") {
      allNum += ".";
      decPoints++;
    }
    else if (ch == ".") {
      allNum += ".";
      decPoints++;
    }
    else
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Informe apenas números no campo \"" + label + "\".");
    text.focus();
    return (false);
  }

  if (decPoints > 1)
  {
    alert("Informe o separador decimal apenas uma vez no campo \"" + label + "\".");
    text.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseFloat(allNum);
  if (chkVal != "" && !(prsVal >= "0"))
  {
    alert("Informe um valor maior ou igual a \"0\" no campo \"" + label + "\".");
    text.focus();
    return (false);
  }
  else
    return (true);

}

// SE RADIO ESTA CHECKED
function checkado(radioName,label){

   for (var i = 0; i < radioName.length; i++) {
      if (radioName[i].checked) {
         break;
      }
   }
   if (i == radioName.length){
      alert("Informe o campo \"" + label + "\".");
      return (false);
   }
   else
      return (true);
}

// ESTASELECIONADO
function estaselecionado(combo, label) {
   if (combo.selectedIndex == 0) {
      alert("Selecione um valor no campo \"" + label + "\".");
      combo.focus();
      return (false);
   }
   else
      return (true);
}

// QUEMESTASELECIONADO
function quemestaselecionado(combo) {
  return combo.options[combo.selectedIndex].value;
}

// Retira brancos do inicio e final do string
function trim(str) {

   while (str.charAt(0) == " ")
      str = str.substr(1,str.length - 1);

   while (str.charAt(str.length-1) == " ")
      str = str.substr(0,str.length - 1);

   return str;
}

// EEMAIL
function eemail(text, label) {
   var emailStr = text.value;
   var emailPat=/^(.+)@(.+)$/;
   var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
   var validChars="\[^\\s" + specialChars + "\]";
   var quotedUser="(\"[^\"]*\")";
   var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
   var atom=validChars + '+';
   var word="(" + atom + "|" + quotedUser + ")";
   var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
   var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
   var matchArray=emailStr.match(emailPat);

   if (matchArray==null) {
   	alert(label+" incorreto");
   	text.focus();
   	return (false);
   }
   
   var user=matchArray[1];
   var domain=matchArray[2];
   if (user.match(userPat)==null) {
       alert(label+" incorreto ( EX: usuario@dominio.com.br )");
       text.focus();
       return (false);
   }


   var IPArray=domain.match(ipDomainPat);
   if (IPArray!=null) {
      for (var i=1;i<=4;i++) {
         if (IPArray[i]>255) {
            alert(label+" incorreto. (#1)");
            text.focus();
            return (false);
         }
      }
      return true;
   }

   var domainArray=domain.match(domainPat);

   if (domainArray==null) {
      alert("Dominio incorreto.");
      text.focus();
      return (false);
   }

   var atomPat=new RegExp(atom,"g");
   var domArr=domain.match(atomPat);
   var len=domArr.length;
   if ( (domArr[domArr.length-1].length<2)  ||  (domArr[domArr.length-1].length>3 )) {
      alert("O endereço deve terminar com um dominio de três letras, ou duas letras para países.");
      text.focus();
      return (false);
   }

   if (len<2) {
      alert("Dominio incorreto!");
      text.focus();
      return (false);
   }

   return (true);
}
// MOVE O ELEMENTO DE UM SELECT PARA OUTRO
function selectMove(selectOrigem, selectDestino) {
   i = 0;
   while (i < selectOrigem.length) {
      if (selectOrigem.options[i].selected) {
         selectDestino.options[selectDestino.length] = new Option(selectOrigem.options[i].text,
                                                                  selectOrigem.options[i].value);
         selectDestino.options[selectDestino.length-1].selected = true;
         selectOrigem.options[i] = null;
      }
      else
         i++;
   }
}

// MOVE TODOS OS ELEMENTOS DE UM SELECT PARA OUTRO
function selectMoveAll(selectOrigem, selectDestino) {
   while (selectOrigem.length > 0) {
      selectDestino.options[selectDestino.length] = new Option(selectOrigem.options[0].text,
                                                               selectOrigem.options[0].value);
      selectDestino.options[selectDestino.length-1].selected = true;
      selectOrigem.options[0] = null;
   }
}

// PREPARA UM SELECT PARA POST PARA OUTRA PAGINA
function selectPrepare(selectName) {
   selectName.multiple = true;
   for (i=0; i < selectName.length; i++) {
      if (!selectName.options[i].selected) {
         selectName.options[i].selected = true;
      }
   }
   selectName.name = selectName.name + "[]";
   return (true);
}

// EDATA
function edata(text, label) {
   var x = text.value;
   var dia, mes, ano, i1, i2;
   i1 = x.indexOf("/");
   if (i1 == -1) {
      alert('Data incorretamente informada em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }
   i2 = x.indexOf("/", i1+1);
   if (i2 == -1) {
      alert('Data incorretamente informada em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }
   dia = x.substring(0, i1);
   mes = x.substring(i1+1, i2);
   ano = x.substring(i2+1, x.length);
   if (dia.length == 1)
      dia = '0' + dia;
   if (mes.length == 1)
      mes = '0' + mes;
   if (ano.length <= 2) {
      if (parseInt(ano) < 50)
         ano = 2000 + parseInt(ano);
      else
         ano = 1900 + parseInt(ano);
   }
   if (dia.length > 2) {
      alert ('Dia incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }   
   if (!(dia >= 1 && dia <= 31)) {
      alert ('Dia incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }
   if (mes.length > 2) {
      alert ('Mês incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }   
   if (!(mes >= 1 && mes <= 12)) {
      alert ('Mês incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }
   if (ano.length > 4) {
      alert ('Ano incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }   
   if (!(ano >= 1901 && ano <= 2099)) {
      alert ('Ano incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }
   if ((mes == 2) && (ano % 4 != 0) && (dia > 28)) {
      alert ('Dia incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }
   if ((mes == 2) && (ano % 4 == 0) && (dia > 29)) {
      alert ('Dia incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
      text.focus();
      return (false);
   }
   if ((mes == 1) || (mes == 3) || (mes == 5) || (mes == 7) || (mes == 8) || (mes == 10) || (mes == 12)) {
      if (!(dia >=1 && dia <= 31)) {
         alert ('Dia incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
         text.focus();
         return (false);
      }
   }
   if ((mes == 4) || (mes == 6) || (mes == 9) || (mes == 11)) {
      if (!(dia >=1 && dia <= 30)) {
         alert ('Dia incorretamente informado em ' + label + '. Formato \"DD/MM/AAAA\"');
         text.focus();
         return (false);
      }
   }
   text.value = dia + "/" + mes + "/" + ano;
   return (true);
}

// EDATAMAIOR
function edatamaior(text, xdata, label) {
   if (edata(text, label)) {
      var dia = text.value.substr(0, 2);
      var mes = text.value.substr(3, 2);
      var ano = text.value.substr(6, 4);
      var xdia = xdata.substr(0, 2);
      var xmes = xdata.substr(3, 2);
      var xano = xdata.substr(6, 4);
      if (!(ano+mes+dia >= xano+xmes+xdia)) {
         alert("Informe uma data maior ou igual a \"" + xdata + "\" em \"" + label + "\"");
         text.focus();
         return (false);
      }
      else
         return (true);
   }
   else
      return (false);
}

// EDATAMENOR
function edatamenor(text, xdata, label) {
   if (edata(text, label)) {
      var dia = text.value.substr(0, 2);
      var mes = text.value.substr(3, 2);
      var ano = text.value.substr(6, 4);
      var xdia = xdata.substr(0, 2);
      var xmes = xdata.substr(3, 2);
      var xano = xdata.substr(6, 4);
      if (!(ano+mes+dia <= xano+xmes+xdia)) {
         alert("Informe uma data menor ou igual a \"" + xdata + "\" em \"" + label + "\"");
         text.focus();
         return (false);
      }
      else
         return (true);
   }
   else
      return (false);
}

//verifica se a extensão do arquivo é valida
function comprova_extensao(formulario, arquivo) { 
   extensoes_permitidas = new Array(".gif", ".jpg", ".png", ".jpeg"); 
   meuerro = ""; 
   if (!arquivo) { 
      //Se não tenho arquivo, é porque não se selecionou um arquivo no formulário. 
       meuerro = "Não foi selecionado nenhum arquivo"; 
   }else{ 
      //recupero a extensão deste nome de arquivo 
      extensao = (arquivo.substring(arquivo.lastIndexOf("."))).toLowerCase(); 
      //alert (extensao); 
      //comprovo se a extensão está entre as permitidas 
      permitida = false; 
      for (var i = 0; i < extensoes_permitidas.length; i++) { 
         if (extensoes_permitidas[i] == extensao) { 
         permitida = true; 
         break; 
         } 
      } 
      if (!permitida) { 
         meuerro = "Só se podem publicar imagens com extensões: " + extensoes_permitidas.join(); 
       }else{ 
          //submeto! 
         //alert ("Tudo correto. Vou submeter o formulário."); 
         //formulario.submit(); 
         return true; 
       } 
   } 
   //se estou aqui é porque não se pode submeter 
   alert (meuerro); 
   return false; 
} 
//Função que permite em tempo de digitação apenas números em um field.
function somente_numeros(e,args){  
	//  onkeypress="return somente_numeros(event,'')" 
    if (document.all){var evt=event.keyCode;} // caso seja IE
    else{var evt = e.charCode;}    // do contrário deve ser Mozilla
    var valid_chars = '0123456789'+args;    // criando a lista de teclas permitidas
    var chr= String.fromCharCode(evt);    // pegando a tecla digitada
    if (valid_chars.indexOf(chr)>-1 ){return true;}    // se a tecla estiver na lista de permissão permite-a
    // para permitir teclas como <BACKSPACE> adicionamos uma permissão para
    // códigos de tecla menores que 09 por exemplo (geralmente uso menores que 20)
    if (valid_chars.indexOf(chr)>-1 || evt < 20){return true;}    // se a tecla estiver na lista de permissão permite-a
    return false;    // do contrário nega
}
//funcao para formatar os campos de telefone
function FormatarTelefone(objeto,tammax,teclapres)
{
	var tecla = teclapres.keyCode;
	vr = objeto.value;
	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 <= 4 ) { 
	 		objeto.value = vr ;
		}
	 	if ( (tam > 4) && (tam <= 8) ) {
	 		objeto.value = vr.substr(0,tam-4) + '-' + vr.substr( tam - 4, tam ) ;
		}
	 	if ( (tam >= 9) && (tam <= 10) ) {
			objeto.value = '(' + vr.substr(0,2) + ') ' + vr.substr(2,tam-6) + '-' + vr.substr(tam-4,tam) ;			
		}
	}		
}
//-->