String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

function mostraMensagem (mensagem, indice) {	
	document.getElementById('tableMsg').style.backgroundColor = PAINEL_BGCOLOR[indice];
	document.getElementById('tableMsg').style.borderColor = PAINEL_BORDERCOLOR[indice];
	document.getElementById('imgMsg').src = PAINEL_IMG[indice];
	document.getElementById('textoMsg').innerHTML = mensagem;
	document.getElementById('msg').style.display = "";
	document.location.href = "#mensagem";	
}

function mostraMensagemConfirmacao (mensagem, linkAlvo) {	
	document.getElementById('tableMsgConfirmacao').style.backgroundColor = PAINEL_BGCOLOR[2];
	document.getElementById('tableMsgConfirmacao').style.borderColor = PAINEL_BORDERCOLOR[2];
	document.getElementById('imgMsgConfirmacao').src = PAINEL_IMG[2];
	document.getElementById('textoMsgConfirmacao').innerHTML = mensagem;
	
	var botoes = "<input type=\"button\" class=\"botao\" value=\"SIM\" onclick=\"" + linkAlvo + "\" />";
	botoes += "&nbsp;&nbsp;";
	botoes += "<input type=\"button\" class=\"botao\" value=\"N&Atilde;O\" onclick=\"ocultarMensagemConfirmacao();\" />";
	
	document.getElementById('botoesConfirmacao').innerHTML = botoes;
	document.getElementById('msgConfirmacao').style.display = "";
	document.location.href = "#mensagemConfirmacao";	
}

function ocultar () {
	if (document.getElementById('msg')) {	
		document.getElementById('msg').style.display = 'none';		
	}
}

function ocultarMensagemConfirmacao () {
	if (document.getElementById('msgConfirmacao')) {	
		document.getElementById('msgConfirmacao').style.display = 'none';		
	}
}

function soNumero(e) {
	var keyPressed = (window.event) ? window.event.keyCode : e.which;				
	
	if((keyPressed >= 48 && keyPressed <= 57) || keyPressed == 8 || keyPressed == 0) {	 
		return true;	 
	}
	else {	
		return false;			
	}

}

function mascara(objeto, funcao){
    v_obj = objeto;
    v_fun = funcao;
    setTimeout("execmascara()", 1);    
}

function execmascara(){
    v_obj.value = v_fun(v_obj.value);    
}

function formataTEL(v) {
	//Remove tudo o que não é dígito
    v = v.replace(/\D/g, "");      
        
	v = v.replace(/^(\d{2})(\d)/, "($1) $2");
	        
    v = v.replace(/(\d{4})(\d)/, "$1-$2");
    return v
}

function formataTELsemDDD(v) {
	//Remove tudo o que não é dígito
    v = v.replace(/\D/g, "");      
        		        
    v = v.replace(/(\d{4})(\d)/, "$1-$2");
    return v
}

function formataCEP(v) {
	//Remove tudo o que não é dígito
    v = v.replace(/D/g, "");      
    
	//coloca o ponto depois de 2 numeros
   // v = v.replace(/^(\d{2})(\d)/, "$1.$2");
   
   //coloca o cep em formato 5numeros - 3 numeros
    v = v.replace(/(\d{5})(\d)/, "$1-$2");
    return v
}

function formataCPF(v){
	//Remove tudo o que não é dígito
    v = v.replace(/\D/g,"");
    
    //Coloca um ponto entre o terceiro e o quarto dígitos
    v = v.replace(/(\d{3})(\d)/,"$1.$2");
    
    //Coloca um ponto entre o terceiro e o quarto dígitos
    //de novo (para o segundo bloco de números)
    v = v.replace(/(\d{3})(\d)/,"$1.$2");
                                             
    //Coloca um hífen entre o terceiro e o quarto dígitos
    v = v.replace(/(\d{3})(\d{1,2})$/,"$1-$2");
    return v
}

