function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function inumeros(v){
    return v.replace(/\D/g,"")
}

function itelefoneCompleto(v){
    v=v.replace(/\D/g,"")                
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2")
    v=v.replace(/(\d{4})(\d)/,"$1-$2")  
    return v
}

function itelefoneSimples(v){
    v=v.replace(/\D/g,"")                
    v=v.replace(/(\d{4})(\d)/,"$1-$2")  
    return v
}

function icpf(v){
    v=v.replace(/\D/g,"")                   
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       
    v=v.replace(/(\d{3})(\d)/,"$1.$2")      
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") 
    return v
}

function icep(v){
    v=v.replace(/\D/g,"")             
    v=v.replace(/^(\d{5})(\d)/,"$1-$2") 
    return v
}

function romanos(v){
    v=v.toUpperCase()           
    v=v.replace(/[^IVXLCDM]/g,"") 
    while(v.replace(/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/,"")!="")
        v=v.replace(/.$/,"")
    return v
}

function isite(v){
    v=v.replace(/^http:\/\/?/,"")
    dominio=v
    caminho=""
    if(v.indexOf("/")>-1)
        dominio=v.split("/")[0]
        caminho=v.replace(/[^\/]*/,"")
    dominio=dominio.replace(/[^\w\.\+-:@]/g,"")
    caminho=caminho.replace(/[^\w\d\+-@:\?&=%\(\)\.]/g,"")
    caminho=caminho.replace(/([\?&])=/,"$1")
    if(caminho!="")dominio=dominio.replace(/\.+$/,"")
    v="http://"+dominio+caminho
    return v
}

function icnpj(v){
    v=v.replace(/\D/g,"")				
    v=v.replace(/^(\d{2})(\d)/,"$1.$2")        
    v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3")
    v=v.replace(/\.(\d{3})(\d)/,".$1/$2")       
    v=v.replace(/(\d{4})(\d)/,"$1-$2")
    return v
}

function idata(v){
	v=v.replace(/\D/g,"")
	v=v.replace(/(\d{2})(\d)/,"$1/$2")
	v=v.replace(/(\d{2})(\d)/,"$1/$2")
	return v
}
function ivalor(v){
	v=v.replace(/\D/g,"")
	v=v.replace(/(\d)(\d{2})$/,"$1.$2")
	return v
}

function submeteinscricao(){
	var stat = false;
	if(document.getElementById('nome').value != '' && document.getElementById('nome').value.length > 3 ){
		if(document.getElementById('cpf').value != '' && verificarCPF(document.getElementById('cpf').value) ){
			if(document.getElementById('cidade').value != '' ){
				if(document.getElementById('bairro').value != '' && document.getElementById('bairro').value.length > 2 ){
					if(document.getElementById('cep').value != '' && document.getElementById('cep').value.length == 9 ){
						if(document.getElementById('tel1').value != '' && document.getElementById('tel1').value.length == 9 ){
							if(document.getElementById('remuneracao').value != ''){

								if(document.getElementById('ecivil').value=='CP' || document.getElementById('ecivil').value=='CT'
								|| document.getElementById('ecivil').value=='CS' ){
									if(document.getElementById('remuneracao_conjuge').value!='' && document.getElementById('nome2').value.length > 3
										&& verificarCPF(document.getElementById('cpf2').value) ){
										stat = true;
									}else{
										alert('Complete dados de cônjuge');
										document.getElementById('nome2').focus();
										return false;
									}
								}else{
									stat = true;
								}

								if(document.getElementById('nome3').value.length > 3 || verificarCPF(document.getElementById('cpf3').value) ){
									if(document.getElementById('remuneracao_2titular').value!='' && document.getElementById('nome3').value.length > 3
										&& verificarCPF(document.getElementById('cpf3').value) ){
										stat = true;
									}else{
										alert('Complete dados de 2.o titular');
										document.getElementById('nome3').focus();
										return false;
									}

								}else{
									stat = true;
								}

							}else{
								alert('Especifique renda bruta mensal');
								document.getElementById('remuneracao').focus();
							}
						}else{
							alert('Telefone inválido');
							document.getElementById('tel1').focus();
						}
					}else{
						alert('CEP inválido');
						document.getElementById('cep').focus();
					}
				}else{
					alert('Bairro inválido');
					document.getElementById('bairro').focus();
				}
			}else{
				alert('Escolha uma cidade');
				document.getElementById('cidade').focus();
			}
		}else{
			alert('CPF inválido');
			document.getElementById('cpf').focus();
		}
	}else{
		alert('Nome inválido');
		document.getElementById('nome').focus();
	}

	return stat;
}

