function embeds(url,w,h)  {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+w+'" height="'+h+'">');
	document.write('<param name="movie" value="'+url+'">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="wmode" value="transparent">');
	document.write('<param name="menu" value="false">');
	document.write('<embed src="'+url+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>');
	document.write('</object>');
}

function janelaSecundaria (URL)  { 
	window.open(URL,"janela1","width=300,height=135,scrollbars=NO") 
} 

function limpar(){
 document.form.text.value = '';
}

function arruma_campo(campo,digitos)  {
	var i;
	i = 0;
	while (i < campo.value.length)  {
		if (digitos.indexOf(campo.value.substring(i,i+1)) == -1)
			campo.value = campo.value.substring(0,i) + campo.value.substring(i+1);
		else
			i++;
	}
}

function arruma_qtd(campo)  {
	arruma_campo(campo,"0123456789/");
}

function arruma_cep(campo)  {
	arruma_campo(campo,"0123456789");
}

function arruma_cnpj(campo)  {
	arruma_campo(campo,"0123456789./-");
}

function arruma_cartao(campo)  {
	arruma_campo(campo,"0123456789.");
}

function Comentario (URL){ 
	window.open(URL,"janela1","width=380,height=155,scrollbars=NO") 
} 

function telaComentario()  {
	obj=document.getElementById("tela_Comentario");
	obj.style.display=(obj.style.display=='none')?'':'none';
}

function abrirJanela(url,nome,configs)  {
	window.open(url,nome,configs);
}

function valida_login(x){	
	if(x.email.value == "")  {
		alert ("O campo E-mail é de preenchimento obrigatório");
		x.email.focus();
		return false;
	}
	if( x.email.value == '' || x.email.value.indexOf('@', 0) == -1 || x.email.value.indexOf('.', 0) == -1 )  {
		alert("Por favor, digite um e-mail válido.");
		x.email.focus();
		return false;
	}
	if(x.senha.value == ""){
		alert ("O campo Senha é de preenchimento obrigatório");
		x.senha.focus();
		return false;
	}
}

function valida_esqueceusenha(x)  {
	if(x.emailcadastrado.value == "")  {
		alert ("O campo E-mail é de preenchimento obrigatório");
		x.emailcadastrado.focus();
		return false;
	}
	if( x.emailcadastrado.value == '' || x.emailcadastrado.value.indexOf('@', 0) == -1 || x.emailcadastrado.value.indexOf('.', 0) == -1 )  {
		alert("Por favor, digite um e-mail válido.");
		x.emailcadastrado.focus();
		return false;
	}
}

function barraCartao(objeto){
	if (objeto.value.length == 4 || objeto.value.length == 9 || objeto.value.length == 14){
		objeto.value = objeto.value+".";
	}
}

function barra(objeto){
	if (objeto.value.length == 2){
		objeto.value = objeto.value+"/";
	}
}

function valida_fecha_pedido(x)  {
	var preenchido=false;					
	if(x.tipo_pagto[0].checked)  {
		var preenchido=true;
		if(x.nr_cc.value == "")  {
			alert ("O campo Número do Cartão é de preenchimento obrigatório");
			x.nr_cc.focus();
			return false;
		}
		if(x.cod_seg_cc.value == "")  {
			alert ("O campo Código de Segurança é de preenchimento obrigatório");
			x.cod_seg_cc.focus();
			return false;
		}
		if(x.dt_validade_cc.value == "")  {
			alert ("O campo Data de Validade é de preenchimento obrigatório");
			x.dt_validade_cc.focus();
			return false;
		}
		if(x.dt_validade_cc.value.length < 5)  {							
			alert ("O campo Data de Validade incorreto");
			x.dt_validade_cc.focus();
			return false;
		}
		dt_validade_array = x.dt_validade_cc.value.split("/");
		if(dt_validade_array[0]>12)  {
			alert ("O campo Data de Validade incorreto");
			x.dt_validade_cc.focus();
			return false;
		}
	}

	if(!x.mesmo_endereco.checked)  {
		if(x.destinatario.value == "")  {
			alert ("O campo Destinatário é de preenchimento obrigatório");
			x.destinatario.focus();
			return false;
		}
		if(x.cep.value == "")  {
			alert ("O campo CEP é de preenchimento obrigatório");
			x.cep.focus();
			return false;
		}
		if(x.endereco.value == "")  {
			alert ("O campo Endereço é de preenchimento obrigatório");
			x.endereco.focus();
			return false;
		}
		if(x.numero.value == "")  {
			alert ("O campo Nº é de preenchimento obrigatório");
			x.numero.focus();
			return false;
		}
		if(x.bairro.value == "")  {
			alert ("O campo Bairro é de preenchimento obrigatório");
			x.bairro.focus();
			return false;
		}
		if(x.cidade.value == "")  {
			alert ("O campo Cidade é de preenchimento obrigatório");
			x.cidade.focus();
			return false;
		}
		if(x.uf.value == "")  {
			alert ("O campo UF é de preenchimento obrigatório");
			x.uf.focus();
			return false;
		}
	}
}

