var domain = 'www.mapworks.com.br';

/* Exibe-Oculta formulario de Login */
function show_login_form(){
	if(document.getElementById("login_form").style.display == 'block'){
		document.getElementById("login_form").style.display = 'none';
	}else{
		document.getElementById("login_form").style.display = 'block';
	}
}
/* Exibe-Oculta formulario de Login */

/* Formulario de Avaliacao Gratuita */

function check_nome(){

	if(document.getElementById("nome").value.length == 0){
		document.getElementById("alerta_nome").innerHTML = 'Este campo &eacute; obrigat&oacute;rio!';
		document.getElementById("alerta_nome").style.display = 'block';
		V = 1;
	}
	else{
		document.getElementById("alerta_nome").style.display = 'none';
	}
}

function check_email(){

	exp = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/
    if(exp.test(document.getElementById("email").value)){
		
		document.getElementById("alerta_email").style.display = 'none';

		if(document.getElementById("email").value.length == 0){
			document.getElementById("alerta_email").innerHTML = 'Este campo &eacute; obrigat&oacute;rio!';
			document.getElementById("alerta_email").style.display = 'block';
			V = 1;
		}else{
			document.getElementById("alerta_email").innerHTML = '';
			document.getElementById("alerta_email").style.display = 'none';
		}
		
		$.ajax(
		{	
			type: "POST",
			url: "http://"+ domain +"/mapworks/site/index.php/avaliacao/checkemail/"+document.getElementById("email").value,
			success: function(res){
				if(res > 0){
					document.getElementById("alerta_email").innerHTML = 'Este e-mail j&aacute; est&aacute; cadastrado em nosso banco de dados!';
					document.getElementById("alerta_email").style.display = 'block';
					V = 1;
				}
			},
			error: function(txt) {}
		});
		
	}
	else{
		document.getElementById("alerta_email").innerHTML = 'Confira seu e-mail pode estar escrito errado';
		document.getElementById("alerta_email").style.display = 'block';
		V = 1;	
		
		if(document.getElementById("email").value.length == 0){
			document.getElementById("alerta_email").innerHTML = 'Este campo &eacute; obrigat&oacute;rio!';
			document.getElementById("alerta_email").style.display = 'block';
			V = 1;
		}
		
	}
}

function check_senha(){

	senha = document.getElementById("senha").value;
	forca = 0;

	if((senha.length >= 4) && (senha.length <= 7)){
		forca += 10;
	}else if(senha.length>7){
		forca += 25;
	}
	if(senha.match(/[a-z]+/)){
		forca += 10;
	}
	if(senha.match(/[A-Z]+/)){
		forca += 20;
	}
	if(senha.match(/\d+/)){
		forca += 20;
	}
	if(senha.match(/\W+/)){
		forca += 25;
	}
	
	if((forca > 0) && (forca < 30)){
		document.getElementById("alerta_senha").innerHTML = '<img src="http://'+ domain +'/mapworks/site/app/webroot/img/senha_fraca.gif"/>';
		document.getElementById("alerta_senha").style.display = 'block';
		document.getElementById("alerta_senha").style.background = 'none';
	}else if((forca >= 30) && (forca < 60)){
		document.getElementById("alerta_senha").innerHTML = '<img src="http://'+ domain +'/mapworks/site/app/webroot/img/senha_forte.gif"/>';
		document.getElementById("alerta_senha").style.display = 'block';
		document.getElementById("alerta_senha").style.background = 'none';
	}else if((forca >= 60) && (forca < 85)){
		document.getElementById("alerta_senha").innerHTML = '<img src="http://'+ domain +'/mapworks/site/app/webroot/img/senha_muito_forte.gif"/>';
		document.getElementById("alerta_senha").style.display = 'block';
		document.getElementById("alerta_senha").style.background = 'none';
	}else{
		document.getElementById("alerta_senha").innerHTML = '<img src="http://'+ domain +'/mapworks/site/app/webroot/img/senha_muito_forte.gif"/>';
		document.getElementById("alerta_senha").style.display = 'block';
		document.getElementById("alerta_senha").style.background = 'none';
	}
	
	if(document.getElementById("senha").value.length == 0){
		document.getElementById("alerta_senha").innerHTML = '<img src="http://'+ domain +'/mapworks/site/app/webroot/img/senha_invalida.gif"/>';
		document.getElementById("alerta_senha").style.display = 'block';
		V = 1;
	}
}