function pulaQdoCompleto(input,idProximo){
	if(input.value.length == input.maxLength){
		document.getElementById(idProximo).focus();
	}
}

function verificarCPF(c){
	valido=true;
	strcpf = c;
	str_aux = "";
	for (i = 0; i <= strcpf.length - 1; i++)
		if ((strcpf.charAt(i)).match(/\d/))
			str_aux += strcpf.charAt(i);
		else if (!(strcpf.charAt(i)).match(/[\.\-]/)) {
			//alert ("O campo CPF apresenta caracteres inválidos!");
			return false;
		}
		if (str_aux.length != 11) {
			//alert ("O campo CPF deve conter 11 dígitos!");
			return false;
		}
		if (str_aux == '00000000000' || str_aux == '11111111111' || str_aux == '22222222222' || str_aux == '33333333333' || str_aux == '44444444444' || str_aux == '55555555555' || str_aux == '66666666666' || str_aux == '77777777777' || str_aux == '88888888888' || str_aux == '99999999999') {
			//alert ("O CPF é inválido!");
			return false;
		}
		soma1 = soma2 = 0;
		for (i = 0; i <= 8; i++) {
			soma1 += str_aux.charAt(i) * (10-i);
			soma2 += str_aux.charAt(i) * (11-i);
		}
		d1 = ((soma1 * 10) % 11) % 10;
		d2 = (((soma2 + (d1 * 2)) * 10) % 11) % 10;
		if ((d1 != str_aux.charAt(9)) || (d2 != str_aux.charAt(10))) {
			//alert ("O CPF é inválido!");
			return false;
		}
	return true;
}

function verificaConjuge(select){
	var el = document.getElementById('conj');
	if( select.value=='CT' || select.value=='CP' || select.value=='CS'){
		alert('Complete também os dados de cônjuge');
		el.style.display='block';
	}else{
		el.style.display='none';
	}
	
}

	function acaoAjax(paginaAabrir, idDiv, ativarAlertaDeEspera){
		try{
			req = new XMLHttpRequest();
		}catch(err1){
				try{
					req = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(err2){
					try{
						req = new ActiveXObject("Microsoft.XMLHTTP");
					}catch(err3){
						req = false;
					}
			}
		}
			
		if(req){//se sucesso na criacao da requisicao
			abrirRequisicaoAjax(paginaAabrir, idDiv, req, ativarAlertaDeEspera);
		}else{
			alert('Falha ao criar requisição XMLHTTP');
		}
	}
	function abrirRequisicaoAjax(paginaAabrir, idDiv, req, ativarAlertaDeEspera){
		var aleatorio = parseInt(Math.random()*9999999);
		
		req.open("GET", paginaAabrir+"rand="+aleatorio, true); //abrindo requisicao
		req.onreadystatechange = function(){
			if(req.readyState==4){
				if(req.status==200){
					var div = document.getElementById(idDiv);
					 updateSelect(div,req.responseText)
				}else{//se erro na requisicao
					alert("Ocorreu um erro: " + req.statusText);
					location.href='index.php';
				}	
			}else{//enquanto requisicao em andamento
				if(ativarAlertaDeEspera)
					esperaCarregando(idDiv);
			}
		}
		req.send(null);
	}
	function esperaCarregando(idDiv){
		var elemento = document.getElementById(idDiv);
		elemento.innerHTML = '<option>Carregando...</option>';
	}
	/**
	 * Método usado para jogar conteúdo no innnerHTML de selects.
	 * @param {Object} selectObj - o Select que receberá o texto.
	 * @param {Object} texto -  o texto que será incorporado no objeto select.
	 */
	function updateSelect(selectObj,texto){
		var tempDiv = document.createElement("div");
		var attString = "";
		var att = selectObj.attributes;
		for(var i = 0 ; i < att.length ; i++)
			if(att[ i ].nodeValue != "" && att[ i ].nodeValue != null )//o IE diz que nó tem todos os atributos possíveis com valor null ou "" então temos que filtrar esses valores
				tempDiv.innerHTML = "<select "+attString+">"+texto+"</select>";
		var newSelect = tempDiv.firstChild;
		newSelect.id='cidade'
		newSelect.name='cidade'
		selectObj.parentNode.replaceChild(newSelect,selectObj);
		selectObj = null;tempDiv = null;
		return newSelect;
	}
