// JavaScript Document

    function IsEmpty(inputStr) { 
		if ( null == inputStr || "" == inputStr ){ 
			return true; 
		} else {
			return false;
		}
	}
	function abrejanela(theURL,winName,features) { 
  		window.open(theURL,winName,features);
	}
	function pesquisa(){
		campo = document.getElementById("txtpesquisa");
		if (campo.value == ""){
			alert("Por Favor Digite as Palavras Chaves");
		}else{	
			url = "src.php?q="+campo.value;
			campo.value = "";
			location.href = url;
		}			
	}
	function limpaCampo(obj){
		campo = document.getElementById(obj);
		campo.value = "";
	}
	function saiCampo(obj,txt){
		campo = document.getElementById(obj);
		if(campo.value == ""){
			campo.value = txt;
		}
	}
	function fazLogin(){
		mail = document.getElementById("txtemail").value;
		senha = document.getElementById("txtsenha").value;
		ret = document.getElementById("txtre").value;
		pres = document.getElementById("txtpres").value;
		
		if (mail == "digite seu e-mail" || senha == "digite sua senha"){
			alert("Digite Seu Login e Senha");
		}else{			
			url = "class/execlogin.php?mail="+mail+"&senha="+senha+"&re="+ret+"&pre="+pres;
			location.href = url;
		}
	}
	function rastreio(){
		campo = document.getElementById("txtrastreio");
		if (campo.value == ""){
			alert("Digite o Codigo do Rastreio Fornecido");
		}else{			
			url = "http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI="+campo.value;
			campo.value = "";
			abrejanela(url, "rastreio", "scrollbars=yes, width=300, height=500");
		}
	}
	function ocultaLogin(sit,nome){
		tba = document.getElementById("tblogin");
		user = document.getElementById("usuario");
		
		if(sit=="0"){
			if(!IsEmpty(tba)){
				tba.style.display = "block";
			}
			user.innerHTML = "Efetue seu login no sistema";
		}else if(sit=="1"){
			if(!IsEmpty(tba)){
				tba.style.display = "none";
			}			
			user.innerHTML = nome;
			user.innerHTML = "Seja bem vindo "+nome+" <a href=\"class/execlogin.php?id=0\">Sair</a>";
		}else{
			tba.style.display = "block";
			user.innerHTML = "Efetue seu login no sistema";
		}
	}
function abrejanela(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function calculaproduto(qtd, valor){
	localtotal = document.getElementById("totalproduto");
	quantidade = parseFloat(qtd.value);
	total = parseFloat(quantidade) * parseFloat(valor);
	localtotal.innerHTML = moeda(total);
}

function moeda(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;

}

/*******************************************************
***** FUNÇÕES PARA MASCARAR OS DADOS DO FORMULÁRIO
********************************************************/
function mascara(obj,mask) {
i=obj.value.length;
saida=mask.substring(0,1);
texto=mask.substring(i);
if (texto.substring(0,1)!=saida) {
      obj.value += texto.substring(0,1);
}
}
/******************************************** 
Exemplos:
CEP
OnKeyPress="formatar(this, '#####-###')"
CPF
OnKeyPress="formatar(this, '###.###.###-##')"
DATA
OnKeyPress="formatar(this, '##/##/####')
***********************************************/

function Verifica_CPF(formulario) {
var CPF = formulario; // Recebe o valor digitado no campo

// Aqui começa a checagem do CPF
var POSICAO, I, SOMA, DV, DV_INFORMADO;
var DIGITO = new Array(10);
DV_INFORMADO = CPF.substr(9, 2); // Retira os dois últimos dígitos do número informado

// Desemembra o número do CPF na array DIGITO
for (I=0; I<=8; I++) {
  DIGITO[I] = CPF.substr( I, 1);
}

// Calcula o valor do 10º dígito da verificação
POSICAO = 10;
SOMA = 0;
   for (I=0; I<=8; I++) {
      SOMA = SOMA + DIGITO[I] * POSICAO;
      POSICAO = POSICAO - 1;
   }
DIGITO[9] = SOMA % 11;
   if (DIGITO[9] < 2) {
        DIGITO[9] = 0;
}
   else{
       DIGITO[9] = 11 - DIGITO[9];
}

// Calcula o valor do 11º dígito da verificação
POSICAO = 11;
SOMA = 0;
   for (I=0; I<=9; I++) {
      SOMA = SOMA + DIGITO[I] * POSICAO;
      POSICAO = POSICAO - 1;
   }
DIGITO[10] = SOMA % 11;
   if (DIGITO[10] < 2) {
        DIGITO[10] = 0;
   }
   else {
        DIGITO[10] = 11 - DIGITO[10];
   }

// Verifica se os valores dos dígitos verificadores conferem
DV = DIGITO[9] * 10 + DIGITO[10];
   if (DV != DV_INFORMADO) {
      return "F";
   } else {
	   return "T";
   }
}
