/*------------------------------------------------------
	CONFIRMA ENVIO
------------------------------------------------------*/

function confirmaAcao(frm,msg,act)
{
	if (confirm(msg))
	{
		document[frm].action = act;
		document[frm].submit();	
	}
}

/*------------------------------------------------------
	ENVIA FORM
------------------------------------------------------*/

function enviaForm(frm,act)
{
	if (act == "" || act == null)
	{
		document[frm].submit();	
	}
	else
	{
		//alert("Ação: "+act+" - Form: "+frm);
		document[frm].action = act;
		document[frm].submit();	
	}
}

/*------------------------------------------------------
	PERMITE DIGITOS
------------------------------------------------------*/

function IsNumeric(sText,sDigitos)
{
	var ValidChars = sDigitos;
	var IsNumber   =  true;
	var Char;
	
	for (i=0; i<sText.length && IsNumber == true; i++) 
	{ 
	Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}


/*------------------------------------------------------
	EVENTOS FORM
------------------------------------------------------*/

function nomeCampos(campo,texto,acao)
{
	with(document.forms[0])
	{
		if (acao == "focus")
		{
			if (campo.value == texto)
			{
				campo.value = "";	
			}
		}
		if (acao == "blur")
		{
			if (campo.value == "")
			{
				campo.value = texto;	
			}
		}
	}
}


/*------------------------------------------------------
	FORMATA MOEDA
------------------------------------------------------*/

function maskMoeda(fld, milSep, decSep, e)
{
	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 (whichCode == 13) return true;  // Enter
	if (whichCode == 8) return true;  // Delete
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
	if (j == 3) {
	aux2 += milSep;
	j = 0;
	}
	aux2 += aux.charAt(i);
	j++;
	}
	fld.value = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
	fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}


/*------------------------------------------------------
	MASCARA
	* CEP  -> 99.999-999
	* CPF  -> 999.999.999-99
	* CNPJ -> 99.999.999/9999-99
	* Data -> 99/99/9999
	* Tell -> (99) 9999-9999
------------------------------------------------------*/

function txtBoxFormat(strField, sMask, evtKeyPress) 
{
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

    if(window.event) { // Internet Explorer
      nTecla = evtKeyPress.keyCode; 
	}
    else if(evtKeyPress.which) { // Nestcape / firefox
      nTecla = evtKeyPress.which;
    }
	
    //se for backspace não faz nada
    if (nTecla != 8){
    sValue = document.getElementById(strField).value;
    // alert(sValue);

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i      = 0;
    nCount = 0;
    sCod   = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++; }
      else {
        sCod += sValue.charAt(nCount);
        nCount++;
      }

      i++;
    }

    document.getElementById(strField).value = sCod;

    if (nTecla != 8) { // backspace
      if (sMask.charAt(i-1) == "9") { // apenas números...
        return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
      else { // qualquer caracter...
        return true;
      } }
    else {
      return true;
    }
    }
}


/*---------------------------------------------------------------------------------
	VALIDA LOGIN
---------------------------------------------------------------------------------*/

function validaLogin()
{
	with(document.login)
	{
		if (usuario.value == ""){
			alert("Preencha o usuário.");
			usuario.focus();
			return;
		}
		if (senha.value == ""){
			alert("Preencha a senha.");
			senha.focus();
			return;
		}
		submit();
	}
}

/*---------------------------------------------------------------------------------
	FILTRO GERAL SEM PÁGINAS COM CATEGORIAS
---------------------------------------------------------------------------------*/

function filtroComCategoriasSemPaginas()
{
	with (document.formulario)
	{
		 cat = lista.value;
		 ord = ordem.value;
		 
		 location.href = "index.php?ordem="+ord+"&lista="+cat;
	}
}

/*---------------------------------------------------------------------------------
	FILTRO GERAL COM CATEGORIAS
---------------------------------------------------------------------------------*/

function filtroComCategorias(total,aonde)
{
	with (document.formulario)
	{
		 cat = lista.value;
		 ord = ordem.value;
		 pal = chave.value;
		 
		 if (aonde != null){
			 url = aonde;	 	
		 } else {
			 url = "index.php";	 
		 }
		 
		 if (total > 0){
			 pag = pagina.value; 
		 } else {
			 pag = 0; 
	     }
		 
		 location.href = url+"?ordem="+ord+"&lista="+cat+"&pg="+pag+"&chave="+pal;
	}
}


function filtroComCategorias2(arquivo,total)
{
	with (document.formulario)
	{
		 cat = visita_feita_com.value;
		 ord = ordem.value;
		 pal = acompanhante.value;
		 
		 if (total > 0){
			 pag = pagina.value; 
		 } else {
			 pag = 0; 
	     }
		 
		if (arquivo == null) {  
			url = "index.php";
		} else {
			url = arquivo;	
		}		 
		 
		 location.href = url+"?ordem="+ord+"&lista="+cat+"&pg="+pag+"&chave="+pal+"&status=listar";
	}
}

/*---------------------------------------------------------------------------------
	FILTRO GERAL SEM CATEGORIAS
---------------------------------------------------------------------------------*/

function filtroSemCategorias(arquivo,total)
{
	with (document.formulario)
	{
		 ord = ordem.value;
		 
		 if (total > 0){
			 pag = pagina.value; 
		 } else {
			 pag = 0; 
	     }
		 
		if (arquivo == null) {  
			url = "index.php";
		} else {
			url = arquivo;	
		}		 
		 
		 location.href = url+"?ordem="+ord+"&pg="+pag;
	}
}


/*---------------------------------------------------------------------------------
	VAI PARA PÁGINA
---------------------------------------------------------------------------------*/

function getPage(arquivo,resto)
{
	with (document.formulario) 
	{
		location.href = arquivo+"?pg="+pagina.value+resto;	
	}
}

/*---------------------------------------------------------------------------------
	REMETE AÇÕES
---------------------------------------------------------------------------------*/

function acaoSelecionados()
{
	with (document.formulario) 
	{
		submit();	
	}
}

/*---------------------------------------------------------------------------------
	FILTRO DE PESQUISAS
---------------------------------------------------------------------------------*/

function montaURL()
{
	var query = "";
	var i;
	
	with(document.formulario)
	{
		for (var i=0; i<elements.length; i++)
		{
			if (elements[i].value != "" && elements[i].type != "button" && elements[i].type != "file" && elements[i].type != "submit" && elements[i].type != "image" && elements[i].name != "varios[]" && elements[i].name != "marca_todos")
			{
				query += elements[i].name+"="+elements[i].value+"&";
			}
		}
		
		 return query.replace(/&$/, ""); //Retira o ultimo & da query
	}
}

function imprimiURL(arquivo)
{
	var querystring = montaURL();
	location.href   = arquivo+"?"+querystring;
}
