var isIE = (navigator.appName.indexOf('Internet Explorer') != -1);

function enviarLembrete() {
	var email = /^([a-z]+|\.|\_|[0-9]+)+\@([a-z]+|[0-9]+)(\.([a-z]+|[0-9]+))+$/;
	var doc = document.formulario;
	if (doc.Email.value.length == 0) {
		alert("- E-mail não informado");
		doc.Email.focus();
	} else if (!email.test(doc.Email.value)) {
		erro += "- E-mai inválido\r\n";
		doc.Email.focus();
	} else {
		doc.submit();
	}
}
function fazerLogon() {
	var doc = document.formulario;
	var erro = "";
	if (doc.login.value.length == 0) {
		erro += "- Login não informado\r\n";
	}
	if (doc.senha.value.length == 0) {
		erro += "- Senha não informada";
	}
	if (erro != "")	{
		alert(erro);
	} else {
		doc.submit();
	}
}

function buscaPecas() {
	var doc = document.formulario;
	if (doc.codigo.value.length == 0) {
		alert("- Código não informado");
		doc.codigo.focus();
	} else {
		doc.submit();
	}
}

function excluiPedidoSalvo(pedido) {
	var doc = document.formulario;
	doc.pedidosalvo.value = pedido;
	doc.submit();
}

function pedido(cod,qtde,preco,nome,moeda) {
	var doc = document.formulario;
	doc.pedido.value = cod;
	doc.qtde.value = qtde;
	doc.preco.value = preco;
	doc.nome.value = nome;
	doc.moeda.value = moeda;
	doc.submit();
}

function alteraPedido(cod,item,preco,nome) {
	var qtde = eval("document.formulario.qtde" + item + ".value");
	var doc = document.formulario;
	doc.pedido.value = cod;
	doc.qtde.value = qtde;
	doc.preco.value = preco;
	doc.nome.value = nome;
	doc.submit();
}

function efetuaCadastro() {
	var email = /^([a-z]+|\.|\_|[0-9]+)+\@([a-z]+|[0-9]+)(\.([a-z]+|[0-9]+))+$/;
	var cidade = /^[a-z]+\/[a-z][a-z]$/;
	var doc = document.cadastro;
	var erro = "";
	if (doc.nome.value.length == 0) {
		erro += "- Nome não informado\r\n";
	}
	if (doc.cpf.value.length != 11) {
		erro += "- CPF não informado\r\n";
	} else if (!validaCPF(doc.cpf.value)){
		erro += "- CPF inválido\r\n";
	}
	if (doc.email.value.length == 0) {
		erro += "- E-mail não informado\r\n";
	} else if (!email.test(doc.email.value)) {
		erro += "- E-mai inválido\r\n";
	}
	if (doc.empresa.value.length == 0) {
		erro += "- Empresa não informada\r\n";
	}
	if (doc.cidade.value.length == 0) {
		erro += "- Cidade/UF não informada\r\n";
	} else if (!cidade.test(doc.cidade.value)) {
		erro += "- Cidade/UF inválido\r\n";
	}
	if (doc.login.value.length == 0) {
		erro += "- Login não informado\r\n";
	} else if (doc.login.value.length < 6) {
		erro += "- Login deve ter no mínimo 6 caracteres\r\n";
	}
	if (doc.senha.value.length == 0) {
		erro += "- Senha não informada\r\n";
	} else if (doc.senha.value.length < 6) {
		erro += "- Senha deve ter no mínimo 6 caracteres\r\n";
	} else if (doc.senha.value != doc.senha2.value)	{
		erro += "- Senha e Confirmação diferentes";
	}

	if (erro != "")	{
		alert(erro);
	} else {
		doc.submit();
	}
}