function telaTipoPagamento(tipo)  {
	if(tipo=='1'){
		document.getElementById("tela_cc").style.display="";
		document.getElementById("tela_deposito").style.display="none";
	}
	else if(tipo=='3'){
		document.getElementById("tela_deposito").style.display="";
		document.getElementById("tela_cc").style.display="none";
	}
	else {
		document.getElementById("tela_cc").style.display="none";
		document.getElementById("tela_deposito").style.display="none";
	}
}

function validaContato(x)  {
	if(x.nome.value == "")  {
		alert ("O campo Nome é de preenchimento obrigatório");
		x.nome.focus();
		return false;
	}
	if(x.cpf_cnpj.value == "")  {
		alert ("O campo CPF/CNPJ é de preenchimento obrigatório");
		x.cpf_cnpj.focus();
		return false;
	}
	if(x.email.value == "")  {
		alert ("O campo E-mail é de preenchimento obrigatório");
		x.email.focus();
		return false;
	}
	if( x.email.value == '' || x.email.value.indexOf('@', 0) == -1 || x.email.value.indexOf('.', 0) == -1 )  {
		alert("Por favor, digite um e-mail válido.");
		x.email.focus();
		return false;
	}
	if(x.comentario.value == "")  {
		alert ("O campo Comentário é de preenchimento obrigatório");
		x.comentario.focus();
		return false;
	}
}

function valida_comentario(x){	
	if(x.descricao.value == "")  {
		alert ("Por favor preencher o comentário.");
		x.descricao.focus();
		return false;
	}
}

function exibirTelaBuscaProduto()  {
	obj=document.getElementById("telaBuscaProduto");
	obj.style.display=(obj.style.display=='none')?'':'none';
}

