// JavaScript Document
function _jsValidaCPF(CPF){ 
	if((CPF == '00000000000') || (CPF == '11111111111') || (CPF == '22222222222') || (CPF == '33333333333') || (CPF == '44444444444') || 
	   (CPF == '55555555555') || (CPF == '66666666666') || (CPF == '77777777777') || (CPF == '88888888888') || (CPF == '99999999999')){
		return false;
	}
 	var i; 
	s = CPF  
	var c = s.substr(0,9); 
	var dv = s.substr(9,2); 
	var d1 = 0; 
	for (i = 0; i < 9; i++) { 
		d1 += c.charAt(i)*(10-i); 
	} 
	 
	if (d1 == 0) { 
		return false; 
	} 
	  
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(0) != d1) { 
 		return false;   
	} 
	  
	d1 *= 2; 	  
	for (i = 0; i < 9; i++)  {   
		d1 += c.charAt(i)*(11-i); 
	} 
	  
	d1 = 11 - (d1 % 11); 

	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1) { 
		return false; 
	} 
	  
	return true; 
}

//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/string/is-cnpj [rev. #1]
function _jsValidaCNPJ(CNPJ){
    var b = [6,5,4,3,2,9,8,7,6,5,4,3,2], c = CNPJ;
    if((c = c.replace(/[^\d]/g,"").split("")).length != 14) return false;
    for(var i = 0, n = 0; i < 12; n += c[i] * b[++i]);
    if(c[12] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    for(var i = 0, n = 0; i <= 12; n += c[i] * b[i++]);
    if(c[13] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    return true;
};

function _jsValCadastro() {
	if (document.formCadastro.Nome.value == '') {
		alert('Por favor, preencha o campo Nome.');
		document.formCadastro.Nome.focus();
		return false;
	}
	
	for(var i=0; i < document.formCadastro.Tipo.length; i++){
		if (document.formCadastro.Tipo[i].checked){
			var rad_val = document.formCadastro.Tipo[i].value;
		}
	}
	if(rad_val == 0){
		if (!_jsValidaCPF(document.formCadastro.CPF.value)) {
			alert('Por favor, preencha o CPF corretamente, sem pontos, virgulas ou traços.\nEx: 12345678900');
			document.formCadastro.CPF.focus();
			return false;
		}
	}
	else{
		if (!_jsValidaCNPJ(document.formCadastro.CPF.value)) {
			alert('Por favor, preencha o CNPJ corretamente, sem pontos, virgulas ou traços.\nEx: 11222333000181');
			document.formCadastro.CPF.focus();
			return false;
		}
	}
	if (document.formCadastro.Endereco.value == '') {
		alert('Por favor, preencha o campo Endereço.');
		document.formCadastro.Endereco.focus();
		return false;
	}
	if (document.formCadastro.Numero.value == '') {
		alert('Por favor, preencha o campo Número.');
		document.formCadastro.Numero.focus();
		return false;
	}
	if (document.formCadastro.Bairro.value == '') {
		alert('Por favor, preencha o campo Bairro.');
		document.formCadastro.Bairro.focus();
		return false;
	}
	if (document.formCadastro.Telefone.value == '') {
		alert('Por favor, preencha o campo Telefone.');
		document.formCadastro.Telefone.focus();
		return false;
	}
	if (document.formCadastro.Email.value == '' || document.formCadastro.Email.value.indexOf("@") == -1 || document.formCadastro.Email.value.indexOf(".") == -1) {
		alert("O campo E-mail deve ser preenchido!");
		document.formCadastro.Email.focus();
		return false;
	}
	if (document.formCadastro.Cidade.value == '') {
		alert('Por favor, preencha o campo Cidade.');
		document.formCadastro.Cidade.focus();
		return false;
	}
	if (document.formCadastro.Estado.value == '') {
		alert('Por favor, preencha o campo Estado.');
		document.formCadastro.Estado.focus();
		return false;
	}
	if (document.formCadastro.Senha.value == '') {
		alert('Por favor, preencha o campo Senha.');
		document.formCadastro.Senha.focus();
		return false;
	}
	if (document.formCadastro.Contato.value == '') {
		alert('Por favor, preencha o campo Contato.');
		document.formCadastro.Contato.focus();
		return false;
	}
	if (document.formCadastro.Referencia1.value == '') {
		alert('Por favor, preencha o campo Empresa / Telefone.');
		document.formCadastro.Referencia1.focus();
		return false;
	}
	if (document.formCadastro.Referencia2.value == '') {
		alert('Por favor, preencha o campo Empresa / Telefone.');
		document.formCadastro.Referencia2.focus();
		return false;
	}
	if (document.formCadastro.Senha.value != document.formCadastro.Senha_Confirmacao.value) {
		alert('A confirmaçao da senha está incorreta!');
		document.formCadastro.Senha_Confirmacao.focus();
		return false;
	}
}

function _jsAtuCadastro() {
	if (document.formCadastro.Nome.value == '') {
		alert('Por favor, preencha o campo Nome.');
		document.formCadastro.Nome.focus();
		return false;
	}
	
	for(var i=0; i < document.formCadastro.Tipo.length; i++){
		if (document.formCadastro.Tipo[i].checked){
			var rad_val = document.formCadastro.Tipo[i].value;
		}
	}
	if(rad_val == 0){
		if (!_jsValidaCPF(document.formCadastro.CPF.value)) {
			alert('Por favor, preencha o CPF corretamente, sem pontos, virgulas ou traços.\nEx: 12345678900');
			document.formCadastro.CPF.focus();
			return false;
		}
	}
	else{
		if (!_jsValidaCNPJ(document.formCadastro.CPF.value)) {
			alert('Por favor, preencha o CNPJ corretamente, sem pontos, virgulas ou traços.\nEx: 11222333000181');
			document.formCadastro.CPF.focus();
			return false;
		}
	}
	if (document.formCadastro.Endereco.value == '') {
		alert('Por favor, preencha o campo Endereço.');
		document.formCadastro.Endereco.focus();
		return false;
	}
	if (document.formCadastro.Numero.value == '') {
		alert('Por favor, preencha o campo Número.');
		document.formCadastro.Numero.focus();
		return false;
	}
	if (document.formCadastro.Bairro.value == '') {
		alert('Por favor, preencha o campo Bairro.');
		document.formCadastro.Bairro.focus();
		return false;
	}
	if (document.formCadastro.Telefone.value == '') {
		alert('Por favor, preencha o campo Telefone.');
		document.formCadastro.Telefone.focus();
		return false;
	}
	if (document.formCadastro.Cidade.value == '') {
		alert('Por favor, preencha o campo Cidade.');
		document.formCadastro.Cidade.focus();
		return false;
	}
	if (document.formCadastro.Estado.value == '') {
		alert('Por favor, preencha o campo Estado.');
		document.formCadastro.Estado.focus();
		return false;
	}
	if (document.formCadastro.Email.value == '' || document.formCadastro.Email.value.indexOf("@") == -1 || document.formCadastro.Email.value.indexOf(".") == -1) {
		alert("O campo E-mail deve ser preenchido!");
		document.formCadastro.Email.focus();
		return false;
	}
	if (document.formCadastro.Contato.value == '') {
		alert('Por favor, preencha o campo Contato.');
		document.formCadastro.Contato.focus();
		return false;
	}
	if (document.formCadastro.Referencia1.value == '') {
		alert('Por favor, preencha o campo Empresa / Telefone.');
		document.formCadastro.Referencia1.focus();
		return false;
	}
	if (document.formCadastro.Referencia2.value == '') {
		alert('Por favor, preencha o campo Empresa / Telefone.');
		document.formCadastro.Referencia2.focus();
		return false;
	}
	if (document.formCadastro.Senha.value != '') {
		if (document.formCadastro.Senha.value != document.formCadastro.Senha_Confirmacao.value) {
			alert('A confirmaçao da senha está incorreta!');
			document.formCadastro.Senha_Confirmacao.focus();
			return false;
		}
	}
}

function _jsValidaEmail() {
	if (document.formEmail.Email.value == '' || document.formEmail.Email.value.indexOf("@") == -1 || document.formEmail.Email.value.indexOf(".") == -1) {
		alert('Por favor, preencha o campo e-mail corretamente.');
		document.formEmail.Email.focus();
		return false;
	}
}

function _jsZoom(imagem, descricao, id){
	document.getElementById('imgZoom').src = "../inc/imgResize.php?nW=300&nH=300&img=../img/Produtos/"+imagem;
	document.getElementById('produtoNome').innerHTML = descricao;
	document.getElementById('produtoComprar').innerHTML = "<img src=\"../img/botOrcamento2.png\" width=\"133\" height=\"14\" style=\"cursor: pointer;\" onClick=\"_jsAdicionaProduto2("+id+")\" />";
}

function _jsAdicionaProduto(Produto_Id){
	location.href="acoes.php?Acao=3&Produto_Id="+Produto_Id;	
}

function _jsAdicionaProduto2(Produto_Id){
	window.parent.location.href="acoes.php?Acao=3&Produto_Id="+Produto_Id;	
}

function _jsRemoveProduto(Produto_Id){
	location.href="acoes.php?Acao=4&Produto_Id="+Produto_Id;	
}

function _jsAtualizaProduto(Produto_Id, Produto_Quantidade){
	location.href="acoes.php?Acao=5&Produto_Id="+Produto_Id+"&Produto_Quantidade="+Produto_Quantidade;	
}

function _jsLimpaCarrinho(){
	location.href="acoes.php?Acao=6";	
}

function _jsFecharPedido(){
	location.href="acoes.php?Acao=7";	
}

function popup(x,n,w,h,s) {
    var l = ((window.screen.width  - w) / 2);
    var t = ((window.screen.height - h) / 2);
   janelinha = window.open(x,n,'leftmargin=-15,topmargin=-15,marginheight=0,marginwidth=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+s+',resizable=no,top='+t+',left='+l+',screenX='+l+',screenY='+t+',copyhistory=no,height='+h+',width='+w+'');
}

function _jsChangeTipo(val){
	if(val == 1){
		document.getElementById('lblNome').innerHTML = "Razão Social";	
		document.getElementById('lblApelido').innerHTML = "Nome Fantasia";	
		document.getElementById('lblCPF').innerHTML = "CNPJ";	
	}
	else{
		document.getElementById('lblNome').innerHTML = "Nome";	
		document.getElementById('lblApelido').innerHTML = "Gosta de ser chamado de:";	
		document.getElementById('lblCPF').innerHTML = "CPF";	
	}
}
