// JEffects.js
// contém todos os efeitos do site

var JEffects = {
	exibeItem: function(elemento) {
		JGlobal.$(elemento).style.display = 'block';
	},
	escondeItem: function(elemento) {
		JGlobal.$(elemento).style.display = 'none';
	},
	addTexto: function(elemento, texto) {
		JGlobal.$(elemento).innerHTML = texto;
	},
	focusBusca: function(elemento) {
		JGlobal.$(elemento).focus();
	},
	append: function(elemento, texto) {
		localText = JGlobal.$(elemento).value;
		JGlobal.$(elemento).innerHTML = localText +"<br>"+ texto;
	}
}

// constrole do menu
JEffects.menu = {
	menuEscolhido: null,
	
	exibeMenu: function(emenu) {
		JEffects.exibeItem(emenu);
		if(JEffects.menu.menuEscolhido != null)
				JEffects.escondeItem(JEffects.menu.menuEscolhido);
		if(JEffects.menu.menuEscolhido != emenu)
			JEffects.menu.menuEscolhido = emenu;
		else
			JEffects.menu.menuEscolhido = null;
	}
} // fim pacote JEffects.menu

JEffects.masks = {
	numeros: function(obj) {
		var objValue = obj.value;
		for(i=0;i<objValue.length + 1;i++)
			objValue = objValue.replace(/[^0-9]/,'');
		obj.value = objValue;
	},
	hora: function(vHora) {
		hora = vHora.value;
		nHora = hora.replace(/:/, '');
		nHora = nHora.replace(/[^0-9]/,'');
		var h = nHora.substring(0, 2);
		var m = nHora.substring(2, 4);
		var s = nHora.substring(4, 6);
		if(nHora.length == 1)
			vHora.value = h;
		if(nHora.length == 2)
			vHora.value = h +":";
		if(nHora.length == 3)
			vHora.value = h +":"+ m;
		if(nHora.length == 4)
			vHora.value = h +":"+ m +":";
		if(nHora.length == 5)
			vHora.value = h +":"+ m +":"+ s;
		if(nHora.length == 6)
			vHora.value = h +":"+ m +":"+ s;
	},
	data: function(val) {
		var pass = val.value;
		var expr = /[0123456789]/;
		for(i=0; i<pass.length; i++) {
			// charAt -> retorna o caractere posicionado no índice especificado
			var lchar = val.value.charAt(i);
			var nchar = val.value.charAt(i+1);
			if(i==0){
				// search -> retorna um valor inteiro, indicando a posiçao do inicio da primeira
				// ocorrencia de expReg dentro de instStr. Se nenhuma ocorrencia for encontrada o método retornara -1
				// instStr.search(expReg);
				if ((lchar.search(expr) != 0) || (lchar>3)){
					val.value = "";
				}
			} else if(i==1) {
				if(lchar.search(expr) != 0){
					// substring(indice1,indice2)
					// indice1, indice2 -> será usado para delimitar a string
					var tst1 = val.value.substring(0,(i));
					val.value = tst1;
					continue;
				}
				if ((nchar != '/') && (nchar != '')){
					var tst1 = val.value.substring(0, (i)+1);
					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
					val.value = tst1 + '/' + tst2;
				}
			} else if(i==4) {
				if(lchar.search(expr) != 0) {
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;
					continue;
				}
				if((nchar != '/') && (nchar != '')) {
					var tst1 = val.value.substring(0, (i)+1);
					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
					val.value = tst1 + '/' + tst2;
				}
			}
			if(i>=6) {
				if(lchar.search(expr) != 0) {
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;			
				}
			}
		}
		if(pass.length>10)
			val.value = val.value.substring(0, 10);
			return true;
	},
	telefone: function(objTelefone) {
		telefone = objTelefone.value;
		fTelefone = telefone.replace(/[^0-9]/,'');
		fTelefone = fTelefone.replace(/ /,'');
		fTelefone = fTelefone.replace(/\(/,'');
		fTelefone = fTelefone.replace(/\)/,'');
		if(fTelefone.length > 8) {
			var ddd = fTelefone.substring(0, 2);
			var resto = fTelefone.substring(2, 11);
			objTelefone.value = "("+ ddd +") "+ resto;
		} else {
			objTelefone.value = fTelefone;
		}
	},
	
		telefone1: function(objTelefone1) {
		telefone1 = objTelefone1.value;
		fTelefone1 = telefone1.replace(/[^0-9]/,'');
		fTelefone1 = fTelefone1.replace(/ /,'');
		fTelefone1 = fTelefone1.replace(/\(/,'');
		fTelefone1 = fTelefone1.replace(/\)/,'');
		if(fTelefone1.length > 8) {
			var ddd = fTelefone1.substring(0, 2);
			var resto = fTelefone1.substring(2, 11);
			objTelefone1.value = "("+ ddd +") "+ resto;
		} else {
			objTelefone1.value = fTelefone1;
		}
	},


	valor: function(obj) {
		var valor = obj.value;
		valor = valor.replace(/[^0-9]/,'');
		decimal = valor.substring((valor.length - 2), valor.length);
		if(valor.length == 3)
			inteiro = valor.substring(0, 1);
		else
			inteiro = valor.substring(0, (valor.length - 2));
		if(valor.length <= 2)
			obj.value = valor;
		if(valor.length > 2)
			obj.value = inteiro +","+ decimal;
	},
	cnpj: function(Campo, teclapres){
		if(window.event) {
			var tecla = teclapres.keyCode;
		} else {
			tecla = teclapres.which;
		}
		var vr = new String(Campo.value);
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace("/", "");
		vr = vr.replace("-", "");
		
		tam = vr.length + 1;
		
		if (tecla != 9 && tecla != 8){
			if (tam > 2 && tam < 6)
				Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
			if (tam >= 6 && tam < 9)
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
			if (tam >= 9 && tam < 13)
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
			if (tam >= 13 && tam < 15)
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
		}
	},
	inscricaoEstadual: function(obj) {
		var txt = obj.value;
		for(i=0;i<txt.length + 10;i++)
			txt = txt.replace(/[^0-9]/,'');
		var c1 = txt.substr(0, 2);
		var c2 = txt.substr(2, 3);
		var c3 = txt.substr(5, 3);
		var c4 = txt.substr(8, 1);
		if(txt.length > 0 && txt.length <= 2)
			obj.value = c1;
		if(txt.length >= 3 && txt.length <= 5)
			obj.value = c1 +"."+ c2;
		if(txt.length >= 6 && txt.length <= 8)
			obj.value = c1 +"."+ c2 +"."+ c3;
		if(txt.length == 9)
			obj.value = c1 +"."+ c2 +"."+ c3 +"-"+ c4;
	},
	registroJucepa: function(obj) {
		var txt = obj.value;
		for(i=0;i<txt.length + 1;i++)
			txt = txt.replace(/[^0-9]/,'');
		obj.value = txt;
	},
	cep: function(obj) {
		var txt = obj.value;
		for(i=0;i<txt.length + 1;i++)
			txt = txt.replace(/[^0-9]/,'');
		var c1 = txt.substr(0, 2);
		var c2 = txt.substr(2, 3);
		var c3 = txt.substr(5, 3);
		if(txt.length > 0 && txt.length <= 2)
			obj.value = c1;
		if(txt.length >= 3 && txt.length <= 5)
			obj.value = c1 +"."+ c2;
		if(txt.length >= 6 && txt.length <= 8)
			obj.value = c1 +"."+ c2 +"-"+ c3;
	}
}
