function mostrar(id){
	document.getElementById(id).style.display = "";
}

function esconder(id){
	document.getElementById(id).style.display = "none";
}

function verificaFormulario(formulario){

	var msgCabecalho = "ATENÇÃO:\n============================\nPreencha o(s) seguinte(s) campo(s):";
	var f = formulario;
	var msg = "";

	if (!verificaRadio(f.tipo))
		msg += "\n- Selecione um tipo de pessoa";

	if (f.rs.value == "" && f.tipo[1].checked)
		msg += "\n- Digite a razão social";

	if (!validaCNPJ(f.cnpj) && f.tipo[1].checked)
		msg += "\n- Digite o CNPJ correto";

	if (f.responsavel.value == "")
		msg += "\n- Digite o nome do responsável";
		
	if (f.rg.value == "")
		msg += "\n- Digite o RG";

	if (!validaCPF(f.cpf))
		msg += "\n- Digite o CPF correto";
		
	if (f.cep.value == "")
		msg += "\n- Digite o CEP";
		
	if (f.end.value == "")
		msg += "\n- Digite o endereço";
		
	if (f.numero.value == "")
		msg += "\n- Digite o número do endereço";
		
	if (f.cidade.value == "")
		msg += "\n- Digite a cidade";
		
	if (f.uf.options[f.uf.selectedIndex].value==-1)
		msg += "\n- Selecione um estado";
		
	if (f.tddd.value == "")
		msg += "\n- Digite o DDD";
		
	if (f.tel.value == "")
		msg += "\n- Digite o telefone";
		
	if (f.email.value == "")
		msg += "\n- Digite o email";
		
	if (msg == ""){
		f.submit();
		return false;

	}else{
		alert (msgCabecalho + msg);
		return false;
	}
}	

function verificaRadio(str){  
	var testa;  
	var itemchecked = false;  
	for(var i = 0 ; i < str.length ; i++)  
	{  
	   testa = str[i]; 
	   if(testa.checked)  
	   {  
		 itemchecked = true; 
	   }  
	}          
	   if(itemchecked)  
	   {  
		 return true;  
	   }else  
	   {  
		return false;  
		}  
} 

function validaCNPJ(CNPJ) {
			 CNPJ = CNPJ.value;
			 erro = new String;
			 if (CNPJ.length < 14) erro += "É necessario preencher corretamente o número do CNPJ! \n\n";
			 var nonNumbers = /\D/;
			 if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \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 +="Dígito verificador com problema!";
			 }
			 if (erro.length > 0){
//							 alert(erro);
							 return false;
			 } else {
							 //alert("CNPJ valido!");
			 }
			 return true;
	}
	
	function validaCPF(cpf) {
				 cpf = cpf.value;
				 erro = new String;
				 if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n";
				 var nonNumbers = /\D/; //REGEXP
				 if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
				 if ( cpf == "00000000000" || 
							 cpf == "11111111111" || 
							cpf == "22222222222" || 
							cpf == "33333333333" || 
							cpf == "44444444444" || 
							cpf == "55555555555" || 
							cpf == "66666666666" || 
							cpf == "77777777777" || 
							cpf == "88888888888" || 
							cpf == "99999999999"){
								erro += "Numero de CPF invalido!"
			 }
			 var a = [];
			 var b = new Number;
		 var c = 11;
		 for (i=0; i<11; i++){
					 a[i] = cpf.charAt(i);
						 if (i < 9) b += (a[i] * --c);
		 }
		 if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		 b = 0;
		 c = 11;
		 for (y=0; y<10; y++) b += (a[y] * c--);
		 if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
		 if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
			 erro +="Digito verificador com problema!";
		 }
		 if (erro.length > 0){
//				 alert(erro);
				 return false;
		 }
		 return true;
}

function mostrar(id){
	document.getElementById(id).style.display = "";
}

function esconder(id){
	document.getElementById(id).style.display = "none";
}
	
function arrumaEmail(nome){
	document.getElementById('nomeSubDominio').innerHTML = nome;
}

function checa(id){
	document.getElementById(id).click();
}
	
function verificaSubDominio(nome){
	if(nome.length>2)
		MakeRequest('verificaSubDominio.php?nome='+nome,retornoSubDominio);
	else{
		document.getElementById('divRetornoSubDominio').innerHTML = '<img src="images/icones/no_p.gif">';
		document.getElementById('subDominioOk').value = 0;
	}
}
	
function MakeRequest(url, actionFunction) {

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Seu navegador não suporta objeto XMLHTTP! Tente permitir o uso de Javascript e Active X');
		return false;
	}

	http_request.onreadystatechange = actionFunction;		
	http_request.open('GET', url , true);
	http_request.send(null);
}

function retornoSubDominio(){
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			var xmldoc = http_request.responseXML;			
			var retorno = xmldoc.getElementsByTagName('retorno').item(0).firstChild.data;
			//alert ("error_node:"+error_node);
			switch (retorno){
			case '0':
				document.getElementById('divRetornoSubDominio').innerHTML = '<img src="images/icones/yes_p.gif">';
				document.getElementById('subDominioOk').value = 1;
			break;
			case '1':
				document.getElementById('divRetornoSubDominio').innerHTML = '<img src="images/icones/no_p.gif">';
				document.getElementById('subDominioOk').value = 0;
			break;
			default:
			alert('Ocorreu um problema com o banco de Dados.');
			break;
		}
	} else {
			alert('Ocorreu um problema inesperado');
		}
		
		}
}

function mudaForm(tipo,sessionID){
	form = document.getElementById('cad1');
	
	if(tipo=='contrato'){
		form.action="produtosContrato.php?"+sessionID;
		form.target="_blank";
		form.submit();
	}
	
	if(tipo=='formulario'){
		form.action="contratar04.php?"+sessionID;
		form.target="";
	}
}

function checkPonto(valor){ 
	valor.value = valor.value.replace('.','');
//	alert(valor.value);
}

function numerico(texto)

{

var expReg = /[^0-9]/gi;  

texto.value = texto.value.replace(expReg,"");

}



function mascara(texto, mascara){
	var expReg = /[^0-9]/gi;        // Expressão Regular para validação do campo
	var saida = new Array();        // Array de saida
	var str = texto.value;          // String para formatar
	str = str.replace(expReg,"");   // Limpando dados inválidos da String
	var Ttam = str.length-1;        // Tamanho da String
	var Mtam = mascara.length-1;    // Tamanho da Máscara
	do{                             // Formata texto caracter por caracter utilizando a mascara
		if (mascara.charAt(Mtam) == "#"){
			saida.push(str.charAt(Ttam));
			Ttam = Ttam-1;
			Mtam = Mtam-1;
		}else{
			saida.push(mascara.charAt(Mtam));
			Mtam = Mtam-1;
		}
		}while (Ttam >= 0 && Mtam >= 0)
		texto.value = saida.reverse().join("");
}