function validaCadastro(x)  {
	var preenchido=false;					
	if(x.tipo_cadastro[0].checked)  {
		var preenchido=true;

			if(x.nome.value == "")  {
			alert ("O campo Nome é de preenchimento obrigatório");
			x.nome.focus();
			return false;
			}
			if(x.cpf.value.length  == 0)  {
			alert("O campo CPF é de preenchimento obrigatório");
			x.cpf.focus();
			return false;
			}
			if(!isCpf(x.cpf.value))  {
			alert("O campo CPF está em um formato inválido");
			x.cpf.focus();
			return false;
			}
			if(x.cpf.value  == "000.000.000-00")  {
			alert("Campo CPF inválido");
			x.cpf.focus();
			return false;
			}
			if(x.rg.value == "")  {
			alert ("O campo RG é de preenchimento obrigatório");
			x.rg.focus();
			return false;
			}
			var digitos = "0123456789.-";
			for (var i = 0; i < x.cpf.value.length; i++)  {
				if (digitos.indexOf(x.cpf.value.charAt(i)) == -1)  {
				alert("O campo CPF só pode conter números");
				x.cpf.focus();
				return false;
				}
			}
	}
	
	var preenchido=false;					
	if(x.tipo_cadastro[1].checked)  {
		var preenchido=true;

			if(x.razao_social.value == "")  {
			alert ("O campo Razão Social é de preenchimento obrigatório");
			x.razao_social.focus();
			return false;
			}
			if(x.nome_contato.value == "")  {
			alert ("O campo Nome Contato (Empresa) é de preenchimento obrigatório");
			x.nome_contato.focus();
			return false;
			}
			// if(x.ie.value == "")  {
			// alert ("O campo Inscrição Estadual é de preenchimento obrigatório");
			// x.ie.focus();
			// return false;
			// }
			if(x.cnpj.value.length  == 0)  {
			alert("O campo CNPJ é de preenchimento obrigatório");
			x.cnpj.focus();
			return false;
			}
			if(!isCnpj(x.cnpj.value))  {
			alert("O campo CNPJ está em formato inválido");
			x.cnpj.focus();
			return false;
			}
			if(x.cnpj.value  == "00.000.000/0000-00")  {
			alert("Campo CNPJ inválido");
			x.cnpj.focus();
			return false;
			}
	}
	
	
	if(x.email.value == "")  {
		alert ("O campo E-mail é de preenchimento obrigatório");
		x.email.focus();
		return false;
	}
	if( x.email.value == '' || x.email.value.indexOf('@', 0) == -1 || x.email.value.indexOf('.', 0) == -1 )  {
		alert("Por favor, digite um e-mail válido.");
		x.email.focus();
		return false;
	}
	if(x.senha.value == "")  {
		alert ("O campo Senha é de preenchimento obrigatório");
		x.senha.focus();
		return false;
	}
	if(x.senha1.value == "")  {
		alert ("O campo Confirmar Senha é de preenchimento obrigatório");
		x.senha1.focus();
		return false;
	}
	if(x.cep_end_residencial.value == "")  {
		alert ("O campo CEP é de preenchimento obrigatório");
		x.cep_end_residencial.focus();
		return false;
	}
	if(x.end_residencial.value == "")  {
		alert ("O campo Endereço Residencial é de preenchimento obrigatório");
		x.end_residencial.focus();
		return false;
	}
	if(x.nr_end_residencial.value == "")  {
		alert ("O campo Número é de preenchimento obrigatório");
		x.nr_end_residencial.focus();
		return false;
	}
	if(x.br_end_residencial.value == "")  {
		alert ("O campo Bairro é de preenchimento obrigatório");
		x.br_end_residencial.focus();
		return false;
	}
	if(x.ci_end_residencial.value == "")  {
		alert ("O campo Cidade é de preenchimento obrigatório");
		x.ci_end_residencial.focus();
		return false;
	}
	if(x.uf_end_residencial.value == "")  {
		alert ("O campo UF é de preenchimento obrigatório");
		x.uf_end_residencial.focus();
		return false;
	}
	if(x.ddd_fone1.value == "")  {
		alert ("O campo DDD é de preenchimento obrigatório");
		x.ddd_fone1.focus();
		return false;
	}
	if(x.fone1.value == "")  {
		alert ("O campo Telefone é de preenchimento obrigatório");
		x.fone1.focus();
		return false;
	}
}

function arruma_cpf(campo)  {
	arruma_campo(campo,"0123456789");
	for (i = 0; i < campo.value.length; i++)  {
		if (i == 3 || i == 7)
			campo.value = campo.value.substring(0,i) + "." + campo.value.substring(i);
		if (i == 11)
			campo.value = campo.value.substring(0,i) + "-" + campo.value.substring(i);
	}
}

function DigitoCPFCNPJ(numCIC)  {
	var numDois = numCIC.substring(numCIC.length-2, numCIC.length);
	var novoCIC = numCIC.substring(0, numCIC.length-2);

	switch (numCIC.length)  {
		case 11 :
			numLim = 11;
		break;
		case 14 :
			numLim = 9;
		break;
			default : return false;
	}

	var numSoma = 0;
	var Fator = 1;
	
	for (var i=novoCIC.length-1; i>=0 ; i--)  {
		Fator = Fator + 1;
		if (Fator > numLim)  {
			Fator = 2;
		}
		numSoma = numSoma + (Fator * Number(novoCIC.substring(i, i+1)));
	}
	
	numSoma = numSoma/11;
	var numResto = Math.round( 11 * (numSoma - Math.floor(numSoma)));

	if (numResto > 1)  {
		numResto = 11 - numResto;
	}
	else  {
		numResto = 0;
	}

	var numDigito	= String(numResto)			;
	novoCIC			= novoCIC.concat(numResto)	;

	numSoma = 0;
	Fator = 1;

	for (var i=novoCIC.length-1; i>=0 ; i--)  {
		Fator = Fator + 1;
		if (Fator > numLim)  {
			Fator = 2;
		}
		numSoma = numSoma + (Fator * Number(novoCIC.substring(i, i+1)));
	}

	numSoma = numSoma/11;
	numResto = numResto = Math.round( 11 * (numSoma - Math.floor(numSoma)));

	if (numResto > 1)  {
		numResto = 11 - numResto;
	}
	else  {
		numResto = 0;
	}

	numDigito = numDigito.concat(numResto);
	if (numDigito == numDois)  {
		return true;
	}
	else  {
		return false;
	}
}

