function Objeto(nome) {
if (document.all) { return document.all[nome]; }
else if (document.getElementById) { return document.getElementById(nome); }
else if (document.layers) { return document.layers[nome]; }
}
function obj(id) {
if (document.getElementById(id)) { return document.getElementById(id); }
else if (document.all[id]) { return document.all[id]; }
else if (document.layers[id]) { return document.layers[id]; }
}
function Aviso(nome,id) { 
alert('Por favor, preencha o campo \''+nome+'\'');
if (id) { Objeto(id).focus(); }
}
function Foco(id) { Objeto(id).focus(); }
function Menu(nome) {
	visib = Objeto(nome).style.display;
	if (visib == 'none') { Objeto(nome).style.display = ''; }
	else { Objeto(nome).style.display = 'none'; }
}
function RadioBox(id) {
	retorno = false;
		for(x=0;x<Objeto(id).length;x++) {
		if (Objeto(id)[x].checked == true) { retorno = Objeto(id)[x]; }
		}
		return retorno;
}

function formata(campo,estilo,sonum,e) {
	
if(estilo == 'fisica'){
	estilo = '###.###.###-##';
}else if(estilo == 'juridica'){
	estilo = '##.###.###/####-##';
}
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if (event.keyCode != 8) {
cnum = false;
for(i=45;i<=57;i++) { if (keycode == i) { cnum = true; } }
for(i=96;i<=105;i++) { if (keycode == i) { cnum = true; } }
if (sonum && !cnum) { event.returnValue = false; }
retorno = "";
for(i=0;i<=campo.value.length;i++) {
if (i < estilo.length) {
if (estilo.charAt(i) == '#') { retorno += campo.value.charAt(i); }
else { retorno += estilo.charAt(i); }
}
else { event.returnValue = false; }
}
campo.value = retorno;
}
}
function minmax(campo,troca,nome) {
tamanho = campo.value.replace(troca,'');
tamanho = tamanho.length;
if (tamanho < campo.minlength || tamanho > campo.maxlength) { 
alert('O campo \"'+nome+'\" deve conter no mínimo '+campo.minlength+' e no máximo '+campo.getAttribute('maxlength')+' caracteres');
campo.focus();
}
}

function PopUp(pagina,nome,x,y) {
if (nome == 'preview') {
	x = (window.screen.availWidth-10); y = (window.screen.availHeight-30);
	window.open (pagina,nome,"fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=" + x + ",height=" + y + ",left=0,top=0");	
}
else {
sx = (screen.width - x) / 2;
sy = (screen.height - y) / 2;
window.open (pagina,nome,"fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=" + x + ",height=" + y + ",left="+sx+",top="+sy);
}
}
function fn_entrega(chk,apaga){
	if(chk == "diferente"){
		if (apaga) {
		Objeto('end_cobranca').value = '';
		Objeto('num_cobranca').value = '';
		Objeto('bairro_cobranca').value = '';
		Objeto('cep_cobranca1').value = '';
		Objeto('cep_cobranca2').value = '';
		Objeto('cidade_cobranca').value = '';
		Objeto('estado_cobranca').value = '';
		}
		document.all.div_entrega.style.display = "";
	}else{
		if (apaga) {
		if (confirm('Atenção: Todos os dados serão perdidos. Deseja continuar?')) { document.all.div_entrega.style.display="none"; }
		else { Objeto('radio_entrega')[1].checked = true; }
		}
		else {
			document.all.div_entrega.style.display="none";
		}
	}
}

