<!--//
function mudaCor(vcor, objeto) {

	objeto.style.backgroundColor = vcor;

}

function ValidaPedido() {

     var Form
     Form = document.formPedidos;

	 if (Form.txtNome.value.length == 0)
	 {
		alert("Digite seu nome.");
        Form.txtNome.focus();
        return false;
     }
	 if (Form.txtPedido.value.length == 0)
	 {
		alert("Digite o seu pedido de oração.");
        Form.txtPedido.focus();
        return false;
     }
	else
	{	
		document.forms['formPedidos'].action= "confirma_pedido.php"
		document.forms['formPedidos'].target= ""
		document.forms['formPedidos'].submit();
	}
}

function ValidaContato() {

     var Form
     Form = document.formContato;

	 if (Form.txtNome.value.length == 0)
	 {
		alert("Digite seu nome.");
        Form.txtNome.focus();
        return false;
     }
	 if (Form.txtMensagem.value.length == 0)
	 {
		alert("Digite a mensagem.");
        Form.txtMensagem.focus();
        return false;
     }
	else
	{	
		document.forms['formContato'].action= "confirma_contato.php"
		document.forms['formContato'].target= ""
		document.forms['formContato'].submit();
	}
}



function Cancelar()
{
	if(window.confirm("Tem certeza que deseja cancelar ?"))
	{
		window.location="principal.php";
	}
}

function formataCNPJ(ConteudoCampo)
{
	if (((event.keyCode) > 47) && ((event.keyCode) < 58))
  	{
	   NumDig = ConteudoCampo.value;
	   TamDig = NumDig.length;
	   if (TamDig == 2)
		  ConteudoCampo.value = NumDig.substr(0,2)+".";
	   else if (TamDig == 6)	  
		  ConteudoCampo.value = NumDig.substr(0,6)+".";
	   else if (TamDig == 10)	  
		  ConteudoCampo.value = NumDig.substr(0,10)+"/";
	   else if (TamDig == 15)	  
		  ConteudoCampo.value = NumDig.substr(0,15)+"-";	  	  
   	   return(true);
	 }
else return(false)
}

function formataCPF(ConteudoCampo)
{
	if (((event.keyCode) > 47) && ((event.keyCode) < 58))
  	{
	   NumDig = ConteudoCampo.value;
	   TamDig = NumDig.length;
	   if (TamDig == 3)
		  ConteudoCampo.value = NumDig.substr(0,3)+".";
	   else if (TamDig == 7)	  
		  ConteudoCampo.value = NumDig.substr(0,7)+".";
	   else if (TamDig == 11)	  
		  ConteudoCampo.value = NumDig.substr(0,11)+"-";	  	  
   	   return(true);
	 }
else return(false)
}

function formataCEP(objeto)
{
	if (((event.keyCode) > 47) && ((event.keyCode) < 58))
	{
	   if (objeto.value.indexOf("-") == -1 && objeto.value.length > 5){ objeto.value = ""; }
	   if (objeto.value.length == 5)
	   {
		   objeto.value += "-";
		}
	
		return(true);
	}
	else return(false)
}

function formataPlaca(objeto)
{
	//if (((event.keyCode) > 47) && ((event.keyCode) < 58))
	//{
	   if (objeto.value.indexOf("-") == -1 && objeto.value.length > 9){ objeto.value = ""; }
	   if (objeto.value.length == 3)
	   {
		   objeto.value += "-";
		}
	
		return(true);
	//}
	//else return(false)
}

function formataTelefone(objeto)
{
	if (((event.keyCode) > 47) && ((event.keyCode) < 58))
	{
	   if (objeto.value.indexOf("-") == -1 && objeto.value.length > 4){ objeto.value = ""; }
	   if (objeto.value.length == 4)
	   {
		   objeto.value += "-";
		}
	
		return(true);
	}
	else return(false)
}