function formataCNPJ(v){
	//Remove tudo o que não é dígito
    v = v.replace(/\D/g,"");
    
    //Coloca um ponto entre o terceiro e o quarto dígitos
    v = v.replace(/(\d{2})(\d)/,"$1.$2");
    
    //Coloca um ponto entre o terceiro e o quarto dígitos
    //de novo (para o segundo bloco de números)
    v = v.replace(/(\d{3})(\d)/,"$1.$2");
    
    //Coloca um ponto entre o terceiro e o quarto dígitos
    //de novo (para o segundo bloco de números)
    v = v.replace(/(\d{3})(\d)/,"$1/$2");
                                             
    //Coloca um hífen entre o terceiro e o quarto dígitos
    v = v.replace(/(\d{4})(\d{1,2})$/,"$1-$2");
    return v   
}

function formataPeso(objTextBox, SeparadorMilesimo, e) {    
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.event) ? e.keyCode : e.which;    
    if (whichCode == 13) return true;
    if (whichCode == 0 || whichCode == 8) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida    
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorMilesimo)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    
    if (len == 0) {        
        objTextBox.value = '';
    }
    if (len == 1) {        
        objTextBox.value = '0'+ SeparadorMilesimo + '00' + aux;
    }
    if (len == 2) {        
        objTextBox.value = '0'+ SeparadorMilesimo + '0' + aux;
    }
    if (len == 3) {        
        objTextBox.value = '0'+ SeparadorMilesimo + aux;
    }
    if (len > 3) {
        // remove tudo que não é digito
        objTextBox.value = aux;
        objTextBox.value = objTextBox.value.replace(/\D/g, "");
        objTextBox.value = objTextBox.value.replace(/(\d)(\d{3})$/, "$1.$2");        
    }
    return false;
}


function formataFloat(numero) {
	
	result = numero.replace(/\./g, "");
	result = result.replace(",", ".");
	
	return result;

}

function isEmail(email){
   var arroba = "@",
       ponto = ".",
       posponto = 0,
       posarroba = 0;
   
    if (email == "") {    
    	return false;    	
    }
   
    for (var indice = 0; indice < email.length; indice++){    
       if (email.charAt(indice) == arroba) {       
          posarroba = indice;
          break;          
       }       
    }
   
   	for (var indice = posarroba; indice < email.length; indice++) {   	
      if (email.charAt(indice) == ponto) {      
         posponto = indice;
         break;         
      }      
   }
   
   if (posponto == 0 || posarroba == 0) {   
	   return false;   	
   }	
   
   if (posponto == (posarroba + 1)) {   
   	   return false;   	   
   }
   
   if ((posponto + 1) == email.length) {   
       return false;       
   }
   
   return true;
   
}

// Script para manipulacao de datas
var reDate1 = /^\d{1,2}\/\d{1,2}\/\d{1,4}$/;
var reDate2 = /^[0-3]?\d\/[01]?\d\/(\d{2}|\d{4})$/;
var reDate3 = /^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
var reDate4 = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
var reDate5 = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
var reDate = reDate4;

// Verifica se a data é valida
function doDate(pStr, pFmt) {
	eval("reDate = reDate" + pFmt);	
	if (reDate.test(pStr)) {		
		return 'n';		
	} 
	else if (pStr != null && pStr != "") {		
		return false;		
	}
	
}

/** 
 *	@param string data Uma data no formato DD/MM/AAAA
 *	@return string Uma data no formato AAAA/MM/DD
*/
function formataData (data) {
	
	dados = data.split("/");
	dataFormatada = "";
	dataFormatada += dados[2] + "/" + dados[1] + "/" + dados[0];

	return dataFormatada;
	
}

/*ADICIONAR E REMOVER LINHAS DE UMA TABELA
 *addRowToTable adiciona linha na tabela
 *removeRowFromTable remove linha da tabela
 */
// Autor: Tiago Arruda - 15/10/2008
function addRowToTable(id)
{
  var tbl = document.getElementById(id);
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  if(iteration > 11){
  	return true;
  }
  //alert(iteration);
  var row = tbl.insertRow(lastRow);
  row.id = iteration;
  
  
  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode("");
  cellLeft.appendChild(textNode);
  
  // right cell
  var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'file';
  el.name = 'imagem_' + iteration;
  el.id = 'imagem_' + iteration;
  el.size = 95;
  
  //el.onkeypress = keyPressTest;
  cellRight.appendChild(el);
  
  
}

function removeLastRowFromTable(id)
{
  var tbl = document.getElementById(id);
  var lastRow = tbl.rows.length;
  if (lastRow > 9) tbl.deleteRow(lastRow - 1);
}