function check_confirma_senha(){

	if(document.getElementById("senha").value == document.getElementById("confirma_senha").value){
		document.getElementById("alerta_confirma_senha").style.display = 'none';
	}
	else if(document.getElementById("confirma_senha").value == ''){
			document.getElementById("alerta_confirma_senha").innerHTML = 'Verifique e confirme sua senha!';
			document.getElementById("alerta_confirma_senha").style.display = 'block';
			V = 1;
		}
	else{
		document.getElementById("alerta_confirma_senha").innerHTML = 'Sua senha n&atilde;o confere!';
		document.getElementById("alerta_confirma_senha").style.display = 'block';
		V = 1;
	}
}

function show_colaborador(){

	if(document.getElementById("dica_colaborador").style.display == 'block'){
		document.getElementById("dica_colaborador").style.display = 'none';
	}
	else{
		document.getElementById("dica_colaborador").style.display = 'block';
	}	
}

function check_form_avaliacao(){

	V = 0;
	
	// Verificar caracter especial
	check_nome();
	// Verificar e-mail
	check_email();
	// Verificar forca de senha
	check_senha();
	// Verificar confirmacao de senha
	check_confirma_senha();
	// Exibir dica de colaborador
	//show_colaborador();
	
	if(V == 0){
		document.getElementById('form_avaliacao').submit();
	}
}
/* Formulario de Avaliacao Gratuita */

/* Expande coluna de aparelhos */
function expande_aparelhos(){
	var fixa = document.getElementById('fixa').style.width;
	if(fixa <= '204px'){
		document.getElementById('fixa').style.width = '540px';
		document.getElementById('barra_expande').style.background = '#fbfafa url(http://'+ domain +'/mapworks/site/app/webroot/img/bg_barra_recolhe.gif) center no-repeat';
	}else{
		document.getElementById('fixa').style.width = '204px';
		document.getElementById('barra_expande').style.background = '#fbfafa url(http://'+ domain +'/mapworks/site/app/webroot/img/bg_barra_expande.gif) center no-repeat';
	}
}
/* Expande coluna de aparelhos/grupos */

/* Exibe coluna de aparelhos */
function exibe_aparelhos(){
	var aparelhos = document.getElementById('aparelhos').style.display;
	if(aparelhos != 'block'){
		document.getElementById('botao_grupos').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao.gif") no-repeat';
		document.getElementById('botao_grupos').style.color = '#597B8D';
		document.getElementById('grupos_aparelhos').style.display = 'none';
		document.getElementById('desc_coluna').innerHTML= 'Aparelhos';
		document.getElementById('botao_aparelhos').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao_escuro.gif") no-repeat';
		document.getElementById('botao_aparelhos').style.color = '#FFFFFF';
		document.getElementById('aparelhos').style.display = 'block';
		document.getElementById('ativagrupo').value = '0'; // Seta variavel que controla se mapa utiliza o form de aparelhos ou grupos // Se value = 0 utiliza form aparelhos
	}
}
/* Exibe coluna de aparelhos */

/* Exibe coluna de grupos */
function exibe_grupos(){
	var grupos = document.getElementById('grupos_aparelhos').style.display;
	if(grupos != 'block'){
		document.getElementById('botao_aparelhos').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao.gif") no-repeat';
		document.getElementById('botao_aparelhos').style.color = '#597B8D';
		document.getElementById('aparelhos').style.display = 'none';
		document.getElementById('desc_coluna').innerHTML= 'Grupos';
		document.getElementById('botao_grupos').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao_escuro.gif") no-repeat';
		document.getElementById('botao_grupos').style.color = '#FFFFFF';
		document.getElementById('grupos_aparelhos').style.display = 'block';
		document.getElementById('ativagrupo').value = '1'; // Seta variavel que controla se mapa utiliza o form de aparelhos ou grupos // Se value = 1 utiliza form grupos
	}
}
/* Exibe coluna de grupos */