// Retorna índice do elemento de entrada anterior na coleção c.
function ElementoEntradaAnterior(i, c) {
	var j;

	for (j = i - 1; j >= 0; j--) {
		switch (c.item(j).tagName) {
		case 'INPUT':
			if ( c.item(j).type != 'text'
					&& c.item(j).type != 'password'
					&& c.item(j).type != 'checkbox'
					&& c.item(j).type != 'radio' ) {
				continue;  //próximo loop do for
			}
			break;
		case 'TEXTAREA':
		case 'SELECT':
			break;
		default:
			continue;  //próximo loop do for
		}
		if (c.item(j).disabled == false) return(j);
	}
	return(i);
}


// Retorna índice do próximo elemento de entrada na coleção c.
function ProximoElementoEntrada(i, c) {
	var j;

	for (j = i + 1; j <= c.length - 1; j++) {
		switch (c.item(j).tagName) {
		case 'INPUT':
			if ( c.item(j).type != 'text'
					&& c.item(j).type != 'password'
					&& c.item(j).type != 'checkbox'
					&& c.item(j).type != 'radio' ) {
				continue;  //próximo loop do for
			}
			break;
		case 'TEXTAREA':
		case 'SELECT':
			break;
		default:
			continue;  //próximo loop do for
		}
		if (c.item(j).disabled == false) return(j);
	}
	return(i);
}


function AutoTab() {
	var el = event.srcElement;
	var coll = document.forms(0).all;
	var i = 0, j;
	var kc = event.keyCode;

	switch (kc) {
	//case  8:  //backspace
	//case 46:  //delete
	//	return;
	case 38:  //seta para cima
		while (el != coll.item(i)) i++;  // Descobre em que elemento estamos na coleção de TAGS INPUT
		j = ElementoEntradaAnterior(i, coll);  // Pega índice do input-text anterior
		if (j != i) {
			// Coloca o foco no elemento anterior:
			if (coll.item(j).tagName == 'INPUT') {
				if (coll.item(j).type == 'text' || coll.item(j).type == 'password') {
					try { coll.item(j).select() } catch (e) {}
				}
			}
			try { coll.item(j).focus() } catch (e) {}
		}
		return;
	case 40:  //seta para baixo
		while (el != coll.item(i)) i++;  // Descobre em que elemento estamos na coleção de TAGS INPUT
		j = ProximoElementoEntrada(i, coll);  // Pega índice do próximo input-text
		if (j != i) {
			// Coloca o foco no próximo elemento:
			if (coll.item(j).tagName == 'INPUT') {
				if (coll.item(j).type == 'text' || coll.item(j).type == 'password') {
					try { coll.item(j).select() } catch (e) {}
				}
			}
			try { coll.item(j).focus() } catch (e) {}
		}
		return;
	}

	if ( (kc >= 48 && kc <= 57) ||  //'0' a '9'
			(kc >= 96 && kc <= 105) ||  //'0' a '9' no teclado numérico
			(kc >= 65 && kc <= 90) ) {  // A-Z
 		if (el.value.length == el.maxLength) {
			while (el != coll.item(i)) i++;  // Descobre em que elemento estamos na coleção de TAGS INPUT
			j = ProximoElementoEntrada(i, coll);  // Pega índice do próximo input-text
			if (j != i) {
				// Coloca o foco no próximo elemento:
				if (coll.item(j).tagName == 'INPUT') {
					if (coll.item(j).type == 'text' || coll.item(j).type == 'password') {
						try { coll.item(j).select() } catch (e) {}
					}
				}
				try { coll.item(j).focus() } catch (e) {}
			}
		}
	}
	return;
}//AutoTab