function AlteraDados(pessoa,cobranca) {
	
	if (pessoa == "juridica") if (!Objeto('nome_fantasia').value) { Aviso('Nome Fantasia','nome_fantasia'); Foco('nome_fantasia'); return false; }
	if (pessoa == "juridica") if (!Objeto('razao_social').value) { Aviso('Razão Social','razao_social'); Foco('razao_social'); return false; }
	if (pessoa == "juridica") if (!Objeto('insc_estadual').value) { Aviso('Inscrição Estadual','insc_estadual'); Foco('insc_estadual'); return false; }
	if (!Objeto('tel').value) { Aviso('Telefone','tel'); Foco('tel'); return false; }
	if (!Objeto('cel').value) { Aviso('Celular','cel'); Foco('cel'); return false; }
	if (!Objeto('email').value) { Aviso('E-mail','email'); Foco('email'); return false; }
	if (Objeto('email').value.indexOf('@') == -1 || Objeto('email').value.indexOf('.') == -1) { alert('Por favor, preencha corretamente seu \'E-mail\''); Foco('email'); return false; }
	if (!Objeto('conf_email').value) { Aviso('Confirmação de E-mail','conf_email'); Foco('conf_email'); return false; }	
	if (Objeto('conf_email').value != Objeto('email').value) { alert('Por favor, confirme corretamente seu \'E-mail\''); Foco('conf_email'); return false; }
	if (!Objeto('senha').value) { Aviso('Senha','senha'); Foco('senha'); return false; }
	if (!Objeto('conf_senha').value) { Aviso('Confirmação de Senha','conf_senha'); Foco('conf_senha'); return false; }
	if (Objeto('conf_senha').value != Objeto('senha').value) { alert('Por favor, confirme corretamente sua \'Senha\''); Foco('conf_senha'); return false; }
	
	if (!Objeto('end_entrega').value) { Aviso('Endereço de Entrega','end_entrega'); Foco('end_entrega'); return false; }
	if (!Objeto('num_entrega').value) { Aviso('Nº de Entrega','num_entrega'); Foco('num_entrega'); return false; }
	if (!Objeto('comp_entrega').value) { Aviso('Complemento','comp_entrega'); Foco('comp_entrega'); return false; }
	if (!Objeto('bairro_entrega').value) { Aviso('Bairro','bairro_entrega'); Foco('bairro_entrega'); return false; }
	if (!Objeto('cep_entrega1').value) { Aviso('CEP','cep_entrega1'); Foco('cep_entrega1'); return false; }
	if (!Objeto('cep_entrega2').value) { Aviso('CEP','cep_entrega2'); Foco('cep_entrega2'); return false; }
	if (!Objeto('cidade_entrega').value) { Aviso('Cidade','cidade_entrega'); Foco('cidade_entrega'); return false; }
	if (!Objeto('estado_entrega').value) { Aviso('Estado','estado_entrega'); Foco('estado_entrega'); return false; }

	if (cobranca == "diferente" || Objeto('radio_entrega')[1].checked == true) if (!Objeto('end_cobranca').value) { Aviso('Endereço de Cobrança','end_cobranca'); Foco('end_cobranca'); return false; }
	if (cobranca == "diferente" || Objeto('radio_entrega')[1].checked == true) if (!Objeto('num_cobranca').value) { Aviso('Nº de Cobrança','num_cobranca'); Foco('num_cobranca'); return false; }
	if (cobranca == "diferente" || Objeto('radio_entrega')[1].checked == true) if (!Objeto('bairro_cobranca').value) { Aviso('Bairro','bairro_cobranca'); Foco('bairro_cobranca'); return false; }
	if (cobranca == "diferente" || Objeto('radio_entrega')[1].checked == true) if (!Objeto('cep_cobranca1').value) { Aviso('CEP','cep_cobranca1'); Foco('cep_cobranca1'); return false; }
	if (cobranca == "diferente" || Objeto('radio_entrega')[1].checked == true) if (!Objeto('cep_cobranca2').value) { Aviso('CEP','cep_cobranca2'); Foco('cep_cobranca2'); return false; }
	if (cobranca == "diferente" || Objeto('radio_entrega')[1].checked == true) if (!Objeto('cidade_cobranca').value) { Aviso('Cidade','cidade_cobranca'); Foco('cidade_cobranca'); return false; }
	if (cobranca == "diferente" || Objeto('radio_entrega')[1].checked == true) if (!Objeto('estado_cobranca').value) { Aviso('Estado','estado_cobranca'); Foco('estado_cobranca'); return false; }
	
	form1.submit();
}
function valida_cpf(cpf) 
{ 
for(i=0;i<cpf.length;i++){
	if(cpf.charAt(i)=="."){
		cpf = cpf.replace(".","");
	}
	if(cpf.charAt(i)=="-"){
		cpf = cpf.replace("-","");
	}
}
var numeros, digitos, soma, i, resultado, digitos_iguais; 
digitos_iguais = 1; 
if (cpf.length < 11) 
			return false; 
for (i = 0; i < cpf.length - 1; i++) 
			if (cpf.charAt(i) != cpf.charAt(i + 1)) 
						{ 
						digitos_iguais = 0; 
						break; 
						} 
if (!digitos_iguais) 
			{ 
			numeros = cpf.substring(0,9); 
			digitos = cpf.substring(9); 
			soma = 0; 
			for (i = 10; i > 1; i--) 
						soma += numeros.charAt(10 - i) * i; 
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
			if (resultado != digitos.charAt(0)) 
						return false; 
			numeros = cpf.substring(0,10); 
			soma = 0; 
			for (i = 11; i > 1; i--) 
						soma += numeros.charAt(11 - i) * i; 
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
			if (resultado != digitos.charAt(1)) 
						return false; 
			return true; 
			} 
else 
			return false; 
}
function valida_cnpj(cnpj) 
{ 
for(i=0;i<cnpj.length;i++){
	if(cnpj.charAt(i)=="."){
		cnpj = cnpj.replace(".","");
	}
	if(cnpj.charAt(i)=="-"){
		cnpj = cnpj.replace("-","");
	}
	if(cnpj.charAt(i)=="/"){
		cnpj = cnpj.replace("/","");
	}
}
var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais; 
digitos_iguais = 1; 
if (cnpj.length < 14 && cnpj.length < 15) 
			return false; 
for (i = 0; i < cnpj.length - 1; i++) 
			if (cnpj.charAt(i) != cnpj.charAt(i + 1)) 
						{ 
						digitos_iguais = 0; 
						break; 
						} 
if (!digitos_iguais) 
			{ 
			tamanho = cnpj.length - 2 
			numeros = cnpj.substring(0,tamanho); 
			digitos = cnpj.substring(tamanho); 
			soma = 0; 
			pos = tamanho - 7; 
			for (i = tamanho; i >= 1; i--) 
						{ 
						soma += numeros.charAt(tamanho - i) * pos--; 
						if (pos < 2) 
									pos = 9; 
						} 
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
			if (resultado != digitos.charAt(0)) 
						return false; 
			tamanho = tamanho + 1; 
			numeros = cnpj.substring(0,tamanho); 
			soma = 0; 
			pos = tamanho - 7; 
			for (i = tamanho; i >= 1; i--) 
						{ 
						soma += numeros.charAt(tamanho - i) * pos--; 
						if (pos < 2) 
									pos = 9; 
						} 
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
			if (resultado != digitos.charAt(1)) 
						return false; 
			return true; 
			} 
else 
			return false; 
} 
function GenJsGetRadio(pObjRadio) {
  var lIntRadio;
  var lIntSelected = -1;

  if (pObjRadio == null) {
     return -10; 
  } 

  if (pObjRadio.length != null) {    
     for (lIntRadio=0; lIntRadio < pObjRadio.length;lIntRadio++) {
       if (pObjRadio[lIntRadio].checked) {
      	  lIntSelected = lIntRadio;	
          break;
        } 
     } 
  } else {
     if (pObjRadio.checked) {
        lIntSelected = 0;
     } 
  }  

  return lIntSelected;
}
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;

function doDate(pStr, pFmt)
{
	eval("reDate = reDate" + pFmt);
	if (reDate.test(pStr.value)) {
		void(0);
	} else if (pStr.value != null && pStr.value != "") {
		alert("Data inválida");
		pStr.focus();
	}
}

