// ################### VALIDADORES DE FORMULARIO ###########
// ####### Funcao valida email
function valida_email(vemail){
	if(vemail.indexOf('@') == -1 || vemail.indexOf('.') == -1 || vemail.length < 5){
		return false
	}else{
		return true
	}
}
// ####### Funcao de tabulacao
function tab(vcampo,vqtd,vprox){
//	alert(vcampo.value.length);
	if(vcampo.value.length>=vqtd){
		vprox.focus();
	}
}
function somente_numero(vcampo){
	vcampo.value=vcampo.value.replace(/[^\d]*/gi,'');
}
function numero_e_virgula(vcampo){
	vcampo.value=vcampo.value.replace(/[^,^.^\d]*/gi,'');
}
// Caso seja numérico retorna true, caso não seja retorna false ###############################
function enumero(pStr){
	var num_temp = pStr;
	while(num_temp.indexOf(".")!=-1 || num_temp.indexOf(",")!=-1){
		num_temp = num_temp.replace(",","");
		num_temp = num_temp.replace(".","");
	}

	if(isNaN(num_temp) || num_temp.length == 0){
		return false;
		// nao e digito
	}
	else{
		return true;
		// e digito
	}
}
function enviar_ordem(vform, vordem){
		vform.ordem.value=vordem;
		vform.submit();
}
function enviar_pagina(vform, vpagina){
		vform.pagina.value=vpagina;
		vform.submit();
}
function verifica_endereco(vform){
	if(vform.cep_01.value.length!=5 && enumero(vform.cep_01.value)==false){
		alert("O CEP não foi preenchido corretamente");
		vform.cep_01.focus();
		return false;
	}
	if(vform.cep_02.value.length!=3 && enumero(vform.cep_02.value)==false){
		alert("O CEP não foi preenchido corretamente");
		vform.cep_02.focus();
		return false;
	}
	if(vform.endereco.value==''){
		alert("Digite o endereço de entrega do produto!");
		vform.endereco.focus();
		return false;
	}
	if(vform.numero.value==''){
		alert("Digite o número do local de entrega!");
		vform.numero.focus();
		return false;
	}
	if(vform.bairro.value==''){
		alert("Digite o bairro!");
		vform.bairro.focus();
		return false;
	}
	if(vform.uf.value=='UF'){
		alert("Selecione o Estado!");
		vform.uf.focus();
		return false;
	}
	if(vform.cidade.value==''){
		alert("Digite a cidade!");
		vform.cidade.focus();
		return false;
	}
}
// Formulario para enviar o produto
// PAGINAS: usr_cor_enviar.asp
function validaF_correio_enviar(vform){
	if(vform.alterar.checked==true){
		if(vform.tel_ddd.value.length!=2){
			alert("Digite o telefone corretamente!");
			vform.tel_ddd.focus();
			return false;
		}
		if(vform.tel_01.value.length!=4){
			alert("Digite o telefone corretamente!");
			vform.tel_01.focus();
			return false;
		}
		if(vform.tel_02.value.length!=4){
			alert("Digite o telefone corretamente!");
			vform.tel_02.focus();
			return false;
		}
		if(vform.endereco.value==''){
			alert("Digite o endereço de entrega do produto!");
			vform.endereco.focus();
			return false;
		}
		if(vform.numero.value==''){
			alert("Digite o número do local de entrega!");
			vform.numero.focus();
			return false;
		}
		if(vform.bairro.value==''){
			alert("Digite o bairro!");
			vform.bairro.focus();
			return false;
		}
		if(vform.cidade.value==''){
			alert("Digite a cidade!");
			vform.cidade.focus();
			return false;
		}
		if(vform.cep01.value.length!=5){
			alert("Digite o cep!");
			vform.cep01.focus();
			return false;
		}
		if(vform.cep02.value.length!=3){
			alert("Digite o cep!");
			vform.cep02.focus();
			return false;
		}
	}
	if(vform.problema.value==''){
		alert("Digete o problema do produto!");
		vform.problema.focus();
		return false;
	}
}

