//JavaScript Document
function Dec2Hex(Decimal) {
	var hexChars = "0123456789ABCDEF";
	var a = Decimal % 16;
	var b = (Decimal - a)/16;
	hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
	L = hexChars.charAt(a);
	H = hexChars.charAt(b);
	return hex;
}

function hex2num (s_hex) {
	var re_hexNum = /^[\da-f]+$/i;
	if (!re_hexNum.exec(s_hex))	return false;
	eval("var n_num=0X" + s_hex);
	return n_num;
}

function trocaImagemItem(i, ID, video) {
	$('#' + ID).attr('src', i);
	
	if (video) {
		$('#arquivos' + video + ' .containerVideo').html('');
		$('#arquivos' + video + ' .containerVideo').hide();
		$('#arquivos' + video + ' .containerImg').show();
	}
}

// *****
// carrega imagens nas telas de or�amentos
//
function loadImagensItens(item) {
	$.ajax({
		url: 'loadImagensItens.php?p=' + item,
		cache:false,
		success: function(html) {
			$('#imagensDosProdutos').html(html);
		},
		error: function(text) {
			$('#imagensDosProdutos').text(text);
		}
	});
}


function abrePop(url, width, height) {
	//window.open(url, 'width='+width+', height='+height);
	window.open(url,'',"width="+width+",height="+height+",menubar='no'")
}

function abrePopOrcamento(url, width, height) {
	$.ajax({
		url: url,
		cache: false,
		type: 'GET',		
		success: function(html) {
			$('#listaDeItensOrcados').html(html);
			$('#listaDeItensOrcados').show();
		},
		error: function(text) {
			alert(text);
		}
	})
}

//*****
//Aciona rotina que limpa lista de or�amento.