function ApenasNum(strParm)  {
	strParm = String(strParm);
	var chrPrt = "0";
	var strRet = "";
	var j=0;

	for (var i=0; i < strParm.length; i++)  {
		chrPrt = strParm.substring(i, i+1);
		if ( chrPrt.match(/\d/))  {
			if (j==0)  {
				strRet = chrPrt;
				j=1;
			}
			else  {
				strRet = strRet.concat(chrPrt);
			}
		}
	}
	return strRet;
}
function PreencheCIC(objCIC)  {
	var chrP = objCIC.value.substring(objCIC.value.length-1, objCIC.value.length);

	if ( !chrP.match(/[0-9]/) && !chrP.match(/[\/.-]/) )  {
		objCIC.value = objCIC.value.substring(0, objCIC.value.length-1);
		return false;
	}
	return true;
}

function FormataCIC (numCIC)  {
	numCIC = String(numCIC);
	switch (numCIC.length)  {
		case 11 :
			return numCIC.substring(0,3) + "." + numCIC.substring(3,6) + "." + numCIC.substring(6,9) + "-" + numCIC.substring(9,11);
		case 14 :
			return numCIC.substring(0,2) + "." + numCIC.substring(2,5) + "." + numCIC.substring(5,8) + "/" + numCIC.substring(8,12) + "-" + numCIC.substring(12,14);
		default : 
			alert("Tamanho incorreto do CPF");
		return "";
	}
}

function ConfereCIC(objCIC)  {
	if (objCIC.value == null)  {
		alert("Preenchimento obrigatório CPF");
		return false;
	}

	var strCPFPat = /^\d{3}\.\d{3}\.\d{3}-\d{2}$/;
	var strCNPJPat = /^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/;

	numCPFCNPJ = ApenasNum(objCIC.value);

	if (!DigitoCPFCNPJ(numCPFCNPJ))  {
		alert("Atenção: o Dígito verificador do CPF é inválido!");
		return false;
	}

	objCIC.value = FormataCIC(numCPFCNPJ);

	if (objCIC.value.match(strCNPJPat))  {
		return true;
	}
	else if (objCIC.value.match(strCPFPat))  {
		return true;
	}
	else  {
		alert("Digite um CPF válido!");
		return false;
	}
}
/////// ------------- //////////
function alteraCadastro(x)  {
	var preenchido=false;					
	if(x.tipo_cadastro[0].checked)  {
		var preenchido=true;
			if(x.nome.value == "")  {
			alert ("O campo Nome é de preenchimento obrigatório");
			x.nome.focus();
			return false;
			}
			if(x.rg.value == "")  {
			alert ("O campo RG é de preenchimento obrigatório");
			x.rg.focus();
			return false;
			}
			if(x.cpf.value.length  == 0)  {
			alert("O campo CPF é de preenchimento obrigatório");
			x.cpf.focus();
			return false;
			}
			if(!isCpf(x.cpf.value))  {
			alert("O campo CPF está em um formato inválido");
			x.cpf.focus();
			return false;
			}
			if(x.cpf.value  == "000.000.000-00")  {
				alert("Campo CPF inválido");
				x.cpf.focus();
				return false;
		}
		var digitos = "0123456789.-";
			for (var i = 0; i < x.cpf.value.length; i++)  {
				if (digitos.indexOf(x.cpf.value.charAt(i)) == -1)  {
				alert("O campo CPF só pode conter números");
				x.cpf.focus();
				return false;
				}
			}
	}
		
		var preenchido=false;					
		if(x.tipo_cadastro[1].checked)  {
			var preenchido=true;

			if(x.razao_social.value == "")  {
			alert ("O campo Razão Social é de preenchimento obrigatório");
			x.razao_social.focus();
			return false;
			}
			if(x.contato_empresa.value == "")  {
			alert ("O campo Nome Contato (Empresa) é de preenchimento obrigatório");
			x.contato_empresa.focus();
			return false;
			}
			// if(x.ie.value == "")  {
			// alert ("O campo Inscrição Estadual é de preenchimento obrigatório");
			// x.ie.focus();
			// return false;
			// }
			if(x.cnpj.value.length  == 0)  {
			alert("O campo CNPJ é de preenchimento obrigatório");
			x.cnpj.focus();
			return false;
			}
			if(!isCnpj(x.cnpj.value))  {
			alert("O campo CNPJ está em formato inválido");
			x.cnpj.focus();
			return false;
			}
			if(x.cnpj.value  == "00.000.000/0000-00")  {
			alert("Campo CNPJ inválido");
			x.cnpj.focus();
			return false;
			}
	}
		
		//if(x.nome.value == "")  {
			//x.tipo_cadastro[1].checked;
			//}
		
	if(x.email.value == "")  {
		alert ("O campo E-mail é de preenchimento obrigatório");
		x.email.focus();
		return false;
	}
	if( x.email.value == '' || x.email.value.indexOf('@', 0) == -1 || x.email.value.indexOf('.', 0) == -1 )  {
		alert("Por favor, digite um e-mail válido.");
		x.email.focus();
		return false;
	}
	if(x.end_residencial.value == "")  {
		alert ("O campo Endereço Residencial é de preenchimento obrigatório");
		x.end_residencial.focus();
		return false;
	}
	if(x.ddd_fone1.value == "")  {
		alert ("O campo DDD é de preenchimento obrigatório");
		x.ddd_fone1.focus();
		return false;
	}
	if(x.fone1.value == "")  {
		alert ("O campo Telefone é de preenchimento obrigatório");
		x.fone1.focus();
		return false;
	}
}

