function str_pad (input, pad_length, pad_string, pad_type)
{
  input = String (input);
  pad_string = pad_string != null ? pad_string : " ";
  if (pad_string.length > 0)
  {
    var padi = 0;
    pad_type = pad_type != null ? pad_type : "STR_PAD_RIGHT";
    pad_length = parseInt (pad_length);
    switch (pad_type)
    {
      case "STR_PAD_BOTH":
        input = str_pad (input
                       , input.length + Math.ceil ((pad_length - input.length) / 2.0)
                       , pad_string, "STR_PAD_RIGHT");
     // break;  // kein break!
      case "STR_PAD_LEFT":
        var buffer = "";
        for (var i = 0, z = pad_length - input.length; i < z; ++i)
        {
          buffer += pad_string.charAt(padi); // [padi] IE 6.x bug
          if (++padi == pad_string.length)
            padi = 0;
        }
        input = buffer + input;
        break;
      default:
        for (var i = 0, z = pad_length - input.length; i < z; ++i)
        {
          input += pad_string.charAt(padi);
          if (++padi == pad_string.length)
            padi = 0;
        }
        break;
    }
  }
  return input;
}

function abreIndiqueSite(){
  janLarg = 400;
  janAlt = 340;
  largura = screen.width;
  altura = screen.height;
  xPos = (largura - janLarg) / 2;
  yPos = (altura - janAlt) / 2;
  self.focus();
  window.open("mail.indique_site.php", "indique_site", "width="+janLarg+", height="+janAlt+", top="+yPos+", left="+xPos);
  void(0);
}

function abreIndiquePresente(){
  janLarg = 900;
  janAlt = 650;
  largura = screen.width;
  altura = screen.height;
  xPos = (largura - janLarg) / 2;
  yPos = (altura - janAlt) / 2;
  self.focus();
  window.open("mail.indique_presente.php", "indique_presente", "width="+janLarg+", height="+janAlt+", top="+yPos+", left="+xPos);
  void(0);
}

function checa(obj, focou, padrao){
  if(focou){
    if(obj.value==padrao)
      obj.value='';
     else
       obj.select();
  } else {
    if(obj.value=='')
    obj.value=padrao;
  }
}

function checkSearch(){
  var erro = '';
  var focou = false;
  var objFocus = null;
  var form = document.getElementById('formBusca');

  if(form.arg.value.length == 0){
    erro += "  - Preencha o nome do produto a ser pesquisado\n";
    if(!focou){
      focou = true;
      objFocus = form.arg;
    }
  }

  if(erro!=''){
    alert("Corrija o(s) seguinte(s) erro(s):\n" + erro);
    objFocus.focus();
    return void(0);
  } else {
    form.submit();
    return void(0);
  }
}

function bloqueadorPopUpAtivo(){
  var b;
  var j = window.open('', '', 'width=1,height=1,left=0,top=0,scrollbars=no');
  if(j){
    b = false;
    j.close();
  } else
    b = true;

  return b;
}

function txtBoxFormat(objeto, 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 = objeto.value;

     // Limpa todos os caracteres de formatação que
     // já estiverem no campo.
     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( ")", "" );
     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) == "/") || (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++;
     }

     objeto.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;
     }
}

function mudaFocoBySize(objOrigem, objDestino, intSize, e){
  //se a tecla for tab ou shift faz nada
  if(e!=null && document.all && (e.keyCode == 9 || e.keyCode == 16)) // Internet Explorer
    return true;
  else if(e!=null && document.layers && (e.keyCode == 9 || e.keyCode == 16)) // Nestcape
    return true;

  var tam = Number(objDestino.value.length);

  if(objOrigem.value.length == intSize){
    if(tam > 0 && objDestino.type == "text")
      objDestino.select();
    else
      objDestino.focus();
  } else if(objOrigem.value.length > intSize){
    objOrigem.value = objOrigem.value.substring(0, intSize);
    if(tam > 0 && objDestino.type == "text")
      objDestino.select();
    else
      objDestino.focus();
  }
  return true;
}

function number_format(obj, onlyInteger){
  var newValue = '';
  var curChar;
  var valid = /^[0-9]$/;
  var isDotted = false;
  var oi = onlyInteger == null ? false : onlyInteger;
  for(var i = 0; i < obj.value.length; i++){
    curChar = obj.value.substring(i, i+1);
    if((curChar == ',' || curChar == '.') && !isDotted && !oi){
      newValue += '.';
      isDotted = true;
    } else if(valid.test(curChar))
      newValue += curChar;
  }
  if(!isDotted && newValue.length > 0 && !oi)
    newValue += ".00";
  obj.value = newValue;
}

function LTrim(value) {  
  var re = /\s*((\S+\s*)*)/;
  return value.replace(re, "$1");
}

function RTrim(value) {
  var re = /((\s*\S+)*)\s*/;
  return value.replace(re, "$1");
}

function trim(value) {
  return LTrim(RTrim(value));
}

function checkdate(month, day, year){

  var intDay = parseInt(day, 10);
  var intMonth = parseInt(month, 10);
  var intYear = parseInt(year, 10);
  var checkYear = year != null && year != undefined;
  var arrMeses = new Array("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro");
  var error = "";

  if (intMonth>12 || intMonth<1) {
    error += "  - Mês fora do intervalo esperado (Jan a Dez)\n";
  }

  if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1)) {
    error += "  - Dia fora do intervalo esperado para o mes \""+arrMeses[intMonth-1]+"\"\n";
  } else if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30 || intDay < 1)) {
    error += "  - Dia fora do intervalo esperado para o mes \""+arrMeses[intMonth-1]+"\"\n";
  } else if (intMonth == 2) {
    if (intDay < 1) {
      error += "  - Dia fora do intervalo esperado para o mes \""+arrMeses[intMonth-1]+"\"\n";
    } else if(checkYear) {
      if(LeapYear(intYear) == true) {
        if (intDay > 29) {
          error += "  - Dia fora do intervalo esperado para o mes e ano esperado "+arrMeses[intMonth-1]+"/"+intYear+"\n";
        }
      } else {
        if (intDay > 28) {
          error += "  - Dia fora do intervalo esperado para o mes e ano esperado "+arrMeses[intMonth-1]+"/"+intYear+"\n";
        }
      }
		} else {
      if (intDay > 29) {
        error += "  - Dia fora do intervalo esperado para o mes \""+arrMeses[intMonth-1]+"\"\n";
      }
		}
  }

  return error;

}

function LeapYear(intYear) {
  return ((intYear % 100 == 0 && intYear % 400 == 0) || (intYear % 4 == 0));
}

function numero_moeda( number, decimals, dec_point, thousands_sep ) {
  var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
  var d = dec_point == undefined ? "," : dec_point;
  var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
  var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
  return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
