/*

  DATA...............: 10-05-2007
  SCRIPT.............: VECTOR.JS
  DESCRICAO..........: SCRIPT AJAX
  ----------------------------------------------------------------------------------------------------------
  Nº   DATA      NOME     ALTERAÇÃO
  ----------------------------------------------------------------------------------------------------------
  001  10-05-07  LUIS     OPENAJAX VERIFICA OS BROWSERS
  002  10-05-07  LUIS     FUNÇÃO QUE FORMATA OS CAMPOS HTML
  003  10-05-07  CLEBER   FUNÇÃO QUE RESUME O GetElementByID
  004  14-05-07  CLEBER   FUNÇÃO SAIR DO SISTEMA
  005  21-05-07  CLEBER   FUNÇÃO GLOBAL PARA BUSCAR E SELECAO DE PAGINA
  006  22-05-07  CLEBER   FUNÇÃO GLOBAL PARA BUSCAR E SELECAO DE MENU
  007  24-05-07  CLEBER   FUNCAO GLOBAL PARA BUSCAR OPERADORES CADASTRADOS NO SISTEMA
  008  11-06-07  CLEBER   FUNCAO GLOBAL PARA LIMITAR O NUMERO DE CARACTERES DIGITADOS EM UM CAMPO TEXTAREA
  009  11-06-07  CLEBER   FUNÇÃO QUE VALIDA DATA APOS SER DIGITADA
  010  10-10-07  CLEBER   FUNÇÃO QUE BUSCA CIDADE
  011  10-10-07  CLEBER   FUNÇÃO QUE SELECIONA CIDADE
  012  10-10-07  CLEBER   FUNÇÃO BUSCA E SELECIONA PROFISSAO
  013  10-10-07  CLEBER   BUSCA E LISTA USUÁRIOS
  014  10-10-07  CLEBER   LISTAR OS USUÁRIOS DE UM PROFISSÃO SELECIONADA
*/

var ns  = document.layers;
var ie  = document.all;
var ns6 = (document.getElementById && !document.all);


// ----------------------------------------------------------------------------------------------------------
// 001
// ----------------------------------------------------------------------------------------------------------