function arruma_cpf(campo)  {
	arruma_campo(campo,"0123456789");
	for (i = 0; i < campo.value.length; i++)  {
		if (i == 3 || i == 7)
			campo.value = campo.value.substring(0,i) + "." + campo.value.substring(i);
		if (i == 11)
			campo.value = campo.value.substring(0,i) + "-" + campo.value.substring(i);
	}
}

function DigitoCPFCNPJ(numCIC)  {
	var numDois = numCIC.substring(numCIC.length-2, numCIC.length);
	var novoCIC = numCIC.substring(0, numCIC.length-2);

	switch (numCIC.length)  {
		case 11 :
			numLim = 11;
		break;
		case 14 :
			numLim = 9;
		break;
			default : return false;
	}

	var numSoma = 0;
	var Fator = 1;
	
	for (var i=novoCIC.length-1; i>=0 ; i--)  {
		Fator = Fator + 1;
		if (Fator > numLim)  {
			Fator = 2;
		}
		numSoma = numSoma + (Fator * Number(novoCIC.substring(i, i+1)));
	}
	
	numSoma = numSoma/11;
	var numResto = Math.round( 11 * (numSoma - Math.floor(numSoma)));

	if (numResto > 1)  {
		numResto = 11 - numResto;
	}
	else  {
		numResto = 0;
	}

	var numDigito	= String(numResto)			;
	novoCIC			= novoCIC.concat(numResto)	;

	numSoma = 0;
	Fator = 1;

	for (var i=novoCIC.length-1; i>=0 ; i--)  {
		Fator = Fator + 1;
		if (Fator > numLim)  {
			Fator = 2;
		}
		numSoma = numSoma + (Fator * Number(novoCIC.substring(i, i+1)));
	}

	numSoma = numSoma/11;
	numResto = numResto = Math.round( 11 * (numSoma - Math.floor(numSoma)));

	if (numResto > 1)  {
		numResto = 11 - numResto;
	}
	else  {
		numResto = 0;
	}

	numDigito = numDigito.concat(numResto);
	if (numDigito == numDois)  {
		return true;
	}
	else  {
		return false;
	}
}

function ApenasNum(strParm)  {
	strParm = String(strParm);
	var chrPrt = "0";
	var strRet = "";
	var j=0;

	for (var i=0; i < strParm.length; i++)  {
		chrPrt = strParm.substring(i, i+1);
		if ( chrPrt.match(/\d/))  {
			if (j==0)  {
				strRet = chrPrt;
				j=1;
			}
			else  {
				strRet = strRet.concat(chrPrt);
			}
		}
	}
	return strRet;
}
function PreencheCIC(objCIC)  {
	var chrP = objCIC.value.substring(objCIC.value.length-1, objCIC.value.length);

	if ( !chrP.match(/[0-9]/) && !chrP.match(/[\/.-]/) )  {
		objCIC.value = objCIC.value.substring(0, objCIC.value.length-1);
		return false;
	}
	return true;
}

