var SITE_PATH = "";

function trocaImagem(idThumb, id) {
	document.getElementById("imgPrincipal").src = document.getElementById(idThumb).src;	
	document.getElementById("linkImgPrincipal").href = document.getElementById(id).value;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

var numImagem = 1;

function setImagemInnerHtml(id, endereco){
	conteudo = "<a id=\"linkImgPrincipal\" href=\""+endereco+"\" rel=\"lightbox[cont]\" title=\"\" >";
	conteudo += "<img id=\"imgPrincipal\" src=\""+endereco+"\" width=\"140\" height=\"90\" />";
	conteudo += "</a>";
	
	document.getElementById(id).innerHTML = conteudo;
	initLightbox();
}

function maisImagens() {

    if (numImagem == 0) {
        numImagem++;
    }
    if (numImagem < maxImagem) {
        document.getElementById("linha"+numImagem).style.display = '';      
        numImagem++;
    }
    
    if (numImagem > 1) {
       document.getElementById("linhaMenosImagem").style.display = '';       
    }
}

function menosImagens() {       
    if (numImagem > 0) {
        numImagem--;            
        document.getElementById("linha"+numImagem).style.display = 'none';
        
        var indiceImagem = numImagem + 1;
        document.getElementById("imagem"+indiceImagem).value = '';               
    }
    
    if (numImagem == 1) {
       document.getElementById("linhaMenosImagem").style.display = 'none';
    }
}

function verificaCpf(cpf) {
	cpf = cpf.replace(/\D/g, "");
	
	soma1 = 0;
	for (i = 0; i < 9; i++) {
		soma1 += parseInt(cpf.charAt(i)) * (10 - i);
	}	
	resto1 = soma1 % 11;
	dv1 = (resto1 < 2) ? 0 : 11 - resto1; 
	
	soma2 = 0;
	for (i = 0; i < 10; i++) {
		soma2 += parseInt(cpf.charAt(i)) * (11 - i);
	}	
	resto2 = soma2 % 11;
	dv2 = (resto2 < 2) ? 0 : 11 - resto2;
				
	if (dv1 != parseInt(cpf.charAt(9)) || dv2 != parseInt(cpf.charAt(10))) {
		return false;
	}
	
	return true;
}

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;
   
}

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 formataData(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{2})(\d)/, "$1/$2");    
    return v
}

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 formataCEP(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{3})(\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   
}

//-----------------------------------------------------
//Funcao: MascaraMoeda
//Sinopse: Mascara de preenchimento de moeda
//Parametro:
//   objTextBox : Objeto (TextBox)
//   SeparadorMilesimo : Caracter separador de milésimos
//   SeparadorDecimal : Caracter separador de decimais
//   e : Evento
//Retorno: Booleano
//Autor: Gabriel Fróes - www.codigofonte.com.br
//-----------------------------------------------------
function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, 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) != SeparadorDecimal)) 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'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

// 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 dataBRtoMySQL(data) {
	
	dados = data.split("/");
	dataFormatada = "";
	dataFormatada += dados[2] + "/" + dados[1] + "/" + dados[0];

	return dataFormatada;
	
}

/**
 *	@param string dataInicio Data inicial
 *	@param string dataFim Data final
 *	@param string erro Mensagem de erro no caso de dataFim ser 
 *	                   menor ou igual a dataInicio
 *	@return bool Falso se dataFim menor ou igual a dataInicio, ou se uma
 *				 das datas nao for uma data válida
*/
function ehMaior (dataInicio, dataFim) {	
	// Se as datas não forem válidas, a comparação não será feita
	if (!doDate(dataInicio, 5) || !doDate(dataFim, 5)) {		
		return false;		
	}
 
 	// coloca as datas no formato aaaa/mm/dd
	dataInicio = dataBRtoMySQL(dataInicio);
	dataFim = dataBRtoMySQL(dataFim);
	
    // Instancia-se as datas, para poder usar getTime();
    data1 = new Date(dataInicio);
    data2 = new Date(dataFim);
    if(!data1 || !data2){		
        return false;        
	}
 
    // ms1: quantidade de segundos corridos desde 1/1/1970 0h ate dataInicio
    ms1 = data1.getTime();
    
	// ms2: quantidade de segundos corridos desde 1/1/1970 0h ate dataFim
    ms2 = data2.getTime();
	
	if (ms2 <= ms1) {				
		return false;
	}
	else {		
		return true;
	}
}

