<!--
MM_returnValue = false;

function Limpar(valor, validos) {
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}

function FormataValor(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = Limpar(campo.value,"0123456789");
	tam = vr.length;
	if (tam < tammax && tecla != 8) {
		tam = vr.length + 1;
	}
	if (tecla == 8 ) {
		tam = tam - 1;
	}
	if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) {
		campo.value = adicionaPontoVirgula(tam, vr);
	}
}

function adicionaPontoVirgula(tam, vr) {
	if (tam <= 2) {
		resultado = vr ;
	}
	if ((tam > 2) && (tam <= 5)) {
		resultado = vr.substr(0, tam - 2) + ',' + vr.substr(tam - 2, tam);
	}
	if ((tam >= 6) && (tam <= 8)) {
		resultado = vr.substr(0, tam - 5) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
	}
	if ((tam >= 9) && (tam <= 11)) {
		resultado = vr.substr(0, tam - 8) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
	}
	if ((tam >= 12) && (tam <= 14)) {
		resultado = vr.substr(0, tam - 11) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
	}
	if ((tam >= 15) && (tam <= 17)) {
		resultado = vr.substr(0, tam - 14) + '.' + vr.substr(tam - 14, 3) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
	}
	
	return resultado;
}

function validaEstoque(campo, estoque) {
	campoEstoque = "document." + estoque + ".value";
	estoque = parseInt(eval(campoEstoque));
	quantidade = parseInt(campo.value);
	if (quantidade <= 0) {
		alert("A quantidade seleciona não pode ser igual ou menor que 0.");
		campo.value = 1;
	} else if (quantidade > estoque) {
		alert("A quantidade seleciona está maior que a disponível em estoque (" + estoque + ").");
		campo.value = 1;
	}
}

function FormataCPF(cpf) {
	var mycpf = '';
	mycpf = mycpf + cpf.value;
	if (mycpf.length == 3) {
		mycpf = mycpf + '.';
		cpf.value = mycpf;
	}
	if (mycpf.length == 7) {
		mycpf = mycpf + '.';
		cpf.value = mycpf;
	}
	if (mycpf.length == 11) {
		mycpf = mycpf + '-';
		cpf.value = mycpf;
	}
	if (mycpf.length == 14) {
	}

	if (mycpf.length == 14) {
		if (valida_CPF(mycpf) == false ) {
			alert("O CPF não é válido !");
			cpf.value = "";
			cpf.focus();
			return false;
                        }
	}
}

function FormataCNPJ(Campo, teclapres) {
	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;
	
	if (tecla != 9 && tecla != 8) {
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 15)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
	}

	if (Campo.value.length == 18) {
		if (valida_CNPJ(Campo.value) == false ) {
			alert("O CNPJ não é válido !");
			Campo.value = "";
			Campo.focus();
			return false;
        }
	}
}

function limpaCampo(formName, fieldName) {
	stringField = "document." + formName + "." + fieldName + ".value = ''";
	eval(stringField);
}

function desabilitaEndereco(fieldCheck, addressNumber) {
	fieldAddres = new Array("EN", "NU", "CM", "BR", "CE", "CI", "ES");
	
	for (x = 0; x < fieldAddres.length; x++) {
		stringField = "document.formulario." + fieldAddres[x] + addressNumber + ".value = ''";
		eval(stringField);
	}
	if (fieldCheck.checked == true) {
		for (x = 0; x < fieldAddres.length; x++) {
			stringField = "document.formulario." + fieldAddres[x] + addressNumber + ".disabled = true";
			eval(stringField);
		}
	} else {
		for (x = 0; x < fieldAddres.length; x++) {
			stringField = "document.formulario." + fieldAddres[x] + addressNumber + ".disabled = false";
			eval(stringField);
		}
	}
}

function limpa_string(S) {
	// Deixa so' os digitos no numero
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";

	for (var i=0; i<S.length; i++) {
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0) {
			temp=temp+digito
                        }
	}

	return temp;
}

function valida_CNPJ(s) {
	var i;
	s = limpa_string(s);
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++) {
		d1 += c.charAt(11-i)*(2+(i % 8));
	}
        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 < 12; i++) {
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1) {
		return false;
	}
	return true;
}

function valida_CPF(s)	{
	var i;
	s = limpa_string(s);
	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;
}

function floatToDecimal(campoValor) {
	valor = campoValor.toString();
	valor = valor.replace(".", ",");
	casasDecimais = valor.indexOf(",");
	if (casasDecimais < 0) {
		valor += ",00";
		casasDecimais = valor.indexOf(",");
	} else if (valor.length <= (casasDecimais + 3)) {
		valor += "0";
		casasDecimais = valor.indexOf(",");
	}
	valor = valor.substr(0, (casasDecimais + 3));
	
	return valor;
}