function FormataCIC (numCIC)  {
	numCIC = String(numCIC);
	switch (numCIC.length)  {
		case 11 :
			return numCIC.substring(0,3) + "." + numCIC.substring(3,6) + "." + numCIC.substring(6,9) + "-" + numCIC.substring(9,11);
		case 14 :
			return numCIC.substring(0,2) + "." + numCIC.substring(2,5) + "." + numCIC.substring(5,8) + "/" + numCIC.substring(8,12) + "-" + numCIC.substring(12,14);
		default : 
			alert("Tamanho incorreto do CPF");
		return "";
	}
}

function ConfereCIC(objCIC)  {
	if (objCIC.value == null)  {
		alert("Preenchimento obrigatório CPF");
		return false;
	}

	var strCPFPat = /^\d{3}\.\d{3}\.\d{3}-\d{2}$/;
	var strCNPJPat = /^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/;

	numCPFCNPJ = ApenasNum(objCIC.value);

	if (!DigitoCPFCNPJ(numCPFCNPJ))  {
		alert("Atenção: o Dígito verificador do CPF é inválido!");
		return false;
	}

	objCIC.value = FormataCIC(numCPFCNPJ);

	if (objCIC.value.match(strCNPJPat))  {
		return true;
	}
	else if (objCIC.value.match(strCPFPat))  {
		return true;
	}
	else  {
		alert("Digite um CPF válido!");
		return false;
	}
}
///////////// ------------ ///////////////


function gerarAJAX() {
    if(window.XMLHttpRequest)  {
        return new XMLHttpRequest();
    }
	else
		if(window.ActiveXObject)  {
			var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
			for(var i = 0; i < prefixes.length; i++)  {
            try  {
                return new ActiveXObject(prefixes[i] + ".XMLHTTP");
            }
			catch (e) {}
		}
	}
	window.alert('AJAX não funciona neste navegador.');
	return false;
}

function buscarEndereco(cep,endereco,bairro,cidade,uf,pais,msg)  {
	if(parseInt(cep.value.length)==8)  {
		xmlhttp=gerarAJAX();
		if(!xmlhttp) return false;
		xmlhttp.open("GET","/library/buscaEndereco.asp?cep="+cep.value,true);
		xmlhttp.onreadystatechange=function()  {
			if(xmlhttp.readyState<4)  {
				v_percent=(parseInt(xmlhttp.readyState)*23)+3;
				msg.innerHTML="carregando..."+v_percent+"%";
			}
			else  {
				if(xmlhttp.status==200)  {
					resultado=xmlhttp.responseText;
					if(resultado=="erro")
						msg.innerHTML="CEP não encontrado.<BR>Digite o endereço manualmente.";
					else  {
						v_resultado=resultado.split("//");
						endereco.value=v_resultado[0];
						bairro.value=v_resultado[1];
						cidade.value=v_resultado[2];
						uf.value=v_resultado[3];
						pais.value="Brasil";
						msg.innerHTML="";
					}
				}
				else  {
				//	msg.innerHTML="CEP não encontrado.<BR>Digite o endereço manualmente.";
					msg.innerHTML="Erro ao buscar CEP.<BR>Erro: " + xmlhttp.status + " - " + xmlhttp.statusText;
				}
			}
		}
		xmlhttp.send(null);
	}
}

function valida_aviso(x)  {
	if(x.nome.value == "")  {
		alert ("Por favor preencher seu nome");
		x.nome.focus();
		return false;
	}
	if(x.email.value == "")  {
		alert ("O campo E-mail é de preenchimento obrigatório");
		x.email.focus();
		return false;
	}
	if( x.email.value == '' || x.email.value.indexOf('@', 0) == -1 || x.email.value.indexOf('.', 0) == -1 )  {
		alert("Por favor, digite um e-mail válido.");
		x.email.focus();
		return false;
	}
}

function ExibeTelaPessoaFormCadastro(pessoa)  {
	if(pessoa=='1')  {
		nome_linha="linha_pf";
		nome_linha1="linha_pj";
	}
	else  {
		nome_linha="linha_pj";
		nome_linha1="linha_pf";
	}
	j=0;
	while(1==1)  {
		if(document.getElementById(nome_linha+j)!=undefined)  {
			document.getElementById(nome_linha+j).style.display='';
			j++;
		}
		else
			break;
	}
	j=0;
	while(1==1)  {
		if(document.getElementById(nome_linha1+j)!=undefined)  {
			document.getElementById(nome_linha1+j).style.display='none';
			j++
		}
		else
			break;
	}
}

