// -------------------------------------------------------------------------- //
// Muda cor de fundo da celula                                                //
// -------------------------------------------------------------------------- //
function fundo(obj,acao,cod){
  obj.style.cursor ="hand"
  if (acao =="1"){ obj.style.background ="#FFFFCE" }
  if (acao =="2"){ obj.style.background ="#FFFFFF" }
}


// -------------------------------------------------------------------------- //
// Abre uma janela                                                            //
// -------------------------------------------------------------------------- //
function abreJanela(pag,nom,largura,altura){
  window.open(pag,nom,"height="+ altura +", width="+ largura +", toolbar=no, location=no, status=no, scrollbars=no, resizable=no, top=30, left=30");
  //window.focus();
}


// -------------------------------------------------------------------------- //
// Abre uma janela em full screen                                             //
// -------------------------------------------------------------------------- //
function abreJanelaFull(pag,nom){
  window.open(pag,nom, "toolbar=no, location=no, status=no, scrollbars=no, resizable=no, fullscreen");
  //window.focus();
}


// -------------------------------------------------------------------------- //
// Verifica Caracteres                                                        //
// -------------------------------------------------------------------------- //
function verificaCaracter(objnome){
  caracter = objnome.value;
  if (caracter.length == 0){
    //objnome.style.background = "#FFFFCC"
    objnome.focus();    
    return true;
  }
  return false;
}


// -------------------------------------------------------------------------- //
// Verifica Radio buttom e Check box                                          //
// -------------------------------------------------------------------------- //
function verificaOpcao(objnome){
  resp = true
  for(ii=0; ii < objnome.length; ii++){
    if(objnome[ii].checked){ resp = false }
  }
  if(resp){
    return true
  }else{
    return false;
  }
}


// -------------------------------------------------------------------------- //
// Verifica Objetos SELECT do formulário                                      //
// -------------------------------------------------------------------------- //
function verificaSelect(objnome){ 
  if(objnome.options[objnome.selectedIndex].value ==""){
    objnome.focus();
    return true;
  }
  return false;
}


// -------------------------------------------------------------------------- //
// Verifica data no formato brasil (dd/mm/aaaa)                               //
// -------------------------------------------------------------------------- //
function verificaData(objnome){
  numero  = objnome.value;
  partes  = numero.split("/")
  if (partes.length==3){
    numeros = partes[0] + partes[1] + partes[2]
    if (numeros.length ==0 ){ objnome.focus(); return true }
    if (partes[0] < 1 || partes[0] > 31){ objnome.focus(); return true }
    if (partes[1] < 1 || partes[1] > 12){ objnome.focus(); return true }
    if (partes[2].length < 4){ objnome.focus(); return true }
    if (partes[2] < 1900){ objnome.focus(); return true }
  }else{
    objnome.focus();
    return true;
  }
  return false;
}


// -------------------------------------------------------------------------- //
// Verifica email's quanto ao forma e carácteres válidos                      //
// -------------------------------------------------------------------------- //
function verificaEmail(objnome){
  invalid = Array("~","!","@","#","$","%","^","&","*","(",")","+","=","[","]",":",";",",","\"","'","|","{","}","\\","/","<",">","?"," ");
  xemail = objnome.value;
  if (xemail.indexOf("@")==-1){
    objnome.focus();
    return true;
  }else{
    partes=xemail.split("@");
    if(partes[0]=="" || partes[0].length < 3){
      objnome.focus();
      return true;
    }else{
      for(i=0;i < invalid.length;i++){
        if(partes[0].indexOf(invalid[i])!=-1){ objnome.focus(); return true }
      }
    }
    if(partes[1]==""){
      objnome.focus();
      return true;
    }else{
      if (partes[1].indexOf(".")==-1){
        objnome.focus();
        return true;
      }else{
        ponto=partes[1].split(".")
        if(ponto[0]=="" || ponto[0].length < 2){
          objnome.focus();
          return true;
        }else{
          for(i=0;i < invalid.length;i++){
            if(ponto[0].indexOf(invalid[i])!=-1){ objnome.focus(); return true }
          }
        }
        if(ponto[1]==""){ objnome.focus(); return true; }
      }
    }
  }
  return false
}


// -------------------------------------------------------------------------- //
// Valida CEP no formato 00000-000                                            //
// -------------------------------------------------------------------------- //
function validaCEP(objnome){
  validos = Array("0","1","2","3","4","5","6","7","8","9");
  valor   = objnome.value;
  partes  = valor.split("-");
  resp    = 0;
  if (partes.length ==2){
    numeros = partes[0] + partes[1];
    if (numeros.length ==0 ){ objnome.focus(); return true }
    for(i=0;i < validos.length; i++){
      for(j=0;j < numeros.length; j++){
        if(numeros.substring(j,j+1) == validos[i]) { resp++ }
      }
    }
    if (numeros.length != resp){ objnome.focus(); return true }
    if (partes[0].length != 5){ objnome.focus(); return true }
    if (partes[1].length != 3){ objnome.focus(); return true }
  }else{
    objnome.focus();
    return true;
  }
  return false;
}


// -------------------------------------------------------------------------- //
// Validar valores com 2 casas decimais no formato brasil (0.000,00)          //
// -------------------------------------------------------------------------- //
function validaValor(objnome){
  valor   = objnome.value;
  virgula = valor.split(",");
  if (virgula.length >=2){
    if (virgula[1].length ==2 && virgula[0].length !=0){
      ponto = virgula[0].split(".");
      if (ponto.length == 2){
        for(i=1; i< ponto.length; i++){
          if(ponto[i].length != 3){ objnome.focus(); return true }
        }
      }
    }else{
      objnome.focus();
      return true;
    }
  }else{
    objnome.focus();
    return true;
  }
  return false;
}