/* Exibe numeros dos telefones ligados ao grupo - modulo mapa */
function show_groups_mapa(valor){
	if(document.getElementById('grupo_'+valor).style.display == 'block'){
		document.getElementById('grupo_'+valor).style.display = 'none';
		document.getElementById('img_'+valor).src = 'http://'+ domain +'/mapworks/site/app/webroot/img/ico_expande.gif';
	}else{
		$.ajax(
		{
			type: "POST",
			url: "http://"+ domain +"/mapworks/site/index.php/mapworks/carregaaparelhosgruposmapa/"+valor,
			success: function(res){
				document.getElementById('grupo_'+valor).innerHTML = res;
				document.getElementById('img_'+valor).src = 'http://'+ domain +'/mapworks/site/app/webroot/img/ico_recolhe.gif';
				document.getElementById('grupo_'+valor).style.display = 'block';
			},
			error: function(txt) {}
		});
	}
}
/* Exibe numeros dos telefones ligados ao grupo - modulo mapa */

/* Exibe mensagem */
function show_mensagem(id_conta,alias){
	if(document.getElementById('mensagem').style.display == 'block'){
		document.getElementById('mensagem').style.display = 'none';
	}else{
		document.getElementById('mensagem').style.display = 'block';
		document.getElementById('destinatario').value = id_conta;
		document.getElementById('assunto').value = '';
		document.getElementById('campo_mensagem').value = '';
		document.getElementById('resposta').value = '';
		document.getElementById('span_numero').innerHTML = alias;
	}
}
/* Exibe mensagem */

/* Valida Envio de mensagem */
function check_form_mensagem(){
	V = 0;
	
	if(document.getElementById('assunto').value.length == 0 || document.getElementById('campo_mensagem').value.length == 0){
		document.getElementById('assunto').style.border = '1px solid #ff0000';
		document.getElementById('campo_mensagem').style.border = '1px solid #ff0000';
		V = 1;
	}
	
	if(V == 0){ // submete formulario via ajax
		var mensagem = document.getElementById('mensagem').value;
		var destinatario = document.getElementById('destinatario').value;
		var assunto = document.getElementById('assunto').value;
		var campo_mensagem = document.getElementById('campo_mensagem').value;
		var resposta = document.getElementById('resposta').value;
		var remetente = document.getElementById('remetente').value;
		var dataString = 'destinatario=' + destinatario + '&assunto=' + assunto + '&campo_mensagem=' + campo_mensagem + '&remetente=' + remetente + '&resposta=' + resposta;

		//alert (dataString);
		//return false;
		$.ajax({
			type: "POST",
			url: "http://"+ domain +"/mapworks/site/index.php/mapworks/mensagem",
			data: dataString,
			success: function() {
				alert('Mensagem enviada com sucesso!');
				show_mensagem();
			}
		});
	}
}
/* Valida Envio de mensagem */

/* Exibe legenda */
function show_legenda(){
	if(document.getElementById('legendas').style.display == 'block'){
		document.getElementById('legendas').style.display = 'none';
	}else{
		document.getElementById('legendas').style.display = 'block';
	}
}
/* Exibe legenda */

/* Exibe excel */
function show_excel(){
	if(document.getElementById('excel').style.display == 'block'){
		document.getElementById('excel').style.display = 'none';
	}else{
		document.getElementById('excel').style.display = 'block';
	}
}
/* Exibe legenda */

/* Validação do formulario de cadastro de aparelhos */
function check_numero_radio(valor){

	if(typeof V === 'undefined'){ var V;}

	if(document.getElementById("numero").value.length == 0 && document.getElementById("radio").value.length == 0){
		document.getElementById("alerta_numero").innerHTML = 'Voc&ecirc; deve preencher pelo menos uma op&ccedil;&atilde;o entre n&uacute;mero e r&aacute;dio!';
		document.getElementById("alerta_numero").style.display = 'block';
		V = 1;
	}else{
		document.getElementById("alerta_numero").innerHTML = 'Este campo &eacute; obrigat&oacute;rio!';
		document.getElementById("alerta_numero").style.display = 'none';
	}
	if(valor != 1){ preencheralias();}
	return V;
}
function check_operadora(valor){

	if(typeof V === 'undefined'){ var V;}

	if(document.getElementById("operadoras").value == ''){
		document.getElementById("alerta_operadora").innerHTML = 'Este campo &eacute; obrigat&oacute;rio!';
		document.getElementById("alerta_operadora").style.display = 'block';
		V = 1;
	}else{
		document.getElementById("alerta_operadora").innerHTML = 'Este campo &eacute; obrigat&oacute;rio!';
		document.getElementById("alerta_operadora").style.display = 'none';
	}
	if(valor != 1){ preencheralias();}
	return V;
}