function decimalToFloat(campoValor) {
	valor = campoValor.replace(".", "");
	valor = valor.replace(",", ".");
	valor = parseFloat(valor);
	
	return valor;
}

function calculaTotalItem(quantidade, valor, total) {
	campoQuantidade = eval("document." + quantidade + ".value");
	campoQuantidade = parseInt(campoQuantidade);
	
	campoValor = eval("document." + valor + ".value");
	campoValor = decimalToFloat(campoValor);
	
	if (campoValor > 0) {
		totalItem = (campoValor * campoQuantidade);
		totalItem = floatToDecimal(totalItem);
		
		stringTotal = "document." + total + ".value = '" + totalItem + "'";
	} else {
		stringTotal = "document." + total + ".value = '0'";
	}
	eval(stringTotal);
}

function calculaTotal(nitens, total) {
	campoNItens = eval("document." + nitens + ".value");
	campoNItens = parseInt(campoNItens);
	campoTotal = 0;
	
	for (x = 0; x < campoNItens; x++) {
		campoValor = eval("document.pedido.VLRTOTAL" + x + ".value");
		campoValor = decimalToFloat(campoValor);
		campoTotal = (campoTotal + campoValor);
	}
	campoTotal = floatToDecimal(campoTotal);
	
	stringTotal = "document." + total + ".value = '" + campoTotal + "'";
	eval(stringTotal);
}

function validaValorRow(fieldValor, fieldRevenda, trRowAtual) {
	nItens = document.getElementById("NITENS").value;
	rowColor = "rowColor1";
	
	for (x = 0; x < nItens; x++) {
		valorSugestao = document.getElementById("VLR" + x).value;
		valorSugestao = decimalToFloat(valorSugestao);
		document.getElementById("VLR" + x).value = floatToDecimal(valorSugestao);
		
		valorRevenda = document.getElementById("VLRMIN" + x).value;
		campoRevenda = valorRevenda;
		valorRevenda = decimalToFloat(valorRevenda);
		
		trRow = document.getElementById("rowTRa" + x);
		if (rowColor == "rowColor1") { rowColor = "rowColor2"; } else { rowColor = "rowColor1"; }
		
		if (("rowTRa" + x) == trRowAtual) {
			if (isNaN(valorSugestao) || (parseFloat(valorSugestao) < parseFloat(valorRevenda))) {
				alert("O valor sugerido não pode ser menor que o valor permitido para a revenda (R$ " + campoRevenda + ").");
				document.getElementById(fieldValor).value = campoRevenda;
			}
			trRow.setAttribute("class", rowColor);
			trRow.setAttribute("className", rowColor);
			
			trRowb = document.getElementById("rowTRb" + x);
			trRowb.setAttribute("class", rowColor);
			trRowb.setAttribute("className", rowColor);
		}
	}
}

function validaValor(valorMaior, valorMinimo) {
	submitForm = true;
	valorSugestao = eval("document." + valorMaior + ".value");
	valorSugestao = decimalToFloat(valorSugestao);
	
	valorRevenda = eval("document." + valorMinimo + ".value");
	campoRevenda = valorRevenda;
	// usado para eliminar arredondamentos na comparação e eliminar casas decimais desnecessárias
	valorRevenda = decimalToFloat(valorRevenda);
	valorFinal = valorSugestao - valorRevenda;
	
	if (valorFinal < 0) {
		alert("O valor sugerido não pode ser menor que o valor permitido para a revenda (R$ " + campoRevenda + ").");
		eval("document." + valorMaior + ".value = '" + campoRevenda + "'");
		submitForm = false;
	}
	
	document.MM_returnValue = submitForm;
}

function validaTotais(valorMaiorFinal, valorMinimoFinal, qtdItens) {
	submitForm = true;
	nItens = eval("document." + qtdItens + ".value");
	
	for (x = 0; x < nItens; x++) {
		valorSugestaoFinal = eval("document." + valorMaiorFinal + x + ".value");
		valorSugestaoFinal = decimalToFloat(valorSugestaoFinal);
		
		valorRevendaFinal = eval("document." + valorMinimoFinal + x + ".value");
		// usado para eliminar arredondamentos na comparação e eliminar casas decimais desnecessárias
		valorRevendaFinal = decimalToFloat(valorRevendaFinal);
		valorFinal = valorSugestaoFinal - valorRevendaFinal;
		
		if (valorFinal < 0) {
			alert("Não foi possível atualizar a lista de itens do pedido.\nVerifique se os campos de sugestão de preço\nestão corretamente preenchidos.");
			submitForm = false;
		}
	}
	
	document.MM_returnValue = submitForm;
}