function rmOrcamento(produto) {
	$.ajax({
		url: '/novo/rmListaOrcamento.php',
		cache: false,
		data: {'p': produto},
		type: 'GET',
		success: function(text) {
			
			if (produto == '-1' || text == '0') {	// remover todos os itens.
				$('.trItemOrcamento').remove();
				$('#btConcluirOrcamento').remove();
				$('#btLimparOrcamento').remove();
			} else {
				$('#trItemOrcamento' + produto).remove();
			}
		},
		error: function(text) {
			alert(text)
		}
	})

}
function redefinirSenha(codigo) {
	if (confirm('Deseja realmente redefinir a senha?')) {
		$.ajax({
			url: '/novo/administrar/usuarios/redefinirSenha.php',
			cache: false,
			data: {codigo: codigo},
			type: 'POST',
			success: function(text) {
				$('#msgUsuarios').text(text);
			},
			error: function(text) {
				$('#msgUsuarios').text(text);
			}
		})
	}
}
function alteraTipoPessoa(tipo) {
	if (tipo == '1') {
		$('#cpfPessoa').css('display', '');
		$('#cnpjPessoa').css('display', 'none');
		$('#rgPessoa').css('display', '');
		$('#iePessoa').css('display', 'none');

		$('#trResponsavelEmpresa').css('display', 'none');
		$('#inscRural').css('display', 'none');
		$('#inscricaoRural').css('display', 'none');

		$('#crmvPessoa').css('display', '');
		$('#imPessoa').css('display', 'none');
		$('#nomePessoa').css('display', '');
		$('#empresaPessoa').css('display', 'none');
		$('#cargoPessoa').css('display', 'none');
		$('#contatoPessoa').css('display', 'none');
		$('#cpfCnpjPessoa').attr('size', '15');
		$('#cpfCnpjPessoa').attr('maxlength', '14');
		$('#cpfCnpjPessoa').keypress(function() {
			mascara(this, mcpf);
		});
		$('#trSite').css('display', 'none');
		$('#orgaoExpedidor').css('display', '');
	} else {
		$('#cpfPessoa').css('display', 'none');
		$('#cnpjPessoa').css('display', '');
		$('#rgPessoa').css('display', 'none');
		$('#iePessoa').css('display', '');

		$('#trResponsavelEmpresa').css('display', '');
		$('#inscRural').css('display', '');
		$('#inscricaoRural').css('display', '');

		$('#crmvPessoa').css('display', 'none');
		$('#imPessoa').css('display', '');
		$('#nomePessoa').css('display', 'none');
		$('#empresaPessoa').css('display', '');
		$('#cargoPessoa').css('display', '');
		$('#contatoPessoa').css('display', '');	
		$('#cpfCnpjPessoa').attr('size', '19');
		$('#cpfCnpjPessoa').attr('maxlength', '18');
		$('#cpfCnpjPessoa').keypress(function() {
			mascara(this, mcnpj);
		});	
		$('#trSite').css('display', '');
		$('#orgaoExpedidor').css('display', 'none');		
	}
}
function alteraTipoPessoaTC(tipo) {
	if (tipo == '1') {
		$('#cpfPessoa').css('display', '');
		$('#cnpjPessoa').css('display', 'none');
		$('#rgPessoa').css('display', '');

		$('#nomePessoa').css('display', '');

		$('#iePessoa').css('display', 'none');

		$('#empresaPessoa').css('display', 'none');
		$('#trResponsavelEmpresa').css('display', 'none');
		$('#inscRural').css('display', 'none');
		$('#inscricaoRural').css('display', 'none');


		$('#crmvPessoa').css('display', '');
		$('#imPessoa').css('display', 'none');
		$('#nomePessoa').css('display', '');
		$('#empresaPessoa').css('display', 'none');
		$('#cargoPessoa').css('display', 'none');
		$('#contatoPessoa').css('display', 'none');
		$('#cpfCnpjPessoa').attr('size', '15');
		$('#cpfCnpjPessoa').attr('maxlength', '14');
		$('#cpfCnpjPessoa').keypress(function() {
			mascara(this, mcpf);
		});
		$('#orgaoExpedidor').css('display', '');
		$('.responsavel').css('display','none');
		$('#dataNascimento').css('display', '');
		$('#sexoEstCivil').css('display', '');
	} else {
		$('#cpfPessoa').css('display', 'none');
		$('#cnpjPessoa').css('display', '');
		$('#rgPessoa').css('display', 'none');

		$('#nomePessoa').css('display', 'none');

		$('#iePessoa').css('display', '');

		$('#empresaPessoa').css('display', '');
		$('#trResponsavelEmpresa').css('display', '');
		$('#inscRural').css('display', '');
		$('#inscricaoRural').css('display', '');

		$('#crmvPessoa').css('display', 'none');
		$('#imPessoa').css('display', '');
		$('#nomePessoa').css('display', 'none');
		$('#empresaPessoa').css('display', '');
		$('#cargoPessoa').css('display', '');
		$('#contatoPessoa').css('display', '');	
		$('#cpfCnpjPessoa').attr('size', '19');
		$('#cpfCnpjPessoa').attr('maxlength', '18');
		$('#cpfCnpjPessoa').keypress(function() {
			mascara(this, mcnpj);
		});	
		$('#orgaoExpedidor').css('display', 'none');
		$('.responsavel').css('display','');
		$('#dataNascimento').css('display', 'none');
		$('#sexoEstCivil').css('display', 'none');		
	}
}
function mostraDadosEntrega(acao) {
	if (acao == '1') { 
		$('#divDadosEntrega').hide();
	} else {
		$('#divDadosEntrega').show();
	}
}

function getFormOrcamento(item, chk, tipoOrcamento) {
	tipoOrcamento = tipoOrcamento?tipoOrcamento:0;
	item = chk?item:0;
	$('#msgAguarde').show();
	var self = this;
	$.ajax({
		url: 'getFormOrcamento.php?p=' + item + '&t=' + tipoOrcamento,
		cache: false,
		type: 'GET',
		success: function(html) {
			$('#formOrcamento').html(html);
			setTimeout(function() {$('#alertaQuantidade').fadeOut(2000);}, 5000);
			loadImagensItens(item);
			$('#msgAguarde').hide();
		}
		});
}

function getItensSession() {
	var self = this;
	$.ajax({
		url: 'getItensSession.php',
		cache: false,
		type: 'GET',
		success: function(text) {
			var itens = eval(text);
			$('.chkOrcamento').attr('checked', false);
			for (var item in itens) {
				$('#chk' + itens[item]).attr('checked', true);
			}
		}
		});
}
