function get(id)
{
	return document.getElementById(id);
}

function empty(obj)
{ 
	var type = typeof obj;
	var er = /^\s{0,}$/;
	var retorno = false;

	if(type=="string")
	{
		if(obj.match(er))
			return true;
	}
	if(type!="undefined")
	{
		if(type == "object")
		{
			retorno  = obj.value.match(er);
		}
		else if(get(obj))
		{
			retorno = get(obj).value.match(er);
		}
	}	
	
	return retorno;
}

function tam()
{
	largura = document.body.offsetWidth;
	altura  = document.body.offsetHeight;
	
	window.resizeTo(largura+24,altura+110);
}

function abreEnquete()
{
	window.open('enquete.php', 'resultadoEnquete', 'height=100px,left=300,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,top=100,width=390px');
}

function validaVoto()
{
	var inputs = document.getElementsByName("enquete");
	var valida = false;
	for(var i=0; i<inputs.length; i++){
		valida = valida || inputs[i].checked;
	}
	
	if(valida){
		abreEnquete();
		return true;
	}else{
		return false;
	}
}

function addFav(){
    var url      = "http://www.academiaislamica.org.br/";
    var title    = "Academia Islâmica";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}

function removeMask(value)
{
	var valueLimpo='';
	var er = /[0-9]{1,1}/;	
	
	for(i=0; i < value.length; i++){
		tmp = value.substring(i, i+1);
			
		//if(!isNaN(tmp))
		if(tmp.match(er)){
			valueLimpo+=tmp;
		}
	}
	
	return valueLimpo;
}

function validaCPF(cpf) 
{
	var cpf = new String(cpf);
    var aux_cpf = "";
    var erro = true;

	// retirar caracteres não numéricos
	aux_cpf = removeMask(cpf);

	if(aux_cpf.length!=11){
		return false;
	}else{
    	var sub = aux_cpf.substr(0,1);
    
    	if(aux_cpf.match(sub+'{11}'))
    		return false;
    		
    	var cpf1 = String(aux_cpf);
    	var cpf2 = cpf.substr(cpf.length-2,2);
      	var controle = "";
      	var start = 2;
      	var end = 10;

      	for(var i=1;i<=2;i++){
      		var soma = 0;
      		
      		for(j=start;j<=end;j++){
      			soma += cpf1.substr((j-i-1),1)*(end+1+i-j);
      		}
        	if(i==2){
          		soma += digito * 2;
        	}
        	
        	digito = (soma * 10) % 11;
        	
        	if(digito==10){
          		digito = 0;
        	}
        	
        	controle += digito;
        	start = 3;
        	end = 11;
      	}
      	if(controle!=cpf2){
        	erro = false;
      	}
    }
  
	return erro;
}

function validaEMAIL(email)
{
//	var er = /^[a-z0-9-_.]+@+[a-z0-9-_.]+\.[a-z]{2,4}((\.)[a-z]{2})?$/;
	var er = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	
	return email.match(er);
}

function validaFormulario()
{
	var msg = '';
	if (empty(get('login')))
		msg += 'O campo "Login" é obrigatório.\n';
	if (empty(get('senha')))
		msg += 'O campo "Senha" é obrigatório.\n';
	if (empty(get('nome')))
		msg += 'O campo "Nome Completo" é obrigatório.\n';
	if (!get('masculino').checked && !get('feminino').checked)
		msg += 'O campo "Sexo" é obritatório.\n';
	if (!validaEMAIL(get('email').value))
		msg += 'O Email fornecido não é válido.\n';
	if (!validaCPF(get('cpf').value))
		msg += 'O CPF fornecido não é válido.\n';
	if (empty(get('nascimento')))
		msg += 'O campo "Data de Nascimento" é obrigatório.\n';	
	if (empty(get('telefone')))
		msg += 'O campo "Telefone" é obrigatório.\n';
	if (empty(get('nacionalidade')))
		msg += 'O campo "Nacionalidade" é obrigatório.\n';
	if (empty(get('cep')))
		msg += 'O campo "CEP" é obrigatório.\n';
	if (empty(get('endereco')))
		msg += 'O campo "Endereço" é obrigatório.\n';
	if (empty(get('numero')))
		msg += 'O campo "Número" é obrigatório.\n';
	if (empty(get('bairro')))
		msg += 'O campo "Bairro" é obrigatório.\n';
	if (empty(get('cidade')))
		msg += 'O campo "Cidade" é obrigatório.\n';
	if (empty(get('especialidade')))
		msg += 'O campo "Especialidade" é obrigatório.\n';
	if (empty(get('atividade_atual')))
		msg += 'O campo "Atividade Atual" é obrigatório.\n';
	if (!get('nao').checked && !get('sim').checked)
		msg += 'O campo "Atua na área de formação?" é obrigatório.\n';
	if (get('nao').checked && empty(get('porque')))
		msg += 'O campo "Porque não atua na área de formação?" é obrigatório.\n';
	if (!get('nao2').checked && !get('sim2').checked)
		msg += 'O campo "Frequenta algum centro Islâmico?" é obrigatório.\n';
	if (get('sim2').checked && (empty(get('qual')) || empty(get('desde'))))
		msg += 'Os campos "Qual?" e "Desde" são obrigatórios.\n';
	if (empty(get('tempo_islam')))
		msg += 'O campo "Tempo no Islam" é obrigatório.\n';
	if (empty(get('motivos1')))
		msg += 'O campo "Motivos que o(a) levaram a estudar Islam - 1" é obrigatório.\n';
	if (!msg)
		return true;
	else{
		alert('Os seguintes erros foram encontrados: \n\n'+msg);
		return false;
	}
}

function validaContato()
{
	var msg = '';
	if (empty(get('nome')))
		msg += 'O campo "Nome Completo" é obrigatório.\n';
	if (!validaEMAIL(get('email').value))
		msg += 'O Email fornecido não é válido.\n';
	if (empty(get('mensagem')))
		msg += 'O campo "O Campo mensagem" é obrigatório.\n';
	if (!msg)
		return true;
	else{
		alert('Os seguintes erros foram encontrados: \n\n'+msg);
		return false;
	}
}

function validaIndique()
{
	var msg = '';
	if (empty(get('nome')))
		msg += 'O campo "Nome Completo" é obrigatório.\n';
	if (!validaEMAIL(get('email').value))
		msg += 'O Email fornecido não é válido.\n';
	if (empty(get('nome2')))
		msg += 'O campo "Nome Completo" é obrigatório.\n';
	if (!validaEMAIL(get('email2').value))
		msg += 'O Email fornecido não é válido.\n';
	if (!msg)
		return true;
	else{
		alert('Os seguintes erros foram encontrados: \n\n'+msg);
		return false;
	}
}

function testaOutroIdioma()
{
	if(empty(get('outros')))
		get('nivelOutro').disabled = true;
	else
		get('nivelOutro').disabled = false;
}

function adicionaProduto(check)
{
	var cod = check.id.substring(7);
	if (check.checked){
		get('qtd'+cod).className = "qtdProdutoComprando";
		get('qtd'+cod).value = "1";
		check.parentNode.parentNode.style.backgroundColor = '#eadac0';
	}else{
		get('qtd'+cod).className = "qtdProduto";
		get('qtd'+cod).value = "0";
		check.parentNode.parentNode.style.backgroundColor = '#FFFFFF';
	}
}
