String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function isEmpty(str) {
   x = str.trim();

   if (x.length==0) {
       return true;
   } else {
       return false;
   }
}

function isArray(ob){
        if(typeof(ob.length) == 'number'){
                return true;
        }else{
                return false;
        }
}

// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

// ABRIR POP UP
function popUp(url,largura,altura){
        var lado = (screen.width - largura) / 2;
        var topo = (screen.height - altura) / 2;
        window.open(url,'janela','width='+largura+',height='+altura+',menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,top='+topo+',left='+lado);
}

/**  * Função para aplicar máscara em campos de texto

     * APLICAÇÃO:
         1.  CPF: onkeyup="maskIt(this,event,'###.###.###-##')"
         2.  TELEFONE: onkeyup="maskIt(this,event,'(##)####-####')"
         3.  DATA: onkeyup="maskIt(this,event,'##/##/####')"
         4.  DINHEIRO: onkeyup="maskIt(this,event,'###.###.###,##',true)"
         5.  CEP: onkeyup="maskIt(this,event,'#####-###')"
         6.  CNPJ: onkeyup="maskIt(this,event,'##.###.###/####-##')"
     */

     function maskIt(w,e,m,r,a){
             // Cancela se o evento for Backspace
             if (!e) var e = window.event
             if (e.keyCode) code = e.keyCode;
             else if (e.which) code = e.which;

             // Variáveis da função
             var txt  = (!r) ? w.value.replace(/[^\d]+/gi,'') : w.value.replace(/[^\d]+/gi,'').reverse();
             var mask = (!r) ? m : m.reverse();    var pre  = (a ) ? a.pre : "";
             var pos  = (a ) ? a.pos : "";
             var ret  = "";

             if(code == 9 || code == 8 || txt.length == mask.replace(/[^#]+/g,'').length) return false;

             // Loop na máscara para aplicar os caracteres
             for(var x=0,y=0, z=mask.length;x<z && y<txt.length;){
                     if(mask.charAt(x)!='#'){
                            ret += mask.charAt(x); x++;
                      } else{
                              ret += txt.charAt(y); y++; x++;
                       }
                }

              // Retorno da função
              ret = (!r) ? ret : ret.reverse()
              w.value = pre+ret+pos;
     }
        // Novo método para o objeto 'String'
      String.prototype.reverse = function(){
              return this.split('').reverse().join('');
  };

  function changeField(evento){
          var a = document.getElementById("tipo");
          var b = document.getElementById("cpf_cnpj");
          if( a.value == 1){
                  maskIt(b,evento,'##.###.###/####-##',false,false);
          }else if(a.value == 0){
                  maskIt(b,evento,'###.###.###-##',false,false);
          }else{
            alert("Selecione o Tipo de Pessoa!");
            deixarBranco("cpf_cnpj", "rg_ie");
          }
  }

  function deixarBranco(id1,id2){
  //        for(var i=0; i<11; i++){
  //                 document.getElementById(id+1).value=""
  //         }
    document.getElementById(id1).value="";
          document.getElementById(id2).value="";
  }



  function verifMascData(id){ //APLICAÇÂO: onblur="verifMascData(this.id)"
          if(document.getElementById(id).value != "" && document.getElementById(id).value.length < 10){
            alert("O campo Data esta incompleto!");
            document.getElementById(id).value="";
          }
  }
    function verifMascCpf(id){ //APLICAÇÂO: onblur="verifMascCpf(this.id)"
          if(document.getElementById(id).value.length < 14){
            alert("O campo CPF é obrigatório esta incompleto!");
            document.getElementById(id).value="";
          }
  }
    function verifMascTel(id){ //APLICAÇÂO: onblur="verifMascTel(this.id)"
          if(document.getElementById(id).value != "" && document.getElementById(id).value.length < 9){
            alert("O campo esta incompleto!");
            document.getElementById(id).value="";
          }
  }
    function verifMascCep(id){ //APLICAÇÂO: onblur="verifMascCep(this.id)"
          if(document.getElementById(id).value != "" && document.getElementById(id).value.length < 9){
            alert("O campo esta incompleto!");
            document.getElementById(id).value="";
          }
  }
function changeEnter()
{
//muda a funcao do enter pra tab
//desenvolvido por: willian b. silveira
//www.neuronio.org
  if (event.keyCode == 3 || event.keyCode == 13)
  {
   event.keyCode = 9;
  }
}


// Autor: Diego Fleury - dfleury<arroba>gmail.com
// Versao: 1.3.1
// Este código é de uso livre a todos mas nao deixe de dar os créditos. Nao remova estes comentário para nao violar os direitos autorais.

function DateField(objectId) {

  var data = new String();
  var maxlength = 8;
  var obj = objectId;
  if (document.getElementById(obj).value != "  /  /    ")
      data = document.getElementById(obj).value.replace(/\/|\s/g,"");
  var len = data.length;

  function getKey(event) {
    return event?(event.keyCode?event.keyCode:(event.which?event.which:event.charCode)):null;
  }

  this.getLength = function() {
    return data.length;
  }

  this.mask = function(event) {

    var k = getKey(event);
    if (k != 9) {
          if (k > 95 && k < 106) k -= 48;

        if (/[0-9]/.test(String.fromCharCode(k))) {
          if (len < maxlength) {
            data += String.fromCharCode(k);
            len++;
          } else return false;
        } else if (k == 8) {
          if (len > 0) data = data.substring(0,--len);
        }
        else return false;

        var mask = new String("  /  /    ");
        var v = new String();

        if (len <= 2)                 v = data + mask.substring(len,maxlength);
        else if (len > 2 && len <= 4) v = data.substring(0,2) + "/" + data.substring(2,len) + mask.substring(len+1,maxlength);
        else if (len >= 5)            v = data.substring(0,2) + "/" + data.substring(2,4) + "/" + data.substring(4,len) + (len<maxlength?mask.substring(len+1,maxlength):"");

        document.getElementById(obj).value = v;

        return false;
    }
    return true;
  }
}

function addInput(Name,Value, Parent){
//alert(Parent);

var oInput = document.createElement("input");
oInput.setAttribute("name", Name);
oInput.setAttribute("type ", "text");
oInput.setAttribute("value ", Value);
oInput.setAttribute("size ", 20);
return Parent.appendChild(oInput);
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);

    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
          return true;
        }
    }else{
        return false;
    }
}