function verificaCampoPreenchido(campo,botao) {
	if (campo.value != "") {
		campo.style.background = "#FFFFFF";
		if (botao != "") {
			document.getElementById(botao).style.display = "";
		}
	} else {
		campo.style.background = "#FFC5C5";
		if (botao != "") {
			document.getElementById(botao).style.display = "none";
		}
	}
}

function showHideFaturar(mShow) {
	if (mShow == 1) {
		if (document.getElementById("faturarCliente")) {
			document.getElementById("faturarCliente").style.display = "";
		}
		if (document.getElementById("faturarEmpresa")) {
			document.getElementById("faturarEmpresa").style.display = "";
		}
		if (document.getElementById("cotacao")) {
			document.getElementById("cotacao").style.display = "";
		}
		if (document.getElementById("continuar")) {
			document.getElementById("continuar").style.display = "";
		}
	} else {
		if (document.getElementById("faturarCliente")) {
			document.getElementById("faturarCliente").style.display = "none";
		}
		if (document.getElementById("faturarEmpresa")) {
			document.getElementById("faturarEmpresa").style.display = "none";
		}
		if (document.getElementById("cotacao")) {
			document.getElementById("cotacao").style.display = "none";
		}
		if (document.getElementById("continuar")) {
			document.getElementById("continuar").style.display = "none";
		}
	}
}

function calculaComissao(precoField, qtdField, markup, bmkcCliente, bmkRevenda, promocao, comissaoField) {
	preco = eval("document." + precoField + ".value");
	preco = decimalToFloat(preco);
	
	qtd = eval("document." + qtdField + ".value");
	
	bmk = preco / markup;
	if (promocao == 1) {
		comissao = bmk - bmkRevenda;
	} else {
		comissao = bmk - bmkcCliente;
	}
	
	if (qtd > 0) {
		comissao = comissao * qtd;
	}
	
	if (comissao <= 0) {
		comissaoRound = floatToDecimal(0);
	} else {
		comissaoRound = floatToDecimal(comissao);
	}
	
	stringComissao = "document." + comissaoField + ".value = '" + comissaoRound + "'";
	eval(stringComissao);
}

function submitComissoes(type) {
	document.getElementById("action").value = type;
	document.formulario.submit();
}

function returnOpenerFields(nRow, field, factor) {
	valor = window.opener.document.getElementById(field + nRow).value;
	if (factor) {
		qtde = window.opener.document.getElementById("QTD" + nRow).value;
		valor = decimalToFloat(valor);
		valor = valor * qtde;
		valor =  floatToDecimal(valor);
	}
	
	document.write(valor);
}

function returnOpenerFieldsALL(nItens, field, factor) {
	valorTotal = 0;
	for (x = 0; x < nItens; x++) {
		valor = window.opener.document.getElementById(field + x).value;
		valor = decimalToFloat(valor);
		if (factor) {
			qtde = window.opener.document.getElementById("QTD" + x).value;
			valor = valor * qtde;
		}
		valorTotal += valor;
	}
	valorTotal =  floatToDecimal(valorTotal);
	
	document.write(valorTotal);
}

function regularField(fieldObj) {
	if (!fieldObj) return false;
	var Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var stringField = fieldObj.value;
	
	for (var i = 0; i < stringField.length; i++) {
		if (Chars.indexOf(stringField.charAt(i)) == -1) {
			fieldObj.value = "";
			return false;
		}
	}
	
	return true;
}

function validateRegularField() {
	fields = ['Tipo','Série','Número'];
	var Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	
	for (var x = 0; x < fields.length; x++) {
		stringField = eval("document.formulario." + fields[x] + ".value");
		for (var i = 0; i < stringField.length; i++) {
			if (Chars.indexOf(stringField.charAt(i)) == -1) {
				eval("document.formulario." + fields[x] + ".value = ''");
				alert('Caracter inválido!');
				document.MM_returnValue = true;
			}
		}
	}
	
	//document.MM_returnValue = false;
}

function validaComissao(valor1) {
	casasDecimais = valor1.indexOf(".");
	if (casasDecimais < 0) {
		valor1 = decimalToFloat(valor1);
	}
	valor2 = document.formulario.VLR.value;
	
	if (valor1 != valor2) {
		alert("O valor da NF não confere com o valor de pagamento!\nRetorne para o relatório e confira o valor!");
		
		document.formulario.Comissão.value = "";
		document.formulario.Comissão.focus();
	}
}
//-->