function check_alias(){
	if(document.getElementById("alias").value == ''){
		document.getElementById("alerta_alias").innerHTML = 'Este campo &eacute; obrigat&oacute;rio!';
		document.getElementById("alerta_alias").style.display = 'block';
		V = 1;
	}else{
		document.getElementById("alerta_alias").innerHTML = 'Este campo &eacute; obrigat&oacute;rio!';
		document.getElementById("alerta_alias").style.display = 'none';
	}
	return V;
}

function preencheralias(){
	var inicio = '<select name="data[Accounts][alias]" id="alias">';	
	var option = '';
	var fim = '</select>';
	
	if(document.getElementById("numero").value.length > 0){ // verifica se possui dados no campo
		var numero = document.getElementById("numero").value;
		option += '<option value="'+numero+'">'+numero+'</option>';
	}
	
	if(document.getElementById("radio").value.length > 0){ // verifica se possui dados no campo
		var radio = document.getElementById("radio").value;
		option += '<option value="'+radio+'">'+radio+'</option>';
	}
	
	if(document.getElementById("nome").value.length > 0){ // verifica se possui dados no campo
		var nome = document.getElementById("nome").value;
		option += '<option value="'+nome+'">'+nome+'</option>';
	}
	
	if(document.getElementById("apelido").value.length > 0){ // verifica se possui dados no campo
		var apelido = document.getElementById("apelido").value;
		option += '<option value="'+apelido+'">'+apelido+'</option>';
	}
	/*if(document.getElementById("placa").value.length > 0){ // verifica se possui dados no campo
		var placa = document.getElementById("placa").value;
		option += '<option value="'+placa+'">'+placa+'</option>';
	}
	if(document.getElementById("apelidoveiculo").value.length > 0){ // verifica se possui dados no campo
		var apelidoveiculo = document.getElementById("apelidoveiculo").value;
		option += '<option value="'+apelidoveiculo+'">'+apelidoveiculo+'</option>';
	}*/
	document.getElementById("div_alias").innerHTML = inicio+option+fim;
}

function check_form_aparelhos(){
	V = 0;
	
	// Verificar radio ou numero
	check_numero_radio(1);
	// Verificar operadora
	check_operadora(1);
	// Verificar alias
	check_alias();
	
	if(V == 0){
		document.getElementById('adiocionaparelhos').submit();
	}
}
/* Validação do formulario de cadastro de aparelhos */

/* Exibe box para adicionar usuario ao grupo */
function show_devices_groups(valor){
	if(document.getElementById('legendas').style.display == 'block'){
		document.getElementById('legendas').style.display = 'none';
		document.getElementById('id_grupo').value = valor;
	}else{
		document.getElementById('legendas').style.display = 'block';
		document.getElementById('id_grupo').value = valor;
	}
}
/* Exibe box para adicionar usuario ao grupo */

/* Exibe numeros dos telefones ligados ao grupo - modulo grupos e aparelhos */
function show_groups(valor){
	if(document.getElementById('grupo_'+valor).style.display == 'block'){
		document.getElementById('grupo_'+valor).style.display = 'none';
		document.getElementById('img_'+valor).src = 'http://'+ domain +'/mapworks/site/app/webroot/img/ico_expande.gif';
	}else{
		$.ajax(
		{
			type: "POST",
			url: "http://"+ domain +"/mapworks/site/index.php/mapworks/carregaaparelhosgrupos/"+valor,
			success: function(res){
				document.getElementById('grupo_'+valor).innerHTML = res;
				document.getElementById('img_'+valor).src = 'http://'+ domain +'/mapworks/site/app/webroot/img/ico_recolhe.gif';
				document.getElementById('grupo_'+valor).style.display = 'block'
			},
			error: function(txt) {}
		});
	}
}
/* Exibe numeros dos telefones ligados ao grupo - modulo grupos e aparelhos */

/* Formulario de Esqeuci Senha */
function check_form_esquecisenha(){

	V = 0;
	
	// Verificar e-mail
	check_email();
	
	if(V == 0){
		document.getElementById('esquecisenha').submit();
	}
}
/* Formulario de Esqeuci Senha */

/* Formulario de Nova Senha */
function check_form_novasenha(){

	V = 0;

	// Verificar forca de senha
	check_senha();
	// Verificar confirmacao de senha
	check_confirma_senha();

	
	if(V == 0){
		document.getElementById('novasenha').submit();
	}
}
/* Formulario de Nova Senha */