function form_busca(vform){
	if(vform.busca.value==''){
		alert("Para fazer uma pesquisa é necessário que digite algum valor");
		return false;
	}
	if(vform.campo.value=='..'){
		alert("Selecione qual campo busca");
		vform.campo.focus();
		return false;
	}
}
function form_enviar(vform,vcampo){
	if(vcampo.value!='..'){
		vform.submit();
	}
}
function atualiza_cep(vtipo, vcampo01, vcampo02, vform){
	var vcep = vcampo01.value+vcampo02.value;
	if(vcep.length==8){
		var oHTTPRequest = createXMLHTTP();
		var vindicador = document.getElementById('id_indicador');
		if(vtipo=='admin'){
			oHTTPRequest.open("post","../funcoes/funcoes_calcular.asp",true);
			vindicador.innerHTML = "<img src='../imagem/indicator.gif' />";
		}
		else{
			oHTTPRequest.open("post","funcoes/funcoes_calcular.asp",true);
			vindicador.innerHTML = "<img src='imagem/indicator.gif' />";
		}
		oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oHTTPRequest.onreadystatechange=function(){
			if (oHTTPRequest.readyState==4){
				var vendereco = document.getElementById('id_endereco');
				vendereco.innerHTML = oHTTPRequest.responseText;
				vindicador.innerHTML = '';
			}
		}
		oHTTPRequest.send('tipo=calcular_cep&cep='+vcep+'&form='+vform);
	}
	else{
		alert("Cep incorreto!");
	}
}
function atualiza_cepxx(vtipo, vcampo01, vcampo02, vresultado){
	var vcep01 = vcampo01.value;
	var vcep02 = vcampo02.value;
//id_tabela_usr
	if(vcep01.length==5 && vcep02.length==3 ){
		if(vtipo=='admin'){
			document.all.id_indicador.innerHTML = "<img src='../imagem/indicator.gif' />";
		}
		else{
			document.all.id_indicador.innerHTML = "<img src='imagem/indicator.gif' />";
		}
		var oHTTPRequest = createXMLHTTP(); 
		oHTTPRequest.open("post","http://www.nostel.com.br/funcoes/funcoes_calcular.asp",true);
		oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oHTTPRequest.onreadystatechange=function(){
			if (oHTTPRequest.readyState==4){
				document.all.id_endereco.innerHTML = oHTTPRequest.responseText;
				document.all.id_indicador.innerHTML = "";
			}
		}
		oHTTPRequest.send("cep=" + vcep01 + vcep02 + "&tipo=calcular_cep");
		/*---------------------------------------------------------------*/
	}
	else{
		alert("Cep inválido.");
	}
}
function atualiza_pac(vform){
	var vcep = vform.cep.value;
	var vpeso = vform.peso.value;
	var volume = vform.volume.value;
//id_tabela_usr
	if(vcep.length==8){
		document.all.id_indicador_pac.innerHTML = "<img src='../imagem/indicator.gif' />";
		var oHTTPRequest = createXMLHTTP(); 
		oHTTPRequest.open("post","http://www.midtec.com.br/funcoes/funcoes_calcular.asp",true);
		oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oHTTPRequest.onreadystatechange=function(){
			if (oHTTPRequest.readyState==4){
				document.all.id_indicador_pac.innerHTML = oHTTPRequest.responseText;
			}
		}
		oHTTPRequest.send("cep="+vcep+"&peso="+vpeso+"&volume="+volume+"&tipo=calcular_pac");
		/*---------------------------------------------------------------*/
	}
	else{
		alert("Cep inválido.");
	}
}