function fazerContato() {
	var email = /^([a-z]+|\.|\_)+\@[a-z]+(\.[a-z]+)+$/;
	var doc = document.contato;
	var erro = "";
	if (doc.Nome.value.length == 0) {
		erro += "- Nome não informado\r\n";
	}
	if (doc.Email.value.length == 0) {
		erro += "- E-mail não informado\r\n";
	} else if (!email.test(doc.Email.value)) {
		erro += "- E-mai inválido\r\n";
	}
	if (doc.Assunto.value.length == 0) {
		erro += "- Assunto não informado\r\n";
	}
	if (doc.Mensagem.value.length == 0) {
		erro += "- Mensagem não informada\r\n";
	}

	if (erro != "")	{
		alert(erro);
	} else {
		doc.submit();
	}
}

function validaCPF(s){ 

	var i; 
	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; 

} 


// Função utilizada para validação de CPF e CNPJ
function modulo(str) {

   	soma=0;
   	ind=2;
   	for(pos=str.length-1;pos>-1;pos=pos-1) {
   		soma = soma + (parseInt(str.charAt(pos)) * ind);
   		ind++;
   		if(str.length>11) {
   			if(ind>9) ind=2;
   		}
	}
   	resto = soma - (Math.floor(soma / 11) * 11);
   	if(resto < 2) {
    	return 0
   	}
   	else {
   		return 11 - resto
   	}
}

// Função para validação do CPF
function IsCPF(valor) {

	primeiro=valor.substr(1,1);
	falso=true;
	size=valor.length;
	if (size!=11){
		return false;
	}
	size--;
	for (i=2; i<size-1; ++i){
		proximo=(valor.substr(i,1));
		if (primeiro!=proximo) {
			falso=false
		}
	}
	if (falso){
		return false;
	}
   	if(modulo(valor.substring(0,valor.length - 2)) + "" + modulo(valor.substring(0,valor.length - 1)) != valor.substring(valor.length - 2,valor.length)) {
   		return false;
   	}
   	return true
}


// Função para validação do cnpj
function IsCNPJ(valor) {

	primeiro=valor.substr(1,1);
	falso=true;
	size=valor.length;
	if (size!=14){
		return false;
	}
	size--;
	for (i=2; i<size-1; ++i){
		proximo=(valor.substr(i,1));
		if (primeiro!=proximo) {
			falso=false
		}
	}
	
	if (falso){
		return;
	}
	
   	if(modulo(valor.substring(0,valor.length - 2)) + "" + modulo(valor.substring(0,valor.length - 1)) !=valor.substring(valor.length - 2,valor.length)) {
   		return false;
   	}
   	return true
}

function revendedor(acao, id) {
	var doc = document.revendedor;	
	
	doc.acao.value = acao;
	doc.id.value = id;
	
	doc.submit();
}

function usuario(acao, id) {
	var doc = document.usuario;	
	
	doc.acao.value = acao;
	doc.id.value = id;
	
	doc.submit();
}

function email(acao, id) {
	var doc = document.email;	
	
	doc.acao.value = acao;
	doc.id.value = id;
	
	doc.submit();
}

function pecausada(acao, id) {
	var doc = document.pecausada;	
	
	doc.acao.value = acao;
	doc.id.value = id;
	
	doc.submit();
}

function news(acao, id) {
	var doc = document.news;	
	
	doc.acao.value = acao;
	doc.id.value = id;
	
	doc.submit();
}

function SoNumero(e) {
	var keyNumber = (isIE) ? event.keyCode : e.which;
	//alert(keyNumber);
	if (((keyNumber<48)||(keyNumber>57)) && (keyNumber!=13) && (keyNumber!="0") && (keyNumber!=8)) {
		if (isIE) event.keyCode=0;
		return false;
	}
}
function FormataValor(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.forms[0][campo].value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	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 ){
		if ( tam <= 2 ){ 
	 		document.forms[0][campo].value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		document.forms[0][campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		document.forms[0][campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		document.forms[0][campo].value = 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) ){
	 		document.forms[0][campo].value = 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) ){
	 		document.forms[0][campo].value = 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 ) ;}
	}
}
