//ValidaFormAllElements(<NOME DO FORM>,"<INDICE DO OBJETO>,<INDICE DO OBJETO>,<INDICE DO OBJETO>")
//ValidaFormAllElements(Form1,"3,5,8")
function ValidaFormAllElements(form,strVectorNaoBloquear)
{	
	vectorArray = strVectorNaoBloquear.split(',');
	for(i=0;i<form.elements.length;i++){	
		verificaSeExiste = false;
		for(j=0;j<vectorArray.length;j++){
			//alert(i+' - '+form.elements[i].name);		
			//alert(j+' (j) - '+form.elements[j].name);
			//alert('i='+i+' - vectorArray[J]='+vectorArray[j]);
			if(form.elements[i].name==vectorArray[j]){ verificaSeExiste = true; };
		}
		//alert(form.elements[i].name + " - " + form.elements[i].value + " - " + verificaSeExiste);
		if((form.elements[i].value=='')&&(verificaSeExiste==false)){
			alert('Preencher todos os campos obrigatórios!');
			form.elements[i].focus();
			return false;
		}
		if(((form.elements[i].type=="radio")||(form.elements[i].type=="checkbox"))&&(verificaSeExiste==false)){
			var radiogroup = form.elements[form.elements[i].name];
			var itemchecked = false;
			for(r=0; r<radiogroup.length; r++) {
				if(radiogroup[r].checked) {
					itemchecked = true;
					break;
				}
			}
			if(!itemchecked) {
				alert('Preencher todos os campos obrigatórios! ' + form.elements[i].name + " - " + form.elements[i].value + " - " + verificaSeExiste);
				form.elements[i].focus();
				return false;
			}
		}
	}
	return true;
}

function fcSubmit(vForm,vMethod,vCaminho){
	vForm.method = vMethod;
	vForm.action = vCaminho;
	vForm.submit();
}

function SoNumeros(event) {
	Tecla = event.which; 
	if (Tecla == null)
		Tecla = event.keyCode; 
		//alert(Tecla);
	if ((Tecla >= 48)&&(Tecla <= 57))
		return true;
	if ((Tecla == 8)||(Tecla == 13))
		return true;
	if ((Tecla == 46)||(Tecla == 16))
		return true;
	if ((Tecla == 35)||(Tecla == 36))
		return true;
	if ((Tecla == 0)||(Tecla == 9))
		return true;
	
return false;
}

function Ajusta_Data(input, evnt){  
//Ajusta máscara de Data e só permite digitação de números  
 if (input.value.length == 2 || input.value.length == 5){  
	/*
	if(clientNavigator == "IE"){  
		 input.value += "/";  
	 }else{  
		 if(evnt.keyCode == 0){  
	*/
			 input.value += "/";  
	/*
		 }  
	 }
	 */
 }   
//Chama a função SoNumeros para só permitir a digitação de números 
 return SoNumeros(evnt);  
}  

//---------------------------------------------------
// AJAX DAS COMBOS DE ESTADOS E CIDADES DO BRASIL
//---------------------------------------------------
var xmlHttp
/* Essa funo manda a ID selecionada do combo de estados para a pgina que ir filtrar as cidades */
function MandaID(str,source)
{
xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Este browser no suporta HTTP Request")
		return
	}
document.getElementById('exibe_cidade').innerHTML = "Aguarde Carregando...";
var url=source // o arquivo que ir executar a SQL das cidades
url=url+"?id_estado="+str // recebe o ID do estado para filtrar as cidade
url=url+"&sidjs="+Math.random()
//window.open(url);
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

/* Essa funo ir exibir o resultado na DIV */
function stateChanged()
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("exibe_cidade").innerHTML=xmlHttp.responseText
	}
}

/* Instancia */
function GetXmlHttpObject()
{
var objXMLHttp=null

	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
return objXMLHttp
}
//---------------------------------------------------
// FIM DE AJAX DAS COMBOS DE ESTADOS E CIDADES DO BRASIL
//---------------------------------------------------

function FormataCPF(Campo, teclapres)
{
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8)
		{
		if (tam > 3 && tam < 7)
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if (tam >= 7 && tam <10)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		if (tam >= 10 && tam < 12)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
		}
	
}

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);
		}

}

//mascara de formatação para preenchimento de CEP, coloca o ponto e o traço automaticamente.
function FormataCEP(Campo, teclapres)
{
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");
	
	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8)
		{
		if (tam > 2 && tam < 5)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam > 5 && tam < 8)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '-' + vr.substr(5, tam);
		}
		
}

 function Verifica_Email(email, obrigatorio){  
 //Se o parâmetro obrigatório for igual à zero, significa que elepode estar vazio, caso contrário, não  
     var email = document.getElementById(email);  
     if((obrigatorio == 1) || (obrigatorio == 0 && email.value != "")){  
         if(!email.value.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+)/gi)){  
             alert("Informe um e-mail válido");  
             email.focus();  
             return false  
         }  
     }  
 }  