function abrirIndiqueAmigo(e) {
	if (navigator.appName == 'Netscape')  {
		xcurs = e.pageX-100;//window.pageXOffset
		ycurs = e.pageY-50;//window.pageYOffset
	}
	else  {
		xcurs = event.clientX+document.body.scrollLeft-100;
		ycurs = event.clientY+document.body.scrollTop-50;
	}
	tela=document.getElementById("telaIndiqueAmigo");
	tela.style.left=xcurs;
	tela.style.top=ycurs;
	tela.style.display='';
}

function fecharIndiqueAmigoFinalizado()  {
	tela=document.getElementById("telaIndiqueAmigo_Finalizado");
	tela.style.display='none';
}

function fecharIndiqueAmigo()  {
	tela=document.getElementById("telaIndiqueAmigo");
	tela.style.display='none';
}

function enviarIndiqueAmigo()  {
	formulario=document.getElementById("form_indique_amigo");
	vid_produto=formulario.id_produto.value;
	v_nome=formulario.nome.value;
	v_nome_amigo=formulario.nome_amigo.value;
	v_email_amigo=formulario.email_amigo.value;
	xmlhttp=gerarAJAX();
	xmlhttp.open("GET","indique_amigo.asp?id_produto="+vid_produto+"&nome="+v_nome+"&nome_amigo="+v_nome_amigo+"&email_amigo="+v_email_amigo,true);
	xmlhttp.onreadystatechange=function()  {
		if(xmlhttp.readyState<4)  {
			tela=document.getElementById("telaIndiqueAmigo_Carregando");
			tela_anterior=document.getElementById("telaIndiqueAmigo");
			tela_anterior.style.display='none';
			tela.style.left=xcurs;
			tela.style.top=ycurs;
			tela.style.display='';
		}
		else if(xmlhttp.status==200) {
			tela=document.getElementById("telaIndiqueAmigo_Finalizado");
			tela_anterior=document.getElementById("telaIndiqueAmigo_Carregando");
			tela_anterior.style.display='none';
			tela.style.left=xcurs;
			tela.style.top=ycurs;
			tela.innerHTML=tela.innerHTML.replace(v_nome_amigo_antigo,v_nome_amigo);
			v_nome_amigo_antigo=v_nome_amigo;
			formulario.nome_amigo.value="";
			formulario.email_amigo.value="";
			tela.style.display='';
		}
		else  {
			tela=document.getElementById("telaIndiqueAmigo_Carregando");
			tela.style.display='none';
			window.alert('ERRO: '+xmlhttp.status+' - '+xmlhttp.statusText);
		}
	}
	xmlhttp.send(null);
}

/**
 * String unformatNumber(String pNum)
 * String formatCpfCnpj(String pCpfCnpj, boolean pUseSepar, boolean pIsCnpj)
 * String dvCpfCnpj(String pEfetivo, boolean pIsCnpj)
 * boolean isCpf(String pCpf)
 * boolean isCnpj(String pCnpj)
 * boolean isCpfCnpj(String pCpfCnpj)
 */


NUM_DIGITOS_CPF  = 11;
NUM_DIGITOS_CNPJ = 14;
NUM_DGT_CNPJ_BASE = 8;


String.prototype.lpad = function(pSize, pCharPad)
{
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) str = ch + str;
	return (str);
} //String.lpad


String.prototype.trim = function()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
} //String.trim


function unformatNumber(pNum)
{
	return String(pNum).replace(/\D/g, "").replace(/^0+/, "");
} //unformatNumber


function formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	if (pUseSepar==null) pUseSepar = true;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var numero = unformatNumber(pCpfCnpj);

	numero = numero.lpad(maxDigitos, '0');
	if (!pUseSepar) return numero;

	if (pIsCnpj)
	{
		reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
		numero = numero.replace(reCnpj, "$1.$2.$3/$4-$5");
	}
	else
	{
		reCpf  = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
		numero = numero.replace(reCpf, "$1.$2.$3-$4");
	}
	return numero;
} //formatCpfCnpj