// -------------------------------------------------------------------------- //
// Permite somente números inteiros                                           //
// -------------------------------------------------------------------------- //
function validaNumero(objnome){
  numero  = objnome.value;
  resp    = 0;
  if(numero.length==0) { objnome.focus(); return true }
  for(i=0;i < numero.length; i++){
    for(j=0;j < 11; j++){
      if (numero.substring(i,i+1) == j) resp++;
    }
  }
  if (numero.length != resp) { objnome.focus(); return true }
  return false;
}


// -------------------------------------------------------------------------- //
// Valida campo CPF                                                           //
// -------------------------------------------------------------------------- //
function validaCPF(objnome) {
  cpf   = objnome.value;
  valor = true;
  erro  = false;
  if (cpf.length < 11) erro = true; 
  var nonNumbers = /\D/;
  if (nonNumbers.test(cpf)) erro = true;	
  if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
    erro = true;
  }
  var a = [];
  var b = new Number;
  var c = 11;
  for (i=0; i<11; i++){
    a[i] = cpf.charAt(i);
    if (i < 9) b += (a[i] *  --c);
  }
  if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
  b = 0;
  c = 11;
  for (y=0; y<10; y++) b += (a[y] *  c--); 
  if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
  if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
    erro = true;
  }
  return erro;
}


// -------------------------------------------------------------------------- //
// Valida campo CNPJ                                                          //
// -------------------------------------------------------------------------- //
function validaCNPJ(objnome) {
  CNPJ = objnome.value;
  erro = false;
  if (CNPJ.length < 18) erro = true;
  if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
  	if (erro) erro = true;
  }
  if(document.layers && parseInt(navigator.appVersion) == 4){
    x = CNPJ.substring(0,2);
    x += CNPJ.substring(3,6);
    x += CNPJ.substring(7,10);
    x += CNPJ.substring(11,15);
    x += CNPJ.substring(16,18);
    CNPJ = x;	
  }else{
    CNPJ = CNPJ.replace(".","");
    CNPJ = CNPJ.replace(".","");
    CNPJ = CNPJ.replace("-","");
    CNPJ = CNPJ.replace("/","");
  }
  var nonNumbers = /\D/;
  if (nonNumbers.test(CNPJ)) erro = true;	
  var a = [];
  var b = new Number;
  var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
  for(i=0; i<12; i++){
    a[i] = CNPJ.charAt(i);
    b += a[i] * c[i+1];
  }
  if((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
  b = 0;
  for(y=0; y<13; y++) {
    b += (a[y] * c[y]); 
  }
  if((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
  if((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
    erro = true;
  }
  return erro;
}


// -------------------------------------------------------------------------- //
// Cria mascara para qualquer campo                                           //
// -------------------------------------------------------------------------- //
function exibeMascara(strField, sMask, evtKeyPress) {
  var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

  if(document.all) { // Internet Explorer
    nTecla = evtKeyPress.keyCode; }
  else if(document.layers) { // Nestcape
    nTecla = evtKeyPress.which;
  }
  
  sValue = strField.value;
  sValue = sValue.toString().replace( "-", "" );
  sValue = sValue.toString().replace( "-", "" );
  sValue = sValue.toString().replace( ".", "" );
  sValue = sValue.toString().replace( ".", "" );
  sValue = sValue.toString().replace( "/", "" );
  sValue = sValue.toString().replace( "/", "" );
  sValue = sValue.toString().replace( "(", "" );
  sValue = sValue.toString().replace( "(", "" );
  sValue = sValue.toString().replace( ")", "" );
  sValue = sValue.toString().replace( ")", "" );
  sValue = sValue.toString().replace( " ", "" );
  sValue = sValue.toString().replace( " ", "" );
  fldLen = sValue.length;
  mskLen = sMask.length;
  
  i = 0;
  nCount = 0;
  sCod = "";
  mskLen = fldLen;
  
  while (i <= mskLen) {
    bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
    bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

    if (bolMask) {
      sCod += sMask.charAt(i);
      mskLen++; }
    else {
      sCod += sValue.charAt(nCount);
      nCount++;
    }
    
    i++;
  }
  
  strField.value = sCod;
  
  if (nTecla != 8) { // backspace
    if (sMask.charAt(i-1) == "9") { // apenas números...
      return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
    else { // qualquer caracter...
      return true;
    } }
  else {
    return true;
  }
}


// -------------------------------------------------------------------------- //
// Função de Data                                                             //
// -------------------------------------------------------------------------- //
function FuncaoData(){

	//<!-- Begin
	// Get today's current date.
	var now = new Date();

	// Array list of days.
	var days = new Array('Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado');

	// Array list of months.
	var months = new Array('Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro');

	// Calculate the number of the current day in the week.
	var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

	// Calculate four digit year.
	function fourdigits(number)	{
		return (number < 1000) ? number + 1900 : number;
								}

	// Join it all together
	today =  days[now.getDay()] + ", " +
        	 date + " de " +
			 months[now.getMonth()] + " de " +
    	     (fourdigits(now.getYear())) ;

	// Print out the data.
	document.write(today);

	//  End -->
}
