/**
  @copyright Creative Commons Share-Alike
  @license http://creativecommons.org/licenses/by-sa/2.5/br/
  @version 2.0
*/


/**
 * @return {Object}
 * @param {String} e Id do objeto a ser procurado
 */ 
function $(e) {
 return document.getElementById(e);
}


/**
 * @return {Array}
 * @param {String} e Tag do objeto a ser procurado
 */ 
function $T(e) {
 return document.getElementsByTagName(e);
}


/**
 * @return {Array}
 * @param {String} e Nome do objeto a ser procurado
 */ 
function $N(e) {
 return document.getElementsByName(e);
}


/**
 * @return {number} Valor numérico da porcentagem
 * @param {number} p Valor em porcentagem
 * @param {number} n Valor
 */
function Perc(p,n) {
 return (p*n)/100;
}


/**
 * @return {void}
 * @param {Object} e Objeto a ser centralizado
 * @param {String} p Tipo do posicionamento
 */
function everCenter(e,p) {
 e.style.top = 50+"%";
 e.style.left = 50+"%";
 e.style.position = p;
 e.style.marginLeft = -1*parseInt(e.offsetWidth/2) + "px";
 e.style.marginTop = -1*parseInt(e.offsetHeight/2) + "px";
}


/**
 * @param {Object} e Objeto
 * @param {String} evt Evento a ser agregado
 * @param {String} f Funcao que deve ser executada
 */
function addEvent(e, evt, f) {
 if (e.addEventListener)
  e.addEventListener(evt, f, true);
 else 
  e.attachEvent("on"+evt, f);
}


/**
 * @return {void} 
 * @param {String} url Url que deve ser aberta
 * @param {String} titulo Titulo da janela (opcional)
 * @param {Integer} width Largura da janela
 * @param {Integer} height Altura da janela
 * @param {yes|no} tootlbar [Padrao no] Barra de ferramentas
 * @param {yes|no} directories [Padrao no] Pasta Links dos Favoritos
 * @param {yes|no} status [Padrao no] Barra de status
 * @param {yes|no} location [Padrao no] Barra de enderecos
 * @param {yes|no} menubar [Padrao no] Menu padrao
 * @param {yes|no} resizable [Padrao no] Redimensionamento 
 * @param {yes|no} scrollbars [Padrao no] Barra de rolagem
 */
function PopupWindow() {
 this.url = "";
 this.titulo = "";
 this.width = "";
 this.height = "";
 this.toolbar = "no";
 this.directories = "no";
 this.status = "no";
 this.location = "no";
 this.menubar = "no";
 this.resizable = "no";
 this.scrollbars = "no";
 this.openWindow = function() {
  var popup = window.open(this.url, this.titulo, "width=" + this.width +
                                                 ",height=" + this.height +
                                                 ",toolbar=" + this.toolbar + 
                                                 ",directories=" + this.directories + 
                                                 ",status=" + this.status + 
                                                 ",location=" + this.location + 
                                                 ",menubar=" + this.menubar + 
                                                 ",resizable=" + this.resizable + 
                                                 ",scrollbars=" + this.scrollbars);
 
  if (popup == false) {
   window.alert("Este site utiliza popups!\nPor favor desabilite seu bloqueador de popups e tente novamente!");
  }
 };
}


/**
 * @return {void}
 * @param {Object} e Dá foco a objeto selecionado
 */
function Focus(e) {
 e.className = e.className == "selecionado" ? "nao_selecionado" : "selecionado";
}


/**
 * @return {void}
 * @param {Object} e Apaga o formulário, inclusive os valores default
 */
function FormClean(e) {
 for (i = 0; i < $(e).length; i++) { 
  switch ($(e).elements[i].type) {
   case "select-one":
    $(e).elements[i].options[0].selected = true;
   break;
   
   case "select-multiple":
    for(j = 0; j < $(e).elements[i].length; j++) {
     $(e).elements[i].options[j].selected = false;
    }
   break;
     
   case "radio":
   case "checkbox":
    $(e).elements[i].checked = false;
   break; 
   
   case "text":
   case "hidden":
   case "textarea":
    $(e).elements[i].value = "";
   break; 
  }
 }  
}


/**
 * Emula a função ereg do PHP
 * @param {String} patern
 * @param {String} value
 * @return {Boolean}
 */
function ereg(pattern, value) {
 var patt1 = new RegExp(pattern);
 return patt1.test(value);
}


/**
 * Formata um número para o formato americano
 * @param {Number} n número a ser formatado
 * @return {Number}
 */
function toUSANumber(n) {
 if (ereg("^[0-9]{1,3}(\.[0-9]{3})*\,[0-9]{2}$",n)) {
  n = n.replace(".","");
  n = n.replace(",",".");  
 }
 return n;
}

// ---------- Moto Garra ----------

function enviarFormBoletim() {
  if($("nome").value == "" || $("nome").value == null ||
     $("email").value == "" || $("email").value == null)
  {
    alert("Por favor, preencha todos os campos antes de enviar.");
  } else {
    if(eValido($("email").value,"email")) {
      $("form_boletim").submit;
    } else {
      $("email").value = "";
    }
  }
}