function dvCpfCnpj(pEfetivo, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	var i, j, k, soma, dv;
	var cicloPeso = pIsCnpj? NUM_DGT_CNPJ_BASE: NUM_DIGITOS_CPF;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var calculado = formatCpfCnpj(pEfetivo, false, pIsCnpj);
	calculado = calculado.substring(2, maxDigitos);
	var result = "";

	for (j = 1; j <= 2; j++)
	{
		k = 2;
		soma = 0;
		for (i = calculado.length-1; i >= 0; i--)
		{
			soma += (calculado.charAt(i) - '0') * k;
			k = (k-1) % cicloPeso + 2;
		}
		dv = 11 - soma % 11;
		if (dv > 9) dv = 0;
		calculado += dv;
		result += dv
	}

	return result;
} //dvCpfCnpj


function isCpf(pCpf)
{
	var numero = formatCpfCnpj(pCpf, false, false);
	var base = numero.substring(0, numero.length - 2);
	var digitos = dvCpfCnpj(base, false);
	var algUnico, i;

	// Valida dígitos verificadores
	if (numero != base + digitos) return false;

	algUnico = true;
	for (i=1; algUnico && i<NUM_DIGITOS_CPF; i++)
	{
		algUnico = (numero.charAt(i-1) == numero.charAt(i));
	}
	return (!algUnico);
} //isCpf

function isCnpj(pCnpj)
{
	var numero = formatCpfCnpj(pCnpj, false, true);
	var base = numero.substring(0, NUM_DGT_CNPJ_BASE);
	var ordem = numero.substring(NUM_DGT_CNPJ_BASE, 12);
	var digitos = dvCpfCnpj(base + ordem, true);
	var algUnico;

	// Valida dígitos verificadores
	if (numero != base + ordem + digitos) return false;

	algUnico = numero.charAt(0) != '0';
	for (i=1; algUnico && i<NUM_DGT_CNPJ_BASE; i++)
	{
		algUnico = (numero.charAt(i-1) == numero.charAt(i));
	}
	if (algUnico) return false;

	if (ordem == "0000") return false;
	return (base == "00000000"
		|| parseInt(ordem, 10) <= 300 || base.substring(0, 3) != "000");
} //isCnpj


function isCpfCnpj(pCpfCnpj)
{
	var numero = pCpfCnpj.replace(/\D/g, "");
	if (numero.length > NUM_DIGITOS_CPF)
		return isCnpj(pCpfCnpj)
	else
		return isCpf(pCpfCnpj);
} //isCpfCnpj

function abrirSubMenu(id, tipo)  {
	obj=document.getElementById("submenu_cidades_"+id);
	obj.style.display=(tipo=="1")?"":"none";
}

function formSoliciteProposta(formulario)  {
	erro="";
	if(formulario.empresa.value=="")  {
		erro+="Empresa obrigatória!\n";
		formulario.empresa.focus();
	}
	if(formulario.contato.value=="")  {
		erro+="Contato obrigatório!\n";
		formulario.contato.focus();
	}
	if(formulario.cidade.value=="")  {
		erro+="Cidade obrigatória!\n";
		formulario.cidade.focus();
	}
	if(formulario.uf.value=="")  {
		erro+="UF obrigatória!\n";
		formulario.uf.focus();
	}
	if(formulario.ddd.value=="")  {
		erro+="DDD obrigatório!\n";
		formulario.ddd.focus();
	}
	if(formulario.telefone.value=="")  {
		erro+="Telefone obrigatório!\n";
		formulario.telefone.focus();
	}
	if(formulario.email.value=="")  {
		erro+="E-Mail obrigatório!\n";
		formulario.email.focus();
	}
	if(formulario.codigo_seguranca.value=="")  {
		erro+="Código de Segurança obrigatório!\n";
		formulario.codigo_seguranca.focus();
	}
	if(erro!="")  {
		window.alert(erro);
		return false
	}
	return true;
}

function validaNewsletter(x){
		if(x.nome.value == "" || x.nome.value.toLowerCase() == "nome"){
			alert ("O campo Nome é de preenchimento obrigatório");
			x.email.focus();
			return false;
		}
		if(x.email.value == ""){
			alert ("O campo E-mail é de preenchimento obrigatório");
			x.email.focus();
			return false;
		}
		if( x.email.value.indexOf('@', 0) == -1 || x.email.value.indexOf('.', 0) == -1 )
		{
			alert("Por favor, digite um e-mail válido.");
			x.email.focus();
			return false;
		}
}