function formataCPF2(pForm,pCampo,pTamMax,pPos1,pPos2,pPosTraco,pTeclaPres){
 var wTecla, wVr, wTam;
  
 wTecla = pTeclaPres.keyCode;
 wVr = pForm[pCampo].value;
 wVr = wVr.toString().replace( "-", "" );
 wVr = wVr.toString().replace( ".", "" );
 wVr = wVr.toString().replace( ".", "" );
 wVr = wVr.toString().replace( "/", "" );
 wTam = wVr.length ;

 if (wTam < pTamMax && wTecla != 8) { 
    wTam = wVr.length + 1 ; 
 }

 if (wTecla == 8 ) { 
    wTam = wTam - 1 ; 
 }
   
 if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
  if ( wTam <= 2 ){
    pForm[pCampo].value = wVr ;
  }
  if (wTam > pPosTraco && wTam <= pTamMax) {
        wVr = wVr.substr(0, wTam - pPosTraco) + '-' + wVr.substr(wTam - pPosTraco, wTam);
  }
  if ( wTam == pTamMax){
        wVr = wVr.substr( 0, wTam - pPos1 ) + '.' + wVr.substr(wTam - pPos1, 3) + '.' + wVr.substr(wTam - pPos2, wTam);
  }
  pForm[pCampo].value = wVr;
 
 }

}

function FormataDecimais(obj, indice)
{

  if (indice != -1)
    valor = obj[indice].value;
  else 
    valor = obj.value;

  novoValor = "";

  var code;

  if (!e) var e = window.event;

  if (e.keyCode) code = e.keyCode;
  else if (e.which) code = e.which;


  //se a tecla precionada for diferente de seta ou page up/down

  if (!(code > 34 && code < 41)){

    //tira tudo que não e número do valor

    limpa = "";

    for (i=0;i<valor.length;i++)
	{
      aux = valor.substring(i,i+1);
      if (aux >= 0 && aux < 10 && aux != " ")limpa += aux;

    }

    valor = limpa;

    //tira os pontos e virgula

    for (i=0;i<valor.length;i++)
	{
      valor = valor.replace(',','');
      valor = valor.replace('.','');
    }

    novoValor = valor;

    //divide o valor pelo milhar

    centavos = valor.substring(valor.length-2);
    unidade = valor.substring(valor.length-5,valor.length-2);
    milhar = valor.substring(valor.length-8,valor.length-5);
    milhao = valor.substring(valor.length-11,valor.length-8);
    bilhao = valor.substring(valor.length-14,valor.length-11);

    //concatena o novo valor com os pontos e a virgula

	if (bilhao != "" && bilhao != 0)
	novoValor = bilhao+"."+milhao+"."+milhar+"."+unidade+","+centavos;
	else

	if (milhao != "" && milhao != 0)

	novoValor = milhao+"."+milhar+"."+unidade+","+centavos;

	else

	if (milhar != "" && milhar != 0)

	novoValor = milhar+"."+unidade+","+centavos;

	else

	if (unidade != "" && unidade != 0)

	novoValor = unidade+","+centavos;

	else

	if (centavos != "" && centavos != 0){

	if (novoValor.length == 1)

	novoValor = "0,0"+centavos;

	else novoValor = "0,"+centavos;

	}else novoValor = "";



	if (novoValor.substring(1,2) != ",")

	if (novoValor.substring(0,1) == 0 )

	novoValor = novoValor.substring(1);

	if (indice != -1)

	obj[indice].value = novoValor;

	else obj.value = novoValor;

  }
}