function clt_form_enviar(vform){
	if(vform.representante.value=='..'){
		alert("Selecione o representante");
		vform.representante.focus();
		return false;
	}
	if(vform.fantasia.value==''){
		alert("Digite o nome fantasia");
		vform.fantasia.focus();
		return false;
	}
	if(vform.razaosocial.value==''){
		alert("Digite o nome ou a razão social corretamente");
		vform.razaosocial.focus();
		return false;
	}
	if(vform.fisico_juridico.value=='F'){
		if(vform.cpf01.value.length!=3){
			alert("cpf não foi preenchido corretamente");
			vform.cpf01.focus();
			return false;
		}
		if(vform.cpf02.value.length!=3){
			alert("cpf não foi preenchido corretamente");
			vform.cpf02.focus();
			return false;
		}
		if(vform.cpf03.value.length!=3){
			alert("cpf não foi preenchido corretamente");
			vform.cpf03.focus();
			return false;
		}
		if(vform.cpf04.value.length!=2){
			alert("cpf não foi preenchido corretamente");
			vform.cpf04.focus();
			return false;
		}
		vform.cpf.value = vform.cpf01.value+vform.cpf02.value+vform.cpf03.value+vform.cpf04.value;
		vform.cnpj.value ='';
		vform.inscricao.value ='';
	}else{
		if(vform.cnpj01.value.length!=2){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj01.focus();
			return false;
		}
		if(vform.cnpj02.value.length!=3){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj02.focus();
			return false;
		}
		if(vform.cnpj03.value.length!=3){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj03.focus();
			return false;
		}
		if(vform.cnpj04.value.length!=4){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj04.focus();
			return false;
		}
		if(vform.cnpj05.value.length!=2){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj05.focus();
			return false;
		}
		vform.cnpj.value = vform.cnpj01.value+vform.cnpj02.value+vform.cnpj03.value+vform.cnpj04.value+vform.cnpj05.value;
		vform.cpf.value = '';
	}
	if(vform.cep01.value!=''){
		if(vform.cep01.value.length!=5){
			alert("O CEP não foi preenchido corretamente");
			vform.cep01.focus();
			return false;
		}
		if(vform.cep02.value.length!=3){
			alert("O CEP não foi preenchido corretamente");
			vform.cep03.focus();
			return false;
		}
		vform.cep.value = vform.cep01.value+vform.cep02.value;
	}
	if(vform.tel01.value!=''){
		if(vform.telddd.value.length!=2){
			alert("O Telefone não foi preenchido corretamente");
			vform.telddd.focus();
			return false;
		}
		if(vform.tel01.value.length!=4){
			alert("O Telefone não foi preenchido corretamente");
			vform.tel01.focus();
			return false;
		}
		if(vform.tel02.value.length!=4){
			alert("O Telefone não foi preenchido corretamente");
			vform.tel02.focus();
			return false;
		}
		vform.tel.value = '('+vform.telddd.value+')'+vform.tel01.value+'-'+vform.tel02.value;
	}
	if(vform.fax01.value!=''){
		if(vform.faxddd.value.length!=2){
			alert("O Fax não foi preenchido corretamente");
			vform.faxddd.focus();
			return false;
		}
		if(vform.fax01.value.length!=4){
			alert("O Fax não foi preenchido corretamente");
			vform.fax01.focus();
			return false;
		}
		if(vform.fax02.value.length!=4){
			alert("O Fax não foi preenchido corretamente");
			vform.fax02.focus();
			return false;
		}
		vform.fax.value = '('+vform.faxddd.value+')'+vform.fax01.value+'-'+vform.fax02.value;
	}
}
//Verifica formulario cliente do representante
function rpt_clt_form_enviar(vform){
	if(vform.fantasia.value==''){
		alert("Digite o nome fantasia");
		vform.fantasia.focus();
		return false;
	}
	if(vform.razaosocial.value==''){
		alert("Digite o nome ou a razão social corretamente");
		vform.razaosocial.focus();
		return false;
	}
	if(vform.fisico_juridico.value=='F'){
		if(vform.cpf01.value.length!=3){
			alert("cpf não foi preenchido corretamente");
			vform.cpf01.focus();
			return false;
		}
		if(vform.cpf02.value.length!=3){
			alert("cpf não foi preenchido corretamente");
			vform.cpf02.focus();
			return false;
		}
		if(vform.cpf03.value.length!=3){
			alert("cpf não foi preenchido corretamente");
			vform.cpf03.focus();
			return false;
		}
		if(vform.cpf04.value.length!=2){
			alert("cpf não foi preenchido corretamente");
			vform.cpf04.focus();
			return false;
		}
		vform.cpf.value = vform.cpf01.value+vform.cpf02.value+vform.cpf03.value+vform.cpf04.value;
		vform.cnpj.value ='';
		vform.inscricao.value ='';
	}else{
		if(vform.cnpj01.value.length!=2){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj01.focus();
			return false;
		}
		if(vform.cnpj02.value.length!=3){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj02.focus();
			return false;
		}
		if(vform.cnpj03.value.length!=3){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj03.focus();
			return false;
		}
		if(vform.cnpj04.value.length!=4){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj04.focus();
			return false;
		}
		if(vform.cnpj05.value.length!=2){
			alert("CNPJ não foi preenchido corretamente");
			vform.cnpj05.focus();
			return false;
		}
		vform.cnpj.value = vform.cnpj01.value+vform.cnpj02.value+vform.cnpj03.value+vform.cnpj04.value+vform.cnpj05.value;
		vform.cpf.value = '';
	}
	if(verifica_endereco(vform)==false){
		return false;
	}
	if(vform.contato.value.length<4){
		alert("Digite o nome do contato");
		vform.contato.focus();
		return false;
	}
	if(vform.telefone.value.length<4){
		alert("Telefone incorreto");
		vform.telefone.focus();
		return false;
	}
	if(valida_email(vform.email.value)==false){
		alert("Email incorreto");
		vform.email.focus();
		return false;
	}
}

