function abrirPopUp(URL, width, height) {
   // Definindo meio da tela
   var esquerda = (screen.width - width)/2;
   var topo = (screen.height - height)/2;
	window.open(URL, new Date(), ', top=' + topo + ', left=' + esquerda + ', width='+width+', height='+height+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}

/*
 * Função que pega os checkbox marcados e realiza uma função ajax conforme parametros
 */
function gravarValorCheckboxPermissoes(idForm, campo) {
	document.getElementById("tabelaPermissoes").innerHTML = "<br /><div id=\"carregando\" class=\"carregando\"><img src=\"imagens/icones/carregando.gif\"><br />Carregando...</div>";
	xajax_controleAcesso('limpaPermissoesAcesso',document.getElementById("nivelPerSel").value, null);
	var qtde = document.getElementById(idForm).length;
	for(i=0;i<qtde;i++){
		if(document.getElementById(idForm).elements[i].name==campo+'[]' &&	document.getElementById(idForm).elements[i].checked==true)
			xajax_controleAcesso('salvarPermissaoAcesso',document.getElementById(idForm).elements[i].value,document.getElementById('nivelPerSel').value);			
	}
	xajax_controleAcesso('carregarTabelaPermissoesNivel',document.getElementById('nivelPerSel').value, null);
}

/*
 * Função para setar o valor selecionado no drop down para um campo input
 */
function SetarValorInput(idInput, valor) {
	document.getElementById(idInput).value = valor;
}

function MostrarDiv(id) {
	document.getElementById(id).style.display = "block";
}

function OcultarDiv(id) {
	document.getElementById(id).style.display = "none";
}

/*
 * Exemplo de USO:
 * onKeyPress="if (ativarEnterInputAjax(event)) document.getElementById('senha').focus();"
 */
function ativarEnterInputAjax(event, funcao) {
	// Codigo 13 = Enter
	//Internet Explorer
	if(window.event && window.event.keyCode == 13){
	  return true;
	}
	//Netscape e Mozilla
	if (event && event.which == 13){
	  return true;
	}
}

/*
Descrição.: formata um campo do formulário de
acordo com a máscara informada...
Parâmetros: - objForm (o Objeto Form)
- strField (string contendo o nome do textbox)
* onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);">
* Observação: As máscaras podem ser representadas como os exemplos abaixo:
***/
function mascara(objForm, strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
		
	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	} else {
		nTecla = evtKeyPress.which;
		if (nTecla == 8) {
			return true;
		}
	}
	
	sValue = objForm[strField].value;
	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( ")", "" );
	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) == ".") || (sMask.charAt(i) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " ") || (sMask.charAt(i) == "."))
		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++;
		} else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}
		i++;
	}
	
	objForm[strField].value = sCod;
	if (nTecla != 8) { // backspace
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); 
		} else { 
			return true;
		}
	} else {
		return true;
	}
}

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, 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;
    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;
}

function SomenteNumero(campo){
	var digits="0123456789"
	var campo_temp
	for (var i=0;i<campo.value.length;i++){
		campo_temp=campo.value.substring(i,i+1)
		if (digits.indexOf(campo_temp)==-1){
			campo.value = campo.value.substring(0,i);
			break;
		}
	}
}