function validaCPF_Formatado(campo,CPF) {
	while (CPF.indexOf('.') !=  -1) {CPF = CPF.replace('.','');}	
	while (CPF.indexOf('-') !=  -1) {CPF = CPF.replace('-','');}	
	return ValidaCPF(campo,CPF);
}

function ValidaCPF(campo,CPF){
 var RecebeCPF, soma, resultado1, resultado2
 var Numero = new Array(11);
 var Repeticao, Digito
 RecebeCPF = CPF;
 Campo = campo;
 
 if(RecebeCPF.length != 11 && RecebeCPF.length > 0){
	alert("É obrigatório o CPF com 11 dígitos");
	document.formInsereClientes.cpf.value='';
	document.formInsereClientes.cpf.focus();
	return false;
 }else if(RecebeCPF.length == 0){
	return false;
 }
 
 Digito = RecebeCPF.substr(0,1);
 for(i=1; i < 11; i++){
	 if(RecebeCPF.substr(i,1) == Digito){
		Repeticao = true;
	 }else{
		Repeticao = false;
		break;
	 }	 
 }
 
 if(Repeticao){
	alert("CPF Inválido!");
	document.formInsereClientes.cpf.value='';
	document.formInsereClientes.cpf.focus();
	return false;
 }
 
 Numero[0] = parseInt(RecebeCPF.substr(0,1));
 Numero[1] = parseInt(RecebeCPF.substr(1,1));
 Numero[2] = parseInt(RecebeCPF.substr(2,1));
 Numero[3] = parseInt(RecebeCPF.substr(3,1));
 Numero[4] = parseInt(RecebeCPF.substr(4,1));
 Numero[5] = parseInt(RecebeCPF.substr(5,1));
 Numero[6] = parseInt(RecebeCPF.substr(6,1));
 Numero[7] = parseInt(RecebeCPF.substr(7,1));
 Numero[8] = parseInt(RecebeCPF.substr(8,1));
 Numero[9] = parseInt(RecebeCPF.substr(9,1));
 Numero[10] = parseInt(RecebeCPF.substr(10,1));
 
 soma = (10 * Numero[0]) + (9 * Numero[1]) + (8 * Numero[2]) + (7 * Numero[3]) + (6 * Numero[4]) + (5 * Numero[5]) + (4 * Numero[6]) + (3 * Numero[7]) + (2 * Numero[8])
 
 soma = soma -(11 * (parseInt(soma / 11)))
 
 if( soma == 0 || soma == 1)
	resultado1 = 0;
 else
	resultado1 = 11 - soma;
 
 if(resultado1 == Numero[9]){
	 soma = (Numero[0] * 11) + (Numero[1] * 10) + (Numero[2] * 9) + (Numero[3] * 8) + (Numero[4] * 7) + (Numero[5] * 6) + (Numero[6] * 5) + (Numero[7] * 4) + (Numero[8] * 3) + (Numero[9] * 2)
	 
	 soma = soma -(11 * (parseInt(soma / 11)))
	 
	 if( soma == 0 || soma == 1)
		resultado2 = 0;
	 else
		resultado2 = 11 - soma;
	 
	 if( resultado2 != Numero[10]){
	 alert("CPF Inválido!");
	 document.formInsereClientes.cpf.value='';
	 document.formInsereClientes.cpf.focus();
	 return false;
	 }
 }else{
	 alert("CPF Inválido!");
	 document.formInsereClientes.cpf.value='';
	 document.formInsereClientes.cpf.focus();
	 return false;
 }
 //return true;
}//end function

function validaCNPJ(CNPJ) {
	erro = new String;
	//if (CNPJ.length < 18) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
	//if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
	//if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
	//}
	//substituir os caracteres que nao sao numeros
	if(document.layers && parseInt(navigator.appVersion) == 4){
	x = CNPJ.substring(0,2);
	x += CNPJ.substring(3,6);
	x += CNPJ.substring(7,10);
	x += CNPJ.substring(11,15);
	x += CNPJ.substring(16,18);
	CNPJ = x;
	} else {
	CNPJ = CNPJ.replace(".","");
	CNPJ = CNPJ.replace(".","");
	CNPJ = CNPJ.replace("-","");
	CNPJ = CNPJ.replace("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n";
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
	a[i] = CNPJ.charAt(i);
	b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	b = 0;
	for (y=0; y<13; y++) {
	b += (a[y] * c[y]);
	}
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
	erro +="CNPJ Inválido!";
	}
	if (erro.length > 0){
		alert(erro);
		document.formInsereClientes.cnpj.value='';
		document.formInsereClientes.cnpj.focus();
		return false;
	}
	return true;
}