// Verifica se a data apresentada ou no uma data válida
function valida_data(day, month, year) {
    //IsDate(29, 2, 2005)
    //IsDate(29, 2, 2004)
    var date = new Date();
    var blnRet = false;
    var blnDay;
    var blnMonth;
    var blnYear;

    date.setFullYear(year, month -1, day);

    blnDay   = (date.getDate()  == day);
    blnMonth = (date.getMonth() == month -1);
    blnYear  = (date.getYear()  == year);

    if (blnDay && blnMonth && blnYear)
        blnRet = true;

    return blnRet;
}
//Funcao que conta o limite de caracteres
function textContador(vcampo, vcontador, vlimit) {
//	var txtarea    = document.getElementById(x);
//	var remtxtarea = document.getElementById(y);
	if (vcampo.value.length > vlimit) {
		//txtarea.value = txtarea.value.substring(0, limit);
		vcampo.value = vcampo.value.substr(0,vlimit)
		vcontador.value = vcampo.value.length;
		document.getElementById("campostatus").innerHTML = "<span style=\'background-color:#FF0000; color:#fff; padding: 0 5 0 5; font-weight:bold\'>Limite de caracteres excedido !</span>";			
	} else {
		//remtxtarea.value = limit - txtarea.value.length;
		vcontador.value = vcampo.value.length;
		document.getElementById("campostatus").innerHTML = "&nbsp;";
	}
}
function atualiza_cidade(vform){
//	criacao do objeto XMLHTTP do arquivo ajax.js
	var oHTTPRequest = createXMLHTTP(); 
	oHTTPRequest.open("post","funcoes/ajax.asp",true); //enviamos para a página que faz o select do que foi digitado e traz a lista preenchida.
// 	para solicitacoes utilizando o metodo post deve ser acrescentado 
// 	este cabecalho HTTP
	oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// 	a funcao abaixo e executada sempre que o estado do objeto muda (onreadystatechange)
	oHTTPRequest.onreadystatechange=function(){
// 		o valor 4 significa que o objeto ja completou a solicitacao
		if (oHTTPRequest.readyState==4){// abaixo o texto gerado no arquivo executa.asp e colocado no div
			var voutros = document.getElementById('cidade_id')
			voutros.innerHTML = oHTTPRequest.responseText;
//			cat.innerHTML = "teste"
		}
	}
	oHTTPRequest.send('tipo=atualiza_cidade&uid='+vform.uf.value);
}

function atualiza_cidade_admin(vform){
//	criacao do objeto XMLHTTP do arquivo ajax.js
	var oHTTPRequest = createXMLHTTP(); 
	oHTTPRequest.open("post","../funcoes/ajax.asp",true); //enviamos para a página que faz o select do que foi digitado e traz a lista preenchida.
// 	para solicitacoes utilizando o metodo post deve ser acrescentado 
// 	este cabecalho HTTP
	oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// 	a funcao abaixo e executada sempre que o estado do objeto muda (onreadystatechange)
	oHTTPRequest.onreadystatechange=function(){
// 		o valor 4 significa que o objeto ja completou a solicitacao
		if (oHTTPRequest.readyState==4){// abaixo o texto gerado no arquivo executa.asp e colocado no div
			var voutros = document.getElementById('cidade_id')
			voutros.innerHTML = oHTTPRequest.responseText;
//			cat.innerHTML = "teste"
		}
	}
	oHTTPRequest.send('tipo=atualiza_cidade&uid='+vform.uf.value);
}

function atualiza_cidade_usr(vform){
//	criacao do objeto XMLHTTP do arquivo ajax.js
	var oHTTPRequest = createXMLHTTP(); 
	oHTTPRequest.open("post","funcoes/ajax.asp",true); //enviamos para a página que faz o select do que foi digitado e traz a lista preenchida.
// 	para solicitacoes utilizando o metodo post deve ser acrescentado 
// 	este cabecalho HTTP
	oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// 	a funcao abaixo e executada sempre que o estado do objeto muda (onreadystatechange)
	oHTTPRequest.onreadystatechange=function(){
// 		o valor 4 significa que o objeto ja completou a solicitacao
		if (oHTTPRequest.readyState==4){// abaixo o texto gerado no arquivo executa.asp e colocado no div
			var voutros = document.getElementById('cidade_id')
			voutros.innerHTML = oHTTPRequest.responseText;
//			cat.innerHTML = "teste"
		}
	}
	oHTTPRequest.send('tipo=atualiza_cidade&uid='+vform.uf.value);
}
