// JavaScript Document

// Alterar classe com mouseover de um elemento.
// id = id do elemento
function over(id){
  document.getElementById(id).className = 'mouseOver1';
}

// Alterar classe com mouseout de um elemento.
// id = id do elemento
function out(id){
  document.getElementById(id).className = 'mouseOut';
}

// Abre popup
// ex: abrir('arquivo.html', 'he100', 'wi100', 'scno');
function abrir() {
	var windowFeatures = "", nomeArquivo = "", nomeJanela = "", erro = null
	Argumentos = abrir.arguments; noArgumentos = Argumentos.length; nomeArquivo = Argumentos[0]
	for (i = 1; i < noArgumentos; i++) {
		valor = Argumentos[i].substring(2,Argumentos[i].length)
		switch(Argumentos[i].substring(0,2)) {
			case "nj" : nomeJanela = valor; break
			case "to" : windowFeatures += "top=" + valor + ", "; break
			case "le" : windowFeatures += "left=" + valor + ", "; break
			case "he" : windowFeatures += "height=" + valor + ", "; break
			case "wi" : windowFeatures += "width=" + valor + ", "; break
			case "lb" : windowFeatures += "location=" + valor + ", "; break
			case "mb" : windowFeatures += "menubar=" + valor + ", "; break
			case "sc" : windowFeatures += "scrollbars=" + valor + ", "; break
			case "st" : windowFeatures += "status=" + valor + ", "; break
			case "tb" : windowFeatures += "toolbar=" + valor + ", "; break
			case "tt" : windowFeatures += "titlebar=" + valor + ", "; break
			case "re" : windowFeatures += "resizable=" + valor + ", "; break
			default : erro = '"Código de atributo não informado no '+(i+1)+' º argumento (' +Argumentos[i]+ ')"'
		}
	}
	windowFeatures = windowFeatures.substring(0,windowFeatures.lastIndexOf(","))
	if (erro) { alert(erro) } else { novaJanela = window.open(nomeArquivo, nomeJanela, windowFeatures) }
}


// Conta caracteres de um campo texto
// textareaID = id do elemento texto
// spanId = id do elemento que aparece a contagem regressiva
// maxSize = número máxim de caracteres permitidos.
function contaCaracteres(textareaId, spanId, maxSize) 
{
  textarea = document.getElementById(textareaId);
  if (textarea == null) {
	return;
  }
  if (textarea.value.length > maxSize) {
	  textarea.value = textarea.value.substring(0, maxSize);
  }
  document.getElementById(spanId).innerHTML = maxSize - textarea.value.length;
}


// Formata campos textos passando uma máscara
// src = elemento que possuirá a máscara
// mask = tipo da máscara. 
// 0 = numérico
// # = não escreve nada
// A = letra
function formatar(src, evt, mask) {
	var i = src.value.length;
	var saida = mask.substring(i,i+1);
	var ascii = (window.event)?event.keyCode:evt.which;
	if (ascii <= 30) return true;
	if (saida == "A") {
		if ((ascii >=97) && (ascii <= 122)) { 
			return true;
			//event.keyCode -= 32;
		} else {
			return false
		}
	} else if (saida == "0") {
		if ((ascii >= 48) && (ascii <= 57)) {
			return true
		} else {
			return false
//			event.keyCode = 0
		}
	} else if (saida == "#") {
		return false;
	} else {
		src.value += saida;
		i += 1;
		saida = mask.substring(i,i+1);
		if (saida == "A") {
			if ((ascii >=97) && (ascii <= 122)) {
				return true; //event.keyCode -= 32;
			} else {
				return false; //event.keyCode = 0; 
			}
		} else if (saida == "0") {
			if ((ascii >= 48) && (ascii <= 57)) {
				return true;
			} else {
				return false; //event.keyCode = 0 
			}
		} else {
			return true;
		}
	}
}

function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58)) return true;
    else{
    if (tecla != 8) return false;
    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 <= 30) 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 aparece(id, acao){
	if (acao == null){
		if (document.getElementById(id).style.display == 'none') {
		   document.getElementById(id).style.display = 'inline';
		} else {
		   document.getElementById(id).style.display = 'none';
		}
	} else {
	   document.getElementById(id).style.display = acao;
	}
}