/* Exportar pontos das contas */
function exportar(){
	
	show_excel();
	
	var numero = '';

	for (i=0;i<document.Conta.elements.length;i++)
	{
		if(document.Conta.elements[i].checked == true){		
			
			numero += document.Conta.elements[i].value;
			numero += ',';		
		}
	}
	
	datainicial = $('#datainicialexcel').val();
	datafinal = $('#datafinalexcel').val();
	
	pagina = 'http://'+ domain +'/mapworks/site/index.php/mapworks/export/' + numero + '/' + datainicial + '/' + datafinal;
	window.open(pagina,"Exportar registros dos pontos");
}
/* Exportar pontos das contas */

/* Exibe filtro */
function show_filtro_form(){
	if(document.getElementById('filtro_form').style.display == 'block'){
		document.getElementById('filtro_form').style.display = 'none';
	}else{
		document.getElementById('filtro_form').style.display = 'block';
	}
}
/* Exibe filtro */

/* Console de Servicos */
function changeConsole(div){
	document.getElementById('detalhes_console').style.display = 'none';
	document.getElementById('mensagens_console').style.display = 'none';
	document.getElementById('eventos_console').style.display = 'none';
	document.getElementById('alertas_console').style.display = 'none';
	document.getElementById('radar_console').style.display = 'none';
	document.getElementById('detalhes_console_botao').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao.gif") no-repeat';
	document.getElementById('detalhes_console_botao').style.color = '#597B8D';
	document.getElementById('mensagens_console_botao').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao.gif") no-repeat';
	document.getElementById('mensagens_console_botao').style.color = '#597B8D';
	document.getElementById('eventos_console_botao').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao.gif") no-repeat';
	document.getElementById('eventos_console_botao').style.color = '#597B8D';
	document.getElementById('alertas_console_botao').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao.gif") no-repeat';
	document.getElementById('alertas_console_botao').style.color = '#597B8D';
	document.getElementById('radar_console_botao').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao.gif") no-repeat';
	document.getElementById('radar_console_botao').style.color = '#597B8D';
	document.getElementById(div).style.display = 'block';
	document.getElementById(div + '_botao').style.background = 'url("http://'+ domain +'/mapworks/site/app/webroot/img/bg_botao_escuro.gif") no-repeat';
	document.getElementById(div + '_botao').style.color = '#FFFFFF';
}
/* Console de Servicos */

/* Selecionar tudo - aparelhos mapa */

function selecionar_tudo(){

	if(document.getElementById('exibiraparelhos').checked){
	   for (i=0;i<document.Conta.elements.length;i++)
		  if(document.Conta.elements[i].type == "checkbox")
			 document.Conta.elements[i].checked=1
	}else{
		for (i=0;i<document.Conta.elements.length;i++)
		  if(document.Conta.elements[i].type == "checkbox")
			 document.Conta.elements[i].checked=0
	}
}

/* Selecionar tudo - aparelhos mapa */

/* Mascara hora */

function hour(obj){
	
	if(obj.value.length == 2)
	{
		obj.value = +obj.value+":";
	}

}

/* Mascara nextel */

function nextel(obj){
	
	if(obj.value.length == 2 || obj.value.length == 6)
	{
		obj.value = obj.value+"*";
	}
	
}

/* Mascara telefone */

function telefone(obj){
	
	if(obj.value.length == 0)
	{
		obj.value = obj.value+"(";
	}
	
	if(obj.value.length == 3)
	{
		obj.value = obj.value+" - ";
	}
	
	if(obj.value.length == 8)
	{
		obj.value = obj.value+") ";
	}
	
	if(obj.value.length == 14)
	{
		obj.value = obj.value+"-";
	}
	
	if(obj.value.length == 19)
	{
		obj.value = obj.value.substr(0,9) + obj.value.substr(9,2) + "-" + obj.value.substr(11,3) + obj.value.substr(15,1) + "-" + obj.value.substr(16,4);
	}
}

/* Mascara telefone */

/* Alterar alias */
function alterar_alias(alias,id){
	$.ajax({	
		type: "POST",
		url: "http://"+ domain +"/mapworks/site/index.php/mapworks/alterarAlias/"+ id + "/"+ alias,
		success: function(res){
			alert('Alias alterado com sucesso!' + res);
		},
		error: function(txt) {
			alert('Ocorreu um problema ao alterar o alias!');
		}
	});
}
/* Alterar alias */