function openAjax() 
{
	var ajax;
	
	try
	{
		// XMLHttpRequest para browsers decentes, como: Firefox, Safari, dentre outros.
		
		ajax = new XMLHttpRequest(); 
	}
	catch(ee)
	{
		try
		{
			// Para o IE da MS
			
			ajax = new ActiveXObject("Msxml2.XMLHTTP"); 
		}
		catch(e)
		{
			try
			{
				// Para o IE da MS
				
				ajax = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
			catch(E)
			{
				ajax = false;
			}
		}
	}
	
	return ajax;
}

// ----------------------------------------------------------------------------------------------------------
// 001
// ----------------------------------------------------------------------------------------------------------

function include(id,pag,destino)
{
	var arr = pag.split('/');
	
	if (arr[1])
	{
		url = 'vector/ajax/'+arr[1]+'.php?id='+id;
	}
	else
	{
		url = 'ajax/'+pag+'.php?id='+id;
	}
	
	// Inicia o Ajax
	var ajax = openAjax(); 
	ajax.open("GET", url, true);
	ajax.send(null);	
	
	ajax.onreadystatechange = function() 
	{
		if (ajax.readyState == 4) 
		{
			if (ajax.status == 200) 
			{
				document.getElementById(destino).innerHTML = ajax.responseText;
			}
			else
			{
				alert("Erro: " + ajax.statusText);
			}
		}
		else
		{
			document.getElementById(destino).innerHTML = '<p align=\"center\"><img src=\"ico/load.gif\" alt=\"Carregando...\" /></p>';
		}
	}
}

// ----------------------------------------------------------------------------------------------------------
// 002 - EX.: <input type="text" name="cep" id="telefone" onKeyPress="return formata(this, '###.###.###-##', event)"> 
// ----------------------------------------------------------------------------------------------------------

function formata(campo, mask, evt) 
{
	if(document.all) 
	{ 
		// Internet Explorer
		
		key = evt.keyCode; 
	}
	else
	{ 
		// Nestcape
		
		key = evt.which;
	}
	
	if(key != 0 && key!=8)
	{
		string = campo.value;  
		i = string.length;
		
		if (i < mask.length) 
		{
			if (mask.charAt(i) == '#') 
			{
				return (key > 47 && key < 58);
			} 
			else 
			{
				
				if (mask.charAt(i) == '!') 
				{  
					return true;  
				}
			
				for (c = i; c < mask.length; c++) 
				{
					if (mask.charAt(c) != '#' && mask.charAt(c) != '!')
					{
						campo.value = campo.value + mask.charAt(c);
					}
					else if (mask.charAt(c) == '!')
					{
						return true;
					} 
					else 
					{
						return (key > 47 && key < 58);
					}
				}
			}
		} else return false;
	}
	else
	{
		return true; 
	}
}

// ----------------------------------------------------------------------------------------------------------
// 003
// ----------------------------------------------------------------------------------------------------------


function $(strId)
{
	var i, arrReturn,arrStrId;
	
	if(arguments.length > 1)
	{
		arrStrId = new Array();
		for(i=0; i<arguments.length; i++)
		{
			arrStrId.push(arguments[i]);
		}
	}
	
	if(strId instanceof Array)
	{
		arrStrId = strId;
	}
	
	if(arrStrId instanceof Array)
	{
		arrReturn = new Array();
		for(i=0; i<arrStrId.length; i++)
		{
			arrReturn[i] = document.getElementById(arrStrId[i]);
		}
	}
	else
	{		
		arrReturn = document.getElementById(strId);
	}
	
	return arrReturn;
}

// ----------------------------------------------------------------------------------------------------------
// 004
// ----------------------------------------------------------------------------------------------------------

function sair()
{
	include('sair','index','menu');
	$("conteudo").innerHTML = '';
	$("sub_menu").innerHTML = '';
}			

// ----------------------------------------------------------------------------------------------------------
// 005
// ----------------------------------------------------------------------------------------------------------

function busca_pagina(campo)
{
	var dsc = $("dsc_pag").value;
	var aux = 'busca_pagina|'+dsc+'|'+campo;
	
	include(aux,'vector','list_pagina');
}

function selecionar_pagina(id, nome, campo)
{
	$(campo).value = id; 
	$('dsc_pag').value = nome;
	$('list_pagina').innerHTML = '<div id=\"list_pagina\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// 006
// ----------------------------------------------------------------------------------------------------------

function busca_menu(campo)
{
	if ($("id_pag").value < 1)
	{
		alert('Atencao\n\nVoce esta pesquisando um menu.\nMas para pesquisar um menu voce deve informar a qual pagina ele pertence.');
	}
	else
	{
		var dsc = $("dsc_mnu").value;
		var pag = $("id_pag").value;
		var aux = 'busca_menu|'+dsc+'|'+pag+'|'+campo;
		
		include(aux,'vector','list_mnu');		
	}
}

function selecionar_menu(id, nome, campo)
{
	$(campo).value = id; 
	$('dsc_mnu').value = nome;
	$('list_mnu').innerHTML = '<div id=\"list_mnu\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// 007
// ----------------------------------------------------------------------------------------------------------

function busca_op(campo)
{	
	var nome = $("nome_op").value;
	var aux  = 'busca_op|'+nome+'|'+campo;
	
	include(aux,'vector','list_op');
}

function selecionar_op(log, nome, campo)
{
	$(campo).value = log; 
	$('nome_op').value = nome;
	$('list_op').innerHTML = '<div id=\"list_op\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// 008
// ----------------------------------------------------------------------------------------------------------

function contador(obj, obj_view, tamanho) 
{
	if (tamanho > 0)
	{
		if ($(obj.name).value.length > tamanho)  
		{
			$(obj.name).value = $(obj.name).value.substring(0, tamanho);
		}				
	}
	
	$(obj_view.name).value = tamanho - $(obj.name).value.length;
} 

// ----------------------------------------------------------------------------------------------------------
// 009 - FUNÇÃO QUE VALIDA DATA APOS SER DIGITADA
// ----------------------------------------------------------------------------------------------------------

function validarData(campo)
{
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	
	var erro = true;
	
	if ((campo.value.match(expReg)) && (campo.value!=''))
	{
		var dia = campo.value.substring(0,2);
		var mes = campo.value.substring(3,5);
		var ano = campo.value.substring(6,10);

		if (mes==4 || mes==6 || mes==9 || mes==11 && dia>30)
		{
			campo.value = '';
			erro = false;
		} 
		else
		{
			if (ano%4!=0 && mes==2 && dia>28)
			{
				campo.value = '';
				erro = false;
			} 
			else
			{
				if (ano%4==0 && mes==2 && dia>29)
				{
					campo.value = '';
					erro = false;
				} 
				else
				{
					erro = true;
				}
			}
		}
	} 
	else 
	{
		campo.value = '';
		erro = false;
	}
	
	if (erro == false)
	{
		campo.focus();
	}
}

// ----------------------------------------------------------------------------------------------------------
// 010
// ----------------------------------------------------------------------------------------------------------

function busca_cidade()
{
	var uf  = $("uf").value;
	var cid = $("cid").value;

	include('busca_cidade|'+uf+'|'+cid, 'vector', 'list_cidade');
}

function busca_cidade_site()
{
	var uf  = $("uf").value;
	var cid = $("cid").value;

	include('busca_cidade|'+uf+'|'+cid, 'js/vector', 'list_cidade');
}


// ----------------------------------------------------------------------------------------------------------
// 011
// ----------------------------------------------------------------------------------------------------------

function seleciona_cidade(cod, dsc)
{
	$('cod_cid').value = cod;
	$('cid').value = dsc;
	$('list_cidade').innerHTML = '<div id=\"list_cidade\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// 012
// ----------------------------------------------------------------------------------------------------------

function busca_profissao()
{
	var pro = $("pro").value;

	include('busca_profissao|'+pro, 'vector', 'list_profissao');
}

// ----------------------------------------------------------------------------------------------------------
// 012
// ----------------------------------------------------------------------------------------------------------

function seleciona_profissao(cod, dsc)
{
	$('cod_pro').value = cod;
	$('pro').value = dsc;
	$('list_profissao').innerHTML = '<div id=\"list_profissao\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// 013
// ----------------------------------------------------------------------------------------------------------

function busca_usuario(funcao)
{
	var usu = $("nome_usu").value;

	include('busca_usuario|'+usu+'|'+funcao, 'vector', 'lista_usuario');
}

// ----------------------------------------------------------------------------------------------------------
// 013
// ----------------------------------------------------------------------------------------------------------

function seleciona_usuario(cod, dsc)
{
	$('cod_usuario').value = cod;
	$('nome_usu').value = dsc;
	$('lista_usuario').innerHTML = '<div id=\"lista_usuario\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// 014 
// ----------------------------------------------------------------------------------------------------------


function listar_usuarios(campo)
{
	var pro = $('cod_pro').value;
	var uf  = $('uf').value;
	var cid = $('cod_cid').value;
	
	include('listar_usuarios|'+campo+'|'+pro+'|'+uf+'|'+cid,'vector','usuarios_selecionados');
}

// ----------------------------------------------------------------------------------------------------------
// CMC - CRIA UMA LISTA DE CHECKBOX DE USUÁIRO E GRAVA EM UMA VARIAVEL GLOBAL OS USUARIOS MARCADOS
// ----------------------------------------------------------------------------------------------------------

function add_usuario(id, condicao)
{
	include('add_usuario|'+id+'|'+condicao,'vector','usuarios_marcados');	
}

// ----------------------------------------------------------------------------------------------------------
// CMC - LISTA AS SEÇÕES CADASTRADAS NO SISTEMA
// ----------------------------------------------------------------------------------------------------------

function busca_secao(campo)
{
	var dsc = $("dsc_secao").value;
	var aux = 'busca_secao|'+dsc+'|'+campo;
	
	include(aux,'vector','list_secao');
}

function selecionar_secao(id, nome, campo)
{
	$(campo).value = id; 
	$('dsc_secao').value = nome;
	$('list_secao').innerHTML = '<div id=\"list_secao\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// CMC - LISTA AS PAGINAS ASSOCIADAS A UMA SEÇÃO
// ----------------------------------------------------------------------------------------------------------

function busca_secao_pagina(cod_secao_pagina, secao_pai)
{
	if (secao_pai != '')
	{
		var sec = $(secao_pai).value;
	}
	else
	{
		var sec = 0;
	}
	
	var dsc = $("dsc_secao_pagina").value;
	
	var aux = 'busca_secao_pagina|'+dsc+'|'+sec+'|'+cod_secao_pagina;

	include(aux,'vector','list_secao_pagina');
}

function selecionar_secao_pagina(id, nome, campo)
{
	$(campo).value = id; 
	$('dsc_secao_pagina').value = nome;
	$('list_secao_pagina').innerHTML = '<div id=\"list_secao_pagina\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// CMC - LISTA AS EDIÇÕES CADASTRADAS NO SISTEMA
// ----------------------------------------------------------------------------------------------------------

function busca_edicao(campo)
{
	var dsc = $("dsc_edicao").value;
	var aux = 'busca_edicao|'+dsc+'|'+campo;
	
	include(aux,'vector','list_edicao');
}

function selecionar_edicao(id, nome, campo)
{
	$(campo).value = id; 
	$('dsc_edicao').value = nome;
	$('list_edicao').innerHTML = '<div id=\"list_edicao\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// CMC - INCLUI UM FLASH NA PÁGINA
// ----------------------------------------------------------------------------------------------------------

function flash(file, width, height)
{
	document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='" + width + "' height='" + height + "'>");
	document.write("<param name='movie' value='" + file + "'>");
	document.write("<param name='quality' value='high'>");
	document.write("<embed src='" + file + "' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='" + width + "' height='" + height + "'></embed>");
	document.write("</object>");
}

// ----------------------------------------------------------------------------------------------------------
// CMC - BUSCA CATEGORIA PARA O SITE
// ----------------------------------------------------------------------------------------------------------

function busca_cat_site()
{
	var cat = $("cat").value;
	
	if (cat.length >= 3)
	{
		include('busca_categoria|'+cat, 'js/vector', 'list_cat');
	}
}

function seleciona_cat(id, nome)
{
	$('cod_cat').value = id; 
	$('cat').value = nome;
	$('list_cat').innerHTML = '<div id=\"list_cat\"></div>';
}

// ----------------------------------------------------------------------------------------------------------
// CMC - BUSCA ETAPA PARA O SITE E PARA O SISTEMA
// ----------------------------------------------------------------------------------------------------------

function busca_etp_site()
{
	var etp = $("etp").value;
	
	if (etp.length >= 3)
	{
		include('busca_etapa|'+etp, 'js/vector', 'list_etp');
	}
}

function busca_etp(campo)
{
	var dsc = $("etp").value;
	var aux = 'busca_etapas|'+dsc+'|'+campo;
	
	include(aux,'vector','list_etp');
}

function seleciona_etp(id, nome)
{
	$('cod_etp').value = id; 
	$('etp').value = nome;
	$('list_etp').innerHTML = '<div id=\"list_etp\"></div>';
} 

// ----------------------------------------------------------------------------------------------------------
// CMC - FUNÇÃO QUE FORMATA E VALIDA UM CAMPO DECIMAL
// ----------------------------------------------------------------------------------------------------------

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e, proximo)
{
    var sep			= 0;
    var key			= '';
    var i = j		= 0;
    var len = len2	= 0;
    var strCheck	= '0123456789';
    var aux = aux2	= '';
    var whichCode	= (window.Event) ? e.which : e.keyCode;

	if (e.keyCode == 9 || whichCode == 13)
	{
		$(proximo).focus();
	}

	if (whichCode == 8 || e.keyCode == 46)
	{
		objTextBox.value = '';
		return true;		
	}

	if (whichCode == 13)	return true;
    
	key = String.fromCharCode(whichCode);			// Valor para o código da Chave

    if (strCheck.indexOf(key) == -1) return false;	// Chave inválida
    
	len = objTextBox.value.length;

    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;

    aux = '';

    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);

    aux += key;

    len = aux.length;
    
	if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    
	if (len > 2) 
	{
        aux2 = '';
        
		for (j = 0, i = len - 3; i >= 0; i--) 
		{
            if (j == 3) 
			{
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }

        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }

    return false;
}


// Máscara de Valor em reais
function teclas(campo,evt){
    if(((evt.keyCode < 96) || (evt.keyCode > 105)) &&
       ((evt.keyCode < 48) || (evt.keyCode > 57)) ){
           campo.value = campo.value.replace(String.fromCharCode(evt.keyCode).toLowerCase(),"");
    }
}
// Máscara de campo de valor
function numMoeda(campo,evt){
       // para evitar caracteres alfas.
       teclas(campo,evt);
       str = campo.value;

       while(str.search(",") != -1)
           str = str.replace(",","");
       i = 0;

       while(i< str.length){
           if(str.substr(i,1) == ".")
              str = str.replace(".","");
              i++;
       }

       part1 = str.substr(0,str.length - 2);
       while(part1.search(" ") != -1)
           part1 = part1.replace(" ","");

           part2 = str.substr(str.length - 2,2);
           res = "";
           i = part1.length;
           sob = i % 3;
           if((sob != 0) && (i > 2))
              res = part1.substr(0,sob) + ".";
           else
              res = part1.substr(0,sob);
           j = 1;
           part1 = part1.substr(sob);
           i = 0;
           while(i < part1.length){
              if(j == 3){
                 if(i + 1 == part1.length)
                    res = res + part1.substr(i-2,3);
                 else res = res + part1.substr(i-2,3) + ".";
              }
              i++;
              j = j<3?j+1:1;
           }
           campo.value = res + "," + part2;
}