function FormataKm(obj, indice)
{

  if (indice != -1)
    valor = obj[indice].value;
  else 
    valor = obj.value;

  novoValor = "";

  var code;

  if (!e) var e = window.event;

  if (e.keyCode) code = e.keyCode;
  else if (e.which) code = e.which;


  //se a tecla precionada for diferente de seta ou page up/down

  if (!(code > 34 && code < 41)){

    //tira tudo que não e número do valor

    limpa = "";

    for (i=0;i<valor.length;i++)
	{
      aux = valor.substring(i,i+1);
      if (aux >= 0 && aux < 10 && aux != " ")limpa += aux;

    }

    valor = limpa;

    //tira os pontos e virgula

    for (i=0;i<valor.length;i++)
	{
      valor = valor.replace(',','');
      valor = valor.replace('.','');
    }

    novoValor = valor;

    //divide o valor pelo milhar

    //centavos = valor.substring(valor.length-2);
    unidade = valor.substring(valor.length-5,valor.length-2);
    milhar = valor.substring(valor.length-8,valor.length-5);
    milhao = valor.substring(valor.length-11,valor.length-8);
    bilhao = valor.substring(valor.length-14,valor.length-11);

    //concatena o novo valor com os pontos e a virgula

	if (bilhao != "" && bilhao != 0)
	novoValor = bilhao+"."+milhao+"."+milhar+"."+unidade;
	//novoValor = bilhao+"."+milhao+"."+milhar+"."+unidade+","+centavos;
	else

	if (milhao != "" && milhao != 0)

	novoValor = milhao+"."+milhar+"."+unidade;

	else

	if (milhar != "" && milhar != 0)

	novoValor = milhar+"."+unidade;

	else

	if (unidade != "" && unidade != 0)

	novoValor = unidade;
	/*
	else

	if (centavos != "" && centavos != 0){

	if (novoValor.length == 1)

	novoValor = "0,0"+centavos;

	else novoValor = "0,"+centavos;

	}else novoValor = "";
	*/


	if (novoValor.substring(1,2) != ",")

	if (novoValor.substring(0,1) == 0 )

	novoValor = novoValor.substring(1);

	if (indice != -1)

	obj[indice].value = novoValor;

	else obj.value = novoValor;

  }
}

function abrir_pop(url, name, feature)
{
    window.open(url, name, feature);
}


function janela_modal(arq, larg, altu, nome, scrollbars) 
{
	var x = (window.screen.width / 2) - (larg / 2);
	var y = (window.screen.height / 2) - (altu / 2);
	cmd = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scrollbars + ',resizable=no,left=' + x + ',top=' + y + ',width=' + larg + ',height=' + altu;
	window.showModalDialog(arq,window,"dialogWidth=" + larg + "px;dialogHeight=" + altu + "px;" + "center:1;status:0;scroll:0;resizable:100;help:0");
}


function FmascTempoRealDt(ConteudoCampo)
{

if (((event.keyCode) > 47) && ((event.keyCode) < 58))
  {
   NumDig = ConteudoCampo.value;
   TamDig = NumDig.length;
   if (TamDig == 2)
      ConteudoCampo.value = NumDig.substr(0,2)+"/";
   else if (TamDig == 5)	  
      ConteudoCampo.value = NumDig.substr(0,5)+"/";
   else if (TamDig == 9)
       ConteudoCampo.value = NumDig.substr(0,10);

   return(true);}
else return(false)
}

//********************************************************************** FORMATO MOEDA **********************
/**
 * @example 
 * 		moeda.formatar(1000) 
 *      	>> retornar 1.000,00
 * 		moeda.desformatar(1.000,00) 
 * 			>> retornar 1000
 **/
 var moeda = {
 	
	/**
	 * retiraFormatacao
	 * 
	 * Remove a formatação de uma string de moeda e retorna um float
	 * 
	 * @param {Object} num
	 */
	 desformatar: function(num){
	   num = num.replace(".","");
	
	   num = num.replace(",",".");
	
	   return parseFloat(num);
	},

	/**
	 * formatar
	 * 
	 * Deixar um valor float no formato monetário
	 * 
	 * @param {Object} num
	 */
	formatar: function(num){
	   x = 0;
	
	   if(num<0){
	      num = Math.abs(num);
	      x = 1;
	   }
	
	   if(isNaN(num)) num = "0";
	      cents = Math.floor((num*100+0.5)%100);

	   num = Math.floor((num*100+0.5)/100).toString();
	
	   if(cents < 10) cents = "0" + cents;
	      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	         num = num.substring(0,num.length-(4*i+3))+'.'
	               +num.substring(num.length-(4*i+3));
	
	   ret = num + ',' + cents;
	
	   if (x == 1) ret = ' - ' + ret;return ret;
	},
	
	/**
	 * arredondar
	 * 
	 * @abstract Arredonda um valor quebrado para duas casas decimais.
	 * 
	 * @param {Object} num
	 */
	arredondar: function(num){
		return Math.round(num*Math.pow(10,2))/Math.pow(10,2);
	}
 }

